Optimize plugins space by not displaying 0 stats in Curse interface #1787

This commit is contained in:
nicolargo 2021-01-23 09:08:24 +01:00
parent c3ef20c361
commit 18394fa72d
3 changed files with 68 additions and 16 deletions

View File

@ -23,6 +23,7 @@ from __future__ import unicode_literals
from glances.compat import nativestr, n
from glances.timer import getTimeSinceLastUpdate
from glances.plugins.glances_plugin import GlancesPlugin
from glances.logger import logger
import psutil
@ -51,6 +52,9 @@ class Plugin(GlancesPlugin):
# We want to display the stat in the curse interface
self.display_curse = True
# Hide stats if it has never been != 0
self.hide_zero = True
self.hide_zero_fields = ['read_bytes', 'write_bytes']
def get_key(self):
"""Return the key of the list."""
@ -143,9 +147,12 @@ class Plugin(GlancesPlugin):
# Call the father's method
super(Plugin, self).update_views()
# Check if the stats should be hidden
self.update_views_hidden()
# Add specifics informations
# Alert
for i in self.stats:
for i in self.get_raw():
disk_real_name = i['disk_name']
self.views[i[self.get_key()]]['read_bytes']['decoration'] = self.get_alert(int(i['read_bytes'] // i['time_since_update']),
header=disk_real_name + '_rx')
@ -179,6 +186,9 @@ class Plugin(GlancesPlugin):
ret.append(self.curse_add_line(msg))
# Disk list (sorted by name)
for i in self.sorted_stats():
# Hide stats if never be different from 0 (issue #1787)
if all([self.get_views(item=i[self.get_key()], key=f, option='hidden') for f in self.hide_zero_fields]):
continue
# Is there an alias for the disk name ?
disk_real_name = i['disk_name']
disk_name = self.has_alias(i['disk_name'])

View File

@ -61,6 +61,9 @@ class Plugin(GlancesPlugin):
# We want to display the stat in the curse interface
self.display_curse = True
# Hide stats if it has never been != 0
self.hide_zero = True
self.hide_zero_fields = ['rx', 'tx']
def get_key(self):
"""Return the key of the list."""
@ -223,25 +226,17 @@ class Plugin(GlancesPlugin):
# Call the father's method
super(Plugin, self).update_views()
# Check if the stats should be hidden
self.update_views_hidden()
# Add specifics informations
# Alert
for i in self.stats:
for i in self.get_raw():
ifrealname = i['interface_name'].split(':')[0]
# Convert rate in bps (to be able to compare to interface speed)
bps_rx = int(i['rx'] // i['time_since_update'] * 8)
bps_tx = int(i['tx'] // i['time_since_update'] * 8)
# Check if the stats should be hidden
if bps_rx != 0 or bps_tx != 0:
self.views[i[self.get_key(
)]]['rx']['_zero'] = self.views[i[self.get_key()]]['rx']['hidden']
self.views[i[self.get_key(
)]]['tx']['_zero'] = self.views[i[self.get_key()]]['rx']['hidden']
self.views[i[self.get_key(
)]]['rx']['hidden'] = self.views[i[self.get_key()]]['rx']['_zero'] and bps_rx == 0
self.views[i[self.get_key(
)]]['tx']['hidden'] = self.views[i[self.get_key()]]['tx']['_zero'] and bps_tx == 0
# Decorate the bitrate with the configuration file thresolds
alert_rx = self.get_alert(bps_rx, header=ifrealname + '_rx')
alert_tx = self.get_alert(bps_tx, header=ifrealname + '_tx')
@ -302,9 +297,8 @@ class Plugin(GlancesPlugin):
# Do not display interface in down state (issue #765)
if ('is_up' in i) and (i['is_up'] is False):
continue
# Hide 0 value (issue #1787)
if self.get_views(item=i[self.get_key()], key='rx', option='hidden') and \
self.get_views(item=i[self.get_key()], key='tx', option='hidden'):
# Hide stats if never be different from 0 (issue #1787)
if all([self.get_views(item=i[self.get_key()], key=f, option='hidden') for f in self.hide_zero_fields]):
continue
# Format stats
# Is there an alias for the interface name ?

View File

@ -99,6 +99,11 @@ class GlancesPlugin(object):
# Init the views
self.views = dict()
# Hide stats if all the hide_zero_fields has never been != 0
# Default is False, always display stats
self.hide_zero = False
self.hide_zero_fields = []
# Init the stats
self.stats_init_value = stats_init_value
self.stats = None
@ -422,6 +427,49 @@ class GlancesPlugin(object):
"Cannot get item({})=value({}) ({})".format(item, value, e))
return None
def update_views_hidden(self):
"""If the self.hide_zero is set then update the hidden field of the view
It will check if all fields values are already be different from 0
In this case, the hidden field is set to True
Note: This function should be called by plugin (in the update_views method)
Example (for network plugin):
__Init__
self.hide_zero_fields = ['rx', 'tx']
Update views
...
self.update_views_hidden()
"""
if not self.hide_zero:
return False
if (isinstance(self.get_raw(), list) and
self.get_raw() is not None and
self.get_key() is not None):
# Stats are stored in a list of dict (ex: NETWORK, FS...)
for i in self.get_raw():
if any([i[f] for f in self.hide_zero_fields]):
for f in self.hide_zero_fields:
self.views[i[self.get_key(
)]][f]['_zero'] = self.views[i[self.get_key()]][f]['hidden']
for f in self.hide_zero_fields:
self.views[i[self.get_key(
)]][f]['hidden'] = self.views[i[self.get_key()]][f]['_zero'] and i[f] == 0
elif isinstance(self.get_raw(), dict) and self.get_raw() is not None:
#
# Warning: This code has never been tested because
# no plugin with dict instance use the hidden function...
# vvvv
#
# Stats are stored in a dict (ex: CPU, LOAD...)
for key in listkeys(self.get_raw()):
if any([self.get_raw()[f] for f in self.hide_zero_fields]):
for f in self.hide_zero_fields:
self.views[f]['_zero'] = self.views[f]['hidden']
for f in self.hide_zero_fields:
self.views[f]['hidden'] = self.views['_zero'] and self.views[f] == 0
return True
def update_views(self):
"""Update the stats views.