49 lines
1.4 KiB
Python
49 lines
1.4 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):
|
|
return (
|
|
check_in.neck_pain_severity <= threshold
|
|
and check_in.headache_severity <= threshold
|
|
)
|
|
|
|
|
|
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)
|