diff --git a/server/src/uds/core/managers/StatsManager.py b/server/src/uds/core/managers/StatsManager.py index d3d128a2a..d3a0242c2 100644 --- a/server/src/uds/core/managers/StatsManager.py +++ b/server/src/uds/core/managers/StatsManager.py @@ -112,7 +112,7 @@ class StatsManager(object): ''' pass - def getCounters(self, fromWhat, counterType, **kwargs): + def getCounters(self, ownerType, counterType, ownerIds, since, to, limit): ''' Retrieves counters from item @@ -130,7 +130,7 @@ class StatsManager(object): ''' from uds.models import StatsCounters - StatsCounters.get_grouped(None, counterType) + return StatsCounters.get_grouped(ownerType, counterType, owner_id = ownerIds, since = since, to = to, limit = limit) def cleanupCounter(self): diff --git a/server/src/uds/core/util/stats/counters.py b/server/src/uds/core/util/stats/counters.py index 75a141053..232fb6652 100644 --- a/server/src/uds/core/util/stats/counters.py +++ b/server/src/uds/core/util/stats/counters.py @@ -53,12 +53,8 @@ def addCounter(obj, counterType, counterValue, stamp = None): 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: - Object Type Valid Counters Type - ---------- ------------------- - Provider CT_LOAD, CT_STORAGE - Service None Right now - DeployedService CT_ASSIGNED, CT_CACHE1, CT_CACHE2, CT_INUSE, CT_ERROR + 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)) @@ -68,15 +64,43 @@ def addCounter(obj, counterType, counterValue, stamp = None): def getCounters(obj, counterType, **kwargs): + ''' + Get counters - fnc = __caRead.get(type) + 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', None) + to = kwargs.get('to', None) + limit = kwargs.get('limit', 1000) + + readFncTbl = __caRead.get(type(obj), None) - pass - - - - + + 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 + + owner_ids = fnc(obj) + + for i in statsManager().getCounters(__transDict[type(obj)], counterType, owner_ids, since, to, limit): + val = (i.stamp, i.value) + yield val # Data initialization def _initializeData(): @@ -88,6 +112,7 @@ def _initializeData(): from uds.models import Provider, Service, DeployedService global __caWrite + global __caRead global __transDict __caWrite = { diff --git a/server/src/uds/models.py b/server/src/uds/models.py index 6c3b4b74d..ed749f90d 100644 --- a/server/src/uds/models.py +++ b/server/src/uds/models.py @@ -1627,17 +1627,27 @@ class StatsCounters(models.Model): owner_id = None if kwargs.has_key('owner_id'): - owner_id = kwargs['owner_id'] - filt = ' AND owner_id='+str(owner_id) + filt += ' AND OWNER_ID' + oid = kwargs['owner_id'] + if type(oid) in (list, tuple): + filt += ' in (' + ','.join(str(x) for x in oid) + ')' + else: + filt += '='+str(oid) filt += ' AND counter_type='+str(counter_type) - - since = int(kwargs.get('since', NEVER_UNIX)) - to = int(kwargs.get('to', getSqlDatetime(True))) + + since = kwargs.get('since', None) + to = kwargs.get('to', None) + + since = since and int(since) or NEVER_UNIX + to = to and int(to) or getSqlDatetime(True) interval = 600 # By default, group items in ten minutes interval (600 seconds) - if kwargs.has_key('limit'): + limit = kwargs.get('limit', None) + + if limit is not None: + limit = int(limit) elements = kwargs['limit'] # Protect for division a few lines below... :-)