feat: Phase 0c - nutrition_metrics.py module complete
Deploy Development / deploy (push) Successful in 45s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 14s

Data Layer:
- get_nutrition_average_data() - all macros in one call
- get_nutrition_days_data() - coverage tracking
- get_protein_targets_data() - 1.6g/kg and 2.2g/kg targets
- get_energy_balance_data() - deficit/surplus/maintenance
- get_protein_adequacy_data() - 0-100 score
- get_macro_consistency_data() - 0-100 score

Placeholder Layer:
- get_nutrition_avg() - refactored to use data layer
- get_nutrition_days() - refactored to use data layer
- get_protein_ziel_low() - refactored to use data layer
- get_protein_ziel_high() - refactored to use data layer

All 6 nutrition data functions + 4 placeholder refactors complete.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Opus 4.6 committed 2026-03-28 18:45:24 +01:00
1 parent c79cc9eafb
commit e1d7670971
3 files changed
+548 -57

No files matched your search

+57 -56
View File
@@ -18,6 +18,11 @@ from data_layer.body_metrics import (
get_body_composition_data,
get_circumference_summary_data
)
from data_layer.nutrition_metrics import (
get_nutrition_average_data,
get_nutrition_days_data,
get_protein_targets_data
)
# ── Helper Functions ──────────────────────────────────────────────────────────
@@ -81,33 +86,32 @@ def get_latest_bf(profile_id: str) -> Optional[str]:
def get_nutrition_avg(profile_id: str, field: str, days: int = 30) -> str:
"""Calculate average nutrition value."""
with get_db() as conn:
cur = get_cursor(conn)
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
"""
Calculate average nutrition value.
# Map field names to actual column names
field_map = {
'protein': 'protein_g',
'fat': 'fat_g',
'carb': 'carbs_g',
'kcal': 'kcal'
}
db_field = field_map.get(field, field)
Phase 0c: Refactored to use data_layer.nutrition_metrics.get_nutrition_average_data()
This function now only FORMATS the data for AI consumption.
"""
data = get_nutrition_average_data(profile_id, days)
cur.execute(
f"""SELECT AVG({db_field}) as avg FROM nutrition_log
WHERE profile_id=%s AND date >= %s AND {db_field} IS NOT NULL""",
(profile_id, cutoff)
)
row = cur.fetchone()
if row and row['avg']:
if field == 'kcal':
return f"{int(row['avg'])} kcal/Tag (Ø {days} Tage)"
else:
return f"{int(row['avg'])}g/Tag (Ø {days} Tage)"
if data['confidence'] == 'insufficient':
return "nicht verfügbar"
# Map field names to data keys
field_map = {
'protein': 'protein_avg',
'fat': 'fat_avg',
'carb': 'carbs_avg',
'kcal': 'kcal_avg'
}
data_key = field_map.get(field, f'{field}_avg')
value = data.get(data_key, 0)
if field == 'kcal':
return f"{int(value)} kcal/Tag (Ø {days} Tage)"
else:
return f"{int(value)}g/Tag (Ø {days} Tage)"
def get_caliper_summary(profile_id: str) -> str:
"""Get latest caliper measurements summary."""
@@ -178,48 +182,45 @@ def get_goal_bf_pct(profile_id: str) -> str:
def get_nutrition_days(profile_id: str, days: int = 30) -> str:
"""Get number of days with nutrition data."""
with get_db() as conn:
cur = get_cursor(conn)
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
cur.execute(
"""SELECT COUNT(DISTINCT date) as days FROM nutrition_log
WHERE profile_id=%s AND date >= %s""",
(profile_id, cutoff)
)
row = cur.fetchone()
return str(row['days']) if row else "0"
"""
Get number of days with nutrition data.
Phase 0c: Refactored to use data_layer.nutrition_metrics.get_nutrition_days_data()
This function now only FORMATS the data for AI consumption.
"""
data = get_nutrition_days_data(profile_id, days)
return str(data['days_with_data'])
def get_protein_ziel_low(profile_id: str) -> str:
"""Calculate lower protein target based on current weight (1.6g/kg)."""
with get_db() as conn:
cur = get_cursor(conn)
cur.execute(
"""SELECT weight FROM weight_log
WHERE profile_id=%s ORDER BY date DESC LIMIT 1""",
(profile_id,)
)
row = cur.fetchone()
if row:
return f"{int(float(row['weight']) * 1.6)}"
"""
Calculate lower protein target based on current weight (1.6g/kg).
Phase 0c: Refactored to use data_layer.nutrition_metrics.get_protein_targets_data()
This function now only FORMATS the data for AI consumption.
"""
data = get_protein_targets_data(profile_id)
if data['confidence'] == 'insufficient':
return "nicht verfügbar"
return f"{int(data['protein_target_low'])}"
def get_protein_ziel_high(profile_id: str) -> str:
"""Calculate upper protein target based on current weight (2.2g/kg)."""
with get_db() as conn:
cur = get_cursor(conn)
cur.execute(
"""SELECT weight FROM weight_log
WHERE profile_id=%s ORDER BY date DESC LIMIT 1""",
(profile_id,)
)
row = cur.fetchone()
if row:
return f"{int(float(row['weight']) * 2.2)}"
"""
Calculate upper protein target based on current weight (2.2g/kg).
Phase 0c: Refactored to use data_layer.nutrition_metrics.get_protein_targets_data()
This function now only FORMATS the data for AI consumption.
"""
data = get_protein_targets_data(profile_id)
if data['confidence'] == 'insufficient':
return "nicht verfügbar"
return f"{int(data['protein_target_high'])}"
def get_activity_summary(profile_id: str, days: int = 14) -> str:
"""Get activity summary for recent period."""