GLAM_Web_App / notifications.py
liammatt5's picture
Create notifications.py
0649e35 verified
Raw
History Blame Contribute Delete
2.69 kB
import re
from typing import List, Dict, Any
def summarize_live_alerts(rows: List[List[Any]], patient_names: List[str] = None) -> Dict[str, Any]:
alerts = []
has_patients = patient_names is not None and any(name is not None for name in patient_names)
for row in rows or []:
if not row:
continue
patient_name = row[0] if len(row) > 0 else "Unknown"
comment = str(row[-1] or "")
level = None
lowered = comment.lower()
if re.search(r"\bred\b", lowered):
level = "red"
elif re.search(r"\borange\b", lowered):
level = "orange"
if level:
alerts.append({
"patient_name": patient_name,
"level": level,
"message": comment,
})
return {"count": len(alerts), "alerts": alerts, "has_patients": has_patients}
def build_live_notification_html(rows: List[List[Any]], patient_names: List[str] = None) -> str:
summary = summarize_live_alerts(rows, patient_names)
count = summary["count"]
alerts = summary["alerts"]
has_patients = summary.get("has_patients", True)
if not alerts and not has_patients:
return (
"<div class='live-notifications empty no-patients' style='opacity:1; filter:none; -webkit-filter:none;'>"
"<div class='notification-header'><span class='notification-badge'>0</span><span>No Critical Patients</span></div>"
"<div class='notification-body'>Begin Live Monitoring to detect critical patients.</div>"
"</div>"
)
if not alerts and has_patients:
return (
"<div class='live-notifications empty' style='opacity:1; filter:none; -webkit-filter:none;'>"
"<div class='notification-header'><span class='notification-badge'>0</span><span>All stable</span></div>"
"<div class='notification-body'>No critical alerts detected.</div>"
"</div>"
)
items = []
for entry in alerts:
items.append(
f"<div class='notification-item notification-{entry['level']}' style='opacity:1; filter:none; -webkit-filter:none;'>"
f"<div class='notification-title'>{entry['patient_name']}</div>"
f"<div class='notification-message'>{entry['message']}</div>"
f"</div>"
)
return (
"<div class='live-notifications' style='opacity:1; filter:none; -webkit-filter:none;'>"
f"<div class='notification-header'><span class='notification-badge'>{count}</span><span>Critical patients</span></div>"
f"<div class='notification-list'>{''.join(items)}</div>"
"</div>"
)