Improve service interface

This commit is contained in:
R. Miles McCain
2020-04-17 14:06:12 -04:00
parent 654b628fc9
commit fc2919d642
8 changed files with 50 additions and 37 deletions

View File

@@ -63,7 +63,7 @@ class Service(models.Model):
Hit = apps.get_model("analytics", "Hit")
currently_online = Session.objects.filter(
service=self, start_time__gt=timezone.now() - timezone.timedelta(seconds=10)
service=self, last_seen__gt=timezone.now() - timezone.timedelta(seconds=10)
).count()
sessions = Session.objects.filter(
@@ -92,6 +92,12 @@ class Service(models.Model):
.order_by("-count")
)
countries = (
sessions.values("country")
.annotate(count=models.Count("country"))
.order_by("-count")
)
operating_systems = (
sessions.values("os").annotate(count=models.Count("os")).order_by("-count")
)
@@ -165,6 +171,7 @@ class Service(models.Model):
"avg_hits_per_session": avg_hits_per_session,
"locations": locations,
"referrers": referrers,
"countries": countries,
"operating_systems": operating_systems,
"browsers": browsers,
"devices": devices,

View File

@@ -3,7 +3,7 @@
<a class="card ~neutral !low service mb-6 p-0" href="{% url 'dashboard:service' object.uuid %}">
{% with stats=object.stats %}
<div class="p-4 md:flex justify-between">
<div class="md:w-4/12 flex items-center mb-4 md:mb-0">
<div class="flex items-center mb-4 md:mb-0">
<h3 class="heading text-xl md:text-2xl mr-2 mb-1 text-purple-600">
{{object.name}}
</h3>

View File

@@ -5,13 +5,5 @@
<span class="chip ~positive !high">
{{stats.currently_online|intcomma}} online
</span>
{% elif stats.online == True %}
<span class="chip ~positive !high">
Online
</span>
{% elif stats.online == False %}
<span class="chip ~critical !high">
Offline
</span>
{% endif %}
{% endwith %}

View File

@@ -105,6 +105,24 @@
</tbody>
</table>
</div>
<div class="card ~neutral !low limited-height py-2">
<table class="table">
<thead class="text-sm">
<tr>
<th>Country</th>
<th class="rf">Sessions</th>
</tr>
</thead>
<tbody>
{% for country in stats.countries %}
<tr>
<td>{{country.country|flag_emoji}} {{country.country|country_name}}</td>
<td class="rf">{{country.count|intcomma}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="card ~neutral !low limited-height py-2">
<table class="table">
<thead class="text-sm">
@@ -141,24 +159,6 @@
</tbody>
</table>
</div>
<div class="card ~neutral !low limited-height py-2">
<table class="table">
<thead class="text-sm">
<tr>
<th>Device</th>
<th class="rf">Sessions</th>
</tr>
</thead>
<tbody>
{% for device in stats.devices %}
<tr>
<td>{{device.device|default:"Unknown"}}</td>
<td class="rf">{{device.count|intcomma}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="card ~neutral !low limited-height py-2">
<table class="table">
<thead class="text-sm">

View File

@@ -45,7 +45,7 @@
</div>
<div>
<p>Country</p>
<p class="label">{{session.country|flag_emoji}} {{session.country|default:"Unknown"}}</p>
<p class="label">{{session.country|flag_emoji}} {{session.country|country_name}}</p>
</div>
<div>
<p>Location</p>

View File

@@ -1,5 +1,5 @@
from urllib.parse import urlparse
import pycountry
import flag
from django import template
from django.utils import timezone
@@ -30,6 +30,12 @@ def flag_emoji(isocode):
except:
return ""
@register.filter
def country_name(isocode):
try:
return pycountry.countries.get(alpha_2=isocode).name
except:
return "Unknown"
@register.filter
def startswith(text, starts):