diff --git a/backend/data_layer/nutrition_metrics.py b/backend/data_layer/nutrition_metrics.py index 6a695a0..055c337 100644 --- a/backend/data_layer/nutrition_metrics.py +++ b/backend/data_layer/nutrition_metrics.py @@ -575,22 +575,23 @@ def calculate_protein_g_per_kg(profile_id: str) -> Optional[float]: weight = float(weight_row['weight']) - # Get protein intake + # Get protein intake aggregated by day (SUM per day) cur.execute(""" - SELECT protein_g + SELECT date, SUM(protein_g) as daily_protein FROM nutrition_log WHERE profile_id = %s AND date >= CURRENT_DATE - INTERVAL '7 days' AND protein_g IS NOT NULL + GROUP BY date ORDER BY date DESC """, (profile_id,)) - protein_values = [row['protein_g'] for row in cur.fetchall()] + daily_protein = [float(row['daily_protein']) for row in cur.fetchall()] - if len(protein_values) < 4: + if len(daily_protein) < 4: # At least 4 days with data return None - avg_protein = float(sum(protein_values) / len(protein_values)) + avg_protein = sum(daily_protein) / len(daily_protein) protein_per_kg = avg_protein / weight return round(protein_per_kg, 2) @@ -619,28 +620,33 @@ def calculate_protein_days_in_target(profile_id: str, target_low: float = 1.6, t weight = float(weight_row['weight']) - # Get protein intake last 7 days + # Calculate protein target range (absolute values) + target_low_g = target_low * weight + target_high_g = target_high * weight + + # Get protein intake aggregated by day (SUM per day) cur.execute(""" - SELECT protein_g, date + SELECT date, SUM(protein_g) as daily_protein FROM nutrition_log WHERE profile_id = %s AND date >= CURRENT_DATE - INTERVAL '7 days' AND protein_g IS NOT NULL + GROUP BY date ORDER BY date DESC """, (profile_id,)) - protein_data = cur.fetchall() + daily_data = cur.fetchall() - if len(protein_data) < 4: + if len(daily_data) < 4: # At least 4 days with data return None # Count days in target range days_in_target = 0 - total_days = len(protein_data) + total_days = len(daily_data) - for row in protein_data: - protein_per_kg = float(row['protein_g']) / weight - if target_low <= protein_per_kg <= target_high: + for row in daily_data: + daily_protein = float(row['daily_protein']) + if target_low_g <= daily_protein <= target_high_g: days_in_target += 1 return f"{days_in_target}/{total_days}"