-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging_events.py
More file actions
284 lines (248 loc) · 8.42 KB
/
Copy pathlogging_events.py
File metadata and controls
284 lines (248 loc) · 8.42 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"""
logging_events.py — Immutable audit log for all medication safety events.
Design rules:
- Every action writes a row. No silent failures.
- Confirmed events can ONLY be corrected via CAREGIVER_CORRECTION (never deleted).
- Raw transcript always stored for post-incident review.
- All timestamps in UTC; timezone conversion is caller's responsibility.
"""
from __future__ import annotations
import json
import sqlite3
from datetime import datetime, timezone
from typing import Optional
import os as _os
DB_PATH_DEFAULT = _os.getenv("DB_PATH", "med_safety.db")
# ─── Valid event states (mirrors schema.sql comment) ─────────────────────────
VALID_STATES = {
"MED_CONFIRMED",
"MED_UNCERTAIN",
"MED_SKIPPED",
"MED_DUPLICATE_BLOCKED",
"MED_AMBIGUOUS",
"MED_LOW_CONFIDENCE",
"DRUG_INTERACTION_ALERT",
"CAREGIVER_ESCALATION",
"EMERGENCY_ESCALATION",
"CAREGIVER_CORRECTION",
}
# ─── Core log writer ─────────────────────────────────────────────────────────
def log_event(
state_status: str,
raw_transcript: str,
medication_id: Optional[int] = None,
confidence_score: Optional[float] = None,
input_mode: str = "text",
resolved_by: Optional[str] = None,
notes: Optional[str] = None,
dose_window_date: Optional[str] = None, # defaults to today UTC
db_path: str = DB_PATH_DEFAULT,
) -> int:
"""
Write one audit row. Returns the new row ID.
Raises ValueError for unknown state_status — prevents silent typos.
"""
if state_status not in VALID_STATES:
raise ValueError(
f"Unknown state_status {state_status!r}. Valid: {sorted(VALID_STATES)}"
)
if dose_window_date is None:
dose_window_date = datetime.now(timezone.utc).strftime("%Y-%m-%d")
con = sqlite3.connect(db_path)
cur = con.cursor()
cur.execute(
"""
INSERT INTO ingestion_logs
(medication_id, dose_window_date, input_mode, state_status,
raw_transcript, confidence_score, resolved_by, notes)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""",
(
medication_id,
dose_window_date,
input_mode,
state_status,
raw_transcript,
confidence_score,
resolved_by,
notes,
),
)
row_id = cur.lastrowid
con.commit()
con.close()
return row_id
# ─── Convenience wrappers ─────────────────────────────────────────────────────
def log_confirmed(
medication_id: int,
raw_transcript: str,
confidence_score: Optional[float] = None,
input_mode: str = "text",
db_path: str = DB_PATH_DEFAULT,
) -> int:
return log_event(
state_status="MED_CONFIRMED",
raw_transcript=raw_transcript,
medication_id=medication_id,
confidence_score=confidence_score,
input_mode=input_mode,
resolved_by="user",
db_path=db_path,
)
def log_uncertain(
raw_transcript: str,
medication_id: Optional[int] = None,
confidence_score: Optional[float] = None,
notes: Optional[str] = None,
db_path: str = DB_PATH_DEFAULT,
) -> int:
"""
User unsure if dose was taken. Never treat uncertain as confirmed.
medication_id may be None if identity also unknown.
"""
return log_event(
state_status="MED_UNCERTAIN",
raw_transcript=raw_transcript,
medication_id=medication_id,
confidence_score=confidence_score,
notes=notes,
db_path=db_path,
)
def log_duplicate_blocked(
medication_id: int,
raw_transcript: str,
existing_log_id: int,
db_path: str = DB_PATH_DEFAULT,
) -> int:
return log_event(
state_status="MED_DUPLICATE_BLOCKED",
raw_transcript=raw_transcript,
medication_id=medication_id,
notes=f"blocked; prior log id={existing_log_id}",
db_path=db_path,
)
def log_ambiguous(
raw_transcript: str,
candidate_ids: list[int],
confidence_score: Optional[float] = None,
db_path: str = DB_PATH_DEFAULT,
) -> int:
return log_event(
state_status="MED_AMBIGUOUS",
raw_transcript=raw_transcript,
confidence_score=confidence_score,
notes=f"candidates={json.dumps(candidate_ids)}",
db_path=db_path,
)
def log_low_confidence(
raw_transcript: str,
confidence_score: float,
db_path: str = DB_PATH_DEFAULT,
) -> int:
return log_event(
state_status="MED_LOW_CONFIDENCE",
raw_transcript=raw_transcript,
confidence_score=confidence_score,
notes=f"score {confidence_score:.2f} below threshold",
db_path=db_path,
)
def log_escalation(
raw_transcript: str,
reason: str,
medication_id: Optional[int] = None,
emergency: bool = False,
db_path: str = DB_PATH_DEFAULT,
) -> int:
status = "EMERGENCY_ESCALATION" if emergency else "DRUG_INTERACTION_ALERT"
return log_event(
state_status=status,
raw_transcript=raw_transcript,
medication_id=medication_id,
notes=reason,
db_path=db_path,
)
def log_caregiver_correction(
original_log_id: int,
medication_id: int,
raw_transcript: str,
correction_note: str,
db_path: str = DB_PATH_DEFAULT,
) -> int:
"""
Caregiver corrects existing log. Original row is NEVER deleted.
New correction row references the original via notes.
"""
return log_event(
state_status="CAREGIVER_CORRECTION",
raw_transcript=raw_transcript,
medication_id=medication_id,
resolved_by="caregiver",
notes=f"corrects log id={original_log_id}. {correction_note}",
db_path=db_path,
)
# ─── Query helpers ────────────────────────────────────────────────────────────
def get_confirmed_today(
medication_id: int,
dose_window_date: Optional[str] = None,
db_path: str = DB_PATH_DEFAULT,
) -> Optional[dict]:
"""
Return the first confirmed log for this med today, or None.
Used by duplicate_guard.py.
"""
if dose_window_date is None:
dose_window_date = datetime.now(timezone.utc).strftime("%Y-%m-%d")
con = sqlite3.connect(db_path)
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute(
"""
SELECT * FROM ingestion_logs
WHERE medication_id = ?
AND dose_window_date = ?
AND state_status = 'MED_CONFIRMED'
ORDER BY logged_at ASC
LIMIT 1
""",
(medication_id, dose_window_date),
)
row = cur.fetchone()
con.close()
return dict(row) if row else None
def get_recent_logs(
limit: int = 20,
db_path: str = DB_PATH_DEFAULT,
) -> list[dict]:
"""Return most recent log rows for CLI review / caregiver dashboard."""
con = sqlite3.connect(db_path)
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute(
"""
SELECT l.*, m.clinical_name, m.nickname
FROM ingestion_logs l
LEFT JOIN medications m ON l.medication_id = m.id
ORDER BY l.logged_at DESC
LIMIT ?
""",
(limit,),
)
rows = [dict(r) for r in cur.fetchall()]
con.close()
return rows
# ─── Quick smoke test ─────────────────────────────────────────────────────────
if __name__ == "__main__":
row_id = log_confirmed(
medication_id=1,
raw_transcript="I took my heart pill",
confidence_score=0.95,
)
print(f"Logged MED_CONFIRMED → row id {row_id}")
row_id2 = log_uncertain(
raw_transcript="I think I took it, not sure",
notes="user self-reported uncertainty",
)
print(f"Logged MED_UNCERTAIN → row id {row_id2}")
recent = get_recent_logs(limit=5)
for r in recent:
print(r)