fix: Phase 0b - fix last sleep column names in health_stability_score
All checks were successful
Deploy Development / deploy (push) Successful in 51s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 14s

Fixed remaining sleep_log column name errors in calculate_health_stability_score:
- SELECT: total_sleep_min, deep_min, rem_min → duration_minutes, deep_minutes, rem_minutes
- _score_sleep_quality: Updated dict access to use new column names

This was blocking goal_progress_score from calculating.

Changes:
- scores.py: Fixed sleep_log SELECT query and _score_sleep_quality dict access

This should be the LAST column name bug! All Phase 0b calculations should now work.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Lars 2026-03-28 09:35:36 +01:00
parent b230a03fdd
commit 10ea560fcf

View File

@ -218,7 +218,7 @@ def calculate_health_stability_score(profile_id: str) -> Optional[int]:
# 2. Sleep quality (25%)
cur.execute("""
SELECT total_sleep_min, deep_min, rem_min
SELECT duration_minutes, deep_minutes, rem_minutes
FROM sleep_log
WHERE profile_id = %s
AND date >= CURRENT_DATE - INTERVAL '28 days'
@ -317,7 +317,7 @@ def _score_blood_pressure(readings: List) -> int:
def _score_sleep_quality(sleep_data: List) -> int:
"""Score sleep quality (0-100)"""
# Average sleep duration and quality
avg_total = sum(s['total_sleep_min'] for s in sleep_data) / len(sleep_data)
avg_total = sum(s['duration_minutes'] for s in sleep_data) / len(sleep_data)
avg_total_hours = avg_total / 60
# Duration score (7+ hours = good)
@ -333,8 +333,8 @@ def _score_sleep_quality(sleep_data: List) -> int:
# Quality score (deep + REM percentage)
quality_scores = []
for s in sleep_data:
if s['deep_min'] and s['rem_min']:
quality_pct = ((s['deep_min'] + s['rem_min']) / s['total_sleep_min']) * 100
if s['deep_minutes'] and s['rem_minutes']:
quality_pct = ((s['deep_minutes'] + s['rem_minutes']) / s['duration_minutes']) * 100
# 40-60% deep+REM is good
if quality_pct >= 45:
quality_scores.append(100)