Change the way to store the value in the history

This commit is contained in:
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.
#
# 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
# it under the terms of the GNU Lesser General Public License as published by
@ -19,24 +19,24 @@
"""Attribute class."""
from time import time
from datetime import datetime
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
name: Attribute name (string)
description: Attribute human reading description (string)
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._description = description
self._value = None
self._history_max_size = history_max_size
self._history = []
self.is_rate = is_rate
def __repr__(self):
return self.value
@ -71,25 +71,18 @@ class GlancesAttribute(object):
"""
@property
def value(self):
if self.is_rate:
if self.history_len() > 0:
return (self._value[1] - self.history_value()[1]) / (self._value[0] - self.history_value()[0])
else:
return None
if self.history_len() > 0:
return (self._value[1] - self.history_value()[1]) / (self._value[0] - self.history_value()[0])
else:
return self._value
return None
@value.setter
def value(self, new_value):
"""Set a value.
If self.is_rate is True, store a tuple with (<timestamp>, value)
else, store directly the value (wathever type is it)
Value is a tuple: (<timestamp>, <new_value>)
"""
if self.is_rate:
new_value = (time(), new_value)
if self._value is not None:
self.history_add(self._value)
self._value = new_value
self._value = (datetime.now(), new_value)
self.history_add(self._value)
"""
Properties for the attribute history
@ -136,8 +129,5 @@ class GlancesAttribute(object):
def history_mean(self, nb=5):
"""Return the mean on the <nb> values in the history.
"""
if self.is_rate:
h_sum = map(sum, zip(*self._history[-nb:]))
return h_sum[1] / float(self._history[-1][0] - self._history[-nb][0])
else:
return sum(self._history[-nb:]) / float(nb)
h_sum = map(sum, zip(*self._history[-nb:]))
return h_sum[1] / float(self._history[-1][0] - self._history[-nb][0])

View File

@ -121,7 +121,9 @@ class GlancesGraph(object):
plt.ylabel(self.get_graph_yunit(i, pre_label=''))
# Curves
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='-',
color=self.get_graph_color(i),
xdate=True, ydate=False)
@ -145,9 +147,13 @@ class GlancesGraph(object):
index_item += 1
plt.subplot(
len(stats_history_filtered), 1, index_item)
# Legend
plt.ylabel(self.get_graph_yunit(i, pre_label=k))
# Curves
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='-',
color=self.get_graph_color(i),
xdate=True, ydate=False)

View File

@ -26,7 +26,7 @@ class GlancesHistory(object):
"""This class manage a dict of GlancesAttribute
- key: stats name
- GlancesAttribute: history value"""
- value: GlancesAttribute"""
def __init__(self):
"""
@ -36,14 +36,12 @@ class GlancesHistory(object):
def add(self, key, value,
description='',
history_max_size=None,
is_rate=False):
history_max_size=None):
"""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)
history_max_size=history_max_size)
self.stats_history[key].value = value
def reset(self):

View File

@ -106,7 +106,7 @@ class GlancesPlugin(object):
if (self.stats and self.args is not None and
self.args.export_graph and
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():
if isinstance(self.stats, list):
# Stats is a list of data