2
0
Fork 0

Fix metrics

This commit is contained in:
Luca 2023-05-08 00:10:13 +02:00
parent 4a3f7adbee
commit ae82b26644
1 changed files with 26 additions and 35 deletions

View File

@ -1,37 +1,10 @@
from django.http import HttpResponse from django.http import HttpResponse
from shiftregister.app.models import Helper, ShiftRegistration, Message, Shift from shiftregister.app.models import Helper, ShiftRegistration, Message
from django.db.models import F, Count, Q, ExpressionWrapper, Case, When, Sum from django.db.models import Count, Case, When, Sum
from django.db import models from django.db import models
def metrics(request): def metrics(request):
help_wanted = Q(required_helpers__gt=F("reg_count")) | Q(required_helpers=0) & Q(
room__required_helpers__gt=F("reg_count")
)
team_shifts = (
Shift.with_reg_count()
.annotate(team_count=Count("teambackup"))
.filter(
help_wanted,
team_count__gt=0,
deleted=False,
)
)
helping_helpers = Helper.objects.annotate(
shift_count=Count(
Case(
When(
shiftregistration__state__in=[
ShiftRegistration.RegState.CHECKED_IN,
],
then=1,
),
output_field=models.IntegerField(),
)
)
).filter(number_validated=True, shift_count__gte=1)
response = HttpResponse( response = HttpResponse(
"\n".join( "\n".join(
( (
@ -42,24 +15,42 @@ def metrics(request):
"helpers_confirmed_total", "helpers_confirmed_total",
Helper.objects.filter(number_validated=True).count(), Helper.objects.filter(number_validated=True).count(),
), ),
("helpers_helped", helping_helpers.count()),
( (
"worked_shifts", "helpers_helped_total",
Helper.objects.annotate(
shift_count=Count(
Case(
When(
shiftregistration__state__in=[
ShiftRegistration.RegState.CHECKED_IN
],
then=1,
),
output_field=models.IntegerField(),
)
)
)
.filter(number_validated=True, shift_count__gte=1)
.count(),
),
(
"worked_shifts_total",
ShiftRegistration.objects.filter( ShiftRegistration.objects.filter(
state=ShiftRegistration.RegState.CHECKED_IN state=ShiftRegistration.RegState.CHECKED_IN
).count(), ).count(),
), ),
( (
"messages_sent", "messages_sent_total",
Message.objects.all().count(), Message.objects.all().count(),
), ),
( (
"hours_worked", "worked_seconds_total",
ShiftRegistration.objects.filter( ShiftRegistration.objects.filter(
state=ShiftRegistration.RegState.CHECKED_IN state=ShiftRegistration.RegState.CHECKED_IN
).aggregate(sum=Sum("shift__duration"))["sum"], )
.aggregate(sum=Sum("shift__duration"))["sum"]
.total_seconds(),
), ),
("team_shifts", team_shifts.count()),
) )
) )
) )