-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction_logs.py
More file actions
33 lines (24 loc) · 852 Bytes
/
Copy pathaction_logs.py
File metadata and controls
33 lines (24 loc) · 852 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from __future__ import annotations
from typing import Any
from uuid import uuid4
from psycopg.types.json import Json
from db import get_conn
LOG_TYPE_GATE_OPENED = "gate_opened"
PERSON_IDENTIFIED = "person_identified"
_ALLOWED_TYPES = {LOG_TYPE_GATE_OPENED, PERSON_IDENTIFIED}
def insert_action_log(log_type: str, raw: dict[str, Any]) -> str:
if log_type not in _ALLOWED_TYPES:
raise ValueError(f"Unsupported log_type: {log_type}")
log_id = f"action_{uuid4().hex}"
with get_conn() as conn, conn.cursor() as cur:
cur.execute(
"""
INSERT INTO action_logs (id, log_type, raw)
VALUES (%s, %s, %s)
RETURNING id
""",
(log_id, log_type, Json(raw)),
)
row = cur.fetchone()
conn.commit()
return row["id"] if row else log_id