123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- import logging
- import numbers
- import os
- import time
-
- logger_name = 'HELPERS'
- logger = logging.getLogger(logger_name)
-
-
- class continues_statistic(dict):
- KEY_SINGLE_VALUE = '_'
- KEY_MEAN_VALUE = 'mean_val'
- KEY_MIN_VALUE = 'min_val'
- KEY_MAX_VALUE = 'max_val'
- KEY_NUM_VALUES = 'num_val'
- """
- Value can be a single single numeric value: continues_statistic(num) or multivalue statistic: continues_statistic(val_a=continues_statistic(num_a), val_b=continues_statistic(num_b)).
- """
- def __init__(self, *args, **kwargs):
- if len(args) == 1 and isinstance(args[0], numbers.Number):
- dict.__init__(self)
- self[self.KEY_SINGLE_VALUE] = {
- self.KEY_MIN_VALUE: args[0],
- self.KEY_MEAN_VALUE: args[0],
- self.KEY_MAX_VALUE: args[0],
- self.KEY_NUM_VALUES: 1,
- }
- else:
- for key in kwargs:
- if isinstance(kwargs[key], numbers.Number):
- kwargs[key] = continues_statistic(kwargs[key])
- dict.__init__(self, *args, **kwargs)
- for key in self:
- if type(self[key]) is not continues_statistic:
- data = continues_statistic()
- if key == self.KEY_SINGLE_VALUE:
- data[self.KEY_SINGLE_VALUE] = self[key]
- else:
- data[self.KEY_SINGLE_VALUE] = self[key][self.KEY_SINGLE_VALUE]
- self[key] = data
-
- @property
- def __multivalue__(self):
- if len(self.keys()) == 0:
- raise AttributeError('No data stored in continues statistic')
- elif len(self.keys()) == 1:
- if list(self.keys())[0] != self.KEY_SINGLE_VALUE:
- raise AttributeError("The single value key needs to be %s." % self.KEY_SINGLE_VALUE)
- return False
- return True
-
- def __add__(self, other):
- rv = continues_statistic(self)
- if self.__multivalue__:
- if self.__multivalue__ != other.__multivalue__ or self.keys() != other.keys():
- raise AttributeError("Instances are incompatible")
- else:
- for key in rv:
- rv[key] += other[key]
- else:
- rv[self.KEY_SINGLE_VALUE][self.KEY_MEAN_VALUE] = (self[self.KEY_SINGLE_VALUE][self.KEY_MEAN_VALUE] * self[self.KEY_SINGLE_VALUE][self.KEY_NUM_VALUES] + other[self.KEY_SINGLE_VALUE][self.KEY_MEAN_VALUE] * other[self.KEY_SINGLE_VALUE][self.KEY_NUM_VALUES]) / (self[self.KEY_SINGLE_VALUE][self.KEY_NUM_VALUES] + other[self.KEY_SINGLE_VALUE][self.KEY_NUM_VALUES])
- rv[self.KEY_SINGLE_VALUE][self.KEY_MIN_VALUE] = min(self[self.KEY_SINGLE_VALUE][self.KEY_MIN_VALUE], other[self.KEY_SINGLE_VALUE][self.KEY_MIN_VALUE])
- rv[self.KEY_SINGLE_VALUE][self.KEY_MAX_VALUE] = max(self[self.KEY_SINGLE_VALUE][self.KEY_MAX_VALUE], other[self.KEY_SINGLE_VALUE][self.KEY_MAX_VALUE])
- rv[self.KEY_SINGLE_VALUE][self.KEY_NUM_VALUES] = self[self.KEY_SINGLE_VALUE][self.KEY_NUM_VALUES]+other[self.KEY_SINGLE_VALUE][self.KEY_NUM_VALUES]
- return rv
-
- def __str__(self):
- output_for_value = "mean=%%(%s)s, min=%%(%s)s, max=%%(%s)s, num=%%(%s)s" % (self.KEY_MEAN_VALUE, self.KEY_MIN_VALUE, self.KEY_MAX_VALUE, self.KEY_NUM_VALUES)
- if self.__multivalue__:
- rv = ""
- for key in self:
- rv += key + ': ' + str(self[key]) + "\n"
- return rv
- else:
- return output_for_value % self[self.KEY_SINGLE_VALUE]
-
-
- class ringbuffer(list):
- def __init__(self, *args, **kwargs):
- self.__max_length__ = kwargs.pop('length')
- list.__init__(self, *args, **kwargs)
- self.__reduce_list__()
-
- def __reduce_list__(self):
- while len(self) > self.__max_length__:
- self.pop(0)
-
- def append(self, *args, **kwargs):
- rv = list.append(self, *args, **kwargs)
- self.__reduce_list__()
- return rv
-
-
- class data_collector(object):
- def __init__(self, bufferlength, current_max_age=300.):
- self.__current_max_age__ = current_max_age
- self.__current_data_rinbuffer__ = ringbuffer(length=3)
- #
- self.__last_add_data__ = 0
- self.__continues_statistic__ = None
- self.__yesterdays_statistic__ = None
- self.__measurement_day__ = None
- #
- self.__ringbuffer__ = ringbuffer(length=bufferlength)
-
- def __process_continues_statistic__(self, **kwargs):
- # rotate continues_statistic day by day
- today = time.strftime('%Y' + os.path.sep + '%m' + os.path.sep + '%d')
- if self.__measurement_day__ is None:
- self.__measurement_day__ = today
- if self.__measurement_day__ != today:
- self.__yesterdays_statistic__ = self.__continues_statistic__
- self.__continues_statistic__ = None
- self.__measurement_day__ = None
- # Set or update continues statistic
- if self.__continues_statistic__ is None:
- self.__continues_statistic__ = continues_statistic(**kwargs)
- else:
- self.__continues_statistic__ += continues_statistic(**kwargs)
-
-
- def add_data(self, **kwargs):
- logger.debug('Got data %s', repr(kwargs))
- self.__last_add_data__ = time.time()
- self.__current_data_rinbuffer__.append(kwargs)
- self.__process_continues_statistic__(**kwargs)
- self.__ringbuffer__.append(kwargs)
-
- @property
- def current(self):
- if len(self.__current_data_rinbuffer__) == 0:
- logger.info('No data available, yet.')
- return None
- elif time.time() > self.__last_add_data__ + self.__current_max_age__:
- logger.info('Data max age reached. No current data available.')
- return None
- elif len(self.__current_data_rinbuffer__) < 3:
- return self.__current_data_rinbuffer__[-1]
- else:
- rv = {}
- for key in self.__current_data_rinbuffer__[0]:
- data = []
- for entry in self.__current_data_rinbuffer__:
- data.append(entry[key])
- else:
- data.sort()
- rv[key] = data[1]
- return rv
-
- @property
- def todays_statistic(self):
- return self.__continues_statistic__
-
- @property
- def yesterdays_statistic(self):
- return self.__yesterdays_statistic__
-
- @property
- def databuffer(self):
- return self.__ringbuffer__[:]
|