doorcontrol: Add "granted access" count to "detail by day" report
All checks were successful
Ruff / ruff (push) Successful in 29s
Test / test (push) Successful in 4m25s

This commit is contained in:
Adam Goldsmith 2024-05-13 00:38:01 -04:00
parent 007253cdfd
commit 4404223350
3 changed files with 12 additions and 5 deletions

View File

@ -1,4 +1,5 @@
from datetime import datetime from datetime import datetime
from typing import Self
from django.conf import settings from django.conf import settings
from django.db import models from django.db import models
@ -179,6 +180,10 @@ class HIDEvent(models.Model):
DOOR_UNLOCKED = 12032, "Door Unlocked" DOOR_UNLOCKED = 12032, "Door Unlocked"
DOOR_LOCKED = 12033, "Door Locked" DOOR_LOCKED = 12033, "Door Locked"
@classmethod
def any_granted_access(cls) -> list[Self]:
return [t for t in cls if t.name.startswith("GRANTED_ACCESS")]
door = models.ForeignKey(Door, on_delete=models.CASCADE) door = models.ForeignKey(Door, on_delete=models.CASCADE)
timestamp = models.DateTimeField() timestamp = models.DateTimeField()
event_type = models.IntegerField(db_column="eventType", choices=EventType.choices) event_type = models.IntegerField(db_column="eventType", choices=EventType.choices)

View File

@ -46,6 +46,7 @@ class DetailByDayTable(tables.Table):
timestamp__date = tables.DateColumn(verbose_name="Date") timestamp__date = tables.DateColumn(verbose_name="Date")
name = tables.Column() name = tables.Column()
access_count = tables.Column() access_count = tables.Column()
granted_access_count = tables.Column()
class BusiestDayOfWeekTable(tables.Table): class BusiestDayOfWeekTable(tables.Table):

View File

@ -3,7 +3,7 @@ import datetime
from django.contrib.auth.mixins import PermissionRequiredMixin from django.contrib.auth.mixins import PermissionRequiredMixin
from django.core.exceptions import BadRequest from django.core.exceptions import BadRequest
from django.core.paginator import Page from django.core.paginator import Page
from django.db.models import Count, F, FloatField, Window from django.db.models import Count, F, FloatField, Q, Window
from django.db.models.functions import Lead, Trunc from django.db.models.functions import Lead, Trunc
from django.urls import path, reverse_lazy from django.urls import path, reverse_lazy
from django.utils.text import slugify from django.utils.text import slugify
@ -165,13 +165,10 @@ class AccessPerUnitTime(BaseAccessReport):
if unit_time not in self.UNIT_TIMES: if unit_time not in self.UNIT_TIMES:
raise BadRequest("unit time must be one of day, week, month, or year") raise BadRequest("unit time must be one of day, week, month, or year")
granted_event_types = [
t for t in HIDEvent.EventType if t.name.startswith("GRANTED_ACCESS")
]
return ( return (
super() super()
.get_table_data() .get_table_data()
.filter(event_type__in=granted_event_types) .filter(event_type__in=HIDEvent.EventType.any_granted_access())
.with_member_id() .with_member_id()
.values(unit_time=Trunc("timestamp", unit_time)) .values(unit_time=Trunc("timestamp", unit_time))
.annotate( .annotate(
@ -255,6 +252,10 @@ class DetailByDay(BaseAccessReport):
.filter(member_id__isnull=False) .filter(member_id__isnull=False)
.annotate( .annotate(
access_count=Count("member_id"), access_count=Count("member_id"),
granted_access_count=Count(
"member_id",
filter=Q(event_type__in=HIDEvent.EventType.any_granted_access()),
),
name=GroupConcat( name=GroupConcat(
ConcatWS("forename", "surname", separator=" "), distinct=True ConcatWS("forename", "surname", separator=" "), distinct=True
), ),