summaryrefslogtreecommitdiff
path: root/services/pantry_service.py
diff options
context:
space:
mode:
authorTyler Hoang <tyler@tylerhoang.xyz>2026-05-08 01:58:48 -0700
committerTyler Hoang <tyler@tylerhoang.xyz>2026-05-08 01:58:48 -0700
commit2e0a94e88c847a5ed8dc6ad5fa49715cd631bdfe (patch)
tree0c27fc5a8d8cbba60e571bb6690a13c0c0060ff4 /services/pantry_service.py
Initial commit — Commis personal chef app
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 <noreply@anthropic.com>
Diffstat (limited to 'services/pantry_service.py')
-rw-r--r--services/pantry_service.py51
1 files changed, 51 insertions, 0 deletions
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,
+ }