def run(self): logger.debug('Starting statistics cleanup') try: statsManager().cleanupCounters() except: logger.exception('Cleaning up counters') try: statsManager().cleanupEvents() except: logger.exception('Cleaning up events') logger.debug('Done statistics cleanup')
def getEvents(obj, eventType, **kwargs): """ Get events Args: obj: Obj for which to recover stats counters counterType: type of counter to recover since: (optional, defaults to 'Since beginning') Start date for counters to recover to: (optional, defaults to 'Until end') En date for counter to recover limit: (optional, defaults to 1000) Number of counter to recover. This is an 'At most' advice. The returned number of value can be lower, or even 1 more than requested due to a division for retrieving object at database all: (optinal), indicates that get all counters for the type of obj passed in, not only for that obj. Returns: A generator, that contains pairs of (stamp, value) tuples """ since = kwargs.get('since', NEVER) to = kwargs.get('to', datetime.datetime.now()) if kwargs.get('all', False) is True: owner_id = None else: owner_id = obj.pk for i in statsManager().getEvents(__transDict[type(obj)], eventType, owner_id=owner_id, since=since, to=to): val = (datetime.datetime.fromtimestamp(i.stamp), i.fld1, i.fld2, i.fld3, i.fld4, i.event_type) yield val
def addEvent(obj, eventType, **kwargs): """ Adds a event stat to specified object Although any counter type can be added to any object, there is a relation that must be observed or, otherway, the stats will not be recoverable at all: note: Runtime checks are done so if we try to insert an unssuported stat, this won't be inserted and it will be logged """ return statsManager().addEvent(__transDict[type(obj)], obj.id, eventType, **kwargs)
def addCounter(obj, counterType, counterValue, stamp=None): ''' Adds a counter stat to specified object Although any counter type can be added to any object, there is a relation that must be observed or, otherway, the stats will not be recoverable at all: note: Runtime checks are done so if we try to insert an unssuported stat, this won't be inserted and it will be logged ''' if type(obj) not in __caWrite.get(counterType, ()): logger.error('Type {0} does not accepts counter of type {1}'.format(type(obj), counterValue)) return False return statsManager().addCounter(__transDict[type(obj)], obj.id, counterType, counterValue, stamp)
def addCounter(obj: CounterClass, counterType: int, counterValue: int, stamp: typing.Optional[datetime.datetime] = None) -> bool: """ Adds a counter stat to specified object Although any counter type can be added to any object, there is a relation that must be observed or, otherway, the stats will not be recoverable at all: note: Runtime checks are done so if we try to insert an unssuported stat, this won't be inserted and it will be logged """ type_ = type(obj) if type_ not in __caWrite.get(counterType, ()): # pylint: disable logger.error('Type %s does not accepts counter of type %s', type_, counterValue) return False return statsManager().addCounter(__transDict[type(obj)], obj.id, counterType, counterValue, stamp)
def getCounters( obj: CounterClass, counterType: int, **kwargs ) -> typing.Generator[typing.Tuple[datetime.datetime, int], None, None]: """ Get counters Args: obj: Obj for which to recover stats counters counterType: type of counter to recover since: (optional, defaults to 'Since beginning') Start date for counters to recover to: (optional, defaults to 'Until end') En date for counter to recover limit: (optional, defaults to 1000) Number of counter to recover. This is an 'At most' advice. The returned number of value can be lower, or even 1 more than requested due to a division for retrieving object at database Returns: A generator, that contains pairs of (stamp, value) tuples """ since = kwargs.get('since') or NEVER to = kwargs.get('to') or datetime.datetime.now() limit = kwargs.get('limit') use_max = kwargs.get('use_max', False) type_ = type(obj) readFncTbl = __caRead.get(type_) if not readFncTbl: logger.error('Type %s has no registered stats', type_) return fnc = readFncTbl.get(counterType) if not fnc: logger.error('Type %s has no registerd stats of type %s', type_, counterType) return if not kwargs.get('all', False): owner_ids = fnc(obj) else: owner_ids = None for i in statsManager().getCounters(__transDict[type(obj)], counterType, owner_ids, since, to, kwargs.get('interval'), kwargs.get('max_intervals'), limit, use_max): yield (datetime.datetime.fromtimestamp(i.stamp), i.value)
def getCounters(obj: typing.Any, counterType: int, **kwargs): """ Get counters Args: obj: Obj for which to recover stats counters counterType: type of counter to recover since: (optional, defaults to 'Since beginning') Start date for counters to recover to: (optional, defaults to 'Until end') En date for counter to recover limit: (optional, defaults to 1000) Number of counter to recover. This is an 'At most' advice. The returned number of value can be lower, or even 1 more than requested due to a division for retrieving object at database all: (optinal), indicates that get all counters for the type of obj passed in, not only for that obj. Returns: A generator, that contains pairs of (stamp, value) tuples """ from uds.models import NEVER since = kwargs.get('since', NEVER) to = kwargs.get('to', datetime.datetime.now()) limit = kwargs.get('limit', 1000) use_max = kwargs.get('use_max', False) type_ = type(obj) readFncTbl = __caRead.get(type_) if not readFncTbl: logger.error('Type %s has no registered stats', type_) return fnc = readFncTbl.get(counterType) if not fnc: logger.error('Type %s has no registerd stats of type %s', type_, counterType) return if not kwargs.get('all', False): owner_ids = fnc(obj) else: owner_ids = None for i in statsManager().getCounters(__transDict[type(obj)], counterType, owner_ids, since, to, limit, use_max): val = (datetime.datetime.fromtimestamp(i.stamp), i.value) yield val
def addCounter(obj, counterType, counterValue, stamp=None): """ Adds a counter stat to specified object Although any counter type can be added to any object, there is a relation that must be observed or, otherway, the stats will not be recoverable at all: note: Runtime checks are done so if we try to insert an unssuported stat, this won't be inserted and it will be logged """ if type(obj) not in __caWrite.get(counterType, ()): logger.error('Type {0} does not accepts counter of type {1}'.format( type(obj), counterValue)) return False return statsManager().addCounter(__transDict[type(obj)], obj.id, counterType, counterValue, stamp)
def getCounters(obj, counterType, **kwargs): ''' Get counters Args: obj: Obj for which to recover stats counters counterType: type of counter to recover since: (optional, defaults to 'Since beginning') Start date for counters to recover to: (optional, defaults to 'Until end') En date for counter to recover limit: (optional, defaults to 1000) Number of counter to recover. This is an 'At most' advice. The returned number of value can be lower, or even 1 more than requested due to a division for retrieving object at database all: (optinal), indicates that get all counters for the type of obj passed in, not only for that obj. Returns: A generator, that contains pairs of (stamp, value) tuples ''' since = kwargs.get('since', NEVER) to = kwargs.get('to', datetime.datetime.now()) limit = kwargs.get('limit', 1000) use_max = kwargs.get('use_max', False) readFncTbl = __caRead.get(type(obj), None) if readFncTbl is None: logger.error('Type {0} has no registered stats'.format(type(obj))) return fnc = readFncTbl.get(counterType, None) if fnc is None: logger.error('Type {0} has no registerd stats of type {1}'.format(type(obj), counterType)) return if kwargs.get('all', None) is not True: owner_ids = fnc(obj) else: owner_ids = None for i in statsManager().getCounters(__transDict[type(obj)], counterType, owner_ids, since, to, limit, use_max): val = (datetime.datetime.fromtimestamp(i.stamp), i.value) yield val