haha yes it works now
This commit is contained in:
+46
-1
@@ -8,6 +8,7 @@ import aiosqlite
|
||||
|
||||
from server.config_loader import load_meds_config, today_str
|
||||
from server.db import new_id, utc_now_iso
|
||||
from server.slots import compute_status, get_log_for_day, get_snooze_for_day
|
||||
|
||||
|
||||
async def get_first_intake_day(db: aiosqlite.Connection, tz: ZoneInfo) -> str:
|
||||
@@ -88,11 +89,55 @@ async def day_compliance(db: aiosqlite.Connection, day: str, slot_ids: list[str]
|
||||
return int(row[0]) >= len(slot_ids) if row else False
|
||||
|
||||
|
||||
async def _today_streak_state(
|
||||
db: aiosqlite.Connection,
|
||||
tz: ZoneInfo,
|
||||
slot_ids: list[str],
|
||||
) -> str:
|
||||
"""Return 'complete', 'failed', or 'pending' for today."""
|
||||
config = load_meds_config()
|
||||
day = today_str(tz)
|
||||
logs = await get_log_for_day(db, day)
|
||||
slots_by_id = {s.id: s for s in config.slots}
|
||||
|
||||
for sid in slot_ids:
|
||||
entry = logs.get(sid)
|
||||
if entry and entry["status"] == "missed":
|
||||
return "failed"
|
||||
|
||||
now = datetime.now(tz)
|
||||
snoozes = await get_snooze_for_day(db, day)
|
||||
all_taken = True
|
||||
|
||||
for sid in slot_ids:
|
||||
entry = logs.get(sid)
|
||||
if entry and entry["status"] == "taken":
|
||||
continue
|
||||
all_taken = False
|
||||
slot = slots_by_id[sid]
|
||||
status = compute_status(slot, now, tz, entry, snoozes.get(sid))
|
||||
if status == "overdue":
|
||||
return "failed"
|
||||
|
||||
if all_taken:
|
||||
return "complete"
|
||||
return "pending"
|
||||
|
||||
|
||||
async def compute_streak(db: aiosqlite.Connection, tz: ZoneInfo) -> int:
|
||||
config = load_meds_config()
|
||||
slot_ids = [s.id for s in config.slots]
|
||||
if not slot_ids:
|
||||
return 0
|
||||
|
||||
today = datetime.now(tz).date()
|
||||
today_state = await _today_streak_state(db, tz, slot_ids)
|
||||
|
||||
if today_state == "failed":
|
||||
return 0
|
||||
|
||||
day = today if today_state == "complete" else today - timedelta(days=1)
|
||||
streak = 0
|
||||
day = datetime.now(tz).date()
|
||||
while True:
|
||||
day_str = day.isoformat()
|
||||
ok = await day_compliance(db, day_str, slot_ids)
|
||||
|
||||
Reference in New Issue
Block a user