fix: Phase 0b - correct all SQL column names in calculation engine
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:
1 parent
53969f8768
commit
4817fd2b29
6 files changed
+61
-52
No files matched your search
@@ -29,7 +29,7 @@ def calculate_training_minutes_week(profile_id: str) -> Optional[int]:
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute("""
|
||||
SELECT SUM(duration) as total_minutes
|
||||
SELECT SUM(duration_min) as total_minutes
|
||||
FROM activity_log
|
||||
WHERE profile_id = %s
|
||||
AND date >= CURRENT_DATE - INTERVAL '7 days'
|
||||
@@ -87,7 +87,7 @@ def calculate_intensity_proxy_distribution(profile_id: str) -> Optional[Dict]:
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute("""
|
||||
SELECT duration, avg_heart_rate, max_heart_rate
|
||||
SELECT duration_min, hr_avg, hr_max
|
||||
FROM activity_log
|
||||
WHERE profile_id = %s
|
||||
AND date >= CURRENT_DATE - INTERVAL '28 days'
|
||||
@@ -103,9 +103,9 @@ def calculate_intensity_proxy_distribution(profile_id: str) -> Optional[Dict]:
|
||||
high_min = 0
|
||||
|
||||
for activity in activities:
|
||||
duration = activity['duration']
|
||||
avg_hr = activity['avg_heart_rate']
|
||||
max_hr = activity['max_heart_rate']
|
||||
duration = activity['duration_min']
|
||||
avg_hr = activity['hr_avg']
|
||||
max_hr = activity['hr_max']
|
||||
|
||||
# Simple proxy classification
|
||||
if avg_hr:
|
||||
@@ -139,7 +139,7 @@ def calculate_ability_balance(profile_id: str) -> Optional[Dict]:
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute("""
|
||||
SELECT a.duration, tt.abilities
|
||||
SELECT a.duration_min, tt.abilities
|
||||
FROM activity_log a
|
||||
JOIN training_types tt ON a.training_category = tt.category
|
||||
WHERE a.profile_id = %s
|
||||
@@ -162,7 +162,7 @@ def calculate_ability_balance(profile_id: str) -> Optional[Dict]:
|
||||
}
|
||||
|
||||
for activity in activities:
|
||||
duration = activity['duration']
|
||||
duration = activity['duration_min']
|
||||
abilities = activity['abilities'] # JSONB
|
||||
|
||||
if not abilities:
|
||||
@@ -237,7 +237,7 @@ def calculate_proxy_internal_load_7d(profile_id: str) -> Optional[int]:
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute("""
|
||||
SELECT duration, avg_heart_rate, quality_label
|
||||
SELECT duration_min, hr_avg, rpe
|
||||
FROM activity_log
|
||||
WHERE profile_id = %s
|
||||
AND date >= CURRENT_DATE - INTERVAL '7 days'
|
||||
@@ -251,9 +251,18 @@ def calculate_proxy_internal_load_7d(profile_id: str) -> Optional[int]:
|
||||
total_load = 0
|
||||
|
||||
for activity in activities:
|
||||
duration = activity['duration']
|
||||
avg_hr = activity['avg_heart_rate']
|
||||
quality = activity['quality_label'] or 'good'
|
||||
duration = activity['duration_min']
|
||||
avg_hr = activity['hr_avg']
|
||||
# Map RPE to quality (rpe 8-10 = excellent, 6-7 = good, 4-5 = moderate, <4 = poor)
|
||||
rpe = activity.get('rpe')
|
||||
if rpe and rpe >= 8:
|
||||
quality = 'excellent'
|
||||
elif rpe and rpe >= 6:
|
||||
quality = 'good'
|
||||
elif rpe and rpe >= 4:
|
||||
quality = 'moderate'
|
||||
else:
|
||||
quality = 'good' # default
|
||||
|
||||
# Determine intensity
|
||||
if avg_hr:
|
||||
@@ -281,7 +290,7 @@ def calculate_monotony_score(profile_id: str) -> Optional[float]:
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute("""
|
||||
SELECT date, SUM(duration) as daily_duration
|
||||
SELECT date, SUM(duration_min) as daily_duration
|
||||
FROM activity_log
|
||||
WHERE profile_id = %s
|
||||
AND date >= CURRENT_DATE - INTERVAL '7 days'
|
||||
@@ -432,7 +441,7 @@ def _score_cardio_presence(profile_id: str) -> Optional[int]:
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute("""
|
||||
SELECT COUNT(DISTINCT date) as cardio_days, SUM(duration) as cardio_minutes
|
||||
SELECT COUNT(DISTINCT date) as cardio_days, SUM(duration_min) as cardio_minutes
|
||||
FROM activity_log
|
||||
WHERE profile_id = %s
|
||||
AND date >= CURRENT_DATE - INTERVAL '7 days'
|
||||
@@ -486,7 +495,7 @@ def calculate_rest_day_compliance(profile_id: str) -> Optional[int]:
|
||||
|
||||
# Get planned rest days
|
||||
cur.execute("""
|
||||
SELECT date, rest_type
|
||||
SELECT date, type as rest_type
|
||||
FROM rest_days
|
||||
WHERE profile_id = %s
|
||||
AND date >= CURRENT_DATE - INTERVAL '28 days'
|
||||
|
||||
Reference in new issue
Block a user