Improve session linking system

This commit is contained in:
R. Miles McCain 2020-04-15 09:26:19 -04:00
parent 902e5a8324
commit ea78f88377
No known key found for this signature in database
GPG Key ID: 91CB47BDDF2671A5
2 changed files with 14 additions and 10 deletions

View File

@ -7,7 +7,7 @@ from celery import shared_task
from django.conf import settings from django.conf import settings
from django.core.cache import cache from django.core.cache import cache
from django.utils import timezone from django.utils import timezone
from django.db.models import Q
from core.models import Service from core.models import Service
from .models import Hit, Session from .models import Hit, Session
@ -51,13 +51,16 @@ def ingress_request(
log.debug(f"Found geoip2 data") log.debug(f"Found geoip2 data")
# Create or update session # Create or update session
session = Session.objects.filter( session = (
service=service, Session.objects.filter(
last_seen__gt=timezone.now() - timezone.timedelta(minutes=10), service=service,
ip=ip, last_seen__gt=timezone.now() - timezone.timedelta(minutes=10),
user_agent=user_agent, ip=ip,
identifier=identifier, user_agent=user_agent,
).first() )
.filter(Q(identifier=identifier) | Q(identifier=""))
.first()
)
if session is None: if session is None:
log.debug("Cannot link to existing session; creating a new one...") log.debug("Cannot link to existing session; creating a new one...")
ua = user_agents.parse(user_agent) ua = user_agents.parse(user_agent)
@ -75,7 +78,7 @@ def ingress_request(
service=service, service=service,
ip=ip, ip=ip,
user_agent=user_agent, user_agent=user_agent,
identifier=identifier, identifier=identifier.strip(),
browser=ua.browser.family or "", browser=ua.browser.family or "",
device=ua.device.model or "", device=ua.device.model or "",
device_type=device_type, device_type=device_type,
@ -91,6 +94,8 @@ def ingress_request(
initial = False initial = False
# Update last seen time # Update last seen time
session.last_seen = timezone.now() session.last_seen = timezone.now()
if session.identifier == "" and identifier.strip() != "":
session.identifier = identifier.strip()
session.save() session.save()
# Create or update hit # Create or update hit

View File

@ -177,7 +177,6 @@ STATIC_ROOT = "compiledstatic/"
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage" STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
# Redis # Redis
# Redis cache
if not DEBUG: if not DEBUG:
CACHES = { CACHES = {
"default": { "default": {