From e4f09b4e688eaafe5c5322019b4cbea0d1ec1d0e Mon Sep 17 00:00:00 2001 From: "R. Miles McCain" Date: Thu, 26 Nov 2020 19:43:25 +0000 Subject: [PATCH] Ensure times are always correct --- shynet/analytics/models.py | 8 ++++---- shynet/analytics/tasks.py | 9 ++++++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/shynet/analytics/models.py b/shynet/analytics/models.py index 460a182..3b37b93 100644 --- a/shynet/analytics/models.py +++ b/shynet/analytics/models.py @@ -20,8 +20,8 @@ class Session(models.Model): identifier = models.TextField(blank=True, db_index=True) # Time - start_time = models.DateTimeField(auto_now_add=True, db_index=True) - last_seen = models.DateTimeField(auto_now_add=True) + start_time = models.DateTimeField(default=timezone.now, db_index=True) + last_seen = models.DateTimeField(default=timezone.now) # Core request information user_agent = models.TextField() @@ -78,8 +78,8 @@ class Hit(models.Model): initial = models.BooleanField(default=True, db_index=True) # Base request information - start_time = models.DateTimeField(auto_now_add=True, db_index=True) - last_seen = models.DateTimeField(auto_now_add=True) + start_time = models.DateTimeField(default=timezone.now, db_index=True) + last_seen = models.DateTimeField(default=timezone.now) heartbeats = models.IntegerField(default=0) tracker = models.TextField( choices=[("JS", "JavaScript"), ("PIXEL", "Pixel (noscript)")] diff --git a/shynet/analytics/tasks.py b/shynet/analytics/tasks.py index 4e1606c..75b8524 100644 --- a/shynet/analytics/tasks.py +++ b/shynet/analytics/tasks.py @@ -9,7 +9,6 @@ from celery import shared_task from django.conf import settings from django.core.cache import cache from django.db.models import Q -from django.utils import timezone from core.models import Service @@ -123,6 +122,8 @@ def ingress_request( browser=ua.browser.family or "", device=ua.device.family or ua.device.model or "", device_type=device_type, + start_time=time, + last_seen=time, os=ua.os.family or "", asn=ip_data.get("asn") or "", country=ip_data.get("country") or "", @@ -139,7 +140,7 @@ def ingress_request( log.debug("Updating old session with new data...") # Update last seen time - session.last_seen = timezone.now() + session.last_seen = time if session.identifier == "" and identifier.strip() != "": session.identifier = identifier.strip() session.save() @@ -160,7 +161,7 @@ def ingress_request( # this is a heartbeat. log.debug("Hit is a heartbeat; updating old hit with new data...") hit.heartbeats += 1 - hit.last_seen = timezone.now() + hit.last_seen = time hit.save() if hit is None: @@ -176,6 +177,8 @@ def ingress_request( location=payload.get("location", location), referrer=payload.get("referrer", ""), load_time=payload.get("loadTime"), + start_time=time, + last_seen=time, ) # Set idempotency (if applicable) if idempotency is not None: