From 1f1622a625c4d66a100cdb09654f0f56934b2742 Mon Sep 17 00:00:00 2001 From: nicolargo Date: Sun, 10 Dec 2023 19:59:33 +0100 Subject: [PATCH] Looks beter after the last commit ! Some TODO to do in the glances_restful_api.py files... --- glances/programs.py | 8 ++++++-- unitest.py | 6 ++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/glances/programs.py b/glances/programs.py index 57e38672..69fa02dd 100644 --- a/glances/programs.py +++ b/glances/programs.py @@ -7,6 +7,8 @@ # SPDX-License-Identifier: LGPL-3.0-only # +from collections import Counter + # from glances.logger import logger # This constant defines the list of available processes sort key @@ -35,14 +37,15 @@ def create_program_dict(p): 'status': p['status'], } + def update_program_dict(program, p): """Update an existing entry in the dict (existing program)""" # some values can be None, e.g. macOS system processes program['num_threads'] += p['num_threads'] or 0 program['cpu_percent'] += p['cpu_percent'] or 0 program['memory_percent'] += p['memory_percent'] or 0 - program['cpu_times'] += p['cpu_times'] or () - program['memory_info'] += p['memory_info'] or () + program['cpu_times'] = dict(Counter(program['cpu_times']) + Counter(p['cpu_times'])) + program['memory_info'] = dict(Counter(program['memory_info']) + Counter(p['memory_info'])) program['io_counters'] += p['io_counters'] program['childrens'].append(p['pid']) @@ -51,6 +54,7 @@ def update_program_dict(program, p): program['nice'] = p['nice'] if p['nice'] == program['nice'] else '_' program['status'] = p['status'] if p['status'] == program['status'] else '_' + def processes_to_programs(processes): """Convert a list of processes to a list of programs.""" # Start to build a dict of programs (key is program name) diff --git a/unitest.py b/unitest.py index a47af590..eb35b13d 100755 --- a/unitest.py +++ b/unitest.py @@ -280,10 +280,8 @@ class TestGlances(unittest.TestCase): # stats_to_check = [ ] print('INFO: [TEST_017] Check PROGRAM stats') stats_grab = processes_to_programs(stats.get_plugin('processlist').get_raw()) - self.assertTrue(type(stats_grab) is list, msg='Programs stats is not a list') - print('INFO: PROGRAM list stats: %s items in the list' % len(stats_grab)) - # Check if number of processes in the list equal counter - # self.assertEqual(total, len(stats_grab)) + self.assertIsInstance(stats_grab, list, msg='Programs stats list is not a list') + self.assertIsInstance(stats_grab[0], dict, msg='First item should be a dict') def test_018_string_value_to_float(self): """Check string_value_to_float function"""