|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# |
|
3
|
|
|
# This file is part of Glances. |
|
4
|
|
|
# |
|
5
|
|
|
# Copyright (C) 2016 Nicolargo <[email protected]> |
|
6
|
|
|
# |
|
7
|
|
|
# Glances is free software; you can redistribute it and/or modify |
|
8
|
|
|
# it under the terms of the GNU Lesser General Public License as published by |
|
9
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
|
10
|
|
|
# (at your option) any later version. |
|
11
|
|
|
# |
|
12
|
|
|
# Glances is distributed in the hope that it will be useful, |
|
13
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
# GNU Lesser General Public License for more details. |
|
16
|
|
|
# |
|
17
|
|
|
# You should have received a copy of the GNU Lesser General Public License |
|
18
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19
|
|
|
|
|
20
|
|
|
"""Attribute class.""" |
|
21
|
|
|
|
|
22
|
|
|
from datetime import datetime |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
class GlancesAttribute(object): |
|
26
|
|
|
|
|
27
|
|
|
def __init__(self, name, description='', history_max_size=None): |
|
28
|
|
|
"""Init the attribute |
|
29
|
|
|
name: Attribute name (string) |
|
30
|
|
|
description: Attribute human reading description (string) |
|
31
|
|
|
history_max_size: Maximum size of the history list (default is no limit) |
|
32
|
|
|
|
|
33
|
|
|
History is stored as a list for tuple: [(date, value), ...] |
|
34
|
|
|
""" |
|
35
|
|
|
self._name = name |
|
36
|
|
|
self._description = description |
|
37
|
|
|
self._value = None |
|
38
|
|
|
self._history_max_size = history_max_size |
|
39
|
|
|
self._history = [] |
|
40
|
|
|
|
|
41
|
|
|
def __repr__(self): |
|
42
|
|
|
return self.value |
|
43
|
|
|
|
|
44
|
|
|
def __str__(self): |
|
45
|
|
|
return str(self.value) |
|
46
|
|
|
|
|
47
|
|
|
""" |
|
48
|
|
|
Properties for the attribute name |
|
49
|
|
|
""" |
|
50
|
|
|
@property |
|
51
|
|
|
def name(self): |
|
52
|
|
|
return self._name |
|
53
|
|
|
|
|
54
|
|
|
@name.setter |
|
55
|
|
|
def name(self, new_name): |
|
56
|
|
|
self._name = new_name |
|
57
|
|
|
|
|
58
|
|
|
""" |
|
59
|
|
|
Properties for the attribute description |
|
60
|
|
|
""" |
|
61
|
|
|
@property |
|
62
|
|
|
def description(self): |
|
63
|
|
|
return self._description |
|
64
|
|
|
|
|
65
|
|
|
@description.setter |
|
66
|
|
|
def description(self, new_description): |
|
67
|
|
|
self._description = new_description |
|
68
|
|
|
|
|
69
|
|
|
""" |
|
70
|
|
|
Properties for the attribute value |
|
71
|
|
|
""" |
|
72
|
|
|
@property |
|
73
|
|
|
def value(self): |
|
74
|
|
|
if self.history_len() > 0: |
|
75
|
|
|
return (self._value[1] - self.history_value()[1]) / (self._value[0] - self.history_value()[0]) |
|
76
|
|
|
else: |
|
77
|
|
|
return None |
|
78
|
|
|
|
|
79
|
|
|
@value.setter |
|
80
|
|
|
def value(self, new_value): |
|
81
|
|
|
"""Set a value. |
|
82
|
|
|
Value is a tuple: (<timestamp>, <new_value>) |
|
83
|
|
|
""" |
|
84
|
|
|
self._value = (datetime.now(), new_value) |
|
85
|
|
|
self.history_add(self._value) |
|
86
|
|
|
|
|
87
|
|
|
""" |
|
88
|
|
|
Properties for the attribute history |
|
89
|
|
|
""" |
|
90
|
|
|
@property |
|
91
|
|
|
def history(self): |
|
92
|
|
|
return self._history |
|
93
|
|
|
|
|
94
|
|
|
@history.setter |
|
95
|
|
|
def history(self, new_history): |
|
96
|
|
|
self._history = new_history |
|
97
|
|
|
|
|
98
|
|
|
@history.deleter |
|
99
|
|
|
def history(self): |
|
100
|
|
|
del self._history |
|
101
|
|
|
|
|
102
|
|
|
def history_reset(self): |
|
103
|
|
|
self._history = [] |
|
104
|
|
|
|
|
105
|
|
|
def history_add(self, value): |
|
106
|
|
|
"""Add a value in the history |
|
107
|
|
|
""" |
|
108
|
|
|
if self._history_max_size is None or self.history_len() < self._history_max_size: |
|
109
|
|
|
self._history.append(value) |
|
110
|
|
|
else: |
|
111
|
|
|
self._history = self._history[1:] + [value] |
|
112
|
|
|
|
|
113
|
|
|
def history_size(self): |
|
114
|
|
|
"""Return the history size (maximum nuber of value in the history) |
|
115
|
|
|
""" |
|
116
|
|
|
return len(self._history) |
|
117
|
|
|
|
|
118
|
|
|
def history_len(self): |
|
119
|
|
|
"""Return the current history lenght |
|
120
|
|
|
""" |
|
121
|
|
|
return len(self._history) |
|
122
|
|
|
|
|
123
|
|
|
def history_value(self, pos=1): |
|
124
|
|
|
"""Return the value in position pos in the history. |
|
125
|
|
|
Default is to return the latest value added to the history. |
|
126
|
|
|
""" |
|
127
|
|
|
return self._history[-pos] |
|
128
|
|
|
|
|
129
|
|
|
def history_json(self, nb=0): |
|
130
|
|
|
"""Return the history of last nb items (0 for all) In ISO JSON format""" |
|
131
|
|
|
return [(i[0].isoformat(), i[1]) for i in self._history[-nb:]] |
|
132
|
|
|
|
|
133
|
|
|
def history_mean(self, nb=5): |
|
134
|
|
|
"""Return the mean on the <nb> values in the history. |
|
135
|
|
|
""" |
|
136
|
|
|
d, v = zip(*self._history) |
|
137
|
|
|
return sum(v[-nb:]) / float(v[-1] - v[-nb]) |
|
138
|
|
|
|