fix: Phase 0b - fix remaining calculation errors
Deploy Development / deploy (push) Successful in 47s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 13s

Fixes applied:
1. WHERE clause column names (total_sleep_min → duration_minutes, resting_heart_rate → resting_hr)
2. COUNT() column names (avg_heart_rate → hr_avg, quality_label → rpe)
3. Type errors (Decimal * float) - convert to float before multiplication
4. rest_days table (type column removed in migration 010, now uses rest_config JSONB)
5. c_thigh_l → c_thigh (no separate left/right columns)
6. focus_area_definitions queries (focus_area_id → key, label_de → name_de)

Missing functions implemented:
- goal_utils.get_active_goals() - queries goals table for active goals
- goal_utils.get_goal_by_id() - gets single goal
- calculations.scores.calculate_category_progress() - maps categories to score functions

Changes:
- activity_metrics.py: Fixed Decimal/float type errors, rest_config JSONB, data quality query
- recovery_metrics.py: Fixed all WHERE clause column names
- body_metrics.py: Fixed c_thigh column reference
- scores.py: Fixed focus_area queries, added calculate_category_progress()
- goal_utils.py: Added get_active_goals(), get_goal_by_id()

All calculation functions should now work with correct schema.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Opus 4.6 committed 2026-03-28 08:39:31 +01:00
1 parent 4817fd2b29
commit dd3a4111fc
6 files changed
+112 -36

No files matched your search

+9 -9
View File
@@ -149,7 +149,7 @@ def _score_rhr_vs_baseline(profile_id: str) -> Optional[int]:
SELECT AVG(resting_hr) as recent_rhr
FROM vitals_baseline
WHERE profile_id = %s
AND resting_heart_rate IS NOT NULL
AND resting_hr IS NOT NULL
AND date >= CURRENT_DATE - INTERVAL '3 days'
""", (profile_id,))
@@ -164,7 +164,7 @@ def _score_rhr_vs_baseline(profile_id: str) -> Optional[int]:
SELECT AVG(resting_heart_rate) as baseline_rhr
FROM vitals_baseline
WHERE profile_id = %s
AND resting_heart_rate IS NOT NULL
AND resting_hr IS NOT NULL
AND date >= CURRENT_DATE - INTERVAL '28 days'
AND date < CURRENT_DATE - INTERVAL '3 days'
""", (profile_id,))
@@ -339,7 +339,7 @@ def calculate_rhr_vs_baseline_pct(profile_id: str) -> Optional[float]:
SELECT AVG(resting_hr) as recent_rhr
FROM vitals_baseline
WHERE profile_id = %s
AND resting_heart_rate IS NOT NULL
AND resting_hr IS NOT NULL
AND date >= CURRENT_DATE - INTERVAL '3 days'
""", (profile_id,))
@@ -354,7 +354,7 @@ def calculate_rhr_vs_baseline_pct(profile_id: str) -> Optional[float]:
SELECT AVG(resting_heart_rate) as baseline_rhr
FROM vitals_baseline
WHERE profile_id = %s
AND resting_heart_rate IS NOT NULL
AND resting_hr IS NOT NULL
AND date >= CURRENT_DATE - INTERVAL '28 days'
AND date < CURRENT_DATE - INTERVAL '3 days'
""", (profile_id,))
@@ -378,7 +378,7 @@ def calculate_sleep_avg_duration_7d(profile_id: str) -> Optional[float]:
FROM sleep_log
WHERE profile_id = %s
AND date >= CURRENT_DATE - INTERVAL '7 days'
AND total_sleep_min IS NOT NULL
AND duration_minutes IS NOT NULL
""", (profile_id,))
row = cur.fetchone()
@@ -403,7 +403,7 @@ def calculate_sleep_debt_hours(profile_id: str) -> Optional[float]:
FROM sleep_log
WHERE profile_id = %s
AND date >= CURRENT_DATE - INTERVAL '14 days'
AND total_sleep_min IS NOT NULL
AND duration_minutes IS NOT NULL
ORDER BY date DESC
""", (profile_id,))
@@ -473,7 +473,7 @@ def calculate_recent_load_balance_3d(profile_id: str) -> Optional[int]:
with get_db() as conn:
cur = get_cursor(conn)
cur.execute("""
SELECT SUM(duration) as total_duration
SELECT SUM(duration_min) as total_duration
FROM activity_log
WHERE profile_id = %s
AND date >= CURRENT_DATE - INTERVAL '3 days'
@@ -499,7 +499,7 @@ def calculate_sleep_quality_7d(profile_id: str) -> Optional[int]:
FROM sleep_log
WHERE profile_id = %s
AND date >= CURRENT_DATE - INTERVAL '7 days'
AND total_sleep_min IS NOT NULL
AND duration_minutes IS NOT NULL
""", (profile_id,))
sleep_data = cur.fetchall()
@@ -555,7 +555,7 @@ def calculate_recovery_data_quality(profile_id: str) -> Dict[str, any]:
SELECT COUNT(*) as rhr_count
FROM vitals_baseline
WHERE profile_id = %s
AND resting_heart_rate IS NOT NULL
AND resting_hr IS NOT NULL
AND date >= CURRENT_DATE - INTERVAL '28 days'
""", (profile_id,))
rhr_count = cur.fetchone()['rhr_count']