From 50fb9c70a4503239090de384385ba7c5b4976b0e Mon Sep 17 00:00:00 2001 From: nicolargo Date: Sat, 8 Sep 2018 16:08:17 +0200 Subject: [PATCH] Sort docker stats #1276 --- NEWS | 1 + glances/plugins/glances_docker.py | 24 ++++++++++++++++++++---- glances/processes.py | 13 +++++++------ 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/NEWS b/NEWS index 173d26e1..0ad9b52e 100644 --- a/NEWS +++ b/NEWS @@ -8,6 +8,7 @@ Version 3.1 Enhancements and new features: * Feature request: HDD S.M.A.R.T. reports (thanks to @tnibert) #1288 + * Sort docker stats #1276 Bugs corrected: diff --git a/glances/plugins/glances_docker.py b/glances/plugins/glances_docker.py index aff135ef..78085e1c 100644 --- a/glances/plugins/glances_docker.py +++ b/glances/plugins/glances_docker.py @@ -23,10 +23,11 @@ import os import threading import time -from glances.compat import iterkeys, itervalues, nativestr from glances.logger import logger +from glances.compat import iterkeys, itervalues, nativestr from glances.timer import getTimeSinceLastUpdate from glances.plugins.glances_plugin import GlancesPlugin +from glances.processes import sort_stats as sort_stats_processes, weighted, glances_processes # Docker-py library (optional and Linux-only) # https://github.com/docker/docker-py @@ -206,7 +207,7 @@ class Plugin(GlancesPlugin): # Export name (first name in the Names list, without the /) container_stats['name'] = nativestr(container.name) # Export global Names (used by the WebUI) - container_stats['Names'] = [ nativestr(container.name)] + container_stats['Names'] = [nativestr(container.name)] # Container Id container_stats['Id'] = container.id # Container Image @@ -245,8 +246,10 @@ class Plugin(GlancesPlugin): # Not available pass - # Update the stats - self.stats = stats + # Sort and update the stats + self.stats = sort_stats(stats) + + logger.info('Docker stats (sorted): {}'.format(self.stats)) return self.stats @@ -676,3 +679,16 @@ class ThreadDockerGrabber(threading.Thread): def stopped(self): """Return True is the thread is stopped.""" return self._stopper.isSet() + + +def sort_stats(stats): + # Sort Docker stats using the same function than processes + sortedby = 'cpu_percent' + sortedby_secondary = 'memory_usage' + if glances_processes.sort_key.startswith('memory'): + sortedby = 'memory_usage' + sortedby_secondary = 'cpu_percent' + sort_stats_processes(stats['containers'], + sortedby=sortedby, + sortedby_secondary=sortedby_secondary) + return stats diff --git a/glances/processes.py b/glances/processes.py index bdd20c73..19f880ac 100644 --- a/glances/processes.py +++ b/glances/processes.py @@ -375,17 +375,17 @@ def weighted(value): return -float('inf') if value is None else value -def sort_stats(stats, sortedby=None, reverse=True): +def sort_stats(stats, + sortedby='cpu_percent', + sortedby_secondary='memory_percent', + reverse=True,): """Return the stats (dict) sorted by (sortedby). Reverse the sort if reverse is True. """ - sortedby_secondary = 'cpu_percent' - if sortedby is None: + if sortedby is None and sortedby_secondary is None: # No need to sort... return stats - elif sortedby is 'cpu_percent': - sortedby_secondary = 'memory_percent' if sortedby == 'io_counters': # Specific case for io_counters @@ -398,7 +398,7 @@ def sort_stats(stats, sortedby=None, reverse=True): reverse=reverse) except Exception: stats.sort(key=lambda x: (weighted(x['cpu_percent']), - weighted(x['memory_percent'])), + weighted(x[sortedby_secondary])), reverse=reverse) else: # Others sorts @@ -407,6 +407,7 @@ def sort_stats(stats, sortedby=None, reverse=True): weighted(x[sortedby_secondary])), reverse=reverse) except (KeyError, TypeError): + # Fallback to name stats.sort(key=lambda x: x['name'] if x['name'] is not None else '~', reverse=False) -- GitLab