diff --git a/shynet/analytics/migrations/0006_hit_service.py b/shynet/analytics/migrations/0006_hit_service.py new file mode 100644 index 0000000..bf8d593 --- /dev/null +++ b/shynet/analytics/migrations/0006_hit_service.py @@ -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'), + ), + ] diff --git a/shynet/analytics/migrations/0007_auto_20210328_1634.py b/shynet/analytics/migrations/0007_auto_20210328_1634.py new file mode 100644 index 0000000..7efceff --- /dev/null +++ b/shynet/analytics/migrations/0007_auto_20210328_1634.py @@ -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'), + ), + ] diff --git a/shynet/analytics/models.py b/shynet/analytics/models.py index 3400068..2776426 100644 --- a/shynet/analytics/models.py +++ b/shynet/analytics/models.py @@ -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}, ) diff --git a/shynet/analytics/tasks.py b/shynet/analytics/tasks.py index 6603711..b41bb29 100644 --- a/shynet/analytics/tasks.py +++ b/shynet/analytics/tasks.py @@ -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: diff --git a/shynet/core/models.py b/shynet/core/models.py index 356cb8f..596bf39 100644 --- a/shynet/core/models.py +++ b/shynet/core/models.py @@ -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()