From ea78f883776dfb7acdc3b17bb4292e03b56fb4e1 Mon Sep 17 00:00:00 2001 From: "R. Miles McCain" Date: Wed, 15 Apr 2020 09:26:19 -0400 Subject: [PATCH] Improve session linking system --- shynet/analytics/tasks.py | 23 ++++++++++++++--------- shynet/shynet/settings.py | 1 - 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/shynet/analytics/tasks.py b/shynet/analytics/tasks.py index 12dbdb2..c6ac590 100644 --- a/shynet/analytics/tasks.py +++ b/shynet/analytics/tasks.py @@ -7,7 +7,7 @@ from celery import shared_task from django.conf import settings from django.core.cache import cache from django.utils import timezone - +from django.db.models import Q from core.models import Service from .models import Hit, Session @@ -51,13 +51,16 @@ def ingress_request( log.debug(f"Found geoip2 data") # Create or update session - session = Session.objects.filter( - service=service, - last_seen__gt=timezone.now() - timezone.timedelta(minutes=10), - ip=ip, - user_agent=user_agent, - identifier=identifier, - ).first() + session = ( + Session.objects.filter( + service=service, + last_seen__gt=timezone.now() - timezone.timedelta(minutes=10), + ip=ip, + user_agent=user_agent, + ) + .filter(Q(identifier=identifier) | Q(identifier="")) + .first() + ) if session is None: log.debug("Cannot link to existing session; creating a new one...") ua = user_agents.parse(user_agent) @@ -75,7 +78,7 @@ def ingress_request( service=service, ip=ip, user_agent=user_agent, - identifier=identifier, + identifier=identifier.strip(), browser=ua.browser.family or "", device=ua.device.model or "", device_type=device_type, @@ -91,6 +94,8 @@ def ingress_request( initial = False # Update last seen time session.last_seen = timezone.now() + if session.identifier == "" and identifier.strip() != "": + session.identifier = identifier.strip() session.save() # Create or update hit diff --git a/shynet/shynet/settings.py b/shynet/shynet/settings.py index 2787d54..92e1f3b 100644 --- a/shynet/shynet/settings.py +++ b/shynet/shynet/settings.py @@ -177,7 +177,6 @@ STATIC_ROOT = "compiledstatic/" STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage" # Redis -# Redis cache if not DEBUG: CACHES = { "default": {