From 2e0a94e88c847a5ed8dc6ad5fa49715cd631bdfe Mon Sep 17 00:00:00 2001 From: Tyler Hoang Date: Fri, 8 May 2026 01:58:48 -0700 Subject: Initial commit — Commis personal chef app MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit AI-powered local chef tool: pantry tracking, meal logging, rotating weekly menu generation, and grocery list optimization via Ollama (llama3). FastAPI backend, SQLite, vanilla JS frontend. Co-Authored-By: Claude Sonnet 4.6 --- services/pantry_service.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 services/pantry_service.py (limited to 'services/pantry_service.py') diff --git a/services/pantry_service.py b/services/pantry_service.py new file mode 100644 index 0000000..19271e5 --- /dev/null +++ b/services/pantry_service.py @@ -0,0 +1,51 @@ +from sqlalchemy.orm import Session +from datetime import datetime, timedelta +from models import Ingredient, MealLog +from collections import Counter + + +def build_pantry_context(db: Session) -> dict: + """Return a dict with pantry state for use as AI context.""" + today = datetime.utcnow().date() + soon = today + timedelta(days=3) + + # All ingredients + all_ingredients = db.query(Ingredient).all() + available = [ + { + "name": i.name, + "quantity": i.quantity, + "unit": i.unit, + "category": i.category, + "expiry": i.expiry_date.isoformat() if i.expiry_date else None, + } + for i in all_ingredients + ] + + # Expiring within 3 days + expiring = [ + i for i in available + if i["expiry"] and i["expiry"] <= soon.isoformat() + ] + + # Recent meals (last 14 days) + cutoff = datetime.utcnow() - timedelta(days=14) + recent_meal_logs = db.query(MealLog).filter(MealLog.eaten_at >= cutoff).order_by(MealLog.eaten_at.desc()).all() + recent_meals = [ + { + "date": m.eaten_at.date().isoformat(), + "meal_type": m.meal_type, + "meal_name": m.meal_name, + } + for m in recent_meal_logs + ] + + # Meal frequency (count by name) + meal_frequency = dict(Counter(m["meal_name"] for m in recent_meals)) + + return { + "available_ingredients": available, + "expiring_soon": expiring, + "recent_meals": recent_meals, + "meal_frequency": meal_frequency, + } -- cgit v1.3-2-g0d8e