File size: 2,689 Bytes
0649e35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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>"
    )