-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatientping.py
More file actions
461 lines (394 loc) · 14.8 KB
/
patientping.py
File metadata and controls
461 lines (394 loc) · 14.8 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import html
import os
import random
import threading
import time
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from typing import Optional
from urllib.parse import urlparse
from urllib.request import Request, urlopen
import boto3
import psycopg
from dotenv import load_dotenv
load_dotenv()
AWS_REGION = "us-east-1"
SSM_PARAM_DB = "/DATABASE_URL"
SSM_PARAM_CMO = "/CMO_NAME"
def _fetch_ssm_config() -> tuple[str, str]:
ssm = boto3.client("ssm", region_name=AWS_REGION)
response = ssm.get_parameters(
Names=[SSM_PARAM_DB, SSM_PARAM_CMO],
WithDecryption=True,
)
invalid = response.get("InvalidParameters", [])
if invalid:
raise RuntimeError(f"invalid parameters: {invalid}")
values_by_name = {
str(item.get("Name", "")): str(item.get("Value", "")).strip()
for item in response.get("Parameters", [])
}
db_url = values_by_name.get(SSM_PARAM_DB, "")
cmo_name = values_by_name.get(SSM_PARAM_CMO, "")
if not db_url or not cmo_name:
raise RuntimeError("missing required SSM values")
return db_url, cmo_name
def _load_runtime_config() -> tuple[str, str]:
try:
db_url, cmo_name = _fetch_ssm_config()
print(f"Loaded DATABASE_URL and CMO_NAME from SSM ({AWS_REGION})")
return db_url, cmo_name
except Exception as e:
print(f"SSM config unavailable; falling back to .env: {e!r}")
db_url = os.getenv("DATABASE_URL", "").strip()
cmo_name = os.getenv("CMO_NAME", "").strip()
return db_url, cmo_name
DB_URL, CMO_NAME = _load_runtime_config()
PORT = int(os.getenv("PORT", "8080"))
HOST = os.getenv("HOST", "0.0.0.0")
INSERT_INTERVAL_SEC = float(os.getenv("INSERT_INTERVAL_SEC", "5"))
MAX_ROWS = int(os.getenv("MAX_ROWS", "20"))
EGRESS_TEST_URL = os.getenv("EGRESS_TEST_URL", "http://example.com").strip()
EGRESS_TEST_INTERVAL_SEC = float(os.getenv("EGRESS_TEST_INTERVAL_SEC", "10"))
EGRESS_TEST_TIMEOUT_SEC = float(os.getenv("EGRESS_TEST_TIMEOUT_SEC", "3"))
PATIENT_NAMES = [
"Sleve McDichael",
"Willie Dustice",
"Onson Sweemey",
"Jeromy Gride",
"Darryl Archideld",
"Scott Dorque",
"Anatoli Smorin",
"Shown Furcotte",
"Rey McSriff",
"Dean Wesrey",
"Glenallen Mixon",
"Mike Truk",
"Mario McRlwain",
"Dwigt Rortugal",
"Raul Chamgerlain",
"Tim Sandaele",
"Kevin Nogilny",
"Karl Dandleton",
"Tony Smehrik",
"Mike Sernandez",
"Bobson Dugnutt",
"Todd Bonzalez",
]
UPCOMING_PROCEDURES = [
"MRI Scan (Spleen)",
"CT Scan w/ Red 40 Contrast Dye",
"Posterior Ultrasound",
"COVID-19 Vaccine (Incl. Govt. Tracking Nanochip)",
"Blood Panel Debate",
"Kidney Stress Test",
"Colonoscopy (Extra Thorough)",
"Psychosomatic Therapy Intake",
"Rib Cage Transplant",
"Post-Black-Op Checkup",
]
def _db_enabled() -> bool:
return bool(DB_URL)
def _randomized_reminder_message() -> str:
procedure = random.choice(UPCOMING_PROCEDURES)
patient_name = random.choice(PATIENT_NAMES)
return f"Reminder – Upcoming {procedure} for {patient_name}"
_REMINDER_PREFIX = "Reminder – Upcoming "
_REMINDER_SEP = " for "
def _render_reminder_html(text: str) -> str:
if not text.startswith(_REMINDER_PREFIX):
return html.escape(text)
rest = text[len(_REMINDER_PREFIX) :]
if _REMINDER_SEP not in rest:
return html.escape(text)
procedure, patient = rest.split(_REMINDER_SEP, 1)
return (
html.escape(_REMINDER_PREFIX)
+ "<strong>"
+ html.escape(procedure)
+ "</strong>"
+ html.escape(_REMINDER_SEP)
+ "<strong>"
+ html.escape(patient)
+ "</strong>"
)
class ReminderStore:
def __init__(self) -> None:
self._lock = threading.Lock()
self._schema_ready = False
self._connected = False
self._last_error: Optional[str] = None
def last_error(self) -> Optional[str]:
with self._lock:
return self._last_error
def schema_ready(self) -> bool:
with self._lock:
return self._schema_ready
def connected(self) -> bool:
with self._lock:
return self._connected
def _set_error(self, msg: Optional[str]) -> None:
with self._lock:
self._last_error = msg
def _set_schema_ready(self, ready: bool) -> None:
with self._lock:
self._schema_ready = ready
def _set_connected(self, connected: bool) -> None:
with self._lock:
self._connected = connected
def init(self) -> None:
if not _db_enabled():
return
try:
with psycopg.connect(DB_URL, autocommit=True) as conn:
with conn.cursor() as cur:
cur.execute(
"""
create table if not exists reminders (
id bigserial primary key,
created_at timestamptz not null default now(),
text text not null
);
"""
)
self._set_schema_ready(True)
self._set_connected(True)
self._set_error(None)
print("DB init ok: reminders table ready")
except Exception as e:
self._set_schema_ready(False)
self._set_connected(False)
err = f"DB init failed: {e!r}"
self._set_error(err)
print(err)
def insert_reminder(self, text: str) -> None:
if not _db_enabled():
return
if not self.schema_ready():
self.init()
if not self.schema_ready():
return
try:
with psycopg.connect(DB_URL, autocommit=True) as conn:
with conn.cursor() as cur:
cur.execute("insert into reminders (text) values (%s);", (text,))
self._set_connected(True)
self._set_error(None)
print(f"DB insert ok: {text}")
except Exception as e:
self._set_connected(False)
err = f"DB insert failed: {e!r}"
self._set_error(err)
print(err)
def fetch_latest(self, limit: int) -> list[tuple[str, str]]:
if not _db_enabled():
return []
if not self.schema_ready():
self.init()
if not self.schema_ready():
return []
try:
with psycopg.connect(DB_URL, autocommit=True) as conn:
with conn.cursor() as cur:
cur.execute(
"select created_at::text, text from reminders order by id desc limit %s;",
(limit,),
)
rows = cur.fetchall()
self._set_connected(True)
self._set_error(None)
return [(str(r[0]), str(r[1])) for r in rows]
except Exception as e:
self._set_connected(False)
self._set_error(f"DB fetch failed: {e!r}")
return []
class EgressProbe:
def __init__(self) -> None:
self._lock = threading.Lock()
self._ok: Optional[bool] = None
self._last_error: Optional[str] = None
self._last_checked_at: Optional[str] = None
def snapshot(self) -> tuple[Optional[bool], Optional[str], Optional[str]]:
with self._lock:
return self._ok, self._last_error, self._last_checked_at
def check(self) -> None:
req = Request(EGRESS_TEST_URL, method="HEAD")
now = time.strftime("%Y-%m-%d %H:%M:%S UTC", time.gmtime())
try:
with urlopen(req, timeout=EGRESS_TEST_TIMEOUT_SEC) as resp:
status_code = getattr(resp, "status", None) or resp.getcode()
ok = 200 <= int(status_code) < 400
err = None if ok else f"HTTP status {status_code}"
except Exception as e:
ok = False
err = repr(e)
with self._lock:
self._ok = ok
self._last_error = err
self._last_checked_at = now
store = ReminderStore()
egress = EgressProbe()
def reminder_loop() -> None:
# Insert immediately, then continue on interval
while True:
if _db_enabled():
msg = _randomized_reminder_message()
store.insert_reminder(msg)
time.sleep(INSERT_INTERVAL_SEC)
def egress_loop() -> None:
# Probe immediately, then continue on interval
while True:
egress.check()
time.sleep(EGRESS_TEST_INTERVAL_SEC)
PAGE_STYLE = """
:root { color-scheme: light; }
body { font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Arial; margin: 0; }
header { padding: 24px; border-bottom: 1px solid #e5e7eb; background: #fafafa; }
main { max-width: 860px; margin: 0 auto; padding: 24px; }
.card { border: 1px solid #e5e7eb; border-radius: 14px; padding: 18px; box-shadow: 0 1px 2px rgba(0,0,0,.04); }
h1 { margin: 0 0 10px 0; font-size: 22px; }
p.sub { margin: 0; color: #6b7280; font-size: 14px; }
.badge { display:inline-block; padding: 4px 10px; border-radius: 999px; font-size: 12px; border:1px solid #e5e7eb; background:#fff; }
.badge.ok { border-color:#bbf7d0; background:#f0fdf4; color:#166534; }
.badge.bad { border-color:#fecaca; background:#fef2f2; color:#991b1b; }
.badge.warn { border-color:#fde68a; background:#fffbeb; color:#92400e; }
.list { margin: 14px 0 6px; padding: 0; list-style: none; }
.list li { padding: 10px 12px; border: 1px solid #f3f4f6; border-radius: 10px; margin-bottom: 10px; background: #fff; }
.list li:last-child { margin-bottom: 0; }
.meta { color:#6b7280; font-size: 12px; }
code { background: #f3f4f6; padding: 2px 6px; border-radius: 8px; }
"""
def render_page(
rows: list[tuple[str, str]],
db_connected: bool,
db_error: Optional[str],
egress_ok: Optional[bool],
egress_error: Optional[str],
egress_checked_at: Optional[str],
) -> bytes:
safe_err = html.escape(db_error) if db_error else ""
if db_connected:
db_badge = '<span class="badge ok">DB connected</span>'
else:
db_badge = '<span class="badge bad">DB disconnected</span>'
safe_egress_error = html.escape(egress_error) if egress_error else ""
safe_egress_checked_at = (
html.escape(egress_checked_at) if egress_checked_at else "never"
)
if egress_ok is True:
egress_badge = '<span class="badge ok">Internet egress OK</span>'
elif egress_ok is False:
egress_badge = '<span class="badge bad">Internet egress failed</span>'
else:
egress_badge = '<span class="badge warn">Internet egress unknown</span>'
items = ""
if not db_connected:
items = "<li><div style='margin-bottom:4px;'><strong>Database disconnected</strong></div><div class='meta'>Make sure to set <code>DATABASE_URL</code> to enable reminders.</div></li>"
if safe_err:
items += f"<li><div style='margin-bottom:4px;'><strong>Last DB error:</strong></div><div class='meta'>{safe_err}</div></li>"
else:
if safe_err:
items += f"<li><div><strong>DB warning</strong></div><div class='meta'>{safe_err}</div></li>"
for created_at, text in rows:
items += (
"<li>"
f"<div>{_render_reminder_html(text)}</div>"
f"<div class='meta'>{html.escape(created_at)}</div>"
"</li>"
)
if not rows:
items += "<li><div><strong>No reminders yet.</strong></div><div class='meta'>Wait for the next insert interval and refresh.</div></li>"
html_doc = f"""<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>PatientPing</title>
<style>{PAGE_STYLE}</style>
</head>
<body>
<header>
<div style="padding:0;max-width:860px;margin:0 auto;">
<h1>PatientPing</h1>
<p class="sub">Because memory is not a medical device. Ping now, panic never.</p>
{f"<p class='sub' style='margin-top:4px;'>PatientPing Chief Medical Officer: {html.escape(CMO_NAME)}</p>" if CMO_NAME else ""}
</div>
</header>
<main>
<div class="card">
<div style="display:flex;justify-content:space-between;align-items:flex-start;gap:12px;flex-wrap:wrap;">
<div>
<div class="meta" style="margin-bottom:6px;">Status</div>
{db_badge}
{egress_badge}
</div>
<div class="meta">DB insert interval: <code>{INSERT_INTERVAL_SEC}</code>s • Showing latest <code>{MAX_ROWS}</code></div>
</div>
<div class="meta" style="margin-top:8px;">Egress target: <code>{html.escape(EGRESS_TEST_URL)}</code> • Last checked: {safe_egress_checked_at}</div>
{f"<div class='meta' style='margin-top:4px;'>Last egress error: {safe_egress_error}</div>" if safe_egress_error else ""}
<ul class="list">
{items}
</ul>
</div>
</main>
</body>
</html>
"""
return html_doc.encode("utf-8")
class Handler(BaseHTTPRequestHandler):
def do_GET(self) -> None:
path = urlparse(self.path).path
if path not in ("/", "/healthz"):
payload = b"not found"
self.send_response(404)
self.send_header("Content-Type", "text/plain; charset=utf-8")
self.send_header("Content-Length", str(len(payload)))
self.end_headers()
self.wfile.write(payload)
return
if path == "/healthz":
payload = b"ok"
self.send_response(200)
self.send_header("Content-Type", "text/plain; charset=utf-8")
self.send_header("Content-Length", str(len(payload)))
self.end_headers()
self.wfile.write(payload)
return
rows = store.fetch_latest(MAX_ROWS) if _db_enabled() else []
db_connected = _db_enabled() and store.connected()
egress_ok, egress_error, egress_checked_at = egress.snapshot()
payload = render_page(
rows,
db_connected,
store.last_error(),
egress_ok,
egress_error,
egress_checked_at,
)
self.send_response(200)
self.send_header("Content-Type", "text/html; charset=utf-8")
self.send_header("Content-Length", str(len(payload)))
self.end_headers()
self.wfile.write(payload)
def log_message(self, format: str, *args) -> None:
# quieter logs
return
def main() -> None:
if _db_enabled():
store.init()
egress.check()
t = threading.Thread(target=reminder_loop, daemon=True)
t.start()
t_egress = threading.Thread(target=egress_loop, daemon=True)
t_egress.start()
httpd = ThreadingHTTPServer((HOST, PORT), Handler)
print(
f"PatientPing listening on http://{HOST}:{PORT} (db={'on' if _db_enabled() else 'off'})"
)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
finally:
httpd.server_close()
if __name__ == "__main__":
main()