Change the way to store the value in the history

pull/893/head
nicolargo 2016-05-29 14:38:44 +02:00
parent ad0a6ee34e
commit fd23868c4b
4 changed files with 25 additions and 31 deletions

View File

@ -2,7 +2,7 @@
# #
# This file is part of Glances. # This file is part of Glances.
# #
# Copyright (C) 2015 Nicolargo <nicolas@nicolargo.com> # Copyright (C) 2016 Nicolargo <nicolas@nicolargo.com>
# #
# Glances is free software; you can redistribute it and/or modify # 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 # it under the terms of the GNU Lesser General Public License as published by
@ -19,24 +19,24 @@
"""Attribute class.""" """Attribute class."""
from time import time from datetime import datetime
class GlancesAttribute(object): class GlancesAttribute(object):
def __init__(self, name, description='', history_max_size=None, is_rate=False): def __init__(self, name, description='', history_max_size=None):
"""Init the attribute """Init the attribute
name: Attribute name (string) name: Attribute name (string)
description: Attribute human reading description (string) description: Attribute human reading description (string)
history_max_size: Maximum size of the history list (default is no limit) history_max_size: Maximum size of the history list (default is no limit)
is_rate: If True then the value is manage like a rate (store timestamp in the history)
History is stored as a list for tuple: [(date, value), ...]
""" """
self._name = name self._name = name
self._description = description self._description = description
self._value = None self._value = None
self._history_max_size = history_max_size self._history_max_size = history_max_size
self._history = [] self._history = []
self.is_rate = is_rate
def __repr__(self): def __repr__(self):
return self.value return self.value
@ -71,25 +71,18 @@ class GlancesAttribute(object):
""" """
@property @property
def value(self): def value(self):
if self.is_rate: if self.history_len() > 0:
if self.history_len() > 0: return (self._value[1] - self.history_value()[1]) / (self._value[0] - self.history_value()[0])
return (self._value[1] - self.history_value()[1]) / (self._value[0] - self.history_value()[0])
else:
return None
else: else:
return self._value return None
@value.setter @value.setter
def value(self, new_value): def value(self, new_value):
"""Set a value. """Set a value.
If self.is_rate is True, store a tuple with (<timestamp>, value) Value is a tuple: (<timestamp>, <new_value>)
else, store directly the value (wathever type is it)
""" """
if self.is_rate: self._value = (datetime.now(), new_value)
new_value = (time(), new_value) self.history_add(self._value)
if self._value is not None:
self.history_add(self._value)
self._value = new_value
""" """
Properties for the attribute history Properties for the attribute history
@ -136,8 +129,5 @@ class GlancesAttribute(object):
def history_mean(self, nb=5): def history_mean(self, nb=5):
"""Return the mean on the <nb> values in the history. """Return the mean on the <nb> values in the history.
""" """
if self.is_rate: h_sum = map(sum, zip(*self._history[-nb:]))
h_sum = map(sum, zip(*self._history[-nb:])) return h_sum[1] / float(self._history[-1][0] - self._history[-nb][0])
return h_sum[1] / float(self._history[-1][0] - self._history[-nb][0])
else:
return sum(self._history[-nb:]) / float(nb)

View File

@ -121,7 +121,9 @@ class GlancesGraph(object):
plt.ylabel(self.get_graph_yunit(i, pre_label='')) plt.ylabel(self.get_graph_yunit(i, pre_label=''))
# Curves # Curves
plt.grid(True) plt.grid(True)
plt.plot_date(h['date'], h[i['name']], # Points are stored as tuple (date, value)
x, y = zip(*h[i['name']])
plt.plot_date(x, y,
fmt='', drawstyle='default', linestyle='-', fmt='', drawstyle='default', linestyle='-',
color=self.get_graph_color(i), color=self.get_graph_color(i),
xdate=True, ydate=False) xdate=True, ydate=False)
@ -145,9 +147,13 @@ class GlancesGraph(object):
index_item += 1 index_item += 1
plt.subplot( plt.subplot(
len(stats_history_filtered), 1, index_item) len(stats_history_filtered), 1, index_item)
# Legend
plt.ylabel(self.get_graph_yunit(i, pre_label=k)) plt.ylabel(self.get_graph_yunit(i, pre_label=k))
# Curves
plt.grid(True) plt.grid(True)
plt.plot_date(h['date'], h[k], # Points are stored as tuple (date, value)
x, y = zip(*h[k])
plt.plot_date(x, y,
fmt='', drawstyle='default', linestyle='-', fmt='', drawstyle='default', linestyle='-',
color=self.get_graph_color(i), color=self.get_graph_color(i),
xdate=True, ydate=False) xdate=True, ydate=False)

View File

@ -26,7 +26,7 @@ class GlancesHistory(object):
"""This class manage a dict of GlancesAttribute """This class manage a dict of GlancesAttribute
- key: stats name - key: stats name
- GlancesAttribute: history value""" - value: GlancesAttribute"""
def __init__(self): def __init__(self):
""" """
@ -36,14 +36,12 @@ class GlancesHistory(object):
def add(self, key, value, def add(self, key, value,
description='', description='',
history_max_size=None, history_max_size=None):
is_rate=False):
"""Add an new item (key, value) to the current history.""" """Add an new item (key, value) to the current history."""
if key not in self.stats_history: if key not in self.stats_history:
self.stats_history[key] = GlancesAttribute(key, self.stats_history[key] = GlancesAttribute(key,
description=description, description=description,
history_max_size=history_max_size, history_max_size=history_max_size)
is_rate=is_rate)
self.stats_history[key].value = value self.stats_history[key].value = value
def reset(self): def reset(self):

View File

@ -106,7 +106,7 @@ class GlancesPlugin(object):
if (self.stats and self.args is not None and if (self.stats and self.args is not None and
self.args.export_graph and self.args.export_graph and
self.get_items_history_list() is not None): self.get_items_history_list() is not None):
self.stats_history.add('date', datetime.now()) # self.stats_history.add('date', datetime.now())
for i in self.get_items_history_list(): for i in self.get_items_history_list():
if isinstance(self.stats, list): if isinstance(self.stats, list):
# Stats is a list of data # Stats is a list of data