52 lines
1.7 KiB
Python
52 lines
1.7 KiB
Python
"""Display and streak helpers for private daily symptom check-ins."""
|
|
|
|
from dataclasses import dataclass
|
|
from datetime import timedelta
|
|
|
|
|
|
# Keeping this policy value here makes it straightforward to tune later.
|
|
LOW_PAIN_STREAK_THRESHOLD = 2
|
|
ANNUAL_PHYSIOTHERAPY_SESSION_ALLOWANCE = 12
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class CheckInStreaks:
|
|
current: int
|
|
longest: int
|
|
|
|
|
|
def is_low_pain(check_in, threshold=LOW_PAIN_STREAK_THRESHOLD):
|
|
"""A confirmed low-pain day needs both scores in every time period."""
|
|
for period, _, _ in check_in.PERIODS:
|
|
neck = getattr(check_in, f"{period}_neck_pain_severity")
|
|
headache = getattr(check_in, f"{period}_headache_severity")
|
|
if neck is None or headache is None or neck > threshold or headache > threshold:
|
|
return False
|
|
return True
|
|
|
|
|
|
def calculate_check_in_streaks(check_ins, as_of_date, threshold=LOW_PAIN_STREAK_THRESHOLD):
|
|
"""Return consecutive confirmed low-pain runs; absent dates always break a run."""
|
|
by_date = {check_in.check_in_date: check_in for check_in in check_ins}
|
|
if not by_date:
|
|
return CheckInStreaks(current=0, longest=0)
|
|
|
|
first_date = min(by_date)
|
|
longest = run = 0
|
|
day = first_date
|
|
while day <= as_of_date:
|
|
check_in = by_date.get(day)
|
|
if check_in is not None and is_low_pain(check_in, threshold):
|
|
run += 1
|
|
longest = max(longest, run)
|
|
else:
|
|
run = 0
|
|
day += timedelta(days=1)
|
|
|
|
current = 0
|
|
day = as_of_date
|
|
while (check_in := by_date.get(day)) is not None and is_low_pain(check_in, threshold):
|
|
current += 1
|
|
day -= timedelta(days=1)
|
|
return CheckInStreaks(current=current, longest=longest)
|