fix: Offene Lebensmittel nach Listen-Import wieder anzeigen
Unmapped ohne Kreuzjoin, Listen-GET kurz, Rebuild im Hintergrund. Tandoor-Zugang (URL/Token) in den Einstellungen mit ausliefern. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
fa542e0fb9
commit
d6838abda7
23 files changed
+786
-114
No files matched your search
@@ -99,6 +99,91 @@ def filter_unmapped_since(rows: list[dict], since_days: int, today: date | None
|
||||
return out
|
||||
|
||||
|
||||
def fetch_unmapped_rows(cur, profile_id: str) -> list[dict[str, Any]]:
|
||||
"""Diary lines without food/recipe plus list ingredients without mapping.
|
||||
|
||||
Ingredient dates come from recipe.updated_at or last diary use of that list
|
||||
(items that already have recipe_id) — not a join of every recipe name to
|
||||
every diary row.
|
||||
"""
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT source_name_normalized
|
||||
FROM food_name_mappings
|
||||
WHERE profile_id = %s
|
||||
""",
|
||||
(profile_id,),
|
||||
)
|
||||
mapped = {r["source_name_normalized"] for r in cur.fetchall()}
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT i.source_name_raw, i.source_name_normalized,
|
||||
COUNT(*) AS count, MIN(i.date) AS first_date, MAX(i.date) AS last_date,
|
||||
MIN(r.id::text) AS matching_recipe_id,
|
||||
MIN(i.quantity_raw) AS sample_quantity_raw
|
||||
FROM nutrition_items i
|
||||
LEFT JOIN food_recipes r
|
||||
ON r.profile_id = i.profile_id AND r.name_normalized = i.source_name_normalized
|
||||
WHERE i.profile_id = %s AND i.food_id IS NULL AND i.recipe_id IS NULL
|
||||
GROUP BY i.source_name_raw, i.source_name_normalized
|
||||
""",
|
||||
(profile_id,),
|
||||
)
|
||||
diary = [dict(r) | {"kind": "diary"} for r in cur.fetchall()]
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT i.source_name_raw, i.source_name_normalized,
|
||||
COUNT(*) AS count,
|
||||
MIN(COALESCE(u.last_used, r.updated_at::date, r.created_at::date)) AS first_date,
|
||||
MAX(COALESCE(u.last_used, r.updated_at::date, r.created_at::date)) AS last_date,
|
||||
MIN(i.quantity_raw) AS sample_quantity_raw
|
||||
FROM food_recipe_ingredients i
|
||||
JOIN food_recipes r ON r.id = i.recipe_id
|
||||
LEFT JOIN (
|
||||
SELECT recipe_id, MIN(date) AS first_used, MAX(date) AS last_used
|
||||
FROM nutrition_items
|
||||
WHERE profile_id = %s AND recipe_id IS NOT NULL
|
||||
GROUP BY recipe_id
|
||||
) u ON u.recipe_id = r.id
|
||||
WHERE r.profile_id = %s
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM food_name_mappings m
|
||||
WHERE m.profile_id = r.profile_id
|
||||
AND m.source_name_normalized = i.source_name_normalized
|
||||
)
|
||||
GROUP BY i.source_name_raw, i.source_name_normalized
|
||||
""",
|
||||
(profile_id, profile_id),
|
||||
)
|
||||
ings = [dict(r) | {"kind": "recipe_ingredient"} for r in cur.fetchall()]
|
||||
out = []
|
||||
for row in merge_unmapped_rows(diary + ings):
|
||||
key = row.get("source_name_normalized") or normalize_food_name(row.get("source_name_raw"))
|
||||
if not key or key in mapped:
|
||||
continue
|
||||
row["last_date"] = as_iso_date(row.get("last_date"))
|
||||
row["first_date"] = as_iso_date(row.get("first_date"))
|
||||
out.append(row)
|
||||
return out
|
||||
|
||||
|
||||
def list_unmapped_payload(
|
||||
cur,
|
||||
profile_id: str,
|
||||
since_days: int = 0,
|
||||
count_only: bool = False,
|
||||
meta: bool = False,
|
||||
) -> Any:
|
||||
days = max(0, min(int(since_days or 0), 3650))
|
||||
out = fetch_unmapped_rows(cur, profile_id)
|
||||
items = sort_unmapped_rows(filter_unmapped_since(out, days) if days else out, days)
|
||||
if count_only:
|
||||
return {"count": len(items), "total": len(out), "since_days": days}
|
||||
if meta:
|
||||
return {"items": items, "count": len(items), "total": len(out), "since_days": days}
|
||||
return items
|
||||
|
||||
|
||||
def sort_unmapped_rows(rows: list[dict], since_days: int = 0) -> list[dict]:
|
||||
rows = list(rows)
|
||||
if int(since_days or 0) > 0:
|
||||
|
||||
Reference in new issue
Block a user