fix: close sets restored_at, SLA alert type, error shape, prefix label

This commit is contained in:
Atharv1705
2026-11-12 18:11:08 +05:30
parent e2a6fa601e
commit 1cf218ca00
6 changed files with 200 additions and 20 deletions
+35 -11
View File
@@ -278,7 +278,7 @@ def run_alert_check(db: Session) -> dict[str, object]:
created_at=eff_now,
)
# 7. Critical complaints open >= critical_complaint_alert_hours
# 7. Critical complaints open >= escalation_open_complaint_hours
crit_complaints = (
db.query(Complaint)
.options(joinedload(Complaint.asset))
@@ -288,16 +288,7 @@ def run_alert_check(db: Session) -> dict[str, object]:
threshold_secs = cfg["critical_hours"] * 3600
for c in crit_complaints:
elapsed = (_ensure_utc(eff_now) - _ensure_utc(c.registered_at)).total_seconds()
sla_breached, _target_h = compute_sla_status(
c.priority,
c.registered_at,
c.attended_at,
c.restored_at,
critical_sla_hours=cfg["critical_sla_hours"],
non_critical_sla_hours=cfg["non_critical_sla_hours"],
now_utc=eff_now,
)
if elapsed >= threshold_secs or sla_breached:
if elapsed >= threshold_secs:
created += _add_if_new(
db,
existing_keys,
@@ -313,6 +304,39 @@ def run_alert_check(db: Session) -> dict[str, object]:
created_at=eff_now,
)
# 8. Open unrestored complaints breaching configured SLA hours
open_unrestored = (
db.query(Complaint)
.options(joinedload(Complaint.asset))
.filter(Complaint.call_status != "closed", Complaint.restored_at.is_(None))
.all()
)
for c in open_unrestored:
sla_breached, sla_hours = compute_sla_status(
c.priority,
c.registered_at,
c.attended_at,
c.restored_at,
critical_sla_hours=cfg["critical_sla_hours"],
non_critical_sla_hours=cfg["non_critical_sla_hours"],
now_utc=eff_now,
)
if sla_breached:
created += _add_if_new(
db,
existing_keys,
dedupe_key=f"cmp:{c.id}:sla_breach",
alert_type="sla_breach",
severity="red",
title=f"SLA Breach: Open > {sla_hours}h: {c.complaint_no}",
message=f"Complaint {c.complaint_no} for {c.asset.name} ({c.asset.asset_code}) exceeded {sla_hours}h SLA without restoration.",
entity_type="complaint",
entity_id=c.id,
due_date=today,
target_role="biomedical,management,system_admin,department_user",
created_at=eff_now,
)
db.flush()
return {"created": created, "evaluated_today": today.isoformat()}
+2 -2
View File
@@ -131,8 +131,8 @@ SETTINGS_SCHEMA: list[dict[str, Any]] = [
"key": "asset_code_prefix",
"type": "str",
"default": "PHRC",
"label": "Asset Code Prefix",
"description": "Default prefix used when formatting hospital asset codes (e.g. PHRC-ICU-0001).",
"label": "Asset Code Prefix (used in bulk-import template sample row only)",
"description": "Sample code prefix shown in the bulk-import template sample row (e.g. PHRC-ICU-0001). Hand-typed asset codes are not restricted.",
"min": None,
"max": None,
"options": None,