diff --git a/backend/calculations/body_metrics.py b/backend/calculations/body_metrics.py index 5bb6d39..1558d1d 100644 --- a/backend/calculations/body_metrics.py +++ b/backend/calculations/body_metrics.py @@ -224,14 +224,13 @@ def calculate_arm_28d_delta(profile_id: str) -> Optional[float]: def calculate_thigh_28d_delta(profile_id: str) -> Optional[float]: - """Calculate 28-day thigh circumference change (cm, average of L/R)""" - left = _calculate_circumference_delta(profile_id, 'c_thigh', 28) - right = _calculate_circumference_delta(profile_id, 'c_thigh_r', 28) + """Calculate 28-day thigh circumference change (cm)""" + delta = _calculate_circumference_delta(profile_id, 'c_thigh', 28) - if left is None or right is None: + if delta is None: return None - return round((left + right) / 2, 1) + return round(delta, 1) def _calculate_circumference_delta(profile_id: str, column: str, days: int) -> Optional[float]: diff --git a/backend/calculations/recovery_metrics.py b/backend/calculations/recovery_metrics.py index 821c6bc..529a824 100644 --- a/backend/calculations/recovery_metrics.py +++ b/backend/calculations/recovery_metrics.py @@ -161,7 +161,7 @@ def _score_rhr_vs_baseline(profile_id: str) -> Optional[int]: # Get baseline (28d average, excluding last 3 days) cur.execute(""" - SELECT AVG(resting_heart_rate) as baseline_rhr + SELECT AVG(resting_hr) as baseline_rhr FROM vitals_baseline WHERE profile_id = %s AND resting_hr IS NOT NULL @@ -351,7 +351,7 @@ def calculate_rhr_vs_baseline_pct(profile_id: str) -> Optional[float]: # Baseline cur.execute(""" - SELECT AVG(resting_heart_rate) as baseline_rhr + SELECT AVG(resting_hr) as baseline_rhr FROM vitals_baseline WHERE profile_id = %s AND resting_hr IS NOT NULL @@ -407,7 +407,7 @@ def calculate_sleep_debt_hours(profile_id: str) -> Optional[float]: ORDER BY date DESC """, (profile_id,)) - sleep_data = [row['total_sleep_min'] for row in cur.fetchall()] + sleep_data = [row['duration_minutes'] for row in cur.fetchall()] if len(sleep_data) < 10: # Need at least 10 days return None diff --git a/backend/calculations/scores.py b/backend/calculations/scores.py index 3cf146f..f75e882 100644 --- a/backend/calculations/scores.py +++ b/backend/calculations/scores.py @@ -404,7 +404,7 @@ def get_top_priority_goal(profile_id: str) -> Optional[Dict]: for goal in goals: # Progress gap (0-100, higher = further from target) - goal['progress_gap'] = 100 - goal.get('progress_pct', 0) + goal['progress_gap'] = 100 - (goal.get('progress_pct') or 0) # Get focus areas for this goal with get_db() as conn: @@ -480,7 +480,7 @@ def calculate_focus_area_progress(profile_id: str, focus_area_id: str) -> Option JOIN goal_focus_contributions gfc ON g.id = gfc.goal_id WHERE g.profile_id = %s AND gfc.focus_area_id = ( - SELECT id FROM focus_area_definitions WHERE focus_area_id = %s + SELECT id FROM focus_area_definitions WHERE key = %s ) AND g.status = 'active' """, (profile_id, focus_area_id)) @@ -533,6 +533,7 @@ def calculate_category_progress(profile_id: str, category: str) -> Optional[int] from calculations.activity_metrics import calculate_activity_score return calculate_activity_score(profile_id) elif score_func_name == 'recovery_score': + from calculations.recovery_metrics import calculate_recovery_score_v2 return calculate_recovery_score_v2(profile_id) elif score_func_name == 'data_quality_score': return calculate_data_quality_score(profile_id)