get_nutrition_days_summary
Retrieve nutrition summaries for a specified date range to track dietary intake and monitor nutritional patterns over time.
Instructions
Get nutrition summaries for a date range.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| from_date | No | ||
| to_date | No | ||
| limit | No | ||
| offset | No |
Implementation Reference
- src/main.py:540-585 (handler)Implementation of the get_nutrition_days_summary tool. Queries the nutrition_days table and calculates totals for related meals/meal_items.
def get_nutrition_days_summary( from_date: Optional[str] = None, to_date: Optional[str] = None, limit: int = 31, offset: int = 0, ) -> dict[str, list[dict[str, Any]]]: """Get nutrition summaries for a date range.""" conn = get_connection() cursor = conn.cursor() filters = [] params = [] if from_date: filters.append("date >= ?") params.append(_ensure_date(from_date)) if to_date: filters.append("date <= ?") params.append(_ensure_date(to_date)) base = "SELECT * FROM nutrition_days" if filters: base += " WHERE " + " AND ".join(filters) base += " ORDER BY date DESC LIMIT ? OFFSET ?" params.extend([limit, offset]) cursor.execute(base, params) days = [] for row in cursor.fetchall(): day = _row_to_dict(row) cursor.execute("SELECT * FROM meals WHERE day_id = ?", (day["id"],)) meals = cursor.fetchall() items = [] for m in meals: cursor.execute("SELECT * FROM meal_items WHERE meal_id = ?", (m["id"],)) items.extend(cursor.fetchall()) day["totals"] = { "calories": sum(i["calories"] for i in items) if items else 0, "protein_g": sum(i["protein_g"] for i in items) if items else 0, "carbs_g": sum(i["carbs_g"] for i in items) if items else 0, "fats_g": sum(i["fats_g"] for i in items) if items else 0, "fiber_g": sum(i["fiber_g"] for i in items) if items else 0, } days.append(day) conn.close() return {"days": days}