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)
|
latitude = models.FloatField(null=True)
|
||||||
time_zone = models.TextField(blank=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):
|
class Hit(models.Model):
|
||||||
session = models.ForeignKey(Session, on_delete=models.CASCADE)
|
session = models.ForeignKey(Session, on_delete=models.CASCADE)
|
||||||
@ -60,13 +50,4 @@ class Hit(models.Model):
|
|||||||
referrer = models.TextField(blank=True)
|
referrer = models.TextField(blank=True)
|
||||||
loadTime = models.FloatField(null=True)
|
loadTime = models.FloatField(null=True)
|
||||||
httpStatus = models.IntegerField(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}")
|
log.debug(f"Linked to service {service}")
|
||||||
|
|
||||||
# Create or update session
|
# Create or update session
|
||||||
session_metadata = payload.get("sessionMetadata", {})
|
|
||||||
session = Session.objects.filter(
|
session = Session.objects.filter(
|
||||||
service=service,
|
service=service,
|
||||||
last_seen__gt=timezone.now() - timezone.timedelta(minutes=30),
|
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(),
|
browser=f"{ua.browser.family or ''} {ua.browser.version_string or ''}".strip(),
|
||||||
device=f"{ua.device.model or ''}",
|
device=f"{ua.device.model or ''}",
|
||||||
os=f"{ua.os.family or ''} {ua.os.version_string or ''}".strip(),
|
os=f"{ua.os.family or ''} {ua.os.version_string or ''}".strip(),
|
||||||
metadata_raw=json.dumps(session_metadata),
|
|
||||||
asn=ip_data.get("asn", ""),
|
asn=ip_data.get("asn", ""),
|
||||||
country=ip_data.get("country", ""),
|
country=ip_data.get("country", ""),
|
||||||
longitude=ip_data.get("longitude"),
|
longitude=ip_data.get("longitude"),
|
||||||
@ -79,16 +77,11 @@ def ingress_request(
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
log.debug("Updating old session with new data...")
|
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
|
# Update last seen time
|
||||||
session.last_seen = timezone.now()
|
session.last_seen = timezone.now()
|
||||||
session.save()
|
session.save()
|
||||||
|
|
||||||
# Create or update hit
|
# Create or update hit
|
||||||
hit_metadata = payload.get("hitMetadata", {})
|
|
||||||
idempotency = payload.get("idempotency")
|
idempotency = payload.get("idempotency")
|
||||||
idempotency_path = f"hit_idempotency_{idempotency}"
|
idempotency_path = f"hit_idempotency_{idempotency}"
|
||||||
hit = None
|
hit = None
|
||||||
@ -103,9 +96,6 @@ def ingress_request(
|
|||||||
log.debug("Hit is a heartbeat; updating old hit with new data...")
|
log.debug("Hit is a heartbeat; updating old hit with new data...")
|
||||||
hit.heartbeats += 1
|
hit.heartbeats += 1
|
||||||
hit.duration = (timezone.now() - hit.start).total_seconds()
|
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()
|
hit.save()
|
||||||
if hit is None:
|
if hit is None:
|
||||||
log.debug("Hit is a page load; creating new hit...")
|
log.debug("Hit is a page load; creating new hit...")
|
||||||
@ -116,10 +106,10 @@ def ingress_request(
|
|||||||
location=location,
|
location=location,
|
||||||
referrer=payload.get("referrer", ""),
|
referrer=payload.get("referrer", ""),
|
||||||
loadTime=payload.get("loadTime"),
|
loadTime=payload.get("loadTime"),
|
||||||
metadata_raw=json.dumps(hit_metadata),
|
|
||||||
)
|
)
|
||||||
# Set idempotency (if applicable)
|
# Set idempotency (if applicable)
|
||||||
if idempotency is not None:
|
if idempotency is not None:
|
||||||
cache.set(idempotency_path, hit.pk, timeout=30 * 60)
|
cache.set(idempotency_path, hit.pk, timeout=30 * 60)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error(e)
|
log.exception(e)
|
||||||
|
raise e
|
||||||
|
@ -13,12 +13,6 @@ window.onload = function () {
|
|||||||
loadTime:
|
loadTime:
|
||||||
window.performance.timing.domContentLoadedEventEnd -
|
window.performance.timing.domContentLoadedEventEnd -
|
||||||
window.performance.timing.navigationStart,
|
window.performance.timing.navigationStart,
|
||||||
hitMetadata:
|
|
||||||
typeof shynetHitMetadata !== "undefined" ? shynetHitMetadata : {},
|
|
||||||
sessionMetadata:
|
|
||||||
typeof shynetSessionMetadata !== "undefined"
|
|
||||||
? shynetSessionMetadata
|
|
||||||
: {},
|
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user