Compare commits

...

2 Commits

Author SHA1 Message Date
4404223350 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
2024-05-13 00:38:01 -04:00
007253cdfd doorcontrol: Add management command to scrape HID events 2024-05-13 00:30:47 -04:00
4 changed files with 32 additions and 5 deletions

View File

@ -0,0 +1,20 @@
from django.core.management.base import BaseCommand, CommandError
from doorcontrol.models import Door
from doorcontrol.tasks.scrapehidevents import getMessages
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument("door_names", nargs="*")
def handle(self, *args, door_names: list[str], verbosity: int, **options):
doors = Door.objects.all()
if door_names:
doors = doors.filter(name__in=door_names)
if len(doors) != len(door_names):
raise CommandError("Not all door names matched doors in database")
for door in doors:
print(door)
getMessages(door)

View File

@ -1,4 +1,5 @@
from datetime import datetime
from typing import Self
from django.conf import settings
from django.db import models
@ -179,6 +180,10 @@ class HIDEvent(models.Model):
DOOR_UNLOCKED = 12032, "Door Unlocked"
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)
timestamp = models.DateTimeField()
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")
name = tables.Column()
access_count = tables.Column()
granted_access_count = tables.Column()
class BusiestDayOfWeekTable(tables.Table):

View File

@ -3,7 +3,7 @@ import datetime
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.core.exceptions import BadRequest
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.urls import path, reverse_lazy
from django.utils.text import slugify
@ -165,13 +165,10 @@ class AccessPerUnitTime(BaseAccessReport):
if unit_time not in self.UNIT_TIMES:
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 (
super()
.get_table_data()
.filter(event_type__in=granted_event_types)
.filter(event_type__in=HIDEvent.EventType.any_granted_access())
.with_member_id()
.values(unit_time=Trunc("timestamp", unit_time))
.annotate(
@ -255,6 +252,10 @@ class DetailByDay(BaseAccessReport):
.filter(member_id__isnull=False)
.annotate(
access_count=Count("member_id"),
granted_access_count=Count(
"member_id",
filter=Q(event_type__in=HIDEvent.EventType.any_granted_access()),
),
name=GroupConcat(
ConcatWS("forename", "surname", separator=" "), distinct=True
),