-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert_manager.py
40 lines (29 loc) · 1.16 KB
/
alert_manager.py
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
from uuid import UUID
from datetime import datetime
from sqlalchemy.orm import Session
from starlette.websockets import WebSocket
import crud
import schemas
def serialize_alert(alert: schemas.Alert):
return {
"surgery_id": str(alert.surgery_id),
"message": alert.message,
"severity": alert.severity,
"time_started": datetime.isoformat(alert.time_started),
"time_resolved": datetime.isoformat(alert.time_resolved),
"overridden": alert.overridden,
"resolved": alert.resolved,
"id": str(alert.id)
}
class AlertManager:
def __init__(self):
self.active_connections: list[WebSocket] = []
async def connect(self, websocket: WebSocket):
await websocket.accept()
self.active_connections.append(websocket)
def disconnect(self, websocket: WebSocket):
self.active_connections.remove(websocket)
async def broadcast_alerts(self, db: Session, surgery_id: UUID):
alerts = list(map(serialize_alert, crud.get_surgery_alerts(db, surgery_id)))
for connection in self.active_connections:
await connection.send_json({"type": "alerts", "data": alerts})