Sort docker stats #1276

This commit is contained in:
nicolargo 2018-09-08 16:08:17 +02:00
parent 2caac3803a
commit 50fb9c70a4
3 changed files with 28 additions and 10 deletions

1
NEWS
View File

@ -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:

View File

@ -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

View File

@ -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)