Update utils.py
Browse files
utils.py
CHANGED
@@ -7,6 +7,52 @@ from typing import Dict, List, Tuple
|
|
7 |
from bson import ObjectId
|
8 |
import logging
|
9 |
from config import logger
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
def clean_text_response(text: str) -> str:
|
12 |
text = re.sub(r'\n\s*\n', '\n\n', text)
|
|
|
7 |
from bson import ObjectId
|
8 |
import logging
|
9 |
from config import logger
|
10 |
+
# Add to your utils.py
|
11 |
+
from fastapi import WebSocket
|
12 |
+
import asyncio
|
13 |
+
|
14 |
+
class NotificationManager:
|
15 |
+
def __init__(self):
|
16 |
+
self.active_connections = {}
|
17 |
+
self.notification_queue = asyncio.Queue()
|
18 |
+
|
19 |
+
async def connect(self, websocket: WebSocket, user_id: str):
|
20 |
+
await websocket.accept()
|
21 |
+
self.active_connections[user_id] = websocket
|
22 |
+
|
23 |
+
def disconnect(self, user_id: str):
|
24 |
+
if user_id in self.active_connections:
|
25 |
+
del self.active_connections[user_id]
|
26 |
+
|
27 |
+
async def broadcast_notification(self, notification: dict):
|
28 |
+
"""Broadcast to all connected clients"""
|
29 |
+
for connection in self.active_connections.values():
|
30 |
+
try:
|
31 |
+
await connection.send_json({
|
32 |
+
"type": "notification",
|
33 |
+
"data": notification
|
34 |
+
})
|
35 |
+
except Exception as e:
|
36 |
+
logger.error(f"Error sending notification: {e}")
|
37 |
+
|
38 |
+
notification_manager = NotificationManager()
|
39 |
+
|
40 |
+
async def broadcast_notification(notification: dict):
|
41 |
+
"""Broadcast notification to relevant users"""
|
42 |
+
# Determine recipients based on notification type/priority
|
43 |
+
recipients = []
|
44 |
+
if notification["priority"] == "high":
|
45 |
+
recipients = ["psychiatrist", "emergency_team", "primary_care"]
|
46 |
+
else:
|
47 |
+
recipients = ["primary_care", "case_manager"]
|
48 |
+
|
49 |
+
# Add to each recipient's notification queue
|
50 |
+
await notification_manager.notification_queue.put({
|
51 |
+
"recipients": recipients,
|
52 |
+
"notification": notification
|
53 |
+
})
|
54 |
+
|
55 |
+
|
56 |
|
57 |
def clean_text_response(text: str) -> str:
|
58 |
text = re.sub(r'\n\s*\n', '\n\n', text)
|