fix: reopen clears restored_at, wire settings, docs, startup warning
This commit is contained in:
@@ -17,7 +17,7 @@ from ..models import (
|
||||
User,
|
||||
Warranty,
|
||||
)
|
||||
from .calculations import alert_state
|
||||
from .calculations import alert_state, compute_sla_status
|
||||
from .permissions import Role
|
||||
from .settings_service import get_setting_value
|
||||
|
||||
@@ -50,7 +50,13 @@ def _alert_settings(db: Session) -> dict[str, int]:
|
||||
get_setting_value(db, "contract_expiry_alert_days", 30),
|
||||
)
|
||||
),
|
||||
"calibration": int(get_setting_value(db, "calibration_lead_days", 15)),
|
||||
"calibration": int(
|
||||
get_setting_value(
|
||||
db,
|
||||
"calibration_due_window_days",
|
||||
get_setting_value(db, "calibration_lead_days", 15),
|
||||
)
|
||||
),
|
||||
"pm_in_house": int(get_setting_value(db, "pm_inhouse_lead_days", 15)),
|
||||
"pm_outsourced": int(get_setting_value(db, "pm_outsourced_lead_days", 15)),
|
||||
"insurance": int(
|
||||
@@ -67,6 +73,13 @@ def _alert_settings(db: Session) -> dict[str, int]:
|
||||
get_setting_value(db, "warranty_expiry_alert_days", 30),
|
||||
)
|
||||
),
|
||||
"contract_red": int(
|
||||
get_setting_value(
|
||||
db,
|
||||
"contract_critical_alert_days",
|
||||
get_setting_value(db, "red_threshold_days", 7),
|
||||
)
|
||||
),
|
||||
"red": int(get_setting_value(db, "red_threshold_days", 7)),
|
||||
"critical_hours": int(
|
||||
get_setting_value(
|
||||
@@ -75,6 +88,8 @@ def _alert_settings(db: Session) -> dict[str, int]:
|
||||
get_setting_value(db, "escalation_open_complaint_hours", 24),
|
||||
)
|
||||
),
|
||||
"critical_sla_hours": int(get_setting_value(db, "critical_complaint_sla_hours", 4)),
|
||||
"non_critical_sla_hours": int(get_setting_value(db, "non_critical_complaint_sla_hours", 24)),
|
||||
}
|
||||
|
||||
|
||||
@@ -130,7 +145,7 @@ def run_alert_check(db: Session) -> dict[str, object]:
|
||||
.all()
|
||||
)
|
||||
for c in contracts:
|
||||
state = alert_state(c.end_date, today, cfg["amc"], cfg["red"])
|
||||
state = alert_state(c.end_date, today, cfg["amc"], cfg["contract_red"])
|
||||
if state in ("yellow", "red"):
|
||||
created += _add_if_new(
|
||||
db,
|
||||
@@ -273,7 +288,16 @@ 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()
|
||||
if elapsed >= threshold_secs:
|
||||
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:
|
||||
created += _add_if_new(
|
||||
db,
|
||||
existing_keys,
|
||||
|
||||
@@ -12,6 +12,7 @@ from sqlalchemy.orm import Session
|
||||
from ..core.exceptions import CodedHTTPException
|
||||
from ..models import Asset, Category, Department, Location, Vendor
|
||||
from .audit import log_audit
|
||||
from .settings_service import get_setting_value
|
||||
|
||||
IMPORT_COLUMNS = [
|
||||
"asset_code",
|
||||
@@ -93,8 +94,10 @@ def build_import_template_xlsx(db: Session) -> BytesIO:
|
||||
sample_cat = categories[0] if categories else None
|
||||
sample_sup = vendors[0] if vendors else None
|
||||
|
||||
sample_prefix = str(get_setting_value(db, "asset_code_prefix", "PHRC")).strip() or "PHRC"
|
||||
sample_dept_code = sample_dept.code if sample_dept else "ICU"
|
||||
sample_row = [
|
||||
"PHRC-ICU-0901",
|
||||
f"{sample_prefix}-{sample_dept_code}-0901",
|
||||
"SN-DEMO-901",
|
||||
"Infusion Pump",
|
||||
sample_cat.name if sample_cat else "Infusion Pump",
|
||||
|
||||
@@ -124,6 +124,26 @@ def compute_ce_response_time_minutes(
|
||||
return max(0, int((ce_utc - reg_utc).total_seconds() // 60))
|
||||
|
||||
|
||||
def compute_sla_status(
|
||||
priority: str,
|
||||
registered_at: datetime,
|
||||
attended_at: datetime | None,
|
||||
restored_at: datetime | None,
|
||||
critical_sla_hours: int = 4,
|
||||
non_critical_sla_hours: int = 24,
|
||||
now_utc: datetime | None = None,
|
||||
) -> tuple[bool, int]:
|
||||
"""Return (is_sla_breached, target_sla_hours) using configured SLA thresholds."""
|
||||
target_hours = critical_sla_hours if priority == "critical" else non_critical_sla_hours
|
||||
reg_utc = _ensure_utc(registered_at)
|
||||
end_ref = restored_at or attended_at
|
||||
end_utc = _ensure_utc(end_ref) if end_ref is not None else (
|
||||
_ensure_utc(now_utc) if now_utc is not None else utc_now()
|
||||
)
|
||||
elapsed_seconds = max(0.0, (end_utc - reg_utc).total_seconds())
|
||||
return elapsed_seconds > (target_hours * 3600), target_hours
|
||||
|
||||
|
||||
def clip_downtime_to_month(
|
||||
start_utc: datetime,
|
||||
end_utc: datetime,
|
||||
|
||||
@@ -643,7 +643,13 @@ def _build_calibration_due(
|
||||
agency_id = params.get("agency_id")
|
||||
status_filter = params.get("status")
|
||||
|
||||
cal_lead = int(get_setting_value(db, "calibration_lead_days", 15))
|
||||
cal_lead = int(
|
||||
get_setting_value(
|
||||
db,
|
||||
"calibration_due_window_days",
|
||||
get_setting_value(db, "calibration_lead_days", 15),
|
||||
)
|
||||
)
|
||||
red_days = int(get_setting_value(db, "red_threshold_days", 7))
|
||||
|
||||
query = (
|
||||
|
||||
Reference in New Issue
Block a user