From ada7f248badb0feec9495e6ef5c9eb0c8bdd6938 Mon Sep 17 00:00:00 2001 From: nicolargo Date: Thu, 9 May 2024 10:41:24 +0200 Subject: [PATCH] Improve code --- glances/plugins/plugin/model.py | 50 ++++++++++++++++----------------- unittest-core.py | 4 +-- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/glances/plugins/plugin/model.py b/glances/plugins/plugin/model.py index cfc846a8..247d4cd8 100644 --- a/glances/plugins/plugin/model.py +++ b/glances/plugins/plugin/model.py @@ -188,33 +188,31 @@ class GlancesPluginModel(object): def update_stats_history(self): """Update stats history.""" # Build the history - if self.get_export() and self.history_enable(): - # If the plugin data is a dict, the dict's key should be used - if self.get_key() is None: - item_name = '' + if not (self.get_export() and self.history_enable()): + return + # Itern through items history + item_name = '' if self.get_key() is None else self.get_key() + for i in self.get_items_history_list(): + if isinstance(self.get_export(), list): + # Stats is a list of data + # Iter through stats (for example, iter through network interface) + for l_export in self.get_export(): + if i['name'] in l_export: + self.stats_history.add( + nativestr(l_export[item_name]) + '_' + nativestr(i['name']), + l_export[i['name']], + description=i['description'], + history_max_size=self._limits['history_size'], + ) else: - item_name = self.get_key() - for i in self.get_items_history_list(): - if isinstance(self.get_export(), list): - # Stats is a list of data - # Iter through it (for example, iter through network interface) - for l_export in self.get_export(): - if i['name'] in l_export: - self.stats_history.add( - nativestr(l_export[item_name]) + '_' + nativestr(i['name']), - l_export[i['name']], - description=i['description'], - history_max_size=self._limits['history_size'], - ) - else: - # Stats is not a list - # Add the item to the history directly - self.stats_history.add( - nativestr(i['name']), - self.get_export()[i['name']], - description=i['description'], - history_max_size=self._limits['history_size'], - ) + # Stats is not a list + # Add the item to the history directly + self.stats_history.add( + nativestr(i['name']), + self.get_export()[i['name']], + description=i['description'], + history_max_size=self._limits['history_size'], + ) def get_items_history_list(self): """Return the items history list.""" diff --git a/unittest-core.py b/unittest-core.py index 87a7718a..6e45c0ce 100755 --- a/unittest-core.py +++ b/unittest-core.py @@ -90,18 +90,16 @@ class TestGlances(unittest.TestCase): # Check stats self.assertIsInstance(plugin_instance.get_raw(), (dict, list)) if isinstance(plugin_instance.get_raw(), dict): - # self.assertTrue(any([f in plugin_instance.get_raw() for f in plugin_instance.fields_description])) res = False for f in plugin_instance.fields_description: if f not in plugin_instance.get_raw(): - print(f"WARNING: {f} not found in {plugin} plugin fields_description") + print(f"WARNING: {f} field not found in {plugin} plugin stats") else: res = True self.assertTrue(res) elif isinstance(plugin_instance.get_raw(), list): res = False for i in plugin_instance.get_raw(): - # self.assertTrue(all([f in plugin_instance.fields_description for f in i])) for f in i: if f in plugin_instance.fields_description: res = True