def generate(self) -> typing.Any: since = self.date_start.date() to = self.date_end.date() interval = self.getIntervalInHours() * 3600 stats = [] for a in self.getModelItems(): # Will show a.name on every change... stats.append({'date': a.name, 'users': None}) services = 0 userServices = 0 servicesCounterIter = iter(counters.getCounters(a, counters.CT_AUTH_SERVICES, since=since, interval=interval, limit=MAX_ELEMENTS, use_max=True)) usersWithServicesCounterIter = iter(counters.getCounters(a, counters.CT_AUTH_USERS_WITH_SERVICES, since=since, interval=interval, limit=MAX_ELEMENTS, use_max=True)) for userCounter in counters.getCounters(a, counters.CT_AUTH_USERS, since=since, interval=interval, limit=MAX_ELEMENTS, use_max=True): try: while True: servicesCounter = next(servicesCounterIter) if servicesCounter[0] >= userCounter[0]: break if userCounter[0] == servicesCounter[0]: services = servicesCounter[1] except StopIteration: pass try: while True: uservicesCounter = next(usersWithServicesCounterIter) if uservicesCounter[0] >= userCounter[0]: break if userCounter[0] == uservicesCounter[0]: userServices = uservicesCounter[1] except StopIteration: pass stats.append({ 'date': userCounter[0], 'users': userCounter[1] or 0, 'services': services, 'user_services': userServices }) logger.debug('Report Data Done') return self.templateAsPDF( 'uds/reports/stats/authenticator_stats.html', dct={ 'data': stats }, header=ugettext('Users usage list'), water=ugettext('UDS Report of users usage') )
def getServicesPoolsCounters(servicePool, counter_type): # pylint: disable=no-value-for-parameter try: cacheKey = (servicePool and servicePool.id or 'all') + str(counter_type) + str(POINTS) + str(SINCE) to = getSqlDatetime() since = to - timedelta(days=SINCE) val = cache.get(cacheKey) if val is None: if servicePool is None: us = DeployedService() complete = True # Get all deployed services stats else: us = servicePool complete = False val = [] for x in counters.getCounters(us, counter_type, since=since, to=to, limit=POINTS, use_max=USE_MAX, all=complete): val.append({'stamp': x[0], 'value': int(x[1])}) if len(val) > 2: cache.put(cacheKey, pickle.dumps(val).encode('zip'), 600) else: val = [{'stamp': since, 'value': 0}, {'stamp': to, 'value': 0}] else: val = pickle.loads(val.decode('zip')) return val except: logger.exception('exception') raise ResponseError('can\'t create stats for objects!!!')
def getData(self) -> typing.List[typing.Dict[str, typing.Any]]: # Generate the sampling intervals and get dataUsers from db start = self.startDate.date() end = self.startDate.date() + datetime.timedelta(days=1) data = [] pool: ServicePool for poolUuid in self.pools.value: try: pool = ServicePool.objects.get(uuid=poolUuid) except Exception: continue hours = [0] * 24 for x in counters.getCounters(pool, counters.CT_ASSIGNED, since=start, to=end, max_intervals=24, use_max=True, all=False): hour = x[0].hour val = int(x[1]) if hours[hour] < val: hours[hour] = val data.append({'uuid': pool.uuid, 'name': pool.name, 'hours': hours}) logger.debug('data: %s', data) return data
def getData(self): # Generate the sampling intervals and get dataUsers from db start = self.startDate.date() end = self.startDate.date() + datetime.timedelta(days=1) data = [] for poolUuid in self.pools.value: try: pool = ServicePool.objects.get(uuid=poolUuid) except Exception: pass # Ignore pool hours = {} for i in range(24): hours[i] = 0 for x in counters.getCounters(pool, counters.CT_ASSIGNED, since=start, to=end, limit=24, use_max=True, all=False): hour = x[0].hour val = int(x[1]) if hours[hour] < val: hours[hour] = val data.append({'uuid':pool.uuid, 'name': pool.name, 'hours': hours}) logger.debug('data: {}'.format(data)) return data
def getServicesPoolsCounters(servicePool, counter_type): # pylint: disable=no-value-for-parameter try: cacheKey = (servicePool and servicePool.id or 'all') + str(counter_type) + str(POINTS) + str(SINCE) to = getSqlDatetime() since = to - timedelta(days=SINCE) val = cache.get(cacheKey) if val is None: if servicePool is None: us = ServicePool() complete = True # Get all deployed services stats else: us = servicePool complete = False val = [] for x in counters.getCounters(us, counter_type, since=since, to=to, limit=POINTS, use_max=USE_MAX, all=complete): val.append({'stamp': x[0], 'value': int(x[1])}) if len(val) > 2: cache.put(cacheKey, encoders.encode(pickle.dumps(val), 'zip') , 600) else: val = [{'stamp': since, 'value': 0}, {'stamp': to, 'value': 0}] else: val = pickle.loads(encoders.decode(val, 'zip')) return val except: logger.exception('exception') raise ResponseError('can\'t create stats for objects!!!')
def getServicesPoolsCounters( servicePool: typing.Optional[models.ServicePool], counter_type: int) -> typing.List[typing.Mapping[str, typing.Any]]: try: cacheKey = ((servicePool and str(servicePool.id) or 'all') + str(counter_type) + str(POINTS) + str(SINCE)) to = models.getSqlDatetime() since: datetime.datetime = to - datetime.timedelta(days=SINCE) cachedValue: typing.Optional[bytes] = cache.get(cacheKey) if not cachedValue: if not servicePool: us = models.ServicePool() complete = True # Get all deployed services stats else: us = servicePool complete = False val: typing.List[typing.Mapping[str, typing.Any]] = [] for x in counters.getCounters( us, counter_type, since=since, to=to, max_intervals=POINTS, use_max=USE_MAX, all=complete, ): val.append({'stamp': x[0], 'value': int(x[1])}) if len(val) > 2: cache.put(cacheKey, codecs.encode(pickle.dumps(val), 'zip'), 600) else: val = [{'stamp': since, 'value': 0}, {'stamp': to, 'value': 0}] else: val = pickle.loads(codecs.decode(cachedValue, 'zip')) # return [{'stamp': since + datetime.timedelta(hours=i*10), 'value': i*i} for i in range(300)] return val except: logger.exception('exception') raise ResponseError('can\'t create stats for objects!!!')
def getServicesPoolsCounters( servicePool: typing.Optional[models.ServicePool], counter_type: int) -> typing.List[typing.Dict[str, typing.Any]]: # pylint: disable=no-value-for-parameter try: cacheKey = (servicePool and servicePool.id or 'all') + str(counter_type) + str(POINTS) + str(SINCE) to = models.getSqlDatetime() since: datetime.datetime = to - datetime.timedelta(days=SINCE) val: typing.Any = cache.get(cacheKey) if not val: if not servicePool: us = models.ServicePool() complete = True # Get all deployed services stats else: us = servicePool complete = False val = [] for x in counters.getCounters(us, counter_type, since=since, to=to, max_intervals=POINTS, use_max=USE_MAX, all=complete): val.append({'stamp': x[0], 'value': int(x[1])}) if len(val) > 2: cache.put(cacheKey, encoders.encode(pickle.dumps(val), 'zip'), 600) else: val = [{'stamp': since, 'value': 0}, {'stamp': to, 'value': 0}] else: val = pickle.loads(typing.cast(bytes, encoders.decode(val, 'zip'))) return val except: logger.exception('exception') raise ResponseError('can\'t create stats for objects!!!')