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 ( "