fix: Phase 0b - correct all SQL column names in calculation engine
Deploy Development / deploy (push) Successful in 50s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 14s

Schema corrections applied:
- weight_log: weight_kg → weight
- nutrition_log: calories → kcal
- activity_log: duration → duration_min, avg_heart_rate → hr_avg, max_heart_rate → hr_max
- rest_days: rest_type → type (aliased for backward compat)
- vitals_baseline: resting_heart_rate → resting_hr
- sleep_log: total_sleep_min → duration_minutes, deep_min → deep_minutes, rem_min → rem_minutes, waketime → wake_time
- focus_area_definitions: fa.focus_area_id → fa.key (proper join column)

Affected files:
- body_metrics.py: weight column (all queries)
- nutrition_metrics.py: kcal column + weight
- activity_metrics.py: duration_min, hr_avg, hr_max, quality via RPE mapping
- recovery_metrics.py: sleep + vitals columns
- correlation_metrics.py: kcal, weight
- scores.py: focus_area key selection

All 100+ Phase 0b placeholders should now calculate correctly.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Opus 4.6 committed 2026-03-28 08:28:20 +01:00
1 parent 53969f8768
commit 4817fd2b29
6 files changed
+61 -52

No files matched your search

+9 -9
View File
@@ -146,7 +146,7 @@ def _score_rhr_vs_baseline(profile_id: str) -> Optional[int]:
# Get recent RHR (last 3 days average)
cur.execute("""
SELECT AVG(resting_heart_rate) as recent_rhr
SELECT AVG(resting_hr) as recent_rhr
FROM vitals_baseline
WHERE profile_id = %s
AND resting_heart_rate IS NOT NULL
@@ -336,7 +336,7 @@ def calculate_rhr_vs_baseline_pct(profile_id: str) -> Optional[float]:
# Recent RHR (3d avg)
cur.execute("""
SELECT AVG(resting_heart_rate) as recent_rhr
SELECT AVG(resting_hr) as recent_rhr
FROM vitals_baseline
WHERE profile_id = %s
AND resting_heart_rate IS NOT NULL
@@ -374,7 +374,7 @@ def calculate_sleep_avg_duration_7d(profile_id: str) -> Optional[float]:
with get_db() as conn:
cur = get_cursor(conn)
cur.execute("""
SELECT AVG(total_sleep_min) as avg_sleep_min
SELECT AVG(duration_minutes) as avg_sleep_min
FROM sleep_log
WHERE profile_id = %s
AND date >= CURRENT_DATE - INTERVAL '7 days'
@@ -399,7 +399,7 @@ def calculate_sleep_debt_hours(profile_id: str) -> Optional[float]:
with get_db() as conn:
cur = get_cursor(conn)
cur.execute("""
SELECT total_sleep_min
SELECT duration_minutes
FROM sleep_log
WHERE profile_id = %s
AND date >= CURRENT_DATE - INTERVAL '14 days'
@@ -427,12 +427,12 @@ def calculate_sleep_regularity_proxy(profile_id: str) -> Optional[float]:
with get_db() as conn:
cur = get_cursor(conn)
cur.execute("""
SELECT bedtime, waketime, date
SELECT bedtime, wake_time, date
FROM sleep_log
WHERE profile_id = %s
AND date >= CURRENT_DATE - INTERVAL '14 days'
AND bedtime IS NOT NULL
AND waketime IS NOT NULL
AND wake_time IS NOT NULL
ORDER BY date
""", (profile_id,))
@@ -495,7 +495,7 @@ def calculate_sleep_quality_7d(profile_id: str) -> Optional[int]:
with get_db() as conn:
cur = get_cursor(conn)
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 '7 days'
@@ -509,8 +509,8 @@ def calculate_sleep_quality_7d(profile_id: str) -> Optional[int]:
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)