Remove metadata
This commit is contained in:
parent
844c44ae6c
commit
8cb867f11b
21
shynet/analytics/migrations/0003_auto_20200410_1325.py
Normal file
21
shynet/analytics/migrations/0003_auto_20200410_1325.py
Normal file
@ -0,0 +1,21 @@
|
||||
# Generated by Django 3.0.5 on 2020-04-10 17:25
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('analytics', '0002_auto_20200410_0258'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='hit',
|
||||
name='metadata_raw',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='session',
|
||||
name='metadata_raw',
|
||||
),
|
||||
]
|
@ -35,16 +35,6 @@ class Session(models.Model):
|
||||
latitude = models.FloatField(null=True)
|
||||
time_zone = models.TextField(blank=True)
|
||||
|
||||
# Additional metadata, stored as JSON string
|
||||
metadata_raw = models.TextField()
|
||||
|
||||
@property
|
||||
def metadata(self):
|
||||
try:
|
||||
return json.loads(self.metadata_raw)
|
||||
except: # Metadata is not crucial; in the case of a read error, just ignore it
|
||||
return {}
|
||||
|
||||
|
||||
class Hit(models.Model):
|
||||
session = models.ForeignKey(Session, on_delete=models.CASCADE)
|
||||
@ -61,12 +51,3 @@ class Hit(models.Model):
|
||||
loadTime = models.FloatField(null=True)
|
||||
httpStatus = models.IntegerField(null=True)
|
||||
|
||||
# Additional metadata, stored as JSON string
|
||||
metadata_raw = models.TextField()
|
||||
|
||||
@property
|
||||
def metadata(self):
|
||||
try:
|
||||
return json.loads(self.metadata_raw)
|
||||
except: # Metadata is not crucial; in the case of a read error, just ignore it
|
||||
return {}
|
||||
|
@ -50,7 +50,6 @@ def ingress_request(
|
||||
log.debug(f"Linked to service {service}")
|
||||
|
||||
# Create or update session
|
||||
session_metadata = payload.get("sessionMetadata", {})
|
||||
session = Session.objects.filter(
|
||||
service=service,
|
||||
last_seen__gt=timezone.now() - timezone.timedelta(minutes=30),
|
||||
@ -70,7 +69,6 @@ def ingress_request(
|
||||
browser=f"{ua.browser.family or ''} {ua.browser.version_string or ''}".strip(),
|
||||
device=f"{ua.device.model or ''}",
|
||||
os=f"{ua.os.family or ''} {ua.os.version_string or ''}".strip(),
|
||||
metadata_raw=json.dumps(session_metadata),
|
||||
asn=ip_data.get("asn", ""),
|
||||
country=ip_data.get("country", ""),
|
||||
longitude=ip_data.get("longitude"),
|
||||
@ -79,16 +77,11 @@ def ingress_request(
|
||||
)
|
||||
else:
|
||||
log.debug("Updating old session with new data...")
|
||||
# Update old metadata with new metadata
|
||||
new_metadata = session.metadata
|
||||
new_metadata.update(session_metadata)
|
||||
session.metadata_raw = json.dumps(new_metadata)
|
||||
# Update last seen time
|
||||
session.last_seen = timezone.now()
|
||||
session.save()
|
||||
|
||||
# Create or update hit
|
||||
hit_metadata = payload.get("hitMetadata", {})
|
||||
idempotency = payload.get("idempotency")
|
||||
idempotency_path = f"hit_idempotency_{idempotency}"
|
||||
hit = None
|
||||
@ -103,9 +96,6 @@ def ingress_request(
|
||||
log.debug("Hit is a heartbeat; updating old hit with new data...")
|
||||
hit.heartbeats += 1
|
||||
hit.duration = (timezone.now() - hit.start).total_seconds()
|
||||
new_metadata = hit.metadata
|
||||
new_metadata.update(hit_metadata)
|
||||
hit.metadata_raw = json.dumps(new_metadata)
|
||||
hit.save()
|
||||
if hit is None:
|
||||
log.debug("Hit is a page load; creating new hit...")
|
||||
@ -116,10 +106,10 @@ def ingress_request(
|
||||
location=location,
|
||||
referrer=payload.get("referrer", ""),
|
||||
loadTime=payload.get("loadTime"),
|
||||
metadata_raw=json.dumps(hit_metadata),
|
||||
)
|
||||
# Set idempotency (if applicable)
|
||||
if idempotency is not None:
|
||||
cache.set(idempotency_path, hit.pk, timeout=30 * 60)
|
||||
except Exception as e:
|
||||
log.error(e)
|
||||
log.exception(e)
|
||||
raise e
|
||||
|
@ -13,12 +13,6 @@ window.onload = function () {
|
||||
loadTime:
|
||||
window.performance.timing.domContentLoadedEventEnd -
|
||||
window.performance.timing.navigationStart,
|
||||
hitMetadata:
|
||||
typeof shynetHitMetadata !== "undefined" ? shynetHitMetadata : {},
|
||||
sessionMetadata:
|
||||
typeof shynetSessionMetadata !== "undefined"
|
||||
? shynetSessionMetadata
|
||||
: {},
|
||||
})
|
||||
);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user