Skip to content

Commit

Permalink
django: add logic to summarize events from same pkt
Browse files Browse the repository at this point in the history
Signed-off-by: Jonas Remmert <jremmert@gmx.net>
  • Loading branch information
jonas-rem committed Sep 14, 2024
1 parent c95d49e commit 9980160
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions server/django/sensordata/serializers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
)
from ..tasks import process_pending_operations
import logging
from django.utils import timezone
from datetime import timedelta

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -47,17 +49,36 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.event = None

def get_existing_event(self, ep, event_type):
"""
Check for an existing event for the given endpoint and event type
created within the last 5 seconds.
"""
five_seconds_ago = timezone.now() - timedelta(seconds=5)
return Event.objects.filter(
endpoint=ep,
event_type=event_type,
created_at__gte=five_seconds_ago
).first()


def create_event(self, ep, event_type):
"""
Create an Event instance for the given endpoint and event type. If no event
is created, the resource data won't be associated with any event.
Create an Event instance for the given endpoint and event type. If no
event is created, the resource data won't be associated with any event.
If a recent event exists already, we assume that the new data is just
an additional package and belongs to the same event.
"""
event_data = {
'endpoint': ep,
'event_type': event_type,
}
self.event = Event.objects.create(**event_data)
existing_event = self.get_existing_event(ep, event_type)

if existing_event:
self.event = existing_event
else:
event_data = {
'endpoint': ep,
'event_type': event_type,
}
self.event = Event.objects.create(**event_data)


def handle_resource(self, ep, obj_id, res, ts=None):
Expand Down

0 comments on commit 9980160

Please sign in to comment.