feat(backend): monitoring service — snapshot, rules, alerts, status colour, clock persistence
CI / ruff · pytest (pull_request) Successful in 1m11s

- app/services/snapshot.py: PatientSnapshot from the clinical DB (field mapping, age, therapy day,
  recent C-SSRS risk, weapon access from dialog facts)
- app/services/monitoring.py: RuleMonitoring registered via register_monitoring; persists alert/crisis
  actions, crisis_acute audit, enhanced_monitoring flag, cached status_color, notifies the doctor (and
  the doctor on duty for crises) after commit; a running crisis is not restarted
- demo clock offset persisted in clock_state, restored on scheduler start
- tests: Andrey's 13 days through the service → G G G Y Y G G G G Y Y Y R

Refs #38

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-04 03:13:57 +03:00
co-authored by Claude Opus 5.5
parent 20973bef17
commit 4c0836a1be
5 changed files with 548 additions and 3 deletions
+13 -3
View File
@@ -1,14 +1,24 @@
from fastapi.testclient import TestClient
from app.db import session
from app.db.bootstrap import init_db
from app.dialog.contracts import Event, get_engine
from app.main import app
from app.services.contracts import get_monitoring
MEM = "sqlite+aiosqlite:///:memory:"
async def test_stubs_are_usable() -> None:
replies = await get_engine().handle("tok", Event(type="text", text="привет"))
assert replies and replies[0].text
assert await get_monitoring().after_data_change("tok") == []
await session.configure(identity_url=MEM, clinical_url=MEM)
await init_db()
try:
replies = await get_engine().handle("tok", Event(type="text", text="привет"))
assert replies and replies[0].text
assert await get_monitoring().after_data_change("tok") == [] # unknown patient → no actions
assert await get_monitoring().is_in_crisis("tok") is False
finally:
await session.configure()
def test_api_fixture_routes() -> None: