From a4d3781613acec0095b7c7ee03721c1261521cd7 Mon Sep 17 00:00:00 2001 From: Waqar Ahmed Date: Mon, 26 Aug 2024 20:54:21 +0500 Subject: [PATCH] Fix stats docker func --- .../plugins/apps/ix_apps/docker/stats.py | 43 +++++++++++-------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/middlewared/middlewared/plugins/apps/ix_apps/docker/stats.py b/src/middlewared/middlewared/plugins/apps/ix_apps/docker/stats.py index 6a0857ba2d5b3..104921dd97d41 100644 --- a/src/middlewared/middlewared/plugins/apps/ix_apps/docker/stats.py +++ b/src/middlewared/middlewared/plugins/apps/ix_apps/docker/stats.py @@ -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