Include device count in stats

This commit is contained in:
Paul Bienkowski 2023-03-30 14:18:20 +02:00
parent cf8358d14b
commit de8d371b65
5 changed files with 35 additions and 16 deletions

View file

@ -4,12 +4,12 @@ from typing import Optional
from operator import and_
from functools import reduce
from sqlalchemy import select, func, desc
from sqlalchemy import distinct, select, func, desc
from sanic.response import json
from obs.api.app import api
from obs.api.db import Track, OvertakingEvent, User, Region
from obs.api.db import Track, OvertakingEvent, User, Region, UserDevice
from obs.api.utils import round_to
@ -92,6 +92,14 @@ async def stats(req):
.where(track_condition)
)
).scalar()
device_count = (
await req.ctx.db.execute(
select(func.count(distinct(UserDevice.id)))
.select_from(UserDevice)
.join(Track.user_device)
.where(track_condition)
)
).scalar()
result = {
"numEvents": event_count,
@ -100,6 +108,7 @@ async def stats(req):
"trackDuration": round_to(track_duration or 0, TRACK_DURATION_ROUNDING),
"publicTrackCount": public_track_count,
"trackCount": track_count,
"deviceCount": device_count,
}
return json(result)

View file

@ -98,20 +98,27 @@ export default function Stats({ user = null }: { user?: null | string }) {
</Statistic.Value>
<Statistic.Label>{t("Stats.eventsConfirmed")}</Statistic.Label>
</Statistic>
{user ? (
<Statistic>
<Statistic.Value>
{stats?.trackCount ?? placeholder}
</Statistic.Value>
<Statistic.Label>{t("Stats.tracksRecorded")}</Statistic.Label>
</Statistic>
) : (
<Statistic>
<Statistic.Value>
{stats?.userCount ?? placeholder}
</Statistic.Value>
<Statistic.Label>{t("Stats.membersJoined")}</Statistic.Label>
</Statistic>
<Statistic>
<Statistic.Value>
{stats?.trackCount ?? placeholder}
</Statistic.Value>
<Statistic.Label>{t("Stats.tracksRecorded")}</Statistic.Label>
</Statistic>
{!user && (
<>
<Statistic>
<Statistic.Value>
{stats?.userCount ?? placeholder}
</Statistic.Value>
<Statistic.Label>{t("Stats.membersJoined")}</Statistic.Label>
</Statistic>
<Statistic>
<Statistic.Value>
{stats?.deviceCount ?? placeholder}
</Statistic.Value>
<Statistic.Label>{t("Stats.deviceCount")}</Statistic.Label>
</Statistic>
</>
)}
</Statistic.Group>
</Segment>

View file

@ -59,6 +59,7 @@ Stats:
eventsConfirmed: Bestätigte Vorgänge
tracksRecorded: Aufgezeichnete Fahrten
membersJoined: Neue Mitglieder
deviceCount: Geräte benutzt
thisMonth: Dieser Monat
thisYear: Dieses Jahr
allTime: Immer

View file

@ -64,6 +64,7 @@ Stats:
eventsConfirmed: Events confirmed
tracksRecorded: Tracks recorded
membersJoined: Members joined
deviceCount: Devices used
thisMonth: This month
thisYear: This year
allTime: All time

View file

@ -65,6 +65,7 @@ Stats:
eventsConfirmed: Evénements confirmées
tracksRecorded: Traces enregistrées
membersJoined: Membres enregistrés
deviceCount: Appareils utilisés
thisMonth: Ce mois
thisYear: Cette annéee
allTime: De tout temps