File size: 5,326 Bytes
2758540
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""
database/crud.py - CRUD operations for Persons, Events, and Incident Reports
"""
import uuid
from datetime import datetime
from typing import List, Optional, Dict, Any
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, update, and_, desc
from database.models import Person, Event, IncidentReport, ActivityType
from loguru import logger


# ─── Person Operations ────────────────────────────────────────────────────────

async def create_person(
    db: AsyncSession,
    faiss_id: Optional[int] = None,
    attributes: Optional[Dict] = None,
    thumbnail_path: Optional[str] = None,
) -> Person:
    person = Person(
        faiss_id=faiss_id,
        attributes=attributes or {},
        thumbnail_path=thumbnail_path,
        track_ids=[],
    )
    db.add(person)
    await db.flush()
    logger.info(f"Created person: {person.id}")
    return person


async def get_person(db: AsyncSession, person_id: uuid.UUID) -> Optional[Person]:
    result = await db.execute(select(Person).where(Person.id == person_id))
    return result.scalar_one_or_none()


async def get_all_persons(db: AsyncSession, limit: int = 100, offset: int = 0) -> List[Person]:
    result = await db.execute(
        select(Person).order_by(desc(Person.last_seen)).limit(limit).offset(offset)
    )
    return result.scalars().all()


async def update_person_last_seen(db: AsyncSession, person_id: uuid.UUID) -> None:
    await db.execute(
        update(Person).where(Person.id == person_id).values(last_seen=datetime.utcnow())
    )


async def update_person_faiss_id(db: AsyncSession, person_id: uuid.UUID, faiss_id: int) -> None:
    await db.execute(
        update(Person).where(Person.id == person_id).values(faiss_id=faiss_id)
    )


async def update_person_attributes(db: AsyncSession, person_id: uuid.UUID, attributes: Dict) -> None:
    await db.execute(
        update(Person).where(Person.id == person_id).values(attributes=attributes)
    )


# ─── Event Operations ─────────────────────────────────────────────────────────

async def create_event(
    db: AsyncSession,
    person_id: uuid.UUID,
    camera_id: str,
    activity_type: ActivityType = ActivityType.DETECTED,
    bounding_box: Optional[Dict] = None,
    confidence: Optional[float] = None,
    track_id: Optional[int] = None,
    location_zone: Optional[str] = None,
    anomaly_score: float = 0.0,
    description: Optional[str] = None,
    raw_metadata: Optional[Dict] = None,
) -> Event:
    event = Event(
        person_id=person_id,
        camera_id=camera_id,
        activity_type=activity_type,
        bounding_box=bounding_box,
        confidence=confidence,
        track_id=track_id,
        location_zone=location_zone,
        anomaly_score=anomaly_score,
        description=description,
        raw_metadata=raw_metadata or {},
    )
    db.add(event)
    await db.flush()
    return event


async def get_events_for_person(
    db: AsyncSession,
    person_id: uuid.UUID,
    limit: int = 50,
) -> List[Event]:
    result = await db.execute(
        select(Event)
        .where(Event.person_id == person_id)
        .order_by(desc(Event.timestamp))
        .limit(limit)
    )
    return result.scalars().all()


async def get_recent_events(
    db: AsyncSession,
    camera_id: Optional[str] = None,
    limit: int = 100,
) -> List[Event]:
    query = select(Event).order_by(desc(Event.timestamp)).limit(limit)
    if camera_id:
        query = query.where(Event.camera_id == camera_id)
    result = await db.execute(query)
    return result.scalars().all()


async def get_anomaly_events(db: AsyncSession, threshold: float = 0.75, limit: int = 50) -> List[Event]:
    result = await db.execute(
        select(Event)
        .where(Event.anomaly_score >= threshold)
        .order_by(desc(Event.timestamp))
        .limit(limit)
    )
    return result.scalars().all()


# ─── Incident Report Operations ───────────────────────────────────────────────

async def create_incident_report(
    db: AsyncSession,
    person_id: Optional[uuid.UUID],
    report_text: str,
    summary: Optional[str] = None,
    severity: str = "medium",
    camera_ids: Optional[List[str]] = None,
) -> IncidentReport:
    report = IncidentReport(
        person_id=person_id,
        report_text=report_text,
        summary=summary,
        severity=severity,
        camera_ids=camera_ids or [],
    )
    db.add(report)
    await db.flush()
    return report


async def get_report(db: AsyncSession, report_id: uuid.UUID) -> Optional[IncidentReport]:
    result = await db.execute(select(IncidentReport).where(IncidentReport.report_id == report_id))
    return result.scalar_one_or_none()


async def get_reports_for_person(db: AsyncSession, person_id: uuid.UUID) -> List[IncidentReport]:
    result = await db.execute(
        select(IncidentReport)
        .where(IncidentReport.person_id == person_id)
        .order_by(desc(IncidentReport.generated_at))
    )
    return result.scalars().all()