summaryrefslogtreecommitdiffstats
path: root/glances/plugins/glances_plugin.py
diff options
context:
space:
mode:
Diffstat (limited to 'glances/plugins/glances_plugin.py')
-rw-r--r--glances/plugins/glances_plugin.py86
1 files changed, 77 insertions, 9 deletions
diff --git a/glances/plugins/glances_plugin.py b/glances/plugins/glances_plugin.py
index 936db58c..5588263d 100644
--- a/glances/plugins/glances_plugin.py
+++ b/glances/plugins/glances_plugin.py
@@ -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,16 +427,61 @@ 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.
The V of MVC
A dict of dict with the needed information to display the stats.
Example for the stat xxx:
- 'xxx': {'decoration': 'DEFAULT',
- 'optional': False,
- 'additional': False,
- 'splittable': False}
+ 'xxx': {'decoration': 'DEFAULT', >>> The decoration of the stats
+ 'optional': False, >>> Is the stat optional
+ 'additional': False, >>> Is the stat provide additional information
+ 'splittable': False, >>> Is the stat can be cut (like process lon name)
+ 'hidden': False, >>> Is the stats should be hidden in the UI
+ '_zero': True} >>> For internal purpose only
"""
ret = {}
@@ -440,12 +490,15 @@ class GlancesPlugin(object):
self.get_key() is not None):
# Stats are stored in a list of dict (ex: NETWORK, FS...)
for i in self.get_raw():
+ # i[self.get_key()] is the interface name (example for NETWORK)
ret[i[self.get_key()]] = {}
for key in listkeys(i):
value = {'decoration': 'DEFAULT',
'optional': False,
'additional': False,
- 'splittable': False}
+ 'splittable': False,
+ 'hidden': False,
+ '_zero': self.views[i[self.get_key()]][key]['_zero'] if i[self.get_key()] in self.views and key in self.views[i[self.get_key()]] else True}
ret[i[self.get_key()]][key] = value
elif isinstance(self.get_raw(), dict) and self.get_raw() is not None:
# Stats are stored in a dict (ex: CPU, LOAD...)
@@ -453,7 +506,9 @@ class GlancesPlugin(object):
value = {'decoration': 'DEFAULT',
'optional': False,
'additional': False,
- 'splittable': False}
+ 'splittable': False,
+ 'hidden': False,
+ '_zero': self.views[key]['_zero'] if key in self.views and '_zero' in self.views[key] else True}
ret[key] = value
self.views = ret
@@ -752,6 +807,21 @@ class GlancesPlugin(object):
except KeyError:
return default
+ def is_show(self, value, header=""):
+ """Return True if the value is in the show configuration list.
+ If the show value is empty, return True (show by default)
+
+ The show configuration list is defined in the glances.conf file.
+ It is a comma separed list of regexp.
+ Example for diskio:
+ show=sda.*
+ """
+ # @TODO: possible optimisation: create a re.compile list
+ if self.get_conf_value('show', header=header) == []:
+ return True
+ else:
+ return any(j for j in [re.match(i, value) for i in self.get_conf_value('show', header=header)])
+
def is_hide(self, value, header=""):
"""Return True if the value is in the hide configuration list.
@@ -760,9 +830,7 @@ class GlancesPlugin(object):
Example for diskio:
hide=sda2,sda5,loop.*
"""
- # TODO: possible optimisation: create a re.compile list
- # Old version (see issue #1691)
- #return not all(j is None for j in [re.match(i, value.lower()) for i in self.get_conf_value('hide', header=header)])
+ # @TODO: possible optimisation: create a re.compile list
return any(j for j in [re.match(i, value) for i in self.get_conf_value('hide', header=header)])
def has_alias(self, header):