Store service directly in Hit

This commit is contained in:
R. Miles McCain 2021-03-28 20:54:19 +00:00
parent f2e875d03d
commit 874aad87a8
5 changed files with 63 additions and 2 deletions

View File

@ -0,0 +1,38 @@
# Generated by Django 3.1.7 on 2021-03-28 19:36
from ..models import Hit, Session
from django.db import migrations, models
import django.db.models.deletion
from django.db.models import Subquery, OuterRef
def add_service_to_hits(_a, _b):
service = Session.objects.filter(
pk=OuterRef('session')
).values_list(
'service'
)[:1]
Hit.objects.update(
service=Subquery(service)
)
class Migration(migrations.Migration):
dependencies = [
('core', '0008_auto_20200628_1403'),
('analytics', '0005_auto_20210328_1518'),
]
operations = [
migrations.AddField(
model_name='hit',
name='service',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='core.service'),
),
migrations.RunPython(add_service_to_hits, lambda: ()),
migrations.AlterField(
model_name='hit',
name='service',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='core.service'),
),
]

View File

@ -0,0 +1,17 @@
# Generated by Django 3.1.7 on 2021-03-28 20:34
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('analytics', '0006_hit_service'),
]
operations = [
migrations.AddIndex(
model_name='hit',
index=models.Index(fields=['service', '-start_time'], name='analytics_h_service_f4f41e_idx'),
),
]

View File

@ -94,10 +94,15 @@ class Hit(models.Model):
referrer = models.TextField(blank=True, db_index=True)
load_time = models.FloatField(null=True, db_index=True)
# While not necessary, we store the root service directly for performance.
# It makes querying much easier; no need for inner joins.
service = models.ForeignKey(Service, on_delete=models.CASCADE, db_index=True)
class Meta:
ordering = ["-start_time"]
indexes = [
models.Index(fields=["session", "-start_time"]),
models.Index(fields=["service", "-start_time"]),
models.Index(fields=["session", "location"]),
models.Index(fields=["session", "referrer"]),
]
@ -109,5 +114,5 @@ class Hit(models.Model):
def get_absolute_url(self):
return reverse(
"dashboard:service_session",
kwargs={"pk": self.session.service.pk, "session_pk": self.session.pk},
kwargs={"pk": self.service.pk, "session_pk": self.session.pk},
)

View File

@ -184,6 +184,7 @@ def ingress_request(
load_time=payload.get("loadTime"),
start_time=time,
last_seen=time,
service=service
)
# Set idempotency (if applicable)
if idempotency is not None:

View File

@ -129,7 +129,7 @@ class Service(models.Model):
session_count = sessions.count()
hits = Hit.objects.filter(
session__service=self, start_time__lt=end_time, start_time__gt=start_time
service=self, start_time__lt=end_time, start_time__gt=start_time
)
hit_count = hits.count()