From 05d15264c82c01f6d09a54dcdddb6840eeb5973a Mon Sep 17 00:00:00 2001 From: Lars Date: Sat, 28 Mar 2026 11:32:07 +0100 Subject: [PATCH] fix: Phase 0b - complete Decimal/float conversion in nutrition_metrics Previous commit only converted weight values, but missed: - avg_intake (calories from DB) - avg_protein (protein_g from DB) - protein_per_kg calculations in loops All DB numeric values now converted to float BEFORE arithmetic. Fixed locations: - Line 44: avg_intake conversion - Line 126: avg_protein conversion - Line 175: protein_per_kg in loop - Line 213: protein_values list comprehension Co-Authored-By: Claude Opus 4.6 --- backend/calculations/nutrition_metrics.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/backend/calculations/nutrition_metrics.py b/backend/calculations/nutrition_metrics.py index 8ee66d0..b2bdd5e 100644 --- a/backend/calculations/nutrition_metrics.py +++ b/backend/calculations/nutrition_metrics.py @@ -41,7 +41,7 @@ def calculate_energy_balance_7d(profile_id: str) -> Optional[float]: if len(calories) < 4: # Need at least 4 days return None - avg_intake = sum(calories) / len(calories) + avg_intake = float(sum(calories) / len(calories)) # Get estimated TDEE (simplified - could use Harris-Benedict) # For now, use weight-based estimate @@ -123,7 +123,7 @@ def calculate_protein_g_per_kg(profile_id: str) -> Optional[float]: if len(protein_values) < 4: return None - avg_protein = sum(protein_values) / len(protein_values) + avg_protein = float(sum(protein_values) / len(protein_values)) protein_per_kg = avg_protein / weight return round(protein_per_kg, 2) @@ -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 + protein_per_kg = float(row['protein_g']) / weight if target_low <= protein_per_kg <= target_high: days_in_target += 1 @@ -210,7 +210,7 @@ def calculate_protein_adequacy_28d(profile_id: str) -> Optional[int]: AND protein_g IS NOT NULL """, (profile_id,)) - protein_values = [row['protein_g'] for row in cur.fetchall()] + protein_values = [float(row['protein_g']) for row in cur.fetchall()] if len(protein_values) < 18: # 60% coverage return None