Skip to content

Commit

Permalink
Fix stats docker func
Browse files Browse the repository at this point in the history
  • Loading branch information
sonicaj committed Aug 28, 2024
1 parent d1a5cbc commit a4d3781
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions src/middlewared/middlewared/plugins/apps/ix_apps/docker/stats.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
from collections import defaultdict

from .utils import get_docker_client, PROJECT_KEY


def list_resources_stats_by_project(project_name: str) -> dict:
def list_resources_stats_by_project(project_name: str | None = None) -> dict:
projects = defaultdict(lambda: {
'cpu_usage': 0,
'memory_stats': 0,
'networks': defaultdict(lambda: {'rx_bytes': 0, 'tx_bytes': 0}),
'blkio_stats': {'read': 0, 'write': 0},
})
with get_docker_client() as client:
label_filter = {'label': f'{PROJECT_KEY}={project_name}' if project_name else PROJECT_KEY}
stats = {}
for container in client.containers.list(all=True, filters=label_filter, sparse=False):
cont_stats = container.stats(stream=False, decode=None)
stats[cont_stats['name'].strip('/')] = {
'cpu_usage': cont_stats.get('cpu_stats', {}).get('cpu_usage', {}).get('total_usage', 0),
'memory_stats': cont_stats.get('memory_stats', {}).get('usage', 0),
'networks': {
net_name: {
'rx_bytes': net_values['rx_bytes'],
'tx_bytes': net_values['tx_bytes']
} for net_name, net_values in cont_stats.get('networks', {}).items()
},
'blkio_stats': {
blkio['op']: blkio['value']
for blkio in cont_stats.get('blkio_stats', {}).get('io_service_bytes_recursive', {}) or {}
}
}
return stats
stats = container.stats(stream=False, decode=None, one_shot=True)
project = container.labels.get(PROJECT_KEY)
if not project:
continue

blkio_container_stats = stats.get('blkio_stats', {}).get('io_service_bytes_recursive') or {}
project_stats = projects[project]
project_stats['cpu_usage'] += stats.get('cpu_stats', {}).get('cpu_usage', {}).get('total_usage', 0)
project_stats['memory_stats'] += stats.get('memory_stats', {}).get('usage', 0)
for entry in filter(lambda x: x['op'] in ('read', 'write'), blkio_container_stats):
project_stats['blkio_stats'][entry['op']] += entry['value']
for net_name, net_values in stats.get('networks', {}).items():
project_stats['networks'][net_name]['rx_bytes'] += net_values.get('rx_bytes', 0)
project_stats['networks'][net_name]['tx_bytes'] += net_values.get('tx_bytes', 0)

return projects

0 comments on commit a4d3781

Please sign in to comment.