forked from shaba/openuds
Stats reports fixes
This commit is contained in:
parent
6cf8eb3e77
commit
b7d0fef8bb
@ -108,5 +108,4 @@ def extend_sqlite(connection, **kwargs):
|
||||
connection.connection.create_function("MIN", 2, min)
|
||||
connection.connection.create_function("MAX", 2, max)
|
||||
connection.connection.create_function("CEIL", 1, math.ceil)
|
||||
connection.connection.create_function("FLOOR", 1, math.floor)
|
||||
|
||||
|
@ -67,7 +67,7 @@ if typing.TYPE_CHECKING:
|
||||
) = range(11)
|
||||
|
||||
# Events names
|
||||
EVENT_NAMES = {
|
||||
EVENT_NAMES: typing.Mapping[int, str] = {
|
||||
ET_LOGIN: 'Login',
|
||||
ET_LOGOUT: 'Logout',
|
||||
ET_ACCESS: 'Access',
|
||||
@ -89,7 +89,7 @@ EVENT_NAMES = {
|
||||
OT_OSMANAGER
|
||||
) = range(5)
|
||||
|
||||
TYPES_NAMES = {
|
||||
TYPES_NAMES: typing.Mapping[int, str] = {
|
||||
OT_PROVIDER: 'Provider',
|
||||
OT_SERVICE: 'Service',
|
||||
OT_DEPLOYED: 'Deployed',
|
||||
@ -97,7 +97,7 @@ TYPES_NAMES = {
|
||||
OT_OSMANAGER: 'OS Manager'
|
||||
}
|
||||
|
||||
__transDict: typing.Mapping[typing.Type['models.Model'], int] = {
|
||||
MODEL_TO_EVENT: typing.Mapping[typing.Type['models.Model'], int] = {
|
||||
ServicePool: OT_DEPLOYED,
|
||||
Service: OT_SERVICE,
|
||||
Provider: OT_PROVIDER,
|
||||
@ -207,7 +207,7 @@ def addEvent(obj: EventClass, eventType: int, **kwargs) -> bool:
|
||||
"""
|
||||
|
||||
return StatsManager.manager().addEvent(
|
||||
__transDict[type(obj)], obj.id, eventType, **kwargs
|
||||
MODEL_TO_EVENT[type(obj)], obj.id, eventType, **kwargs
|
||||
)
|
||||
|
||||
|
||||
@ -229,11 +229,7 @@ def getEvents(
|
||||
Returns:
|
||||
A generator, that contains pairs of (stamp, value) tuples
|
||||
"""
|
||||
from uds.models import NEVER_UNIX, getSqlDatetimeAsUnix
|
||||
|
||||
since = kwargs.get('since', NEVER_UNIX)
|
||||
to = kwargs.get('to', getSqlDatetimeAsUnix())
|
||||
type_ = type(obj)
|
||||
objType = type(obj)
|
||||
|
||||
if kwargs.get('all', False):
|
||||
owner_id = None
|
||||
@ -241,7 +237,7 @@ def getEvents(
|
||||
owner_id = obj.pk
|
||||
|
||||
for i in StatsManager.manager().getEvents(
|
||||
__transDict[type_], eventType, owner_id=owner_id, since=since, to=to
|
||||
MODEL_TO_EVENT[objType], eventType, owner_id=owner_id, since=kwargs.get('since'), to=kwargs.get('to')
|
||||
):
|
||||
yield EventTupleType(
|
||||
datetime.datetime.fromtimestamp(i.stamp),
|
||||
|
@ -113,7 +113,7 @@ class StatsCounters(models.Model):
|
||||
if q.count() == 0:
|
||||
return
|
||||
|
||||
interval = kwargs.get('interval', 600)
|
||||
interval = kwargs.get('interval') or 600
|
||||
|
||||
# Max intervals, if present, will adjust interval (that are seconds)
|
||||
max_intervals = kwargs.get('max_intervals', 0)
|
||||
|
@ -81,33 +81,39 @@ class StatsEvents(models.Model):
|
||||
|
||||
Note: if someone cant get this more optimized, please, contribute it!
|
||||
"""
|
||||
if isinstance(event_type, (list, tuple, types.GeneratorType)):
|
||||
fltr = StatsEvents.objects.filter(event_type__in=event_type)
|
||||
else:
|
||||
fltr = StatsEvents.objects.filter(event_type=event_type)
|
||||
if isinstance(owner_type, int):
|
||||
owner_type = [owner_type]
|
||||
if isinstance(event_type, int):
|
||||
event_type = [event_type]
|
||||
q = StatsEvents.objects.filter(owner_type__in=owner_type, event_type__in=event_type)
|
||||
|
||||
if isinstance(owner_type, (list, tuple, types.GeneratorType)):
|
||||
fltr = fltr.filter(owner_type__in=owner_type)
|
||||
else:
|
||||
fltr = fltr.filter(owner_type=owner_type)
|
||||
if 'owner_id' in kwargs:
|
||||
owner_id = kwargs['owner_id']
|
||||
if isinstance(owner_id, int):
|
||||
owner_id = [owner_id]
|
||||
q = q.filter(owner_id__in=owner_id)
|
||||
|
||||
if kwargs.get('owner_id', None) is not None:
|
||||
oid = kwargs.get('owner_id')
|
||||
if isinstance(oid, (list, tuple)):
|
||||
fltr = fltr.filter(owner_id__in=oid)
|
||||
else:
|
||||
fltr = fltr.filter(owner_id=oid)
|
||||
since = kwargs.get('since')
|
||||
if isinstance(since, datetime.datetime):
|
||||
# Convert to unix timestamp
|
||||
since = int(since.timestamp())
|
||||
if not since:
|
||||
# Get first timestamp from table, we knwo table has at least one record
|
||||
since = StatsEvents.objects.order_by('stamp').first().stamp # type: ignore
|
||||
to = kwargs.get('to')
|
||||
if isinstance(to, datetime.datetime):
|
||||
# Convert to unix timestamp
|
||||
to = int(to.timestamp())
|
||||
if not to:
|
||||
# Get last timestamp from table, we know table has at least one record
|
||||
to = StatsEvents.objects.order_by('-stamp').first().stamp # type: ignore
|
||||
|
||||
since = kwargs.get('since', None)
|
||||
to = kwargs.get('to', None)
|
||||
q = q.filter(stamp__gte=since, stamp__lte=to)
|
||||
|
||||
since = int(since) if since else NEVER_UNIX
|
||||
to = int(to) if to else getSqlDatetimeAsUnix()
|
||||
if kwargs.get('limit'):
|
||||
q = q[:kwargs['limit']]
|
||||
|
||||
fltr = fltr.filter(stamp__gte=since, stamp__lt=to)
|
||||
|
||||
# We use result as an iterator
|
||||
return fltr
|
||||
return q
|
||||
|
||||
# Utility aliases for reading
|
||||
@property
|
||||
|
@ -62,7 +62,7 @@ def __loadModules() -> None:
|
||||
addReportCls,
|
||||
reports.Report,
|
||||
__name__,
|
||||
checker=lambda x: x.uuid is not None and x.uuid not in alreadyAdded,
|
||||
checker=lambda x: x.uuid and x.uuid not in alreadyAdded, # type: ignore
|
||||
)
|
||||
|
||||
__loadModules()
|
||||
|
@ -96,10 +96,10 @@ class StatsReportLogin(StatsReport):
|
||||
def getRangeData(self) -> typing.Tuple[str, typing.List, typing.List]:
|
||||
start = self.startDate.stamp()
|
||||
end = self.endDate.stamp()
|
||||
if self.samplingPoints.num() < 8:
|
||||
self.samplingPoints.value = (
|
||||
self.endDate.date() - self.startDate.date()
|
||||
).days
|
||||
# if self.samplingPoints.num() < 8:
|
||||
# self.samplingPoints.value = (
|
||||
# self.endDate.date() - self.startDate.date()
|
||||
# ).days
|
||||
if self.samplingPoints.num() < 2:
|
||||
self.samplingPoints.value = 2
|
||||
if self.samplingPoints.num() > 128:
|
||||
@ -114,13 +114,9 @@ class StatsReportLogin(StatsReport):
|
||||
xLabelFormat = 'SHORT_DATETIME_FORMAT'
|
||||
|
||||
samplingIntervals: typing.List[typing.Tuple[int, int]] = []
|
||||
prevVal = None
|
||||
for val in range(start, end, int((end - start) / (samplingPoints + 1))):
|
||||
if prevVal is None:
|
||||
prevVal = val
|
||||
continue
|
||||
samplingIntervals.append((prevVal, val))
|
||||
prevVal = val
|
||||
samplingIntervalSeconds = (end - start) // samplingPoints
|
||||
for i in range(samplingPoints):
|
||||
samplingIntervals.append((int(start + i * samplingIntervalSeconds), int(start + (i + 1) * samplingIntervalSeconds)))
|
||||
|
||||
data = []
|
||||
reportData = []
|
||||
|
Loading…
x
Reference in New Issue
Block a user