Skip to content

Commit ada7f24

Browse files
committed
Improve code
1 parent 4ddff48 commit ada7f24

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

glances/plugins/plugin/model.py

+24-26
Original file line numberDiff line numberDiff line change
@@ -188,33 +188,31 @@ def reset_stats_history(self):
188188
def update_stats_history(self):
189189
"""Update stats history."""
190190
# Build the history
191-
if self.get_export() and self.history_enable():
192-
# If the plugin data is a dict, the dict's key should be used
193-
if self.get_key() is None:
194-
item_name = ''
191+
if not (self.get_export() and self.history_enable()):
192+
return
193+
# Itern through items history
194+
item_name = '' if self.get_key() is None else self.get_key()
195+
for i in self.get_items_history_list():
196+
if isinstance(self.get_export(), list):
197+
# Stats is a list of data
198+
# Iter through stats (for example, iter through network interface)
199+
for l_export in self.get_export():
200+
if i['name'] in l_export:
201+
self.stats_history.add(
202+
nativestr(l_export[item_name]) + '_' + nativestr(i['name']),
203+
l_export[i['name']],
204+
description=i['description'],
205+
history_max_size=self._limits['history_size'],
206+
)
195207
else:
196-
item_name = self.get_key()
197-
for i in self.get_items_history_list():
198-
if isinstance(self.get_export(), list):
199-
# Stats is a list of data
200-
# Iter through it (for example, iter through network interface)
201-
for l_export in self.get_export():
202-
if i['name'] in l_export:
203-
self.stats_history.add(
204-
nativestr(l_export[item_name]) + '_' + nativestr(i['name']),
205-
l_export[i['name']],
206-
description=i['description'],
207-
history_max_size=self._limits['history_size'],
208-
)
209-
else:
210-
# Stats is not a list
211-
# Add the item to the history directly
212-
self.stats_history.add(
213-
nativestr(i['name']),
214-
self.get_export()[i['name']],
215-
description=i['description'],
216-
history_max_size=self._limits['history_size'],
217-
)
208+
# Stats is not a list
209+
# Add the item to the history directly
210+
self.stats_history.add(
211+
nativestr(i['name']),
212+
self.get_export()[i['name']],
213+
description=i['description'],
214+
history_max_size=self._limits['history_size'],
215+
)
218216

219217
def get_items_history_list(self):
220218
"""Return the items history list."""

unittest-core.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,16 @@ def _common_plugin_tests(self, plugin):
9090
# Check stats
9191
self.assertIsInstance(plugin_instance.get_raw(), (dict, list))
9292
if isinstance(plugin_instance.get_raw(), dict):
93-
# self.assertTrue(any([f in plugin_instance.get_raw() for f in plugin_instance.fields_description]))
9493
res = False
9594
for f in plugin_instance.fields_description:
9695
if f not in plugin_instance.get_raw():
97-
print(f"WARNING: {f} not found in {plugin} plugin fields_description")
96+
print(f"WARNING: {f} field not found in {plugin} plugin stats")
9897
else:
9998
res = True
10099
self.assertTrue(res)
101100
elif isinstance(plugin_instance.get_raw(), list):
102101
res = False
103102
for i in plugin_instance.get_raw():
104-
# self.assertTrue(all([f in plugin_instance.fields_description for f in i]))
105103
for f in i:
106104
if f in plugin_instance.fields_description:
107105
res = True

0 commit comments

Comments
 (0)