diff --git a/NEWS b/NEWS index b0440e3a..a69d5a67 100644 --- a/NEWS +++ b/NEWS @@ -5,6 +5,13 @@ Glances Version 2 Version 2.7 =========== +Deprecated: + + * Drop Python 2.6 support (issue #300) + * Monitoring process list module is replaced by AMP (see issue #780) + * Use --export-graph instead of --enable-history (issue #696) + * Use --path-graph instead of --path-history (issue #696) + Enhancements and new features: * Add Application Monitoring Process plugin (issue #780) @@ -15,6 +22,7 @@ Enhancements and new features: * [Web UI] Add cpu name in quicklook plugin (issue #825) * Allow theme to be set in configuration file (issue #862) * Display a warning message when Glances is outdated (issue #865) + * Refactor stats history and export to graph (issue #696) Bugs corrected: @@ -24,13 +32,6 @@ Bugs corrected: * Idle process is back on FreeBSD and Windows (issue #844) * Top 3 processes are back in the alert summay -Deprecated: - - * Drop Python 2.6 support (issue #300) - * Monitoring process list module is replaced by AMP (see issue #780) - * Use --export-graph instead of --enable-history (issue #696) - * Use --path-graph instead of --path-history (issue #696) - Version 2.6.1 ============= diff --git a/glances/history.py b/glances/history.py new file mode 100644 index 00000000..027ce755 --- /dev/null +++ b/glances/history.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +# +# This file is part of Glances. +# +# Copyright (C) 2016 Nicolargo +# +# Glances is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Glances is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program. If not, see . + +"""Manage stats history""" + +from glances.attribute import GlancesAttribute + + +class GlancesHistory(object): + + """This class manage a dict of GlancesAttribute + - key: stats name + - GlancesAttribute: history value""" + + def __init__(self): + """ + items_history_list: list of stats to historized (define inside plugins) + """ + self.stats_history = {} + + def add(self, key, value, + description='', + history_max_size=None, + is_rate=False): + """Add an new item (key, value) to the current history.""" + if key not in self.stats_history: + self.stats_history[key] = GlancesAttribute(key, + description=description, + history_max_size=history_max_size, + is_rate=is_rate) + self.stats_history[key].value = value + + def reset(self): + """Reset all the stats history""" + for a in self.stats_history: + self.stats_history[a].history_reset() + + def get(self): + """Get the history as a dict of list""" + return {i: self.stats_history[i].history for i in self.stats_history} diff --git a/glances/plugins/glances_plugin.py b/glances/plugins/glances_plugin.py index caf57947..6483ed9c 100644 --- a/glances/plugins/glances_plugin.py +++ b/glances/plugins/glances_plugin.py @@ -30,7 +30,7 @@ from operator import itemgetter from glances.compat import iterkeys, itervalues, listkeys, map from glances.actions import GlancesActions -from glances.attribute import GlancesAttribute +from glances.history import GlancesHistory from glances.logger import logger from glances.logs import glances_logs @@ -87,47 +87,33 @@ class GlancesPlugin(object): """Return the key of the list.""" return None - def add_item_history(self, key, value, - description='', - history_max_size=None, - is_rate=False): - """Add an new item (key, value) to the current history.""" - if key not in self.stats_history: - self.stats_history[key] = GlancesAttribute(key, - description=description, - history_max_size=history_max_size) - self.stats_history[key].value = value - def init_stats_history(self): """Init the stats history (dict of GlancesAttribute).""" - ret = {} if self.args is not None and self.args.export_graph and self.get_items_history_list() is not None: init_list = [a['name'] for a in self.get_items_history_list()] logger.debug("Stats history activated for plugin {0} (items: {1})".format(self.plugin_name, init_list)) - return ret + return GlancesHistory() def reset_stats_history(self): """Reset the stats history (dict of GlancesAttribute).""" if self.args is not None and self.args.export_graph and self.get_items_history_list() is not None: reset_list = [a['name'] for a in self.get_items_history_list()] logger.debug("Reset history for plugin {0} (items: {1})".format(self.plugin_name, reset_list)) - for a in self.stats_history: - self.stats_history[a].history_reset() + self.stats_history.reset() def update_stats_history(self, item_name=''): """Update stats history.""" if (self.stats and self.args is not None and self.args.export_graph and self.get_items_history_list() is not None): - # TODO in attribute ? - self.add_item_history('date', datetime.now()) + self.stats_history.add('date', datetime.now()) for i in self.get_items_history_list(): if isinstance(self.stats, list): # Stats is a list of data # Iter throught it (for exemple, iter throught network # interface) for l in self.stats: - self.add_item_history( + self.stats_history.add( l[item_name] + '_' + i['name'], l[i['name']], description=i['description'], @@ -135,17 +121,17 @@ class GlancesPlugin(object): else: # Stats is not a list # Add the item to the history directly - self.add_item_history(i['name'], - self.stats[i['name']], - description=i['description'], - history_max_size=None) + self.stats_history.add(i['name'], + self.stats[i['name']], + description=i['description'], + history_max_size=None) def get_stats_history(self): """Return the stats history (dict of list).""" - return {i: self.stats_history[i].history for i in self.stats_history} + return self.stats_history.get() def get_items_history_list(self): - """Return the items history list (define inside the plugins scripts).""" + """Return the items history list.""" return self.items_history_list @property