Compare commits
2 Commits
02aee01d20
...
4404223350
Author | SHA1 | Date | |
---|---|---|---|
4404223350 | |||
007253cdfd |
20
doorcontrol/management/commands/scrape_hid_events.py
Normal file
20
doorcontrol/management/commands/scrape_hid_events.py
Normal 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)
|
@ -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)
|
||||
|
@ -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):
|
||||
|
@ -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
|
||||
),
|
||||
|
Loading…
Reference in New Issue
Block a user