Improve code

This commit is contained in:
nicolargo 2024-05-09 10:41:24 +02:00
parent 4ddff48c70
commit ada7f248ba
2 changed files with 25 additions and 29 deletions

View File

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

View File

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