You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.9 KiB

4 years ago
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Glances.
  4. #
  5. # Copyright (C) 2018 Nicolargo <nicolas@nicolargo.com>
  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. """Attribute class."""
  20. from datetime import datetime
  21. class GlancesAttribute(object):
  22. def __init__(self, name, description='', history_max_size=None):
  23. """Init the attribute
  24. name: Attribute name (string)
  25. description: Attribute human reading description (string)
  26. history_max_size: Maximum size of the history list (default is no limit)
  27. History is stored as a list for tuple: [(date, value), ...]
  28. """
  29. self._name = name
  30. self._description = description
  31. self._value = None
  32. self._history_max_size = history_max_size
  33. self._history = []
  34. def __repr__(self):
  35. return self.value
  36. def __str__(self):
  37. return str(self.value)
  38. """
  39. Properties for the attribute name
  40. """
  41. @property
  42. def name(self):
  43. return self._name
  44. @name.setter
  45. def name(self, new_name):
  46. self._name = new_name
  47. """
  48. Properties for the attribute description
  49. """
  50. @property
  51. def description(self):
  52. return self._description
  53. @description.setter
  54. def description(self, new_description):
  55. self._description = new_description
  56. """
  57. Properties for the attribute value
  58. """
  59. @property
  60. def value(self):
  61. if self.history_len() > 0:
  62. return (self._value[1] - self.history_value()[1]) / (self._value[0] - self.history_value()[0])
  63. else:
  64. return None
  65. @value.setter
  66. def value(self, new_value):
  67. """Set a value.
  68. Value is a tuple: (<timestamp>, <new_value>)
  69. """
  70. self._value = (datetime.now(), new_value)
  71. self.history_add(self._value)
  72. """
  73. Properties for the attribute history
  74. """
  75. @property
  76. def history(self):
  77. return self._history
  78. @history.setter
  79. def history(self, new_history):
  80. self._history = new_history
  81. @history.deleter
  82. def history(self):
  83. del self._history
  84. def history_reset(self):
  85. self._history = []
  86. def history_add(self, value):
  87. """Add a value in the history
  88. """
  89. if self._history_max_size is None or self.history_len() < self._history_max_size:
  90. self._history.append(value)
  91. else:
  92. self._history = self._history[1:] + [value]
  93. def history_size(self):
  94. """Return the history size (maximum nuber of value in the history)
  95. """
  96. return len(self._history)
  97. def history_len(self):
  98. """Return the current history lenght
  99. """
  100. return len(self._history)
  101. def history_value(self, pos=1):
  102. """Return the value in position pos in the history.
  103. Default is to return the latest value added to the history.
  104. """
  105. return self._history[-pos]
  106. def history_raw(self, nb=0):
  107. """Return the history in ISO JSON format"""
  108. return self._history[-nb:]
  109. def history_json(self, nb=0):
  110. """Return the history in ISO JSON format"""
  111. return [(i[0].isoformat(), i[1]) for i in self._history[-nb:]]
  112. def history_mean(self, nb=5):
  113. """Return the mean on the <nb> values in the history.
  114. """
  115. _, v = zip(*self._history)
  116. return sum(v[-nb:]) / float(v[-1] - v[-nb])