mirror of https://github.com/nicolargo/glances.git
containers: processes: Refactor
Refactored sort_docker_stats() and sort_stats(). Useful for an upcoming commit. Part-of: #2801 Signed-off-by: Ariel Otilibili <otilibil@eurecom.fr>
This commit is contained in:
parent
dc165930b4
commit
e4cdc14cd0
|
|
@ -552,18 +552,13 @@ class ContainersPlugin(GlancesPluginModel):
|
|||
|
||||
def sort_docker_stats(stats: list[dict[str, Any]]) -> tuple[str, list[dict[str, Any]]]:
|
||||
# Make VM sort related to process sort
|
||||
if glances_processes.sort_key == 'memory_percent':
|
||||
sort_by = 'memory_usage'
|
||||
sort_by_secondary = 'cpu_percent'
|
||||
elif glances_processes.sort_key == 'name':
|
||||
sort_by = 'name'
|
||||
sort_by_secondary = 'cpu_percent'
|
||||
else:
|
||||
sort_by = 'cpu_percent'
|
||||
sort_by_secondary = 'memory_usage'
|
||||
sort_by, sort_by_secondary = {
|
||||
'memory_percent': ('memory_usage', 'cpu_percent'),
|
||||
'name': ('name', 'cpu_percent'),
|
||||
}.get(glances_processes.sort_key, ('cpu_percent', 'memory_usage'))
|
||||
|
||||
# Sort docker stats
|
||||
sort_stats_processes(
|
||||
stats = sort_stats_processes(
|
||||
stats,
|
||||
sorted_by=sort_by,
|
||||
sorted_by_secondary=sort_by_secondary,
|
||||
|
|
|
|||
|
|
@ -725,6 +725,10 @@ def weighted(value):
|
|||
return -float('inf') if value is None else value
|
||||
|
||||
|
||||
def sort_by_these_keys(first, second):
|
||||
return lambda process: (weighted(process[first]), weighted(process[second]))
|
||||
|
||||
|
||||
def _sort_io_counters(process, sorted_by='io_counters', sorted_by_secondary='memory_percent'):
|
||||
"""Specific case for io_counters
|
||||
|
||||
|
|
@ -746,12 +750,7 @@ def _sort_cpu_times(process, sorted_by='cpu_times', sorted_by_secondary='memory_
|
|||
|
||||
def _sort_lambda(sorted_by='cpu_percent', sorted_by_secondary='memory_percent'):
|
||||
"""Return a sort lambda function for the sorted_by key"""
|
||||
ret = None
|
||||
if sorted_by == 'io_counters':
|
||||
ret = _sort_io_counters
|
||||
elif sorted_by == 'cpu_times':
|
||||
ret = _sort_cpu_times
|
||||
return ret
|
||||
return {'io_counters': _sort_io_counters, 'cpu_times': _sort_cpu_times}.get(sorted_by, None)
|
||||
|
||||
|
||||
def sort_stats(stats, sorted_by='cpu_percent', sorted_by_secondary='memory_percent', reverse=True):
|
||||
|
|
@ -770,20 +769,14 @@ def sort_stats(stats, sorted_by='cpu_percent', sorted_by_secondary='memory_perce
|
|||
if sort_lambda is not None:
|
||||
# Specific sort
|
||||
try:
|
||||
stats.sort(key=sort_lambda, reverse=reverse)
|
||||
stats = sorted(stats, key=sort_lambda, reverse=reverse)
|
||||
except Exception:
|
||||
# If an error is detected, fallback to cpu_percent
|
||||
stats.sort(
|
||||
key=lambda process: (weighted(process['cpu_percent']), weighted(process[sorted_by_secondary])),
|
||||
reverse=reverse,
|
||||
)
|
||||
stats = sorted(stats, key=sort_by_these_keys('cpu_percent', sorted_by_secondary), reverse=reverse)
|
||||
else:
|
||||
# Standard sort
|
||||
try:
|
||||
stats.sort(
|
||||
key=lambda process: (weighted(process[sorted_by]), weighted(process[sorted_by_secondary])),
|
||||
reverse=reverse,
|
||||
)
|
||||
stats = sorted(stats, key=sort_by_these_keys(sorted_by, sorted_by_secondary), reverse=reverse)
|
||||
except (KeyError, TypeError):
|
||||
# Fallback to name
|
||||
stats.sort(key=lambda process: process['name'] if process['name'] is not None else '~', reverse=False)
|
||||
|
|
|
|||
Loading…
Reference in New Issue