more fixes to stats counters

This commit is contained in:
Adolfo Gómez García 2020-07-31 13:39:36 +02:00
parent 5602cca2b5
commit aecd933c98
4 changed files with 11 additions and 11 deletions

View File

@ -103,7 +103,8 @@ class StatsManager:
ownerIds: typing.Union[typing.Iterable[int], int], ownerIds: typing.Union[typing.Iterable[int], int],
since: datetime.datetime, since: datetime.datetime,
to: datetime.datetime, to: datetime.datetime,
max_intervals: int, interval: typing.Optional[int],
max_intervals: typing.Optional[int],
limit: typing.Optional[int], limit: typing.Optional[int],
use_max: bool = False use_max: bool = False
) -> typing.Iterable: ) -> typing.Iterable:
@ -132,6 +133,7 @@ class StatsManager:
owner_id=ownerIds, owner_id=ownerIds,
since=sinceInt, since=sinceInt,
to=toInt, to=toInt,
interval=interval,
max_intervals=max_intervals, max_intervals=max_intervals,
limit=limit, limit=limit,
use_max=use_max use_max=use_max

View File

@ -77,7 +77,6 @@ class Config:
self._default = cryptoManager().encrypt(default) self._default = cryptoManager().encrypt(default)
self._data: typing.Optional[str] = None self._data: typing.Optional[str] = None
def get(self, force: bool = False) -> str: def get(self, force: bool = False) -> str:
# Ensures DB contains configuration values # Ensures DB contains configuration values
# From Django 1.7, DB can only be accessed AFTER all apps are initialized, curious at least.. :) # From Django 1.7, DB can only be accessed AFTER all apps are initialized, curious at least.. :)

View File

@ -90,7 +90,6 @@ def getCounters(obj: typing.Any, counterType: int, **kwargs) -> typing.Generator
""" """
since = kwargs.get('since') or NEVER since = kwargs.get('since') or NEVER
to = kwargs.get('to') or datetime.datetime.now() to = kwargs.get('to') or datetime.datetime.now()
max_intervals = kwargs.get('max_intervals') or 1000
limit = kwargs.get('limit') limit = kwargs.get('limit')
use_max = kwargs.get('use_max', False) use_max = kwargs.get('use_max', False)
type_ = type(obj) type_ = type(obj)
@ -112,7 +111,7 @@ def getCounters(obj: typing.Any, counterType: int, **kwargs) -> typing.Generator
else: else:
owner_ids = None owner_ids = None
for i in statsManager().getCounters(__transDict[type(obj)], counterType, owner_ids, since, to, max_intervals, limit, use_max): for i in statsManager().getCounters(__transDict[type(obj)], counterType, owner_ids, since, to, kwargs.get('interval'), kwargs.get('max_intervals'), limit, use_max):
val = (datetime.datetime.fromtimestamp(i.stamp), i.value) val = (datetime.datetime.fromtimestamp(i.stamp), i.value)
yield val yield val
@ -128,7 +127,7 @@ def _initializeData() -> None:
Hides data from global var space Hides data from global var space
""" """
from uds.models import Provider, Service, ServicePool, Authenticator from uds.models import Provider, Service, ServicePool, Authenticator # pylint: disable=import-outside-toplevel
__caWrite.update({ __caWrite.update({
CT_LOAD: (Provider,), CT_LOAD: (Provider,),

View File

@ -92,15 +92,15 @@ class StatsCounters(models.Model):
since = int(since) if since else NEVER_UNIX since = int(since) if since else NEVER_UNIX
to = int(to) if to else getSqlDatetimeAsUnix() to = int(to) if to else getSqlDatetimeAsUnix()
interval = 600 # By default, group items in ten minutes interval (600 seconds) interval = int(kwargs.get('interval') or '600') # By default, group items in ten minutes interval (600 seconds)
elements = kwargs.get('elements', 0) max_intervals = kwargs.get('max_intervals')
limit = kwargs.get('limit') limit = kwargs.get('limit')
if elements: if max_intervals:
# Protect against division by "elements-1" a few lines below # Protect against division by "elements-1" a few lines below
elements = int(elements) if int(elements) > 1 else 2 max_intervals = int(max_intervals) if int(max_intervals) > 1 else 2
if owner_id is None: if owner_id is None:
q = StatsCounters.objects.filter(stamp__gte=since, stamp__lte=to) q = StatsCounters.objects.filter(stamp__gte=since, stamp__lte=to)
@ -115,10 +115,10 @@ class StatsCounters(models.Model):
else: else:
q = q.filter(owner_type=owner_type) q = q.filter(owner_type=owner_type)
if q.count() > elements: if q.count() > max_intervals:
first = q.order_by('stamp')[0].stamp first = q.order_by('stamp')[0].stamp
last = q.order_by('stamp').reverse()[0].stamp last = q.order_by('stamp').reverse()[0].stamp
interval = int((last - first) / (elements - 1)) interval = int((last - first) / (max_intervals - 1))
stampValue = '{ceil}(stamp/{interval})'.format(ceil=getSqlFnc('CEIL'), interval=interval) stampValue = '{ceil}(stamp/{interval})'.format(ceil=getSqlFnc('CEIL'), interval=interval)
filt += ' AND stamp>={since} AND stamp<={to} GROUP BY {stampValue} ORDER BY stamp'.format( filt += ' AND stamp>={since} AND stamp<={to} GROUP BY {stampValue} ORDER BY stamp'.format(