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,14 +29,14 @@ def calculate_energy_balance_7d(profile_id: str) -> Optional[float]:
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute("""
|
||||
SELECT calories
|
||||
SELECT kcal
|
||||
FROM nutrition_log
|
||||
WHERE profile_id = %s
|
||||
AND date >= CURRENT_DATE - INTERVAL '7 days'
|
||||
ORDER BY date DESC
|
||||
""", (profile_id,))
|
||||
|
||||
calories = [row['calories'] for row in cur.fetchall()]
|
||||
calories = [row['kcal'] for row in cur.fetchall()]
|
||||
|
||||
if len(calories) < 4: # Need at least 4 days
|
||||
return None
|
||||
@@ -46,7 +46,7 @@ def calculate_energy_balance_7d(profile_id: str) -> Optional[float]:
|
||||
# Get estimated TDEE (simplified - could use Harris-Benedict)
|
||||
# For now, use weight-based estimate
|
||||
cur.execute("""
|
||||
SELECT weight_kg
|
||||
SELECT weight
|
||||
FROM weight_log
|
||||
WHERE profile_id = %s
|
||||
ORDER BY date DESC
|
||||
@@ -59,7 +59,7 @@ def calculate_energy_balance_7d(profile_id: str) -> Optional[float]:
|
||||
|
||||
# Simple TDEE estimate: bodyweight (kg) × 30-35
|
||||
# TODO: Improve with activity level, age, gender
|
||||
estimated_tdee = weight_row['weight_kg'] * 32.5
|
||||
estimated_tdee = weight_row['weight'] * 32.5
|
||||
|
||||
balance = avg_intake - estimated_tdee
|
||||
|
||||
@@ -95,7 +95,7 @@ def calculate_protein_g_per_kg(profile_id: str) -> Optional[float]:
|
||||
|
||||
# Get recent weight
|
||||
cur.execute("""
|
||||
SELECT weight_kg
|
||||
SELECT weight
|
||||
FROM weight_log
|
||||
WHERE profile_id = %s
|
||||
ORDER BY date DESC
|
||||
@@ -106,7 +106,7 @@ def calculate_protein_g_per_kg(profile_id: str) -> Optional[float]:
|
||||
if not weight_row:
|
||||
return None
|
||||
|
||||
weight_kg = weight_row['weight_kg']
|
||||
weight = weight_row['weight']
|
||||
|
||||
# Get protein intake
|
||||
cur.execute("""
|
||||
@@ -124,7 +124,7 @@ def calculate_protein_g_per_kg(profile_id: str) -> Optional[float]:
|
||||
return None
|
||||
|
||||
avg_protein = sum(protein_values) / len(protein_values)
|
||||
protein_per_kg = avg_protein / weight_kg
|
||||
protein_per_kg = avg_protein / weight
|
||||
|
||||
return round(protein_per_kg, 2)
|
||||
|
||||
@@ -139,7 +139,7 @@ def calculate_protein_days_in_target(profile_id: str, target_low: float = 1.6, t
|
||||
|
||||
# Get recent weight
|
||||
cur.execute("""
|
||||
SELECT weight_kg
|
||||
SELECT weight
|
||||
FROM weight_log
|
||||
WHERE profile_id = %s
|
||||
ORDER BY date DESC
|
||||
@@ -150,7 +150,7 @@ def calculate_protein_days_in_target(profile_id: str, target_low: float = 1.6, t
|
||||
if not weight_row:
|
||||
return None
|
||||
|
||||
weight_kg = weight_row['weight_kg']
|
||||
weight = weight_row['weight']
|
||||
|
||||
# Get protein intake last 7 days
|
||||
cur.execute("""
|
||||
@@ -172,7 +172,7 @@ def calculate_protein_days_in_target(profile_id: str, target_low: float = 1.6, t
|
||||
total_days = len(protein_data)
|
||||
|
||||
for row in protein_data:
|
||||
protein_per_kg = row['protein_g'] / weight_kg
|
||||
protein_per_kg = row['protein_g'] / weight
|
||||
if target_low <= protein_per_kg <= target_high:
|
||||
days_in_target += 1
|
||||
|
||||
@@ -189,7 +189,7 @@ def calculate_protein_adequacy_28d(profile_id: str) -> Optional[int]:
|
||||
|
||||
# Get average weight (28d)
|
||||
cur.execute("""
|
||||
SELECT AVG(weight_kg) as avg_weight
|
||||
SELECT AVG(weight) as avg_weight
|
||||
FROM weight_log
|
||||
WHERE profile_id = %s
|
||||
AND date >= CURRENT_DATE - INTERVAL '28 days'
|
||||
@@ -199,7 +199,7 @@ def calculate_protein_adequacy_28d(profile_id: str) -> Optional[int]:
|
||||
if not weight_row or not weight_row['avg_weight']:
|
||||
return None
|
||||
|
||||
weight_kg = weight_row['avg_weight']
|
||||
weight = weight_row['avg_weight']
|
||||
|
||||
# Get protein intake (28d)
|
||||
cur.execute("""
|
||||
@@ -216,7 +216,7 @@ def calculate_protein_adequacy_28d(profile_id: str) -> Optional[int]:
|
||||
return None
|
||||
|
||||
# Calculate metrics
|
||||
protein_per_kg_values = [p / weight_kg for p in protein_values]
|
||||
protein_per_kg_values = [p / weight for p in protein_values]
|
||||
avg_protein_per_kg = sum(protein_per_kg_values) / len(protein_per_kg_values)
|
||||
|
||||
# Target range: 1.6-2.2 g/kg for active individuals
|
||||
@@ -258,11 +258,11 @@ def calculate_macro_consistency_score(profile_id: str) -> Optional[int]:
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute("""
|
||||
SELECT calories, protein_g, fat_g, carbs_g
|
||||
SELECT kcal, protein_g, fat_g, carbs_g
|
||||
FROM nutrition_log
|
||||
WHERE profile_id = %s
|
||||
AND date >= CURRENT_DATE - INTERVAL '28 days'
|
||||
AND calories IS NOT NULL
|
||||
AND kcal IS NOT NULL
|
||||
ORDER BY date DESC
|
||||
""", (profile_id,))
|
||||
|
||||
@@ -282,7 +282,7 @@ def calculate_macro_consistency_score(profile_id: str) -> Optional[int]:
|
||||
std_dev = statistics.stdev(values)
|
||||
return std_dev / mean
|
||||
|
||||
calories_cv = cv([d['calories'] for d in data])
|
||||
calories_cv = cv([d['kcal'] for d in data])
|
||||
protein_cv = cv([d['protein_g'] for d in data if d['protein_g']])
|
||||
fat_cv = cv([d['fat_g'] for d in data if d['fat_g']])
|
||||
carbs_cv = cv([d['carbs_g'] for d in data if d['carbs_g']])
|
||||
@@ -427,7 +427,7 @@ def _score_macro_balance(profile_id: str) -> Optional[int]:
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute("""
|
||||
SELECT protein_g, fat_g, carbs_g, calories
|
||||
SELECT protein_g, fat_g, carbs_g, kcal
|
||||
FROM nutrition_log
|
||||
WHERE profile_id = %s
|
||||
AND date >= CURRENT_DATE - INTERVAL '28 days'
|
||||
|
||||
Reference in new issue
Block a user