diff --git a/shynet/analytics/migrations/0009_auto_20200414_1008.py b/shynet/analytics/migrations/0009_auto_20200414_1008.py new file mode 100644 index 0000000..2e595d7 --- /dev/null +++ b/shynet/analytics/migrations/0009_auto_20200414_1008.py @@ -0,0 +1,76 @@ +# Generated by Django 3.0.5 on 2020-04-14 14:08 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('analytics', '0008_auto_20200412_0015'), + ] + + operations = [ + migrations.AlterModelOptions( + name='hit', + options={'ordering': ['-start_time']}, + ), + migrations.AlterModelOptions( + name='session', + options={'ordering': ['-start_time']}, + ), + migrations.AlterField( + model_name='hit', + name='initial', + field=models.BooleanField(db_index=True, default=True), + ), + migrations.AlterField( + model_name='hit', + name='location', + field=models.TextField(blank=True, db_index=True), + ), + migrations.AlterField( + model_name='hit', + name='referrer', + field=models.TextField(blank=True, db_index=True), + ), + migrations.AlterField( + model_name='hit', + name='start_time', + field=models.DateTimeField(auto_now_add=True, db_index=True), + ), + migrations.AlterField( + model_name='session', + name='identifier', + field=models.TextField(blank=True, db_index=True), + ), + migrations.AlterField( + model_name='session', + name='ip', + field=models.GenericIPAddressField(db_index=True), + ), + migrations.AlterField( + model_name='session', + name='start_time', + field=models.DateTimeField(auto_now_add=True, db_index=True), + ), + migrations.AddIndex( + model_name='hit', + index=models.Index(fields=['session', '-start_time'], name='analytics_h_session_b2667f_idx'), + ), + migrations.AddIndex( + model_name='hit', + index=models.Index(fields=['session', 'location'], name='analytics_h_session_775f5a_idx'), + ), + migrations.AddIndex( + model_name='hit', + index=models.Index(fields=['session', 'referrer'], name='analytics_h_session_98b8bf_idx'), + ), + migrations.AddIndex( + model_name='session', + index=models.Index(fields=['service', '-start_time'], name='analytics_s_service_4b1137_idx'), + ), + migrations.AddIndex( + model_name='session', + index=models.Index(fields=['service', 'identifier'], name='analytics_s_service_82ab21_idx'), + ), + ] diff --git a/shynet/analytics/models.py b/shynet/analytics/models.py index 7f4e8da..30fada4 100644 --- a/shynet/analytics/models.py +++ b/shynet/analytics/models.py @@ -13,13 +13,13 @@ def _default_uuid(): class Session(models.Model): uuid = models.UUIDField(default=_default_uuid, primary_key=True) - service = models.ForeignKey(Service, on_delete=models.CASCADE) + service = models.ForeignKey(Service, on_delete=models.CASCADE, db_index=True) # Cross-session identification; optional, and provided by the service - identifier = models.TextField(blank=True) + identifier = models.TextField(blank=True, db_index=True) # Time - start_time = models.DateTimeField(auto_now_add=True) + start_time = models.DateTimeField(auto_now_add=True, db_index=True) last_seen = models.DateTimeField(auto_now_add=True) # Core request information @@ -38,7 +38,7 @@ class Session(models.Model): default="OTHER", ) os = models.TextField() - ip = models.GenericIPAddressField() + ip = models.GenericIPAddressField(db_index=True) # GeoIP data asn = models.TextField(blank=True) @@ -47,6 +47,13 @@ class Session(models.Model): latitude = models.FloatField(null=True) time_zone = models.TextField(blank=True) + class Meta: + ordering = ["-start_time"] + indexes = [ + models.Index(fields=["service", "-start_time"]), + models.Index(fields=["service", "identifier"]), + ] + @property def is_currently_active(self): return timezone.now() - self.last_seen < timezone.timedelta(seconds=10) @@ -57,20 +64,28 @@ class Session(models.Model): class Hit(models.Model): - session = models.ForeignKey(Session, on_delete=models.CASCADE) - initial = models.BooleanField(default=True) + session = models.ForeignKey(Session, on_delete=models.CASCADE, db_index=True) + initial = models.BooleanField(default=True, db_index=True) # Base request information - start_time = models.DateTimeField(auto_now_add=True) + start_time = models.DateTimeField(auto_now_add=True, db_index=True) last_seen = models.DateTimeField(auto_now_add=True) heartbeats = models.IntegerField(default=0) tracker = models.TextField() # Tracking pixel or JS # Advanced page information - location = models.TextField(blank=True) - referrer = models.TextField(blank=True) + location = models.TextField(blank=True, db_index=True) + referrer = models.TextField(blank=True, db_index=True) load_time = models.FloatField(null=True) + class Meta: + ordering = ["-start_time"] + indexes = [ + models.Index(fields=["session", "-start_time"]), + models.Index(fields=["session", "location"]), + models.Index(fields=["session", "referrer"]), + ] + @property def duration(self): return self.last_seen - self.start_time