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 operator import and_
from functools import reduce from functools import reduce
from sqlalchemy import select, func, desc from sqlalchemy import distinct, select, func, desc
from sanic.response import json from sanic.response import json
from obs.api.app import api 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 from obs.api.utils import round_to
@ -92,6 +92,14 @@ async def stats(req):
.where(track_condition) .where(track_condition)
) )
).scalar() ).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 = { result = {
"numEvents": event_count, "numEvents": event_count,
@ -100,6 +108,7 @@ async def stats(req):
"trackDuration": round_to(track_duration or 0, TRACK_DURATION_ROUNDING), "trackDuration": round_to(track_duration or 0, TRACK_DURATION_ROUNDING),
"publicTrackCount": public_track_count, "publicTrackCount": public_track_count,
"trackCount": track_count, "trackCount": track_count,
"deviceCount": device_count,
} }
return json(result) return json(result)

View file

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

View file

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

View file

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

View file

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