From b7d84085a301dbe85c0a0819a71b055472afc7e9 Mon Sep 17 00:00:00 2001 From: "R. Miles McCain" Date: Sun, 12 Apr 2020 17:23:46 -0400 Subject: [PATCH] Add charts --- shynet/core/models.py | 29 +++++-- shynet/core/templates/base.html | 4 +- .../core/includes/service_overview.html | 44 ++++++----- .../templates/core/includes/time_chart.html | 76 +++++++++++++++++++ shynet/core/templates/core/pages/service.html | 1 + 5 files changed, 127 insertions(+), 27 deletions(-) create mode 100644 shynet/core/templates/core/includes/time_chart.html diff --git a/shynet/core/models.py b/shynet/core/models.py index ed26cfd..1816223 100644 --- a/shynet/core/models.py +++ b/shynet/core/models.py @@ -1,10 +1,12 @@ import uuid +import json from django.apps import apps from django.contrib.auth.models import AbstractUser from django.db import models from django.db.utils import NotSupportedError from django.utils import timezone +from django.db.models.functions import TruncDate def _default_uuid(): @@ -60,12 +62,9 @@ class Service(models.Model): service=self, start_time__gt=timezone.now() - timezone.timedelta(seconds=10) ).count() - sessions = ( - Session.objects.filter( - service=self, start_time__gt=start_time, start_time__lt=end_time - ) - .order_by("-start_time") - ) + sessions = Session.objects.filter( + service=self, start_time__gt=start_time, start_time__lt=end_time + ).order_by("-start_time") session_count = sessions.count() hits = Hit.objects.filter( @@ -135,6 +134,18 @@ class Service(models.Model): ] ) / max(session_count, 1) + session_chart_data = { + k["date"]: k["count"] + for k in sessions.annotate(date=TruncDate("start_time")) + .values("date") + .annotate(count=models.Count("uuid")) + .order_by("date") + } + for day_offset in range((end_time - start_time).days + 1): + day = (start_time + timezone.timedelta(days=day_offset)).date() + if day not in session_chart_data: + session_chart_data[day] = 0 + return { "currently_online": currently_online, "session_count": session_count, @@ -150,5 +161,11 @@ class Service(models.Model): "browsers": browsers, "devices": devices, "device_types": device_types, + "session_chart_data": json.dumps( + [ + {"x": str(key), "y": value} + for key, value in session_chart_data.items() + ] + ), "online": True, } diff --git a/shynet/core/templates/base.html b/shynet/core/templates/base.html index c0752c0..5879d90 100644 --- a/shynet/core/templates/base.html +++ b/shynet/core/templates/base.html @@ -8,8 +8,8 @@ {% include 'a17t/head.html' %} - + {% block extra_head %} {% endblock %} diff --git a/shynet/core/templates/core/includes/service_overview.html b/shynet/core/templates/core/includes/service_overview.html index ba4c71d..1aaaccf 100644 --- a/shynet/core/templates/core/includes/service_overview.html +++ b/shynet/core/templates/core/includes/service_overview.html @@ -1,30 +1,36 @@ {% load humanize helpers %} - + {% with stats=object.stats %} -
-
-

+
+
+

{{object.name}}

{% include 'core/includes/stats_status_chip.html' %}
-
-

Sessions

-

{{stats.session_count|intcomma}}

-
-
-

Bounce Rate

-

{{stats.bounce_rate_pct|floatformat:"-1"}}%

-
-
-

Avg. Duration

-

{{stats.avg_session_duration|naturaldelta}}

-
-
-

Uptime

-

99.9%

+
+
+

Sessions

+

{{stats.session_count|intcomma}}

+
+
+

Bounce Rate

+

{{stats.bounce_rate_pct|floatformat:"-1"}}%

+
+
+

Avg. Duration

+

{{stats.avg_session_duration|naturaldelta}}

+
+
+

Uptime

+

99.9%

+
+
+
+ {% include 'core/includes/time_chart.html' with data=stats.session_chart_data sparkline=True height=100 %} +
{% endwith %}
\ No newline at end of file diff --git a/shynet/core/templates/core/includes/time_chart.html b/shynet/core/templates/core/includes/time_chart.html new file mode 100644 index 0000000..df77712 --- /dev/null +++ b/shynet/core/templates/core/includes/time_chart.html @@ -0,0 +1,76 @@ +
+ \ No newline at end of file diff --git a/shynet/core/templates/core/pages/service.html b/shynet/core/templates/core/pages/service.html index 4f210f7..b936b80 100644 --- a/shynet/core/templates/core/pages/service.html +++ b/shynet/core/templates/core/pages/service.html @@ -8,6 +8,7 @@ {% endblock %} {% block service_content %} +{% include 'core/includes/time_chart.html' with data=stats.session_chart_data %}

Sessions