A few more advancements

This commit is contained in:
Adolfo Gómez García 2021-11-04 14:03:17 +01:00
parent b55df32db0
commit f273d262ac
3 changed files with 48 additions and 8 deletions

View File

@ -89,6 +89,14 @@ EVENT_NAMES = {
OT_OSMANAGER
) = range(5)
TYPES_NAMES = {
OT_PROVIDER: 'Provider',
OT_SERVICE: 'Service',
OT_DEPLOYED: 'Deployed',
OT_AUTHENTICATOR: 'Authenticator',
OT_OSMANAGER: 'OS Manager'
}
__transDict: typing.Mapping[typing.Type['models.Model'], int] = {
ServicePool: OT_DEPLOYED,
Service: OT_SERVICE,

View File

@ -4,10 +4,7 @@ import datetime
import typing
import logging
from django.db.models import QuerySet
from uds.management.commands.udsfs.events import EventFS
from uds.models import StatsEvents
from uds import models
from uds.core.util.stats.events import EVENT_NAMES, getOwner
from . import types
@ -78,7 +75,13 @@ class StatsFS(types.UDSFSInterface):
# Dispatchers for different stats files
def _read_events(self, size: int, offset: int) -> bytes:
logger.debug('Reading events. offset: %s, size: %s', offset, size)
return b'Events'
# Get stats events from last 24 hours (in UTC) stamp is unix timestamp
virtualFile = models.StatsEvents.getCSVHeader().encode() + b'\n'
for record in models.StatsEvents.objects.filter(
stamp__gte=calendar.timegm(datetime.datetime.utcnow().timetuple()) - 86400
):
virtualFile += record.toCsv().encode() + b'\n'
return virtualFile
def _read_pools(self, size: int, offset: int) -> bytes:
logger.debug('Reading pools. offset: %s, size: %s', offset, size)

View File

@ -30,6 +30,7 @@
"""
.. moduleauthor:: Adolfo Gómez, dkmaster at dkmon dot com
"""
import datetime
import logging
import typing
import types
@ -39,10 +40,8 @@ from django.db import models
from .util import NEVER_UNIX
from .util import getSqlDatetimeAsUnix
logger = logging.getLogger(__name__)
class StatsEvents(models.Model):
"""
Statistics about events (login, logout, whatever...)
@ -74,7 +73,7 @@ class StatsEvents(models.Model):
def get_stats(
owner_type: typing.Union[int, typing.Iterable[int]],
event_type: typing.Union[int, typing.Iterable[int]],
**kwargs
**kwargs,
) -> 'models.QuerySet[StatsEvents]':
"""
Returns a queryset with the average stats grouped by interval for owner_type and owner_id (optional)
@ -126,6 +125,36 @@ class StatsEvents(models.Model):
def uniqueId(self) -> str:
return self.fld4
@property
def isostamp(self) -> str:
"""
Returns the timestamp in ISO format (UTC)
"""
stamp = datetime.datetime.utcfromtimestamp(self.stamp)
return stamp.isoformat()
# returns CSV header
@staticmethod
def getCSVHeader() -> str:
return 'owner_type,owner_id,event_type,stamp,field_1,field_2,field_3,field_4'
# Return record as csv line using separator (default: ',')
def toCsv(self, sep: str = ',') -> str:
from uds.core.util.stats.events import EVENT_NAMES, TYPES_NAMES
return sep.join(
[
TYPES_NAMES.get(self.owner_type, '?'),
str(self.owner_id),
EVENT_NAMES.get(self.event_type, '?'),
str(self.isostamp),
self.fld1,
self.fld2,
self.fld3,
self.fld4,
]
)
def __str__(self):
return 'Log of {}({}): {} - {} - {}, {}, {}'.format(
self.owner_type,