Compare commits
57
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
41d809c68c | ||
|
|
d66eadf88f | ||
|
|
a1b458d228 | ||
|
|
6743814904 | ||
|
|
6ef07a77a5 | ||
|
|
aa6644f44b | ||
|
|
319a9cfafd | ||
|
|
0ab91416d0 | ||
|
|
5fbaa7cc8b | ||
|
|
9b53a4ba68 | ||
|
|
9720cb57ec | ||
|
|
1e1605f878 | ||
|
|
4575bb23ee | ||
|
|
a62c952097 | ||
|
|
a7859f0050 | ||
|
|
19a88e4f97 | ||
|
|
424270f0e8 | ||
|
|
f34e46b04f | ||
|
|
46b703e5a2 | ||
|
|
25c4ecfd48 | ||
|
|
72eb94d186 | ||
|
|
4d81ea2cf3 | ||
|
|
caeed3fbaa | ||
|
|
b5f745fc3e | ||
|
|
2443b5ac3a | ||
|
|
8b731313df | ||
|
|
12893cba78 | ||
|
|
09439ad1a5 | ||
|
|
75736dadec | ||
|
|
8d56c352fc | ||
|
|
ebea9946a7 | ||
|
|
9fb04af7df | ||
|
|
3f5b9b8b58 | ||
|
|
5bbea036c2 | ||
|
|
c762495b6f | ||
|
|
979e734bd9 | ||
|
|
79cb3e0100 | ||
|
|
9d22e7e8af | ||
|
|
f46c367c27 | ||
|
|
ac4c6760d7 | ||
|
|
5a6a140dfd | ||
|
|
931012c16b | ||
|
|
03f4b871a9 | ||
|
|
601fc80178 | ||
|
|
3ff2a1bf45 | ||
|
|
51aa57f304 | ||
|
|
ca4411f30f | ||
|
|
b551365fb5 | ||
|
|
272c123952 | ||
|
|
9387670a7b | ||
|
|
d4a8401a6a | ||
|
|
aaf88a6f12 | ||
|
|
b789c1bd44 | ||
|
|
5062aa8068 | ||
|
|
8a042589e7 | ||
|
|
3898b5ad45 | ||
|
|
9d15336144 |
No files matched your search
+1
-3
@@ -12,8 +12,7 @@ Dieser Ordner ist der **primäre Orientierungspunkt** für Claude Code / Cursor-
|
|||||||
| 2 | **`rules/DOCUMENTATION.md`** – Ablage- und Dokumentationsregeln |
|
| 2 | **`rules/DOCUMENTATION.md`** – Ablage- und Dokumentationsregeln |
|
||||||
| 3 | `rules/ARCHITECTURE.md`, `rules/CODING_RULES.md`, `rules/LESSONS_LEARNED.md` |
|
| 3 | `rules/ARCHITECTURE.md`, `rules/CODING_RULES.md`, `rules/LESSONS_LEARNED.md` |
|
||||||
| 4 | Issue-Landkarte: **`.claude/docs/GITEA_ISSUES_INDEX.md`** |
|
| 4 | Issue-Landkarte: **`.claude/docs/GITEA_ISSUES_INDEX.md`** |
|
||||||
| 5 | **Jinkendo Foundation** (Designprinzipien 1–9): **`docs/jinkendo-foundation/design-principles/README.md`** |
|
| 5 | **Universal CSV Import** (Modul/Executor/Vorlagen): **`docs/technical/UNIVERSAL_CSV_IMPORT_AGENT_GUIDE.md`** (unter `.claude/`) |
|
||||||
| 6 | **Universal CSV Import** (Modul/Executor/Vorlagen): **`docs/technical/UNIVERSAL_CSV_IMPORT_AGENT_GUIDE.md`** (unter `.claude/`) |
|
|
||||||
|
|
||||||
Themen mit UI/Nav/PWA: siehe `../docs/issues/GUI_IA_ADMIN_NAV_2026-04-05.md` (im **Projekt**-`docs/`, nicht hier).
|
Themen mit UI/Nav/PWA: siehe `../docs/issues/GUI_IA_ADMIN_NAV_2026-04-05.md` (im **Projekt**-`docs/`, nicht hier).
|
||||||
|
|
||||||
@@ -26,7 +25,6 @@ Themen mit UI/Nav/PWA: siehe `../docs/issues/GUI_IA_ADMIN_NAV_2026-04-05.md` (im
|
|||||||
├── README.md ← Diese Datei
|
├── README.md ← Diese Datei
|
||||||
├── rules/ ← Verbindliche Regeln (versioniert, wenn konfiguriert)
|
├── rules/ ← Verbindliche Regeln (versioniert, wenn konfiguriert)
|
||||||
├── docs/ ← Spezifikationen + Arbeitspapiere
|
├── docs/ ← Spezifikationen + Arbeitspapiere
|
||||||
│ ├── jinkendo-foundation/ ← Foundation Produktfamilie (Designprinzipien)
|
|
||||||
│ ├── functional/ ← Fachlich (WAS)
|
│ ├── functional/ ← Fachlich (WAS)
|
||||||
│ ├── technical/ ← Technisch (WIE)
|
│ ├── technical/ ← Technisch (WIE)
|
||||||
│ ├── architecture/ ← Querschnitt
|
│ ├── architecture/ ← Querschnitt
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# Gitea Issues – Landkarte (Auswertung)
|
# Gitea Issues – Landkarte (Auswertung)
|
||||||
|
|
||||||
**Quelle:** Gitea `Lars/mitai-jinkendo`, Stand **2026-04-11** (Abfrage `state=all`, ergänzt: #71, #75, #76, #106, 2026-09-12).
|
**Quelle:** Gitea `Lars/mitai-jinkendo`, Stand **2026-04-11** (Abfrage `state=all`, ergänzt: #71, #76).
|
||||||
**URL:** http://192.168.2.144:3000/Lars/mitai-jinkendo/issues
|
**URL:** http://192.168.2.144:3000/Lars/mitai-jinkendo/issues
|
||||||
|
|
||||||
Dieses Dokument ist ein **Orientierungs-Index** für Agenten und Entwickler. Verbindliches Tracking bleibt **in Gitea**; hier: Kategorien, Dubletten-Hinweise, grobe Prioritätseinschätzung.
|
Dieses Dokument ist ein **Orientierungs-Index** für Agenten und Entwickler. Verbindliches Tracking bleibt **in Gitea**; hier: Kategorien, Dubletten-Hinweise, grobe Prioritätseinschätzung.
|
||||||
@@ -82,14 +82,6 @@ Dieses Dokument ist ein **Orientierungs-Index** für Agenten und Entwickler. Ver
|
|||||||
|---|--------|
|
|---|--------|
|
||||||
| 37 | Feature-Enforcement für Activity CSV-Import |
|
| 37 | Feature-Enforcement für Activity CSV-Import |
|
||||||
| 38 | Feature-Enforcement für Nutrition CSV-Import UI |
|
| 38 | Feature-Enforcement für Nutrition CSV-Import UI |
|
||||||
| 71 | Universal CSV Import: Dry-Run, Mapping-Validierung, Fehler-Hints |
|
|
||||||
|
|
||||||
### Ernährung / BLS
|
|
||||||
|
|
||||||
| # | Titel |
|
|
||||||
|---|--------|
|
|
||||||
| 75 | Ernährung: Zucker/Ballaststoffe, Lebensmittelqualität, Timing (Folge nach #106) |
|
|
||||||
| 106 | BLS-Stammdaten, FDDB-Mapping und Item-Tagebuch |
|
|
||||||
|
|
||||||
### Qualität / Sonstiges
|
### Qualität / Sonstiges
|
||||||
|
|
||||||
|
|||||||
+2
-15
@@ -20,7 +20,6 @@ _Dieser Ordner `.claude/docs/` ist per `.gitignore`-Ausnahme **versioniert** (Sp
|
|||||||
├── ROADMAP.md ← Strategische Phasen (0–3)
|
├── ROADMAP.md ← Strategische Phasen (0–3)
|
||||||
├── CLEANUP_PLAN.md ← Historie Bereinigung März 2026
|
├── CLEANUP_PLAN.md ← Historie Bereinigung März 2026
|
||||||
├── prompts/ ← Exportierte Prompt-Artefakte (JSON)
|
├── prompts/ ← Exportierte Prompt-Artefakte (JSON)
|
||||||
├── jinkendo-foundation/ ← Foundation Produktfamilie (Designprinzipien)
|
|
||||||
├── functional/ ← Fachliche Spezifikationen (WAS)
|
├── functional/ ← Fachliche Spezifikationen (WAS)
|
||||||
├── technical/ ← Technische Spezifikationen & Referenz (WIE)
|
├── technical/ ← Technische Spezifikationen & Referenz (WIE)
|
||||||
├── working/ ← Arbeitspapiere, Analysen, Session-Snapshots
|
├── working/ ← Arbeitspapiere, Analysen, Session-Snapshots
|
||||||
@@ -53,11 +52,9 @@ _Dieser Ordner `.claude/docs/` ist per `.gitignore`-Ausnahme **versioniert** (Sp
|
|||||||
|--------|-------------|-------------------|
|
|--------|-------------|-------------------|
|
||||||
| Data Layer / Charts (Phase 0c) | `functional/DATA_ARCHITECTURE.md`, `technical/DATA_LAYER_EXTENSION_GUIDE.md` | `backend/data_layer/`, `backend/routers/charts.py` |
|
| Data Layer / Charts (Phase 0c) | `functional/DATA_ARCHITECTURE.md`, `technical/DATA_LAYER_EXTENSION_GUIDE.md` | `backend/data_layer/`, `backend/routers/charts.py` |
|
||||||
| Platzhalter / Registry | `technical/PLACEHOLDER_REGISTRY_FRAMEWORK.md`, `technical/PLACEHOLDER_DEVELOPMENT_GUIDE.md` | `backend/placeholder_registrations/`, `backend/placeholder_resolver.py` |
|
| Platzhalter / Registry | `technical/PLACEHOLDER_REGISTRY_FRAMEWORK.md`, `technical/PLACEHOLDER_DEVELOPMENT_GUIDE.md` | `backend/placeholder_registrations/`, `backend/placeholder_resolver.py` |
|
||||||
| Dashboard-Widgets | `technical/DASHBOARD_WIDGETS_AGENT_GUIDE.md` | Widget-Katalog + Registrierung (siehe Guide) |
|
| Dashboard-Lab-Widgets | `technical/DASHBOARD_WIDGETS_AGENT_GUIDE.md` | Widget-Katalog + Registrierung (siehe Guide) |
|
||||||
| Training Profiler / Resolver | `technical/TRAINING_PROFILE_RESOLVER_LAYER1.md`, `functional/TRAINING_TYPE_PROFILES.md` | Resolver-Module wie im Guide genannt |
|
| Training Profiler / Resolver | `technical/TRAINING_PROFILE_RESOLVER_LAYER1.md`, `functional/TRAINING_TYPE_PROFILES.md` | Resolver-Module wie im Guide genannt |
|
||||||
| Universal CSV Import | `technical/UNIVERSAL_CSV_IMPORT_AGENT_GUIDE.md` | `backend/csv_parser/`, `routers/csv_import.py`, `routers/admin_csv_templates.py` |
|
| Universal CSV Import | `technical/UNIVERSAL_CSV_IMPORT_AGENT_GUIDE.md` | `backend/csv_parser/`, `routers/csv_import.py`, `routers/admin_csv_templates.py` |
|
||||||
| BLS / Lebensmittel | `functional/BLS_FOOD_REFERENCE.md`, `technical/BLS_FOOD_REFERENCE.md` | Migration 062, `backend/bls/`, `data_layer/food_mapping.py` |
|
|
||||||
| **Designprinzipien (Produktfamilie)** | **`jinkendo-foundation/design-principles/README.md`** | Querschnittsmuster; Foundation für Schwester-Apps |
|
|
||||||
| Aktivität Produktionsreife | `technical/ACTIVITY_PRODUCTION_ARCHITECTURE_AND_PHASES.md` (+ EAV-Guide) | `backend/data_layer/activity_session_metrics.py`, `activity_metrics.py`, CSV-Orchestrierung |
|
| Aktivität Produktionsreife | `technical/ACTIVITY_PRODUCTION_ARCHITECTURE_AND_PHASES.md` (+ EAV-Guide) | `backend/data_layer/activity_session_metrics.py`, `activity_metrics.py`, CSV-Orchestrierung |
|
||||||
| Mitgliedschaft / Features | `technical/MEMBERSHIP_SYSTEM.md`, `architecture/FEATURE_ENFORCEMENT.md` | `backend/auth.py`, Feature-Logging, Router mit Enforcement |
|
| Mitgliedschaft / Features | `technical/MEMBERSHIP_SYSTEM.md`, `architecture/FEATURE_ENFORCEMENT.md` | `backend/auth.py`, Feature-Logging, Router mit Enforcement |
|
||||||
|
|
||||||
@@ -95,16 +92,6 @@ _Dieser Ordner `.claude/docs/` ist per `.gitignore`-Ausnahme **versioniert** (Sp
|
|||||||
|
|
||||||
## Technische Spezifikationen (`technical/`)
|
## Technische Spezifikationen (`technical/`)
|
||||||
|
|
||||||
### Jinkendo Foundation (Produktfamilie)
|
|
||||||
|
|
||||||
| Dokument | Inhalt |
|
|
||||||
|----------|--------|
|
|
||||||
| **[jinkendo-foundation/README.md](jinkendo-foundation/README.md)** | Einstieg Foundation-Ordner |
|
|
||||||
| **[design-principles/README.md](jinkendo-foundation/design-principles/README.md)** | Index: 9 Designprinzipien, Lesereihenfolge, Übergabe-Checkliste |
|
|
||||||
| `design-principles/*_DESIGN_PRINCIPLES.md` | Einzeldokumente (#1–#9) |
|
|
||||||
|
|
||||||
### Referenz & Agent-Guides (`technical/`)
|
|
||||||
|
|
||||||
| Dokument | Thema |
|
| Dokument | Thema |
|
||||||
|----------|--------|
|
|----------|--------|
|
||||||
| `AGGREGATION_METHODS.md` | Aggregation |
|
| `AGGREGATION_METHODS.md` | Aggregation |
|
||||||
@@ -196,4 +183,4 @@ Siehe [`audit/README.md`](./audit/README.md).
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
**Letzte Aktualisierung:** 4. Juli 2026 (Jinkendo Foundation, Designprinzipien nach `jinkendo-foundation/`)
|
**Letzte Aktualisierung:** 9. April 2026 (Universal CSV Agent-Guide, Abgleich-Tabelle)
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
# BLS-Lebensmittelreferenz und FDDB-Mapping
|
|
||||||
|
|
||||||
**Stand:** 2026-09-14 · **Status:** Phase 1 (Umsetzung)
|
|
||||||
|
|
||||||
## WAS
|
|
||||||
|
|
||||||
Optionale Grundlage für verlässliche Nährwerte: offizieller Bundeslebensmittelschlüssel (BLS) 4.0 plus manuelle Katalogerweiterung, lernendes Mapping von FDDB-Bezeichnern, persistierte Tagebuchzeilen. Reine Tagesmakros bleiben First Class.
|
|
||||||
|
|
||||||
## Zuordnung (UX)
|
|
||||||
|
|
||||||
Offene Zuordnungen zeigen **Vorschläge in der Zeile** (z. B. Haferflocken → Hafer Flocken); Bestätigen ohne Dialog. Mehrere nahe Treffer werden gekennzeichnet. Fehlt ein Treffer, **Lebensmittel oder Liste**. Ein Lebensmittel wird ein Katalogeintrag. Eine **Liste / Kombination** öffnet den Dialog: Zutaten per Suche aus bereits gemappten Namen oder dem Katalog (Fettgehalt wie Joghurt 10 %). Jede Zutat hat **Menge + Einheit** (g, ml, EL, TL, Prise, Stück, …). Standardeinheit für Nährwerte ist Gramm; Haushaltsmaße werden umgerechnet (EL 15 g, TL 5 g, Prise 0,3 g, oder Gramm pro Stück am Mapping). Fehlt der Faktor, bleibt die Einheit erhalten und ist später umrechenbar. Katalogtreffer werden beim Speichern zugeordnet. Freitext-Zutaten bleiben offen und erscheinen in der Offene-Liste (Kennzeichnung „Listenzutat“) — dort nur als Lebensmittel zuordenbar. Nicht-Gramm-Einheiten (Stück, EL, TL, …) bekommen ein **Gramm-pro-Einheit**-Feld am Mapping. Vorschläge und Katalogsuche laufen nur für die sichtbare Arbeit. Die Offene-Liste startet bei den **letzten 4 Wochen**; ältere Namen bleiben unter „Alle“. Offene Listenzutaten zählen mit dem Datum der letzten Listen-Nutzung oder der letzten Listenänderung — nicht nur mit einem Tagebuch-Datum der Zutat selbst. FDDB-Listenimport speichert zuerst; Tagesnährwerte folgen im Hintergrund. Zutatenlisten liegen im Tab **Listen** (sichtbar, durchsuchbar, bearbeitbar). Im Listendialog sieht man pro Zutat, ob ein Mapping fehlt, und kann dort bestätigen oder suchen — zusätzlich zum Tab Zuordnen. Die Zutatensuche zeigt zuerst Namen aus importierten/angelegten Listen, dann eigene Lebensmittel, dann den BLS. „Andere Liste wählen“ verknüpft eine Tagebuchzeile mit einer bestehenden Liste; der Dialog zeigt alle Listen, nicht nur Namens-Treffer. Nach dem Verknüpfen verschwindet die Zeile unter Zuordnen — Kennzeichnung „Liste: …“, solange sie noch offen ist.
|
|
||||||
|
|
||||||
## Listen / Kombinationen (Mitai) vs. Gerichte (Tandoor)
|
|
||||||
|
|
||||||
**Mitai speichert nur Roh-Kombinationen** — einen Namen für eine Zutatenliste ohne Kochvorgang (Müsli, Bowl, Smoothie). Summe der Zutatengramm = gegessene Gramm. Tabelle bleibt `food_recipes` (kein Rename).
|
|
||||||
|
|
||||||
**Gekochte Familienrezepte** liegen in **Tandoor**. Kein zweites Rezeptbuch in Mitai. Kochschwund (Wasserverlust beim Köcheln) darf die Rohzutaten nicht 1:1 auf die gegessene Menge skalieren.
|
|
||||||
|
|
||||||
Spätere Ausbeute (nicht in Phase 1):
|
|
||||||
|
|
||||||
- `ingredients_g` = Summe der auf Gramm aufgelösten Zutaten
|
|
||||||
- `cooked_yield_g` = gewogenes Fertiggewicht nach dem Kochen
|
|
||||||
- `yield_factor = cooked_yield_g / ingredients_g` (oft 0,5–0,9 bei langem Köcheln)
|
|
||||||
- Nährwerte **pro 100 g fertig** = `Summe(Zutat_Nährwerte) / cooked_yield_g × 100`
|
|
||||||
- Gegessen: `portion_g / 100 × Werte_pro_100g`
|
|
||||||
|
|
||||||
Ohne Fertiggewicht: nicht raten — FDDB-Makros oder Marke „unvollständig“. Für Listen gilt implizit `cooked_yield_g = ingredients_g` (Faktor 1). Tandoor liefert Zutaten und Schritte; die Ausbeute und die **pro-100-g**-Rechnung führt Mitai. Importiertes Gericht wird ein Katalogeintrag (`catalog_kind` z. B. `recipe_cooked`), keine zweite Wertetabelle.
|
|
||||||
|
|
||||||
## FDDB-Listen
|
|
||||||
|
|
||||||
FDDB-Tagebuchexport fasst selbst angelegte Listen oft zu **einer Zeile** (Listenname + Menge) zusammen. Die Zutaten stehen in einem **separaten Listen-Export** (`lists_*.csv`, Spalte `produkte`). Ablauf: Listen importieren → passende Tagebuchzeilen werden als Liste verknüpft → **Zutaten** zuordnen, nicht die Liste als Ganzes. Unvollständige Zutaten-Mappings fallen auf die FDDB-Makros der Tagebuchzeile zurück.
|
|
||||||
|
|
||||||
Gelernte Zuordnungen und Listen lassen sich als **JSON sichern** und auf einer anderen Instanz (Dev → Prod) wieder einspielen. Offizielle Lebensmittel werden über den BLS-Code gefunden — der BLS-Katalog muss auf dem Ziel bereits importiert sein.
|
|
||||||
|
|
||||||
## Fachliche Regeln
|
|
||||||
|
|
||||||
- BLS-Code (`bls_code`, Stoff-`attr_key`) bleibt die stabile Identität bei Reimports.
|
|
||||||
- BLS 4.0 (~7140 Lebensmittel) ist frei nutzbar (MRI / blsdb.de); Dateien nicht im Git.
|
|
||||||
- Mapping ohne KI: Normalisierung + exakter Lookup (User vor Global) + Bestätigung neuer Namen.
|
|
||||||
- Öl/Supplement mit Etikett (EPA/DHA, Vitamine) ist **ein Katalogeintrag**, keine Zutatenliste. Listen sind nur Rohmischungen aus Lebensmitteln — eine Liste kann keine andere Liste enthalten. Quarkspeise = Liste, Omega-3-Öl = Lebensmittel darin.
|
|
||||||
- Gelernte Zuordnungen bleiben dauerhaft, sind aber änder- und löschbar. Nutzer-Mapping vor globalem Mapping. Globales Mapping nur auf BLS oder zentrale Admin-Foods.
|
|
||||||
- Sichtbarkeit: persönlich (nur Owner) / zentral (alle) / BLS (alle). Vorschläge und Suche filtern danach; bei gleichem Namen gewinnt persönlich vor zentral vor BLS.
|
|
||||||
- Ungemappte / Fertiggerichte: FDDB-Makros, keine erfundenen Mikros.
|
|
||||||
- Fasten und „unvollständig“ sind explizite Marken, kein Auto-Schluss aus fehlendem Import.
|
|
||||||
- Import-Policy (Profil): nachfragen / Katalog überschreiben / FDDB überschreiben / Makros behalten.
|
|
||||||
|
|
||||||
## Manuelle Lebensmittel / Supplemente
|
|
||||||
|
|
||||||
Eigene Einträge des Nutzers sind **persönlich** (`manual_user`). Der Admin legt **zentrale** Einträge für alle an (`manual_admin`). Beide speichern **dieselben Stoffwerte** wie BLS-Lebensmittel (`food_attribute_values`, immer pro 100 g). Beim Anlegen: Makros plus Suche nach weiteren Stoffen (EPA, DHA, Omega-3). Etikett „pro 8 ml“ → Portionsgröße in g angeben, Umrechnung auf 100 g erfolgt serverseitig. Einheit des Stoffs beachten (oft **g**, Etikett in mg: 1100 mg = 1,1 g). Fehlt ein Stoff im Katalog: Nutzer kann ihn selbst anlegen oder Admin → Stoffe & Attribute (Bezeichnung änderbar, Schlüssel fest). In die Makro-Haupttabelle (`nutrition_log`) fließen nur ENERCC→kcal, PROT625→protein_g, FAT→fat_g, CHO→carbs_g (Wert je 100 g × gegessene g / 100). Andere Stoffe gehen nach `nutrition_daily_nutrients`.
|
|
||||||
|
|
||||||
**Listen am Tag:** Eine Mitai-Liste (z. B. Quark-Skyr-Blaubeeren) ist zugeordnet, sobald die Tagebuchzeile `recipe_id` hat — nicht erst bei einem einzelnen `food_id`. In der Tagesübersicht erscheint „Liste: …“. Sind alle Zutaten gemappt, lassen sich die berechneten Katalog-Makros (Summe der Zutaten, skaliert auf die gegessene Menge) per Knopf in `nutrition_log` übernehmen.
|
|
||||||
|
|
||||||
**Nachträglich ändern:** Unter Ernährung → Tab **Lebensmittel** (persönliche Liste + zentrale Admin-Einträge, Suche). Persönliche Einträge legt und ändert der Owner. Zentrale Einträge liest jeder; **ändert nur ein Admin** (gleiche Liste, Zeile oder Bearbeiten). Beim Zuordnen gilt die Reihenfolge persönlich → zentral → BLS. Globale Namens-Mappings (Admin) gelten für alle, dürfen aber nicht auf ein privates Nutzer-Lebensmittel zeigen. Offizielle BLS-Einträge bleiben fest. Beim Speichern gelten die Felder als Werte je 100 g, außer die Portionsgröße wird bewusst auf die Etikett-Portion gesetzt. Betroffene Tagebuch-Tage werden im Hintergrund neu berechnet. Admin → Lebensmittelkatalog → Reiter **Manuell** zeigt **Zentral** und **Persönlich** getrennt; beide sind dort änderbar.
|
|
||||||
|
|
||||||
Weitere Quellen (USDA, Schweizer Nährwertdatenbank) kommen später als zusätzliche Katalogherkunft, nicht als zweite Wertetabelle.
|
|
||||||
|
|
||||||
## Später
|
|
||||||
|
|
||||||
Platzhalter (Registry) für Mikros, Esszeitpunkte (`logged_at`), Fasten; Bezug Gitea #106 (Grundlage) und #75 (Folge).
|
|
||||||
|
|
||||||
**Tandoor:** Familienrezepte bleiben dort. Persönlicher Zugang (URL + API-Token) unter Einstellungen — nicht systemweit; Token nur auf dem Server, nie im Export. Verbindungstest: `GET /api/user/` (Fallback Rezeptliste). Zutaten werden später live gelesen und auf BLS gemappt, kein Zutaten-Sync. Noch kein Rezeptimport; später `cooked_yield_g` und Katalogeintrag Fertiggericht pro 100 g.
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
# Jinkendo Foundation
|
|
||||||
|
|
||||||
**Stand:** 2026-07-04
|
|
||||||
**Zweck:** Geteilte **Architektur-Foundation** für die Jinkendo-Produktfamilie (Mitai, Miken, Ikigai, Shinkan, …) — unabhängig von app-spezifischer Domänenlogik.
|
|
||||||
|
|
||||||
Dieser Ordner enthält **keine** Mitai-Fachspecs und **keine** operativen Agent-Guides zu einzelnen Features. Die Referenzimplementierung bleibt in **Mitai Jinkendo**; die Foundation beschreibt **übertragbare Muster**.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Inhalt
|
|
||||||
|
|
||||||
| Pfad | Beschreibung |
|
|
||||||
|------|--------------|
|
|
||||||
| **[design-principles/README.md](./design-principles/README.md)** | **Einstieg:** Index aller 9 Designprinzipien-Dokumente, Lesereihenfolge, Übergabe-Checkliste |
|
|
||||||
| `design-principles/*_DESIGN_PRINCIPLES.md` | Einzeldokumente (#1 Prompt Engine … #9 Migration & Deploy) |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Mitai-spezifische Implementierung
|
|
||||||
|
|
||||||
Agent-Guides, API-Referenz und Domänen-Specs liegen weiterhin unter:
|
|
||||||
|
|
||||||
- `.claude/docs/technical/` — WIE (Implementierung)
|
|
||||||
- `.claude/docs/functional/` — WAS (Fachlich)
|
|
||||||
- `docs/issues/` — Issue-Epics
|
|
||||||
|
|
||||||
Die Designprinzipien verlinken dorthin, wo Mitai als **Beispiel** dient.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Apps der Familie
|
|
||||||
|
|
||||||
| Domain | App |
|
|
||||||
|--------|-----|
|
|
||||||
| mitai.jinkendo.de | Körper-Tracking (Referenz) |
|
|
||||||
| miken.jinkendo.de | Meditation |
|
|
||||||
| ikigai.jinkendo.de | Lebenssinn |
|
|
||||||
| shinkan.jinkendo.de | Kampfsport |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Pflege
|
|
||||||
|
|
||||||
Neue Querschnittsmuster → neues Dokument unter `design-principles/` + Eintrag im [Index](./design-principles/README.md).
|
|
||||||
|
|
||||||
Regeln zur Ablage: [DOCUMENTATION.md](../../rules/DOCUMENTATION.md)
|
|
||||||
@@ -1,326 +0,0 @@
|
|||||||
# Auth & Session – Designprinzipien (Extraktion)
|
|
||||||
|
|
||||||
**Status:** Analyse / Arbeitspapier
|
|
||||||
**Stand:** 2026-07-04
|
|
||||||
**Geltungsbereich:** Authentifizierung, Session-Management, rollenbasierte API-Zugriffe — kein Mandanten-/SSO-System, keine Zahlungs-Auth
|
|
||||||
|
|
||||||
**Serie:** Designprinzipien für Produktfamilie · Dokument 5 von n
|
|
||||||
**Vorgänger:** [REGISTRY_PLUGIN_DESIGN_PRINCIPLES.md](./REGISTRY_PLUGIN_DESIGN_PRINCIPLES.md)
|
|
||||||
|
|
||||||
**Kernkomponenten:**
|
|
||||||
|
|
||||||
| Bereich | Pfade |
|
|
||||||
|---------|-------|
|
|
||||||
| Auth-Kern | `backend/auth.py` |
|
|
||||||
| Auth-Endpoints | `backend/routers/auth.py` |
|
|
||||||
| Profile | `backend/routers/profiles.py` |
|
|
||||||
| Frontend | `frontend/src/context/AuthContext.jsx`, `ProfileContext.jsx`, `utils/api.js` |
|
|
||||||
| DB | `profiles`, `sessions` |
|
|
||||||
| Architektur-Regeln | `.claude/rules/ARCHITECTURE.md`, `CLAUDE.md` § Auth |
|
|
||||||
| Vision (nicht implementiert) | `CENTRAL_SUBSCRIPTION_SYSTEM.md` (SSO/JWT) |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Modul
|
|
||||||
|
|
||||||
**Auth & Session**
|
|
||||||
|
|
||||||
Server-seitige, token-basierte Authentifizierung mit FastAPI-Dependencies — **Identität und Rolle**, getrennt von Feature-Entitlements und fachlicher Logik.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Fachliche Verantwortung
|
|
||||||
|
|
||||||
Das Modul übernimmt:
|
|
||||||
|
|
||||||
1. **Identität** — Wer ist eingeloggt? (`profiles` + Passwort/bcrypt)
|
|
||||||
2. **Session** — Opaque Token in `sessions`, Ablaufzeit, Logout
|
|
||||||
3. **API-Gate** — `require_auth`, `require_admin`, `require_auth_flexible`
|
|
||||||
4. **Passwort-Lifecycle** — Hash, Verify, Reset, Registrierung, E-Mail-Verifizierung
|
|
||||||
5. **Rollen** — `profiles.role`: `user` \| `admin` (grobbinsenartig)
|
|
||||||
|
|
||||||
Es übernimmt **nicht**:
|
|
||||||
|
|
||||||
- Feature-Limits / Tier (→ Feature & Entitlement System, gleiche `auth.py`-Datei aber logisch getrennt)
|
|
||||||
- Mandanten-Isolation / Org-Workspaces
|
|
||||||
- OAuth/SSO/JWT (nur Vision)
|
|
||||||
- Authorization auf Datensatzebene (Row-Level Security)
|
|
||||||
|
|
||||||
### Was Mitai **ist** vs. **nicht ist**
|
|
||||||
|
|
||||||
| Mitai | Produktfamilien-Muster |
|
|
||||||
|-------|------------------------|
|
|
||||||
| 1 Login = 1 Profil (E-Mail) | ✅ Account-Modell |
|
|
||||||
| Historisch Multi-Profil auf einer Instanz | ⚠️ Legacy (`/profiles`, `X-Profile-Id`) |
|
|
||||||
| Self-hosted Einzelinstanz | ✅ Kein Multi-Tenant-SaaS |
|
|
||||||
| Session-Token in DB | ✅ Server-side Session Store |
|
|
||||||
| Zentrale Jinkendo-Auth (Vision) | ❌ nicht gebaut |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Administrierte vs. code-definierte Konfiguration
|
|
||||||
|
|
||||||
| Konfiguration | Speicherort | Administrierbar? |
|
|
||||||
|---------------|-------------|------------------|
|
|
||||||
| Nutzer-Stammdaten, Rolle | `profiles` | Admin (User-Verwaltung) / Self-Service |
|
|
||||||
| Session-Laufzeit | `profiles.session_days` (Default 30) | Profil/Admin |
|
|
||||||
| Passwort-Hash | `profiles.pin_hash` | Nutzer (change pin) |
|
|
||||||
| E-Mail-Verifizierung | `email_verified`, Token-Felder | System |
|
|
||||||
| Trial-Ende | `trial_ends_at` | System bei Registrierung |
|
|
||||||
| SMTP | Env (`SMTP_*`, `APP_URL`) | Deploy |
|
|
||||||
| Rate Limits Login/Register | Code (`5/min`, `3/hour`) | Code |
|
|
||||||
|
|
||||||
**Hardcodiert:** bcrypt, Token-Länge (`secrets.token_urlsafe(32)`), Rollen-Enum (`user`/`admin`), Header-Name `X-Auth-Token`.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Session- und Auth-Flow
|
|
||||||
|
|
||||||
```
|
|
||||||
Login (email + password)
|
|
||||||
→ verify_pin (bcrypt | legacy SHA256)
|
|
||||||
→ optional bcrypt upgrade
|
|
||||||
→ INSERT sessions (token, profile_id, expires_at)
|
|
||||||
→ Client: localStorage bodytrack_token
|
|
||||||
|
|
||||||
Request
|
|
||||||
→ Header X-Auth-Token (api.js / AuthContext)
|
|
||||||
→ get_session(token) JOIN profiles
|
|
||||||
→ require_auth → session dict (profile_id, role, …)
|
|
||||||
|
|
||||||
Logout
|
|
||||||
→ DELETE sessions WHERE token=…
|
|
||||||
→ Client: localStorage clear
|
|
||||||
```
|
|
||||||
|
|
||||||
**Sonderfall:** `require_auth_flexible` — Token via Header **oder** Query `ssetoken` (SSE, `<img>`, Downloads).
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Rollen
|
|
||||||
|
|
||||||
| Rolle | Mechanismus | Typische Rechte |
|
|
||||||
|-------|-------------|-----------------|
|
|
||||||
| **user** | `profiles.role = 'user'` | Eigene Daten, Features nach Tier |
|
|
||||||
| **admin** | `require_admin` | Admin-Shell, Prompts, User, System |
|
|
||||||
|
|
||||||
Kein feingranulares RBAC (keine Permission-Matrix). Admin ist Binär-Schalter.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Designprinzipien
|
|
||||||
|
|
||||||
### 1. FastAPI-Dependencies als Auth-Gate
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Jeder geschützte Endpoint nutzt `session: dict = Depends(require_auth)` als **separaten** Parameter — nie in `Header()` eingebettet. |
|
|
||||||
| **Begründung** | Verhindert ungeschützte Endpoints durch falsche Parameter-Signatur. |
|
|
||||||
| **Quelle** | `CLAUDE.md` § Kritische Regeln; `auth.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Nicht linter-erzwungen; Legacy-Endpoints existieren. |
|
|
||||||
|
|
||||||
### 2. Server-side opaque Sessions
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Token ist zufällig, in DB gespeichert; Validierung über `sessions` + Ablauf — kein JWT mit Client-Claims. |
|
|
||||||
| **Begründung** | Revocation (Logout), kein Trust in Client-Payload; einfaches Modell für Self-Hosted. |
|
|
||||||
| **Quelle** | `sessions` Tabelle; `make_token()`, `get_session()` |
|
|
||||||
| **Tragfähigkeit** | **hoch** (Single-App, Self-Hosted) |
|
|
||||||
| **Einschränkung** | Skalierung multi-node braucht shared session store; SSO-Familie braucht anderes Modell. |
|
|
||||||
|
|
||||||
### 3. profile_id aus Session, nicht aus Client
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Autoritative Identität für neue Endpoints: `session['profile_id']` — Client darf Profil nicht wählen. |
|
|
||||||
| **Begründung** | Verhindert IDOR (Zugriff auf fremde Profile). |
|
|
||||||
| **Quelle** | `routers/goals.py`, `routers/prompts.py`; Architektur-Intent in `CLAUDE.md` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Legacy `get_pid(x_profile_id)` akzeptiert `X-Profile-Id` **ohne** Session-Abgleich — siehe Nicht übernehmen. |
|
|
||||||
|
|
||||||
### 4. bcrypt mit Legacy-Migration
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Passwörter mit bcrypt; SHA256-Legacy beim Login erkannt und transparent auf bcrypt upgraded. |
|
|
||||||
| **Begründung** | Kein Big-Bang-Migration; sichere Hashes ohne Nutzer-Zwangs-Reset. |
|
|
||||||
| **Quelle** | `verify_pin()`, Login in `routers/auth.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Upgrade nur bei erfolgreichem Login. |
|
|
||||||
|
|
||||||
### 5. Rate Limiting auf Auth-Endpoints
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Login, Register, Forgot-Password, Resend-Verification mit `slowapi`-Limits (IP-basiert). |
|
|
||||||
| **Begründung** | Brute-Force- und Abuse-Schutz. |
|
|
||||||
| **Quelle** | `routers/auth.py` (`5/minute`, `3/hour`); `main.py` Limiter |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | IP-only; kein account-based lockout. |
|
|
||||||
|
|
||||||
### 6. Keine E-Mail-Enumeration bei sensiblen Flows
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Forgot-Password und Resend-Verification liefern generische Erfolgsmeldung, auch wenn E-Mail unbekannt. |
|
|
||||||
| **Begründung** | Privacy; erschwert Account-Scraping. |
|
|
||||||
| **Quelle** | `password_reset_request`, `resend_verification` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Register sagt „E-Mail bereits registriert“ (Enumeration möglich). |
|
|
||||||
|
|
||||||
### 7. E-Mail-Verifizierung vor voller Nutzung
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Self-Register setzt `email_verified=FALSE`; Verify-Endpoint aktiviert + Auto-Login-Session. |
|
|
||||||
| **Begründung** | Valide Kontaktadresse; Spam-Reduktion. |
|
|
||||||
| **Quelle** | `register`, `verify_email` in `routers/auth.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Nicht überall im Backend erzwungen (Login ohne verified check?). |
|
|
||||||
|
|
||||||
### 8. Flexible Auth für technische Clients
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `require_auth_flexible`: gleiche Session-Validierung via Header oder `?ssetoken=` für SSE/Bilder. |
|
|
||||||
| **Begründung** | Browser-APIs ohne Custom Headers. |
|
|
||||||
| **Quelle** | `auth.py`; Prompt SSE `/execute-stream` |
|
|
||||||
| **Tragfähigkeit** | **mittel–hoch** |
|
|
||||||
| **Einschränkung** | Token in URL kann in Logs/Referrer leaken — kurze Sessions / HTTPS Pflicht. |
|
|
||||||
|
|
||||||
### 9. Zentraler API-Client mit Token-Injektion
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Frontend: `api.js` injiziert `X-Auth-Token` automatisch — kein scattered `fetch` ohne Auth. |
|
|
||||||
| **Begründung** | Konsistenz; eine Stelle für Token-Handling. |
|
|
||||||
| **Quelle** | `utils/api.js` → `hdrs()`; `getToken()` aus AuthContext |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Einzelne Komponenten umgehen noch `api.js` (SettingsPage, EmailSettings). |
|
|
||||||
|
|
||||||
### 10. Auth getrennt von Authorization (Features)
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `require_auth` = identifiziert; `check_feature_access` = berechtigt für Aktion — nacheinander im Router. |
|
|
||||||
| **Begründung** | Klare Schichten; Auth-Modul nicht mit Tier-Logik vermischen (auch wenn Datei `auth.py` beides enthält). |
|
|
||||||
| **Quelle** | `FEATURE_ENTITLEMENT_DESIGN_PRINCIPLES.md`; Router-Muster |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Legacy Profil-Flags `ai_enabled`, `export_enabled` parallel zum Feature-System. |
|
|
||||||
|
|
||||||
### 11. Admin-Gate im Frontend und Backend
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Backend: `require_admin`; Frontend: `RequireAdmin` + `isAdmin` aus Session-Rolle. |
|
|
||||||
| **Begründung** | UX-Navigation + API-Sicherheit (Frontend allein reicht nicht). |
|
|
||||||
| **Quelle** | `RequireAdmin.jsx`; `require_admin()` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Einzelne Routen (Workflow-Editor) ohne Frontend-Admin-Gate. |
|
|
||||||
|
|
||||||
### 12. Session-Kontext im Frontend
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `AuthProvider` hält `{ token, profile_id, role, profile }`; App setzt `setProfileId(session.profile_id)` für API. |
|
|
||||||
| **Begründung** | Single React-Tree für Login-State; Re-Validate via `/auth/me` beim Start. |
|
|
||||||
| **Quelle** | `AuthContext.jsx`; `App.jsx` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | `ProfileContext` lädt alle Profile — Multi-Profil-UX Rest; Session-Profil ist Kanon. |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Nicht übernehmen
|
|
||||||
|
|
||||||
1. **`get_pid(X-Profile-Id)` ohne Session-Bindung** — Client kann fremde `profile_id` senden; IDOR-Risiko. Kanon: immer `session['profile_id']` oder explizite Admin-Impersonation mit Audit.
|
|
||||||
|
|
||||||
2. **Profile-CRUD nur mit `require_auth`** — `/profiles` listet alle Nutzer für jeden Authentifizierten (Kommentar „admin“, kein `require_admin`). Für Familien-Architektur: strikte Admin-Gates.
|
|
||||||
|
|
||||||
3. **Dual-System Profil-Flags vs. Features** — `ai_enabled`, `export_enabled`, `ai_limit_day` in Session-Query neben v9c Feature-Registry.
|
|
||||||
|
|
||||||
4. **localStorage-Key-Inkonsistenz** — `bodytrack_token` vs. `mitai-jinkendo_active_profile` (historischer App-Name).
|
|
||||||
|
|
||||||
5. **Direktes `fetch` ohne `api.js`** — umgeht Token-/Error-Konvention.
|
|
||||||
|
|
||||||
6. **Reset-Token in `sessions`-Tabelle** — `reset_{token}` mischt Session-Typen in einer Tabelle; OK für MVP, für Familie: getrennte Token-Typen/Tabellen.
|
|
||||||
|
|
||||||
7. **Kein JWT/SSO trotz Produktfamilien-Vision** — `CENTRAL_SUBSCRIPTION_SYSTEM.md` beschreibt `auth.jinkendo.de` — Mitai-Implementierung ist **nicht** das Zielbild für Cross-App-SSO.
|
|
||||||
|
|
||||||
8. **Multi-Profil-Haushalt ohne klares Modell** — Legacy Multi-Profile auf einer Instanz vs. 1 Account = 1 Profil; für neue Apps Modell explizit wählen.
|
|
||||||
|
|
||||||
9. **Role als einziges RBAC** — reicht für Admin/User, nicht für feingranulare Permissions.
|
|
||||||
|
|
||||||
10. **Session-Query mit veralteten Profil-Spalten** — `get_session` SELECT enthält Legacy-Felder statt nur Identität + Rolle.
|
|
||||||
|
|
||||||
11. **Fehlende erzwungene E-Mail-Verified-Prüfung** — Registrierung setzt Flag, Login prüft es nicht offensichtlich.
|
|
||||||
|
|
||||||
12. **Debug-Print in Auth-Modul** — `print("[AUTH.PY] Module loaded…")` in Produktionscode.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Abgrenzung zu anderen Serien-Dokumenten
|
|
||||||
|
|
||||||
| Thema | Dokument |
|
|
||||||
|-------|----------|
|
|
||||||
| Tier, Limits, Quotas | [FEATURE_ENTITLEMENT_DESIGN_PRINCIPLES.md](./FEATURE_ENTITLEMENT_DESIGN_PRINCIPLES.md) |
|
|
||||||
| Zentrale SSO/Abo-Vision | [CENTRAL_SUBSCRIPTION_SYSTEM.md](../../technical/CENTRAL_SUBSCRIPTION_SYSTEM.md) |
|
|
||||||
| API-First / Router | `ARCHITECTURE.md` §1 |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Modul-Inventar (Ist-Stand)
|
|
||||||
|
|
||||||
```
|
|
||||||
backend/
|
|
||||||
├── auth.py # Session, require_*, Feature-Access (v9c)
|
|
||||||
└── routers/
|
|
||||||
├── auth.py # login, logout, register, verify, reset
|
|
||||||
└── profiles.py # CRUD, get_pid (Legacy)
|
|
||||||
|
|
||||||
frontend/src/
|
|
||||||
├── context/AuthContext.jsx
|
|
||||||
├── context/ProfileContext.jsx
|
|
||||||
├── layouts/RequireAdmin.jsx
|
|
||||||
└── utils/api.js # Token-Injektion
|
|
||||||
|
|
||||||
DB:
|
|
||||||
├── profiles # Identität, Rolle, Hash, Tier, Trial
|
|
||||||
└── sessions # token → profile_id, expires_at
|
|
||||||
```
|
|
||||||
|
|
||||||
**Endpoints (Auswahl):**
|
|
||||||
|
|
||||||
| Endpoint | Auth |
|
|
||||||
|----------|------|
|
|
||||||
| `POST /api/auth/login` | Public + Rate limit |
|
|
||||||
| `POST /api/auth/logout` | Token optional |
|
|
||||||
| `GET /api/auth/me` | require_auth |
|
|
||||||
| `POST /api/auth/register` | Public + Rate limit |
|
|
||||||
| `GET /api/auth/verify/{token}` | Public |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Verwandte Dokumentation
|
|
||||||
|
|
||||||
- Feature-Entitlements: [FEATURE_ENTITLEMENT_DESIGN_PRINCIPLES.md](./FEATURE_ENTITLEMENT_DESIGN_PRINCIPLES.md)
|
|
||||||
- Architektur-Regeln Auth: `CLAUDE.md`, `.claude/rules/ARCHITECTURE.md`
|
|
||||||
- GUI Admin-Guard: `docs/issues/GUI_IA_ADMIN_NAV_2026-04-05.md`
|
|
||||||
- SSO-Vision: [CENTRAL_SUBSCRIPTION_SYSTEM.md](../../technical/CENTRAL_SUBSCRIPTION_SYSTEM.md)
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Geplante Folgedokumente (Serie)
|
|
||||||
|
|
||||||
| # | Modul | Status |
|
|
||||||
|---|-------|--------|
|
|
||||||
| 1 | Prompt Engine | ✅ |
|
|
||||||
| 2 | Data Layer | ✅ |
|
|
||||||
| 3 | Feature & Entitlement | ✅ |
|
|
||||||
| 4 | Registry-/Plugin-Muster | ✅ |
|
|
||||||
| 5 | Auth & Session | ✅ dieses Dokument |
|
|
||||||
| 6 | Universal Import | ✅ `UNIVERSAL_IMPORT_DESIGN_PRINCIPLES.md` |
|
|
||||||
| 7 | Dashboard Widgets | ✅ |
|
|
||||||
| 8 | Navigation / IA | ✅ `NAVIGATION_IA_DESIGN_PRINCIPLES.md` |
|
|
||||||
| 9 | Migration & Deploy | ✅ |
|
|
||||||
-363
@@ -1,363 +0,0 @@
|
|||||||
# Dashboard Widgets – Designprinzipien (Extraktion)
|
|
||||||
|
|
||||||
**Status:** Analyse / Arbeitspapier
|
|
||||||
**Stand:** 2026-07-04
|
|
||||||
**Geltungsbereich:** Konfigurierbare Übersicht (Widget-Katalog, Layout, Entitlements, Frontend-Registry) — keine Chart-/Metrik-Berechnung
|
|
||||||
|
|
||||||
**Serie:** Designprinzipien für Produktfamilie · Dokument 7 von n
|
|
||||||
**Vorgänger:** [UNIVERSAL_IMPORT_DESIGN_PRINCIPLES.md](./UNIVERSAL_IMPORT_DESIGN_PRINCIPLES.md)
|
|
||||||
|
|
||||||
**Kernkomponenten:**
|
|
||||||
|
|
||||||
| Bereich | Pfade |
|
|
||||||
|---------|-------|
|
|
||||||
| Katalog (SSoT) | `backend/widget_catalog.py` |
|
|
||||||
| Layout-Schema | `backend/dashboard_layout_schema.py` |
|
|
||||||
| Config-Validierung | `backend/dashboard_widget_config.py` |
|
|
||||||
| Entitlements | `backend/dashboard_widget_entitlements.py`, `widget_feature_requirements_db.py` |
|
|
||||||
| Produkt-Standard | `backend/system_dashboard_product_default.py` |
|
|
||||||
| HTTP | `backend/routers/app_dashboard.py` |
|
|
||||||
| Frontend-Registry | `frontend/src/widgetSystem/dashboardWidgetRegistry.jsx` |
|
|
||||||
| Registrierung | `frontend/src/widgetSystem/registerDashboardWidgets.js` |
|
|
||||||
| Layout-Editor | `frontend/src/pages/DashboardConfigurePage.jsx` |
|
|
||||||
| Fehler-Isolation | `frontend/src/widgetSystem/WidgetErrorBoundary.jsx` |
|
|
||||||
| Leitfaden | `DASHBOARD_WIDGETS_AGENT_GUIDE.md` |
|
|
||||||
| Registry-Meta | [REGISTRY_PLUGIN_DESIGN_PRINCIPLES.md](./REGISTRY_PLUGIN_DESIGN_PRINCIPLES.md) |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Modul
|
|
||||||
|
|
||||||
**Dashboard Widgets**
|
|
||||||
|
|
||||||
Erweiterbares System für **konfigurierbare Startübersicht**: Backend-Katalog definiert erlaubte Widget-IDs; Nutzer speichern Reihenfolge, Ein/Aus und optionale `config` pro Profil; Frontend rendert über eine lokale Komponenten-Registry.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Fachliche Verantwortung
|
|
||||||
|
|
||||||
Das Modul übernimmt:
|
|
||||||
|
|
||||||
1. **Widget-Katalog** — IDs, Titel, Beschreibung, optionale Feature-Anforderung (`requires_feature`).
|
|
||||||
2. **Layout-Persistenz** — `profiles.dashboard_layout` (JSON v1: `{ version, widgets[] }`).
|
|
||||||
3. **Validierung** — Erlaubte IDs, keine Duplikate, max. 32 Widgets, mindestens eines aktiv.
|
|
||||||
4. **Pro-Widget-Config** — Whitelist pro Widget-ID; Normalisierung beim Speichern.
|
|
||||||
5. **Standard-Layouts** — Code-Fallback (`DEFAULT_PRODUCT_DASHBOARD_WIDGET_IDS`), Admin-Override (`system_config`), Lab-Template (`DEFAULT_LAB_WIDGET_IDS`).
|
|
||||||
6. **Entitlements** — `allowed` im Katalog; Layout bereinigt bei fehlender Berechtigung.
|
|
||||||
7. **Frontend-Rendering** — Registry mappt Katalog-ID → React-Komponente + Props aus `layoutEntry.config`.
|
|
||||||
8. **Nutzer-Konfigurator** — „Übersicht anpassen“ (Sortieren, Toggle, Config-Editoren).
|
|
||||||
|
|
||||||
Es übernimmt **nicht**:
|
|
||||||
|
|
||||||
- Berechnung von KPIs, Charts, Scores (→ Data Layer + Chart-Endpoints, siehe [DATA_LAYER_DESIGN_PRINCIPLES.md](./DATA_LAYER_DESIGN_PRINCIPLES.md))
|
|
||||||
- Tier-/Subscription-Logik in Widgets (→ Feature System, siehe [FEATURE_ENTITLEMENT_DESIGN_PRINCIPLES.md](./FEATURE_ENTITLEMENT_DESIGN_PRINCIPLES.md))
|
|
||||||
- Prompt-/KI-Ausführung (Widget zeigt nur UI; Pipeline läuft über eigene API)
|
|
||||||
|
|
||||||
### Datenfluss (Happy Path)
|
|
||||||
|
|
||||||
```
|
|
||||||
WIDGET_CATALOG (Backend)
|
|
||||||
→ GET /api/app/widgets/catalog (+ allowed via check_feature_access)
|
|
||||||
→ GET /api/app/dashboard-layout
|
|
||||||
→ coalesce_effective_layout (Profil oder Standard)
|
|
||||||
→ merge_missing_catalog_widgets (neue IDs anhängen)
|
|
||||||
→ apply_entitlements_to_layout_dict
|
|
||||||
→ Frontend: ensureDashboardWidgetsRegistered()
|
|
||||||
→ WidgetRenderer: enabled widgets → mapProps(layoutEntry.config) → Component
|
|
||||||
→ PUT /api/app/dashboard-layout (Pydantic + Entitlements + speichern)
|
|
||||||
```
|
|
||||||
|
|
||||||
### Layout-Eintrag (Struktur)
|
|
||||||
|
|
||||||
| Feld | Bedeutung |
|
|
||||||
|------|-----------|
|
|
||||||
| `id` | Muss in `WIDGET_CATALOG` existieren |
|
|
||||||
| `enabled` | Sichtbar auf der Übersicht |
|
|
||||||
| `config` | Optional; nur für whitelisted Widgets mit Inhalt erlaubt |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Administrierte vs. code-definierte Konfiguration
|
|
||||||
|
|
||||||
| Konfiguration | Speicherort | Wer pflegt? |
|
|
||||||
|---------------|-------------|-------------|
|
|
||||||
| Widget-IDs, Metadaten, Default-Aktivierung | `widget_catalog.py` | Entwickler |
|
|
||||||
| Produkt-Standard-Layout (live) | `system_config.dashboard_product_default` | Admin |
|
|
||||||
| Produkt-Standard (Fallback) | `DEFAULT_PRODUCT_DASHBOARD_WIDGET_IDS` | Entwickler |
|
|
||||||
| Lab-/Editor-Standard | `DEFAULT_LAB_WIDGET_IDS` | Entwickler |
|
|
||||||
| Nutzer-Layout | `profiles.dashboard_layout` | Nutzer |
|
|
||||||
| Feature-Gate (Katalog) | `requires_feature` pro Eintrag | Entwickler |
|
|
||||||
| Feature-Gate (Override) | `widget_feature_requirements` + Marker | Admin |
|
|
||||||
| Config-Schema pro Widget | `dashboard_widget_config.py` | Entwickler |
|
|
||||||
| React-Komponente | `registerDashboardWidgets.js` | Entwickler |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Designprinzipien
|
|
||||||
|
|
||||||
### 1. Backend-Katalog als Single Source of Truth für IDs
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `WIDGET_CATALOG` ist die einzige autoritative Liste erlaubter Widget-IDs; `ALLOWED_WIDGET_IDS` wird daraus abgeleitet — nicht manuell duplizieren. |
|
|
||||||
| **Begründung** | Layout-Validator, API und Default-Layouts bleiben synchron; unbekannte IDs werden beim PUT abgewiesen. |
|
|
||||||
| **Quelle** | `widget_catalog.py`; Agent-Guide §4 |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Frontend-Registry ist zweite manuelle Bindung (kein Build-Time-Gate). |
|
|
||||||
|
|
||||||
### 2. Dual Registry: Backend-Kanon + Frontend-Komponentenbindung
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Jede Katalog-ID braucht einen Eintrag in `registerDashboardWidget({ id, Component, mapProps })`; idempotent via `ensureDashboardWidgetsRegistered()`. |
|
|
||||||
| **Begründung** | React-Komponenten können nicht im Python-Katalog leben; explizite Zuordnung hält Bundle tree-shakeable. |
|
|
||||||
| **Quelle** | `registerDashboardWidgets.js`, `dashboardWidgetRegistry.jsx` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Fehlende Registrierung → Laufzeit „Unbekanntes Widget“, kein CI-Fail. |
|
|
||||||
|
|
||||||
### 3. Layout als versioniertes Profil-JSON
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Nutzer-Layout in `profiles.dashboard_layout`; Schema `version: 1`, Liste `{ id, enabled, config? }`. |
|
|
||||||
| **Begründung** | Pro Profil anpassbar; Reset auf NULL → System-Standard. |
|
|
||||||
| **Quelle** | `DashboardLayoutPayload`, `app_dashboard.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Nur v1; Schema-Evolution braucht Migrationspfad. |
|
|
||||||
|
|
||||||
### 4. Validierung an der API-Grenze (Pydantic)
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Jeder GET/PUT-Pfad normalisiert über `DashboardLayoutPayload`: Duplikat-IDs, unbekannte IDs, leeres Layout (kein enabled) → Fehler. |
|
|
||||||
| **Begründung** | Keine korrupten Layouts in der DB; Frontend kann auf gültige Struktur vertrauen. |
|
|
||||||
| **Quelle** | `dashboard_layout_schema.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Ungültiges gespeichertes Layout → Fallback auf Standard (`coalesce_effective_layout`). |
|
|
||||||
|
|
||||||
### 5. Config nur für explizit whitelisted Widgets
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `WIDGETS_ALLOWING_CONFIG`: Widgets **ohne** Eintrag dürfen nur leere `config` haben; sonst Validierungsfehler. |
|
|
||||||
| **Begründung** | Verhindert unkontrollierte JSON-Blobs und stille Ignorierung unbekannter Keys. |
|
|
||||||
| **Quelle** | `dashboard_widget_config.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Pro Widget heterogene Schemas (chart_days vs. KPI-Tiles vs. show_*-Booleans). |
|
|
||||||
|
|
||||||
### 6. Strikte Config-Keys (Whitelist, Normalisierung)
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Unbekannte Keys in `config` werden abgelehnt; bekannte Keys typgeprüft und normalisiert (z. B. `chart_days` 7–90, KPI max. 9 Kacheln). |
|
|
||||||
| **Begründung** | Vorhersagbares Verhalten; Editor und Backend stimmen überein. |
|
|
||||||
| **Quelle** | `_validate_chart_days_only`, `_validate_kpi_board_config`, History-Viz-Defaults |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Frontend-Normalizer (`bodyChartDays.js`, `*VizConfig.js`) teils parallel — Abweichungsrisiko. |
|
|
||||||
|
|
||||||
### 7. Config-Größenlimit
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `MAX_WIDGET_CONFIG_JSON_BYTES` (3072) — keine großen Blobs in Layout-JSON. |
|
|
||||||
| **Begründung** | DB-Spalte und API-Payload bleiben schlank; Config = Präferenzen, nicht Datenspeicher. |
|
|
||||||
| **Quelle** | `dashboard_widget_config.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | — |
|
|
||||||
|
|
||||||
### 8. Katalog-Erweiterung ohne Layout-Reset
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `merge_missing_catalog_widgets` hängt neue Katalog-IDs ans bestehende Layout an (`enabled: false`). |
|
|
||||||
| **Begründung** | Nutzer müssen nach Deploy nicht resetten; „Übersicht anpassen“ zeigt neue Optionen. |
|
|
||||||
| **Quelle** | `dashboard_layout_schema.py`; Agent-Guide |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Reihenfolge neuer Widgets immer am Ende. |
|
|
||||||
|
|
||||||
### 9. Mehrere Standard-Layouts (Produkt vs. Lab vs. Admin)
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | **Produkt:** `get_product_default_base_dict` (DB-Override oder `DEFAULT_PRODUCT_DASHBOARD_WIDGET_IDS`). **Lab:** `lab_default_layout_dict` für Editor/Reset. **Nutzer:** eigenes JSON oder NULL. |
|
|
||||||
| **Begründung** | Onboarding-Default getrennt von Entwickler-/Lab-Template; Admin kann Produkt-Standard ohne Deploy ändern. |
|
|
||||||
| **Quelle** | `system_dashboard_product_default.py`, `widget_catalog.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Feldname `lab_default_layout` historisch irreführend (Servertemplate, nicht nur Lab). |
|
|
||||||
|
|
||||||
### 10. Entitlements zentral, Widgets konsumieren nur `allowed`
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Sichtbarkeit über `check_feature_access` in `widget_id_allowed`; Katalog liefert `allowed` pro Zeile. Widgets/React duplizieren **keine** Tier-Logik. |
|
|
||||||
| **Begründung** | Eine Wahrheit für „darf angezeigt werden“; spätere Feature-Cluster ohne Widget-Refactor. |
|
|
||||||
| **Quelle** | `dashboard_widget_entitlements.py`; Agent-Guide §0 |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Inhalts-Endpoints (Charts, KI) brauchen **eigenes** Feature-Gate (Defense in Depth). |
|
|
||||||
|
|
||||||
### 11. Layout-Persistenz bereinigt nicht erlaubte Widgets
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `apply_entitlements_to_layout_dict`: bei fehlender Berechtigung `enabled: false`; mindestens `welcome` bleibt aktiv. GET und PUT wenden an. |
|
|
||||||
| **Begründung** | Keine „gespeichert aber nie sichtbar“-Zombies; Downgrade/Tier-Wechsel degradieren gracefully. |
|
|
||||||
| **Quelle** | `dashboard_widget_entitlements.py`, `app_dashboard.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Policy ist deaktivieren, nicht entfernen — IDs bleiben im JSON. |
|
|
||||||
|
|
||||||
### 12. DB-Override für Widget-Feature-Anforderungen
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Katalog-`requires_feature` ist Default; Admin kann per `dashboard_widget_requirement_custom` + `widget_feature_requirements` überschreiben (AND-Semantik). |
|
|
||||||
| **Begründung** | Runtime-Anpassung ohne Code-Deploy; Marker-Zeile trennt Custom von Fallback. |
|
|
||||||
| **Quelle** | `widget_feature_requirements_db.py`, Migration 041 |
|
|
||||||
| **Tragfähigkeit** | **mittel–hoch** |
|
|
||||||
| **Einschränkung** | Zwei Quellen (Code + DB) — Dokumentation und Admin-UI nötig. |
|
|
||||||
|
|
||||||
### 13. mapProps: Layout-Config → Komponenten-Props
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Registry-Eintrag mappt `ctx.layoutEntry.config` auf typisierte Props (`chartDays`, `kpiConfig`, `bodyHistoryVizConfig`, …). |
|
|
||||||
| **Begründung** | Widget-Komponenten bleiben layout-agnostisch; Normalisierung an einer Stelle pro ID. |
|
|
||||||
| **Quelle** | `registerDashboardWidgets.js` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Teilweise Normalisierung in Widget statt in `mapProps` (inkonsistent, aber dokumentiert). |
|
|
||||||
|
|
||||||
### 14. Refresh-Koordination über Context
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `refreshTick` + `requestRefresh()` im Render-Context; Widgets laden Daten bei Tick-Änderung neu; Aktionen (z. B. Schnelleingabe) rufen `requestRefresh`. |
|
|
||||||
| **Begründung** | Kein globales State-Monster; gezielte Invalidierung nach Capture. |
|
|
||||||
| **Quelle** | `dashboardWidgetRegistry.jsx`, Widget-Implementierungen |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Kein feingranulares Cache pro Widget. |
|
|
||||||
|
|
||||||
### 15. Fehler-Isolation pro Widget
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `WidgetErrorBoundary` um jede Instanz — Render-Fehler crashen nicht die ganze Übersicht. |
|
|
||||||
| **Begründung** | Robuste PWA; ein defektes Chart blockiert nicht Gewicht-Eingabe. |
|
|
||||||
| **Quelle** | `WidgetErrorBoundary.jsx` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Kein automatisches Retry/Reporting. |
|
|
||||||
|
|
||||||
### 16. Konfigurator filtert nach `allowed`
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `DashboardConfigurePage` blendet Widgets mit `allowed === false` aus der bearbeitbaren Liste aus. |
|
|
||||||
| **Begründung** | Nutzer sehen keine Optionen, die sie nicht nutzen dürfen (Agent-Guide A2). |
|
|
||||||
| **Quelle** | `DashboardConfigurePage.jsx` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Bereits gespeicherte disabled Einträge können im JSON verbleiben. |
|
|
||||||
|
|
||||||
### 17. Widgets konsumieren Data Layer, duplizieren keine Logik
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Chart-/KPI-Widgets rufen Chart-Endpoints bzw. API-Fassaden auf; Berechnungen leben in `data_layer/`, nicht in Widget-JS. |
|
|
||||||
| **Begründung** | Gleiche Zahlen wie Verlauf, KI-Platzhalter und Export. |
|
|
||||||
| **Quelle** | Layer-2b `*_history_viz`-Widgets; [DATA_LAYER_DESIGN_PRINCIPLES.md](./DATA_LAYER_DESIGN_PRINCIPLES.md) |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Legacy-Widgets unter `dashboard-widgets-legacy/` teils ältere Fetch-Pfade. |
|
|
||||||
|
|
||||||
### 18. Dedizierte Config-Editoren für komplexe Widgets
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Einfache `chart_days`: Set `CHART_DAYS_WIDGET_IDS` im Layout-Editor; komplexe Config: eigene Editor-Komponenten (`KpiBoardConfigEditor`, `*VizConfigEditor`). |
|
|
||||||
| **Begründung** | UX skaliert mit Config-Komplexität; Backend-Schema und Editor bleiben parallel pflegbar. |
|
|
||||||
| **Quelle** | `widgetSystem/*ConfigEditor.jsx`, Agent-Guide §3.4 |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Jedes neue komplexe Widget = Editor + Validator + Tests. |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Nicht übernehmen
|
|
||||||
|
|
||||||
1. **Tier-Logik in React-Widgets** — nur `allowed` aus API; keine hardcodierten Plan-Namen.
|
|
||||||
|
|
||||||
2. **`ALLOWED_WIDGET_IDS` manuell pflegen** — immer aus Katalog ableiten.
|
|
||||||
|
|
||||||
3. **Config ohne Backend-Whitelist** — stille Ignorierung unbekannter Keys in Widgets.
|
|
||||||
|
|
||||||
4. **Nur UI-Gating ohne API-Absicherung** — Chart-/KI-/Export-Endpoints weiterhin `check_feature_access` (403).
|
|
||||||
|
|
||||||
5. **Frontend-Registry vergessen** — Katalog-Eintrag ohne `registerDashboardWidget` → Laufzeitfehler statt Build-Fail.
|
|
||||||
|
|
||||||
6. **Große Daten in `config`** — Layout ist Präferenzspeicher, kein Blob-Store (>3072 Bytes).
|
|
||||||
|
|
||||||
7. **Doppelte Widget-IDs im Layout** — Validator verbietet; Editor muss dasselbe erzwingen.
|
|
||||||
|
|
||||||
8. **Neue Katalog-IDs ohne `merge_missing_catalog_widgets`-Pfad** — Nutzer-Layouts veralten unsichtbar.
|
|
||||||
|
|
||||||
9. **Kompletter Katalog nur in DB** — schwer testbar; Code-Katalog + optionale Feature-Overrides ist das Muster.
|
|
||||||
|
|
||||||
10. **Evidence-Pflicht à la Placeholder-Registry** — 22 Metadatenfelder pro Widget wären Overkill; Tiefe an Risiko anpassen ([REGISTRY_PLUGIN_DESIGN_PRINCIPLES.md](./REGISTRY_PLUGIN_DESIGN_PRINCIPLES.md)).
|
|
||||||
|
|
||||||
11. **Ein Default für alles** — Produkt-Onboarding, Lab-Template und Admin-Override haben unterschiedliche Zwecke; nicht vermischen.
|
|
||||||
|
|
||||||
12. **Fehlender Cross-Check Backend ↔ Frontend IDs** — empfohener Test/Gate fehlt im Ist-Stand; nicht als „optional“ ignorieren.
|
|
||||||
|
|
||||||
13. **Berechnungslogik im Widget** — KPIs/Scores gehören in Data Layer, nicht in `useEffect`-Mathe.
|
|
||||||
|
|
||||||
14. **Entitlements beim Speichern ablehnen statt deaktivieren** — Mitai wählt deaktivieren; Policy bewusst festlegen und dokumentieren.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Modul-Inventar (Ist-Stand)
|
|
||||||
|
|
||||||
```
|
|
||||||
backend/
|
|
||||||
├── widget_catalog.py # WIDGET_CATALOG, DEFAULT_*_IDS
|
|
||||||
├── dashboard_layout_schema.py # Pydantic, merge_missing, defaults
|
|
||||||
├── dashboard_widget_config.py # WIDGETS_ALLOWING_CONFIG, Validatoren
|
|
||||||
├── dashboard_widget_entitlements.py # allowed, layout cleanup
|
|
||||||
├── widget_feature_requirements_db.py # Admin-Override
|
|
||||||
├── system_dashboard_product_default.py
|
|
||||||
└── routers/app_dashboard.py
|
|
||||||
|
|
||||||
frontend/src/
|
|
||||||
├── widgetSystem/
|
|
||||||
│ ├── dashboardWidgetRegistry.jsx
|
|
||||||
│ ├── registerDashboardWidgets.js
|
|
||||||
│ ├── layoutEditor.js
|
|
||||||
│ ├── bodyChartDays.js, *VizConfig.js
|
|
||||||
│ └── *ConfigEditor.jsx
|
|
||||||
├── components/dashboard-widgets/ # Produkt-Widgets
|
|
||||||
├── components/dashboard-widgets-legacy/ # ältere Kern-Widgets
|
|
||||||
└── pages/DashboardConfigurePage.jsx
|
|
||||||
|
|
||||||
DB:
|
|
||||||
├── profiles.dashboard_layout
|
|
||||||
├── system_config.dashboard_product_default
|
|
||||||
├── dashboard_widget_requirement_custom
|
|
||||||
└── widget_feature_requirements
|
|
||||||
```
|
|
||||||
|
|
||||||
**Katalog-Umfang:** ~24 Widget-IDs (Stand `widget_catalog.py`); ~13 mit konfigurierbarer `config`.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Verwandte Dokumentation
|
|
||||||
|
|
||||||
- Agent-Guide (normativ): [DASHBOARD_WIDGETS_AGENT_GUIDE.md](../../technical/DASHBOARD_WIDGETS_AGENT_GUIDE.md)
|
|
||||||
- Registry-Meta: [REGISTRY_PLUGIN_DESIGN_PRINCIPLES.md](./REGISTRY_PLUGIN_DESIGN_PRINCIPLES.md)
|
|
||||||
- Feature-Gates: [FEATURE_ENTITLEMENT_DESIGN_PRINCIPLES.md](./FEATURE_ENTITLEMENT_DESIGN_PRINCIPLES.md)
|
|
||||||
- Datenberechnung: [DATA_LAYER_DESIGN_PRINCIPLES.md](./DATA_LAYER_DESIGN_PRINCIPLES.md)
|
|
||||||
- Architektur §9: `.claude/rules/ARCHITECTURE.md`
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Geplante Folgedokumente (Serie)
|
|
||||||
|
|
||||||
| # | Modul | Status |
|
|
||||||
|---|-------|--------|
|
|
||||||
| 1–6 | … | ✅ |
|
|
||||||
| 7 | Dashboard Widgets | ✅ dieses Dokument |
|
|
||||||
| 8 | Navigation / IA | ✅ |
|
|
||||||
| 9 | Migration & Deploy | ✅ |
|
|
||||||
@@ -1,359 +0,0 @@
|
|||||||
# Data Layer – Designprinzipien (Extraktion)
|
|
||||||
|
|
||||||
**Status:** Analyse / Arbeitspapier
|
|
||||||
**Stand:** 2026-07-04
|
|
||||||
**Geltungsbereich:** Multi-Layer Data Architecture (Phase 0c, Issue #53) — keine Mitai-Gesamtarchitektur, keine konkrete Gesundheits-/Ernährungsfachlogik als Produktinhalt
|
|
||||||
|
|
||||||
**Serie:** Designprinzipien für Produktfamilie · Dokument 2 von n
|
|
||||||
**Vorgänger:** [PROMPT_ENGINE_DESIGN_PRINCIPLES.md](./PROMPT_ENGINE_DESIGN_PRINCIPLES.md)
|
|
||||||
|
|
||||||
**Kernkomponenten:**
|
|
||||||
|
|
||||||
| Bereich | Pfade |
|
|
||||||
|---------|-------|
|
|
||||||
| Metriken (Layer 1) | `backend/data_layer/*_metrics.py`, `scores.py`, `correlations.py` |
|
|
||||||
| Utilities | `backend/data_layer/utils.py` |
|
|
||||||
| Visualisierung (Layer 2b) | `*_chart_payloads.py`, `*_viz.py` |
|
|
||||||
| KI-Formatierung (Layer 2a-Hilfe) | `prompt_output_compact.py` |
|
|
||||||
| Persistenz-Orchestrierung | `activity_persistence_orchestrator.py`, `activity_session_metrics.py` |
|
|
||||||
| Konsumenten | `routers/charts.py`, `placeholder_resolver.py`, `routers/exportdata.py` |
|
|
||||||
| Leitfäden | `DATA_LAYER_EXTENSION_GUIDE.md`, `docs/issues/issue-53-phase-0c-multi-layer-architecture.md` |
|
|
||||||
| Architektur-Regel Import-Grenze | `.claude/rules/ARCHITECTURE.md` §8 |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Modul
|
|
||||||
|
|
||||||
**Data Layer** (Phase 0c Multi-Layer Architecture, Issue #53)
|
|
||||||
|
|
||||||
Zentrale Schicht für **Datenabruf, Berechnung und strukturierte Aufbereitung** — ohne UI-Formatierung, ohne Prompt-Texte, ohne Chart.js-spezifische Ausgabe in den Kern-Metrik-Modulen.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Fachliche Verantwortung
|
|
||||||
|
|
||||||
Der Data Layer ist die **Single Source of Truth für alle abgeleiteten Messwerte und Metriken**. Er übernimmt:
|
|
||||||
|
|
||||||
1. **Datenabruf** — Lesen aus PostgreSQL (profile-scoped), optional mit Quality-Filter.
|
|
||||||
2. **Berechnung** — Trends, Scores, Korrelationen, Aggregationen, Projektionen.
|
|
||||||
3. **Strukturierte Rückgabe** — Dicts/Listen mit numerischen Werten, Datumsfeldern, Metadaten (`confidence`, `data_points`).
|
|
||||||
4. **Konsumenten-Bereitstellung** — Charts (Layer 2b), KI-Platzhalter (Layer 2a via Resolver), Export, Router-Anreicherung.
|
|
||||||
|
|
||||||
Er übernimmt **nicht**:
|
|
||||||
|
|
||||||
- CSV-Parsing und Feld-Mapping (Import-Schicht)
|
|
||||||
- Prompt-Template-Auflösung (Prompt Engine)
|
|
||||||
- React-Rendering oder Frontend-Berechnungen
|
|
||||||
- Autorisierung / Feature-Limits (Auth-Schicht)
|
|
||||||
|
|
||||||
### Schichtenmodell (Multi-Layer)
|
|
||||||
|
|
||||||
```
|
|
||||||
┌─────────────────────────────────────────────────────────┐
|
|
||||||
│ Layer 0: Persistenz (PostgreSQL) │
|
|
||||||
│ weight_log, nutrition_log, activity_log, sleep_log, … │
|
|
||||||
└──────────────────────────┬──────────────────────────────┘
|
|
||||||
│
|
|
||||||
┌──────────────────────────▼──────────────────────────────┐
|
|
||||||
│ Layer 1: DATA LAYER (Metriken) │
|
|
||||||
│ Strukturierte Daten · confidence · data_points │
|
|
||||||
│ KEINE formatierten Strings · KEINE Chart.js-Objekte │
|
|
||||||
└──────────────┬───────────────────────┬──────────────────┘
|
|
||||||
│ │
|
|
||||||
▼ ▼
|
|
||||||
┌──────────────────────────┐ ┌────────────────────────────┐
|
|
||||||
│ Layer 2a: KI / Prompts │ │ Layer 2b: Visualisierung │
|
|
||||||
│ placeholder_resolver │ │ *_chart_payloads, *_viz │
|
|
||||||
│ prompt_output_compact │ │ routers/charts.py │
|
|
||||||
└──────────────────────────┘ └────────────────────────────┘
|
|
||||||
```
|
|
||||||
|
|
||||||
### Administrierte vs. code-definierte Konfiguration
|
|
||||||
|
|
||||||
| Was | Wo | Administrierbar? |
|
|
||||||
|-----|-----|------------------|
|
|
||||||
| Berechnungslogik (Formeln, Fenster) | `data_layer/*.py` | ❌ Code + Review |
|
|
||||||
| Confidence-Schwellen | `data_layer/utils.py` | ❌ Code |
|
|
||||||
| Goal Mode / Focus Weights | DB (`profiles`, `user_focus_area_weights`) | ✅ Nutzer/Admin |
|
|
||||||
| Quality Filter (Profil) | DB (`profiles`) | ✅ Admin |
|
|
||||||
| Chart-Zeitfenster | Query-Parameter an API | ✅ Request |
|
|
||||||
| Referenzwerte (persönlich) | DB + `reference_values.py` | ✅ Nutzer |
|
|
||||||
| EAV Session Metrics | DB (`training_*_parameter`) | ✅ Admin |
|
|
||||||
|
|
||||||
**Bewusst nicht hardcodiert in Routern:** Metrik-Berechnungen — Router delegieren an Data Layer.
|
|
||||||
|
|
||||||
**Hardcodiert (Code):** Domänen-Module, Confidence-Regeln, Schwellen pro Metrik-Typ, TDEE-Fallback-Logik, Chart-Payload-Struktur.
|
|
||||||
|
|
||||||
### Trennung: Metriken · Chart-Payloads · KI-Formatierung · Persistenz
|
|
||||||
|
|
||||||
| Schicht | Module | Verantwortung |
|
|
||||||
|---------|--------|---------------|
|
|
||||||
| **Metriken** | `body_metrics.py`, `nutrition_metrics.py`, … | Reine Berechnung, strukturierte Dicts |
|
|
||||||
| **Chart-Payloads** | `nutrition_chart_payloads.py`, `correlation_chart_payloads.py`, … | Chart.js-kompatible `{ labels, datasets, metadata }` aus Layer-1-Daten |
|
|
||||||
| **Viz-Bundles** | `body_viz.py`, `fitness_viz.py`, … | Zusammengesetzte Dashboard-/History-Pakete für Frontend |
|
|
||||||
| **KI-Kompaktierung** | `prompt_output_compact.py` | Token-sparende Zahlen/JSON für Platzhalter |
|
|
||||||
| **Interpretation** | `*_interpretation.py`, `vital_signs_assessment.py` | Textliche Einordnung (WHO-Klassen etc.) — Grenze zu Layer 2a |
|
|
||||||
| **Persistenz-Orchestrator** | `activity_persistence_orchestrator.py` | Schreibpfade REST/CSV → DB + Nebenwirkungen (EAV, Eval) |
|
|
||||||
|
|
||||||
### Konsumenten (wer ruft den Data Layer auf?)
|
|
||||||
|
|
||||||
| Konsument | Muster |
|
|
||||||
|-----------|--------|
|
|
||||||
| `routers/charts.py` | Layer-1-Funktion + Chart-Payload-Builder |
|
|
||||||
| `placeholder_resolver.py` | Layer-1 → Formatierung/JSON für `{{placeholders}}` |
|
|
||||||
| `routers/exportdata.py` | `enrich_sessions_with_metrics`, `serialize_dates` |
|
|
||||||
| `routers/activity.py`, `csv_import.py` | `activity_persistence_orchestrator` (Schreiben) |
|
|
||||||
| `prompt_executor.execute_prompt_with_data` | ⚠️ teils Roh-SQL parallel zum Data Layer (Legacy) |
|
|
||||||
|
|
||||||
### Rollen
|
|
||||||
|
|
||||||
Der Data Layer hat **keine eigene Admin-UI**. Konfiguration erfolgt indirekt:
|
|
||||||
|
|
||||||
- **Admin:** Training-Parameter, Attributprofile, Referenzwert-Typen, Quality-Filter
|
|
||||||
- **Nutzer:** Profildaten, Referenzwerte, Focus-Area-Gewichte (beeinflussen Scores)
|
|
||||||
- **Entwickler:** Neue Funktionen in `data_layer/` nach Extension Guide
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Designprinzipien
|
|
||||||
|
|
||||||
### 1. Single Source of Truth für Berechnungen
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Jede Metrik wird **einmal** in `data_layer/` berechnet; Charts, KI und Export konsumieren dieselbe Funktion. |
|
|
||||||
| **Begründung** | Verhindert divergierende Zahlen zwischen Dashboard, Analyse und KI-Ausgabe. |
|
|
||||||
| **Quelle** | Issue #53 Executive Summary; `nutrition_chart_payloads.py` Kommentar „identisch zu GET /api/charts/energy-balance“ |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Nicht alle Pfade migriert (`insights._prepare_template_vars`, `execute_prompt_with_data` Roh-SQL). |
|
|
||||||
|
|
||||||
### 2. Layer 1 liefert strukturierte Daten, keine formatierten Strings
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Kern-Metrik-Funktionen geben Dicts mit `float`/`int`/`date` zurück — **keine** Strings mit Einheiten („86,1 kg“). |
|
|
||||||
| **Begründung** | Formatierung ist konsumentenspezifisch (DE-Locale, Chart-Achsen, KI-Token). |
|
|
||||||
| **Quelle** | `data_layer/__init__.py` Docstring: „NO FORMATTING. NO STRINGS WITH UNITS.“ |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | `placeholder_resolver` und `*_interpretation` Module formatieren teils direkt — Grenze Layer 1/2a nicht überall scharf. |
|
|
||||||
|
|
||||||
### 3. Pflicht-Metadaten: confidence + data_points
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Jede Metrik-Funktion liefert mindestens `confidence` (`high`\|`medium`\|`low`\|`insufficient`) und `data_points`. |
|
|
||||||
| **Begründung** | UI/KI können Datenqualität kommunizieren; Debugging und Monitoring vereinfacht. |
|
|
||||||
| **Quelle** | `DATA_LAYER_EXTENSION_GUIDE.md` § Pflicht-Felder; `calculate_confidence()` in `utils.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Nicht runtime-validiert; Disziplin per Code-Review. |
|
|
||||||
|
|
||||||
### 4. Confidence nach Metrik-Typ und Zeitfenster
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Schwellen unterscheiden `general`, `correlation`, `trend` und Fensterlänge (7d / 28d / 90d). |
|
|
||||||
| **Begründung** | Korrelationen brauchen mehr Paare; Trends messen Abdeckung (% der Tage). |
|
|
||||||
| **Quelle** | `data_layer/utils.py` → `calculate_confidence()` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Schwellen global hardcodiert, nicht pro Metrik konfigurierbar. |
|
|
||||||
|
|
||||||
### 5. Domänen-Module statt Monolith
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Ein Python-Modul pro fachlichem Bereich (`body_metrics`, `nutrition_metrics`, …), max. ~500 Zeilen, dann Split. |
|
|
||||||
| **Begründung** | Wartbarkeit, klare Ownership, parallele Entwicklung. |
|
|
||||||
| **Quelle** | `DATA_LAYER_EXTENSION_GUIDE.md` § Modul-Struktur |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Einige Module deutlich >500 Zeilen (Phase-0c-Wachstum). |
|
|
||||||
|
|
||||||
### 6. Layer 2b: Chart-Payloads als Adapter
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Chart.js-Strukturen leben in dedizierten `*_chart_payloads.py` / `*_viz.py`, nicht in Metrik-Modulen. |
|
|
||||||
| **Begründung** | Gleiche Metrik, verschiedene Visualisierungen; API-Endpoints bleiben dünn. |
|
|
||||||
| **Quelle** | `nutrition_chart_payloads.py`; `routers/charts.py` Imports |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Teilweise noch SQL-Duplikation in Payload-Buildern neben Layer-1-Aufruf. |
|
|
||||||
|
|
||||||
### 7. Layer 2a-Hilfe: KI-spezifische Kompaktierung getrennt
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Token-Reduktion für LLM-Kontext (`compact_float_for_prompt`, `compact_json_payload_for_prompts`) ist eigenes Modul, nicht in Metrik-Kern. |
|
|
||||||
| **Begründung** | KI hat andere Anforderungen als Charts (Präzision vs. Token-Kosten). |
|
|
||||||
| **Quelle** | `prompt_output_compact.py`; Tests in `tests/test_prompt_output_compact.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Nur für KI-Pfad; Charts nutzen eigene Rundung. |
|
|
||||||
|
|
||||||
### 8. Import-Grenze: Ingest vs. Interpretation
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | CSV-Import macht Mapping + Typkonvertierung + Duplikatlogik — **keine** fachliche Auswertung beim Insert. |
|
|
||||||
| **Begründung** | Semantik gehört in Layer 1+, sonst versteckte Business-Logik in Import-Adaptern. |
|
|
||||||
| **Quelle** | `ARCHITECTURE.md` §8; Issue #53 |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Legacy-Adapter (Apple-Schlaf-Aggregat, dedizierte Import-Endpoints) noch aktiv. |
|
|
||||||
|
|
||||||
### 9. Persistenz-Orchestrator für Schreibpfade
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Alle Schreibwege eines Domänenobjekts (REST, CSV, Legacy) laufen durch **einen** Orchestrator mit Nebenwirkungen (EAV, Evaluation). |
|
|
||||||
| **Begründung** | Konsistente Duplikat-Erkennung, Registry-Felder, keine divergierenden Insert-Logiken. |
|
|
||||||
| **Quelle** | `activity_persistence_orchestrator.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Bisher vor allem Aktivität; andere Domänen noch direkt in Routern. |
|
|
||||||
|
|
||||||
### 10. Registry als Feld-Kanon (Activity)
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Erlaubte persistierbare Felder für CSV/REST leiten sich aus `module_registry` ab, nicht aus Router-Hardcoding. |
|
|
||||||
| **Begründung** | Single Source of Truth für Import-Mappings und DB-Updates. |
|
|
||||||
| **Quelle** | `activity_data_canon.py`, `activity_persistence_orchestrator.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Nur Activity vollständig; andere Module noch klassische Spalten-CRUD. |
|
|
||||||
|
|
||||||
### 11. EAV-Anreicherung als Read-Layer
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Session-Metriken (EAV) werden beim **Lesen** angereichert (`enrich_sessions_with_metrics`), nicht pro Consumer dupliziert. |
|
|
||||||
| **Begründung** | Ein Merge-Kanon für Liste, Detail, Export, Platzhalter. |
|
|
||||||
| **Quelle** | `activity_session_metrics.py`; `ACTIVITY_SESSION_METRICS_EAV_AGENT_GUIDE.md` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Domänenspezifisch (Training); Muster übertragbar. |
|
|
||||||
|
|
||||||
### 12. Scores als composable Layer
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Composite Scores (`scores.py`) kombinieren Domänen-Metriken mit nutzer-spezifischen Focus Weights — keine Score-Logik in Routern. |
|
|
||||||
| **Begründung** | Goal-Mode-/Focus-abhängige Gewichtung zentral, für KI und Dashboard gleich. |
|
|
||||||
| **Quelle** | `data_layer/scores.py`; Phase-0b-Fokus-System |
|
|
||||||
| **Tragfähigkeit** | **mittel–hoch** |
|
|
||||||
| **Einschränkung** | Eng an Mitai-Zielsystem gekoppelt; Muster „gewichtete Composite Scores“ ist generisch. |
|
|
||||||
|
|
||||||
### 13. API-First: Router delegieren, rechnen nicht
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `routers/charts.py` und ähnliche Endpoints rufen Data-Layer-Funktionen auf und mappen auf HTTP — keine Trend-Berechnung im Router. |
|
|
||||||
| **Begründung** | Testbarkeit; Frontend ohne Business-Logik. |
|
|
||||||
| **Quelle** | `ARCHITECTURE.md` §1.2 API-First |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | `charts.py` ist groß (2246+ Zeilen) — viel Adapter-Code, aber Berechnung delegiert. |
|
|
||||||
|
|
||||||
### 14. serialize_dates / safe_float als Querschnitt
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | JSON/API-Serialisierung (Dates, Decimal) zentral in `utils.py`, nicht pro Modul neu erfunden. |
|
|
||||||
| **Begründung** | PostgreSQL-Typen (DATE, DECIMAL) konsistent für API und Export. |
|
|
||||||
| **Quelle** | `data_layer/utils.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | — |
|
|
||||||
|
|
||||||
### 15. Extension Guide als verbindlicher Entwicklungsvertrag
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Neue Metriken folgen Template (Retrieve → Confidence → Early Return → Calculate → Return) und werden in `__init__.py` exportiert. |
|
|
||||||
| **Begründung** | Einheitliche Struktur für 97+ Funktionen und wachsende Codebase. |
|
|
||||||
| **Quelle** | `DATA_LAYER_EXTENSION_GUIDE.md` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Guide und Ist-Code divergieren teils (Modulgröße, `goals.py` noch nicht in `__init__`). |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Nicht übernehmen
|
|
||||||
|
|
||||||
Muster, die sich nicht bewährt haben oder zu produktspezifisch sind:
|
|
||||||
|
|
||||||
1. **Berechnungslogik in `placeholder_resolver.py`** — Phase-0b-Legacy; Resolver soll nur formatieren/aggregieren, nicht rechnen.
|
|
||||||
|
|
||||||
2. **Paralleler Roh-SQL-Kontext in `prompt_executor.execute_prompt_with_data`** — lädt Modul-Rohdaten per SQL, obwohl Layer 1 existiert; zweite Wahrheit.
|
|
||||||
|
|
||||||
3. **Legacy Insights-Pfad (`insights._prepare_template_vars`)** — eigene Variablen-Vorbereitung ohne Data Layer.
|
|
||||||
|
|
||||||
4. **Import mit fachlicher Interpretation** — Apple-Schlaf-Aggregat und ähnliche Adapter verstecken Semantik im Ingest (Gitea #69).
|
|
||||||
|
|
||||||
5. **Monolithische Router mit Inline-Berechnung** — vor Phase 0c; gelegentlich noch Reste in nicht migrierten Pfaden.
|
|
||||||
|
|
||||||
6. **Interpretation vermischt mit Layer 1** — `*_interpretation.py` liefert teils fertige Texte; für Familien-Architektur klar als Layer 2a/2b markieren oder auslagern.
|
|
||||||
|
|
||||||
7. **SQL-Duplikation in Chart-Payloads** — manche Payload-Builder führen eigene Queries statt ausschließlich Layer-1-Ergebnisse zu visualisieren.
|
|
||||||
|
|
||||||
8. **Hardcodierte Confidence global** — funktioniert, aber nicht pro Metrik/Domäne konfigurierbar; Skalierung in Multi-Tenant-Produktfamilie prüfen.
|
|
||||||
|
|
||||||
9. **Domänen-Module als Produktinhalt** — `body_metrics`, TDEE, WHR etc. sind Mitai-spezifisch; **Schichtenmodell** übernehmen, **Formeln** nicht blind kopieren.
|
|
||||||
|
|
||||||
10. **Fehlende runtime-Validierung des Return-Schemas** — `confidence`/`data_points` per Konvention, nicht per TypedDict/Pydantic erzwungen.
|
|
||||||
|
|
||||||
11. **Uneinheitliche Schreib-Orchestrierung** — nur Activity hat `persistence_orchestrator`; andere Domänen noch fragmentiert.
|
|
||||||
|
|
||||||
12. **Riesige Einzeldateien** — einige Metrik-Module >>500 Zeilen widersprechen eigenem Extension Guide.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Modul-Inventar (Ist-Stand)
|
|
||||||
|
|
||||||
```
|
|
||||||
backend/data_layer/
|
|
||||||
├── Kern-Metriken (Layer 1)
|
|
||||||
│ ├── body_metrics.py
|
|
||||||
│ ├── nutrition_metrics.py
|
|
||||||
│ ├── activity_metrics.py
|
|
||||||
│ ├── recovery_metrics.py
|
|
||||||
│ ├── health_metrics.py
|
|
||||||
│ ├── scores.py
|
|
||||||
│ └── correlations.py
|
|
||||||
├── Visualisierung (Layer 2b)
|
|
||||||
│ ├── *_chart_payloads.py (nutrition, recovery, correlation)
|
|
||||||
│ └── *_viz.py (body, nutrition, fitness, recovery, history_overview)
|
|
||||||
├── KI / Format (Layer 2a-Nähe)
|
|
||||||
│ ├── prompt_output_compact.py
|
|
||||||
│ └── *_interpretation.py
|
|
||||||
├── Persistenz / EAV
|
|
||||||
│ ├── activity_persistence_orchestrator.py
|
|
||||||
│ ├── activity_session_metrics.py
|
|
||||||
│ └── activity_data_canon.py
|
|
||||||
├── Querschnitt
|
|
||||||
│ ├── utils.py
|
|
||||||
│ ├── reference_values.py
|
|
||||||
│ └── nutrition_body_merge.py
|
|
||||||
└── __init__.py (Exports)
|
|
||||||
```
|
|
||||||
|
|
||||||
**Konsumenten-Endpoints (Auswahl):** 20+ Chart-Endpoints in `routers/charts.py` (E1–E5, A1–A8, R1–R5, C1–C4).
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Verwandte Dokumentation
|
|
||||||
|
|
||||||
- Issue #53 Abschluss: [issue-53-phase-0c-multi-layer-architecture.md](../../../../docs/issues/issue-53-phase-0c-multi-layer-architecture.md)
|
|
||||||
- Extension Guide: [DATA_LAYER_EXTENSION_GUIDE.md](../../technical/DATA_LAYER_EXTENSION_GUIDE.md)
|
|
||||||
- Fachliche Datenarchitektur: [DATA_ARCHITECTURE.md](../../functional/DATA_ARCHITECTURE.md)
|
|
||||||
- Import-Grenze: [ARCHITECTURE.md](../../../rules/ARCHITECTURE.md) §8
|
|
||||||
- Platzhalter-Anbindung: [PLACEHOLDER_REGISTRY_FRAMEWORK.md](../../technical/PLACEHOLDER_REGISTRY_FRAMEWORK.md)
|
|
||||||
- Prompt Engine (Konsument Layer 2a): [PROMPT_ENGINE_DESIGN_PRINCIPLES.md](./PROMPT_ENGINE_DESIGN_PRINCIPLES.md)
|
|
||||||
- Feature & Entitlement: [FEATURE_ENTITLEMENT_DESIGN_PRINCIPLES.md](./FEATURE_ENTITLEMENT_DESIGN_PRINCIPLES.md)
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Geplante Folgedokumente (Serie)
|
|
||||||
|
|
||||||
| # | Modul | Datei (geplant) |
|
|
||||||
|---|-------|-----------------|
|
|
||||||
| 1 | Prompt Engine | ✅ `PROMPT_ENGINE_DESIGN_PRINCIPLES.md` |
|
|
||||||
| 2 | Data Layer | ✅ dieses Dokument |
|
|
||||||
| 3 | Feature & Entitlement System | ✅ `FEATURE_ENTITLEMENT_DESIGN_PRINCIPLES.md` |
|
|
||||||
| 4 | Registry-/Plugin-Muster | ✅ `REGISTRY_PLUGIN_DESIGN_PRINCIPLES.md` |
|
|
||||||
| 5 | Auth & Session | ✅ `AUTH_SESSION_DESIGN_PRINCIPLES.md` |
|
|
||||||
| 6 | Universal Import | ✅ `UNIVERSAL_IMPORT_DESIGN_PRINCIPLES.md` |
|
|
||||||
| 7 | Dashboard Widgets | ✅ `DASHBOARD_WIDGETS_DESIGN_PRINCIPLES.md` |
|
|
||||||
| 8 | Navigation / IA | ✅ `NAVIGATION_IA_DESIGN_PRINCIPLES.md` |
|
|
||||||
| 9 | Migration & Deploy | ✅ `MIGRATION_DEPLOY_DESIGN_PRINCIPLES.md` |
|
|
||||||
-370
@@ -1,370 +0,0 @@
|
|||||||
# Feature & Entitlement System – Designprinzipien (Extraktion)
|
|
||||||
|
|
||||||
**Status:** Analyse / Arbeitspapier
|
|
||||||
**Stand:** 2026-07-04
|
|
||||||
**Geltungsbereich:** Membership-, Tier- und Feature-Limit-System (v9c) — keine Mitai-Domänenlogik, kein zentrales SSO/Stripe (Vision)
|
|
||||||
|
|
||||||
**Serie:** Designprinzipien für Produktfamilie · Dokument 3 von n
|
|
||||||
**Vorgänger:** [DATA_LAYER_DESIGN_PRINCIPLES.md](./DATA_LAYER_DESIGN_PRINCIPLES.md)
|
|
||||||
|
|
||||||
**Kernkomponenten:**
|
|
||||||
|
|
||||||
| Bereich | Pfade |
|
|
||||||
|---------|-------|
|
|
||||||
| Entitlement-Auflösung | `backend/auth.py` (`get_effective_tier`, `check_feature_access`, `increment_feature_usage`) |
|
|
||||||
| Monitoring | `backend/feature_logger.py` |
|
|
||||||
| Nutzer-API | `backend/routers/subscription.py`, `backend/routers/features.py` |
|
|
||||||
| Admin | `routers/tiers_mgmt.py`, `tier_limits.py`, `coupons.py`, `access_grants.py`, `user_restrictions.py` |
|
|
||||||
| Widget-Gating | `backend/dashboard_widget_entitlements.py`, `widget_feature_requirements_db.py` |
|
|
||||||
| Frontend | `UsageBadge.jsx`, Feature-Usage in Seiten (z. B. `Analysis.jsx`, `WeightPage`) |
|
|
||||||
| Doku | `MEMBERSHIP_SYSTEM.md`, `FEATURE_ENFORCEMENT.md`, `CENTRAL_SUBSCRIPTION_SYSTEM.md` (Vision) |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Modul
|
|
||||||
|
|
||||||
**Feature & Entitlement System** (Membership v9c)
|
|
||||||
|
|
||||||
Zentrale Schicht für **„Darf dieser Nutzer diese Funktion wie oft nutzen?“** — unabhängig von Auth (Identität) und unabhängig von fachlicher Business-Logik in Routern.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Fachliche Verantwortung
|
|
||||||
|
|
||||||
Das System übernimmt:
|
|
||||||
|
|
||||||
1. **Feature-Registry** — Deklarative Liste aller limitierbaren Produktfunktionen mit Metadaten.
|
|
||||||
2. **Tier-Auflösung** — Effektiver Tarif eines Profils (Basis-Tier + zeitliche Grants).
|
|
||||||
3. **Limit-Auflösung** — Pro Feature: Override → Tier-Limit → Feature-Default.
|
|
||||||
4. **Usage-Tracking** — Zähler für Count-Features mit optionalem Reset (daily/monthly/never).
|
|
||||||
5. **Enforcement** — HTTP 403 bei Überschreitung; Frontend-Vorschaum via Badges.
|
|
||||||
6. **Beobachtbarkeit** — Strukturiertes JSON-Logging aller Access-Checks.
|
|
||||||
7. **Promotionen** — Coupons → Access Grants (temporäre Tier-Elevation, Pause/Resume).
|
|
||||||
|
|
||||||
Es übernimmt **nicht**:
|
|
||||||
|
|
||||||
- Login, Session, Passwort (Auth-Modul)
|
|
||||||
- Zahlungsabwicklung / Stripe (geplant, `CENTRAL_SUBSCRIPTION_SYSTEM.md`)
|
|
||||||
- Mandanten-Isolation (Org/Workspace) — Entitlements sind **profile-scoped**
|
|
||||||
- Inhaltliche Berechtigung pro Datensatz (nur Feature-Gates)
|
|
||||||
|
|
||||||
### Zwei Entscheidungsebenen
|
|
||||||
|
|
||||||
| Ebene | Frage | Funktion |
|
|
||||||
|-------|--------|----------|
|
|
||||||
| **Tier** | Welcher Tarif gilt? | `get_effective_tier()` |
|
|
||||||
| **Feature** | Darf Feature X genutzt werden (wie oft)? | `check_feature_access()` |
|
|
||||||
|
|
||||||
Tier beeinflusst Feature-Limits über `tier_limits`; User-Overrides können Limits unabhängig vom Tier setzen.
|
|
||||||
|
|
||||||
### Administrierte Konfigurationen
|
|
||||||
|
|
||||||
| Konfiguration | Speicherort | Admin-UI |
|
|
||||||
|---------------|-------------|----------|
|
|
||||||
| Feature-Definitionen | `features` | Admin Features |
|
|
||||||
| Tier-Stufen | `tiers` | Admin Tiers |
|
|
||||||
| Tier × Feature Matrix | `tier_limits` | Admin Tier Limits |
|
|
||||||
| User-Overrides | `user_feature_restrictions` | Admin User Restrictions |
|
|
||||||
| Coupons | `coupons`, `coupon_redemptions` | Admin Coupons |
|
|
||||||
| Temporäre Tier-Grants | `access_grants` | (via Coupon/Admin) |
|
|
||||||
| Widget → Feature Mapping | `widget_feature_requirements`, Katalog | Admin Widget Features |
|
|
||||||
| Usage-Zähler | `user_feature_usage` | (automatisch) |
|
|
||||||
|
|
||||||
**Nicht hardcodiert:** Limits pro Tier, Feature-Metadaten, Coupon-Parameter, User-Overrides.
|
|
||||||
|
|
||||||
**Hardcodiert (Code):** Feature-IDs in Routern (`'ai_calls'`, `'weight_entries'`, …), Reset-Berechnung, 4-Phasen-Muster, 11 initial registrierte Features.
|
|
||||||
|
|
||||||
### Auflösungs-Hierarchien
|
|
||||||
|
|
||||||
**Effektiver Tier** (`get_effective_tier`):
|
|
||||||
|
|
||||||
1. Aktiver `access_grants`-Eintrag (`is_active`, `valid_from`/`valid_until`)
|
|
||||||
2. Fallback: `profiles.tier`
|
|
||||||
|
|
||||||
**Feature-Limit** (`check_feature_access` → `_check_impl`):
|
|
||||||
|
|
||||||
1. `user_feature_restrictions.limit_value` (höchste Priorität)
|
|
||||||
2. `tier_limits` für effektiven Tier
|
|
||||||
3. `features.default_limit`
|
|
||||||
|
|
||||||
**Limit-Semantik:**
|
|
||||||
|
|
||||||
| `limit_type` | Bedeutung |
|
|
||||||
|--------------|-----------|
|
|
||||||
| `count` | Zählbares Kontingent; `used < limit` |
|
|
||||||
| `boolean` | An/Aus; `limit == 1` erlaubt, `0` gesperrt |
|
|
||||||
|
|
||||||
| `limit_value` | Bedeutung |
|
|
||||||
|---------------|-----------|
|
|
||||||
| `NULL` | Unbegrenzt |
|
|
||||||
| `0` | Deaktiviert |
|
|
||||||
| `> 0` | Kontingent oder Boolean „an“ |
|
|
||||||
|
|
||||||
### Rollen
|
|
||||||
|
|
||||||
| Rolle | Darf |
|
|
||||||
|-------|------|
|
|
||||||
| **Admin** | Features/Tiers/Limits/Coupons/Restrictions CRUD; alle Nutzer-Overrides |
|
|
||||||
| **Nutzer** | Eigene Subscription/Usage lesen (`/subscription/me`, `/features/usage`); keine Limit-Änderung |
|
|
||||||
|
|
||||||
Enforcement gilt für alle authentifizierten Nutzer gleich — Admins haben keine automatische Bypass-Logik in `check_feature_access`.
|
|
||||||
|
|
||||||
### Versionierung, Freigabe, Test
|
|
||||||
|
|
||||||
| Mechanismus | Status |
|
|
||||||
|-------------|--------|
|
|
||||||
| 4-Phasen-Rollout (Monitor → UI → Enforce) | ✅ dokumentiert & angewendet |
|
|
||||||
| JSON-Log `feature-usage.log` | ✅ Phase 2 Monitoring |
|
|
||||||
| DB-Migration v9c für Schema | ✅ |
|
|
||||||
| Automatisierte Enforcement-Tests pro Router | ⚠️ teilweise (Widgets getestet) |
|
|
||||||
| Zentrale Policy „jeder Endpoint muss checken“ | ❌ nicht erzwungen |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Designprinzipien
|
|
||||||
|
|
||||||
### 1. Feature-Registry statt hardcodierter Limits
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Jedes limitierbare Produkt-Feature ist Zeile in `features` — neue Features ohne Schema-Migration für Limits. |
|
|
||||||
| **Begründung** | Admin-UI, Usage-API und Backend-Checks teilen dieselbe ID und Metadaten. |
|
|
||||||
| **Quelle** | `MEMBERSHIP_SYSTEM.md` § Feature-Registry; `routers/features.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Feature-IDs müssen trotzdem in Router-Code referenziert werden. |
|
|
||||||
|
|
||||||
### 2. Eine Auflösungsfunktion für Entitlements
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Alle Backend- und Widget-Checks rufen `check_feature_access(profile_id, feature_id)` auf. |
|
|
||||||
| **Begründung** | Keine duplizierte Tier/Limit-Logik in Routern, Widgets oder Frontend. |
|
|
||||||
| **Quelle** | `auth.py`; `dashboard_widget_entitlements.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Nicht alle Endpoints nutzen es (z. B. `/prompts/execute` fehlt). |
|
|
||||||
|
|
||||||
### 3. Getrennte Tier- und Feature-Auflösung
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `get_effective_tier()` für Tarif; `check_feature_access()` für konkretes Feature — Tier ist Input, nicht Output der Feature-Prüfung. |
|
|
||||||
| **Begründung** | Temporäre Grants heben Tier an; User-Override kann einzelnes Feature unabhängig anpassen. |
|
|
||||||
| **Quelle** | `auth.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | `get_effective_tier` im Code einfacher als in `MEMBERSHIP_SYSTEM.md` (kein `tier_locked`, Trial nicht in Tier-Funktion). |
|
|
||||||
|
|
||||||
### 4. Prioritäts-Kette für Limits
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | User-Override > Tier-Limit > Feature-Default — explizit und dokumentiert. |
|
|
||||||
| **Begründung** | Support/Beta-Fälle ohne Tier-Wechsel; vorhersehbares Verhalten. |
|
|
||||||
| **Quelle** | `_check_impl()` in `auth.py`; `MEMBERSHIP_SYSTEM.md` § Zugriffs-Hierarchie |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | `user_feature_restrictions.enabled` im Schema, aber nicht in `_check_impl` ausgewertet. |
|
|
||||||
|
|
||||||
### 5. Count vs. Boolean als zwei Feature-Klassen
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Zählbare Aktionen (`count` + Usage) vs. Schalter-Features (`boolean`, kein Counter). |
|
|
||||||
| **Begründung** | Pipeline-An/Aus vs. monatliche KI-Calls — unterschiedliche UX und Backend-Logik. |
|
|
||||||
| **Quelle** | `features.limit_type`; `FEATURE_ENFORCEMENT.md` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Boolean-Features nutzen `limit_value` 0/1 — leicht mit Count zu verwechseln. |
|
|
||||||
|
|
||||||
### 6. Reset-Perioden für Count-Features
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `reset_period`: `never` \| `daily` \| `monthly` — Counter-Reset in `check_feature_access` bei abgelaufenem `reset_at`. |
|
|
||||||
| **Begründung** | Monats-Kontingente vs. Lifetime-Limits in einem Modell. |
|
|
||||||
| **Quelle** | `auth.py` → `_calculate_next_reset()` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Reset beim Check, nicht per Cron — Edge Cases bei seltenem Zugriff. |
|
|
||||||
|
|
||||||
### 7. Usage nur bei neuen Entitäten incrementieren
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `increment_feature_usage()` nur nach **INSERT**, nicht nach UPDATE/Upsert-Deduplikat. |
|
|
||||||
| **Begründung** | Limits messen „neue Nutzung“, nicht Bearbeitung bestehender Daten. |
|
|
||||||
| **Quelle** | `FEATURE_ENFORCEMENT.md` § Wichtige Regeln |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Bulk-Import muss explizit zählen; Fehler anfällig. |
|
|
||||||
|
|
||||||
### 8. Vier-Phasen-Rollout (Observe before Enforce)
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Phase 1 Cleanup → 2 Logging → 3 Frontend-Badges → 4 HTTP 403. |
|
|
||||||
| **Begründung** | Limits einführen ohne blind Nutzer zu blockieren; Daten für Limit-Kalibrierung. |
|
|
||||||
| **Quelle** | `FEATURE_ENFORCEMENT.md` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Disziplin pro Feature; kein zentraler Feature-Flag pro Endpoint-Phase. |
|
|
||||||
|
|
||||||
### 9. Strukturiertes Feature-Logging
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Jeder Check: `log_feature_usage(profile_id, feature_id, access, action)` → JSON in `feature-usage.log`. |
|
|
||||||
| **Begründung** | Audit, Debugging, Kalibrierung — auch wenn noch nicht enforced. |
|
|
||||||
| **Quelle** | `feature_logger.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Log-Pfad `/app/logs` container-spezifisch. |
|
|
||||||
|
|
||||||
### 10. Defense in Depth: API 403 + Frontend-Gate
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Backend blockiert autoritativ; Frontend zeigt `UsageBadge`, deaktiviert Buttons, Tooltip bei Limit. |
|
|
||||||
| **Begründung** | UX (frühes Feedback) + Sicherheit (API nicht umgehbar via curl). |
|
|
||||||
| **Quelle** | `FEATURE_ENFORCEMENT.md`; `UsageBadge.jsx` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Frontend-Gate optional pro Seite; nicht generisch erzwungen. |
|
|
||||||
|
|
||||||
### 11. Nutzer-Usage-API ohne Code-Änderung bei neuen Features
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `GET /features/usage` iteriert alle aktiven `features` und ruft `check_feature_access` pro Zeile. |
|
|
||||||
| **Begründung** | Neues DB-Feature erscheint automatisch in Quota-Übersicht. |
|
|
||||||
| **Quelle** | `routers/features.py` → `get_feature_usage()` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Frontend muss Feature-ID kennen, um Badge zu binden. |
|
|
||||||
|
|
||||||
### 12. Access Grants für temporäre Tier-Elevation
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Coupons/Admin erzeugen `access_grants`; effektiver Tier steigt zeitlich begrenzt. |
|
|
||||||
| **Begründung** | Promotions, Partner (Wellpass), Trials ohne permanente Tier-Änderung. |
|
|
||||||
| **Quelle** | `access_grants`; `routers/coupons.py` (Pause/Resume) |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Coupon-Stacking-Logik komplex; dokumentiert vs. Code prüfen bei Neuentwicklung. |
|
|
||||||
|
|
||||||
### 13. Entitlements als Querschnitt für UI-Module (Widgets)
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Dashboard-Widgets mappen auf `features.id`; Katalog liefert `allowed` pro Profil. |
|
|
||||||
| **Begründung** | Tier-Logik nicht in React-Widgets duplizieren (`DASHBOARD_WIDGETS_AGENT_GUIDE` §0). |
|
|
||||||
| **Quelle** | `dashboard_widget_entitlements.py`; `ARCHITECTURE.md` §9 |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Widget-Sichtbarkeit ≠ API-Schutz — Chart-Endpoints brauchen eigenes Gating (A4). |
|
|
||||||
|
|
||||||
### 14. Admin-konfigurierbare Tier × Feature Matrix
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `tier_limits` trennt Tier-Definition von Limits; Tiers ohne hardcodierte Spalten pro Feature. |
|
|
||||||
| **Begründung** | Neue Tiers/Preise ohne Code-Deploy der Limit-Logik. |
|
|
||||||
| **Quelle** | `MEMBERSHIP_SYSTEM.md` § Tier-System; `tier_limits` Tabelle |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Tier-Namen in Seed-Daten (`free`, `premium`, …) — erweiterbar, aber Konvention. |
|
|
||||||
|
|
||||||
### 15. NULL = unlimited, 0 = disabled
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Einheitliche Semantik für Limit-Werte in allen Schichten. |
|
|
||||||
| **Begründung** | Vermeidet Sonderfälle „-1 means unlimited“; klare Admin-UI. |
|
|
||||||
| **Quelle** | `_check_impl()` in `auth.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | SQL NULL vs. Python None — konsistent, aber in UI erklärungsbedürftig. |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Registrierte Features (Referenz)
|
|
||||||
|
|
||||||
| Feature ID | Typ | Reset | Typische Aktion |
|
|
||||||
|------------|-----|-------|-----------------|
|
|
||||||
| `weight_entries` | count | never | Gewicht anlegen |
|
|
||||||
| `circumference_entries` | count | never | Umfang anlegen |
|
|
||||||
| `caliper_entries` | count | never | Caliper anlegen |
|
|
||||||
| `activity_entries` | count | monthly | Training anlegen/import |
|
|
||||||
| `nutrition_entries` | count | monthly | Ernährung anlegen/import |
|
|
||||||
| `photos` | count | monthly | Foto hochladen |
|
|
||||||
| `ai_calls` | count | monthly | KI-Analyse |
|
|
||||||
| `ai_pipeline` | boolean | — | Pipeline-Analyse |
|
|
||||||
| `data_export` | count | monthly | Export/PDF |
|
|
||||||
| `data_import` | count | monthly | ZIP/Universal-Import |
|
|
||||||
|
|
||||||
**Enforcement-Lücken (Ist):** `routers/prompts.py` (`/execute`, `/execute-stream`) ohne `check_feature_access` — Legacy `insights.py` hat Enforcement für `ai_calls`/`ai_pipeline`.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Nicht übernehmen
|
|
||||||
|
|
||||||
1. **Dokumentations-Drift** — `MEMBERSHIP_SYSTEM.md` („Enforcement deaktiviert“) vs. `FEATURE_ENFORCEMENT.md` (Phase 4 komplett) vs. Ist-Code; bei Neuentwicklung einen Kanon festlegen.
|
|
||||||
|
|
||||||
2. **Unvollständige Tier-Auflösung** — Doku beschreibt `tier_locked`, Trial-in-Tier; Code nutzt primär Grants + `profiles.tier`. Trial (`trial_ends_at`) eher UI-Banner als Tier-Engine.
|
|
||||||
|
|
||||||
3. **Feature-IDs in Routern verstreut** — kein zentraler Endpoint-Registry-Eintrag „welcher Router prüft welches Feature“.
|
|
||||||
|
|
||||||
4. **Check und Increment nicht atomar** — Race bei parallelen Requests möglich; kein DB-Level Locking.
|
|
||||||
|
|
||||||
5. **Legacy Profil-Spalten parallel** — `ai_enabled`, `ai_limit_day`, `export_enabled` in Sessions-Query neben Feature-System.
|
|
||||||
|
|
||||||
6. **Frontend ohne Backend-Gate** — reine UI-Deaktivierung ohne 403 ist unsicher (manche Seiten nur teilweise umgesetzt).
|
|
||||||
|
|
||||||
7. **Boolean via limit_value 0/1** — funktioniert, aber für Familien-Architektur explizites `enabled`-Flag oder Capability-Tokens erwägen.
|
|
||||||
|
|
||||||
8. **Unused Schema-Felder** — `user_feature_restrictions.enabled` nicht in Auflösung eingebunden.
|
|
||||||
|
|
||||||
9. **App-lokales Abo ohne Zahlungsanbindung** — Stripe/SSO nur Vision (`CENTRAL_SUBSCRIPTION_SYSTEM.md`); nicht als fertiges Familien-Muster übernehmen.
|
|
||||||
|
|
||||||
10. **Profile als Entitlement-Subject** — kein Org/Mandant; Multi-App-Familie braucht separates Identity/Subscription-Boundary.
|
|
||||||
|
|
||||||
11. **Self-hosted Tier als Sonderfall** — `selfhosted` ist Deploy-Modell, kein generisches SaaS-Tier-Muster.
|
|
||||||
|
|
||||||
12. **Increment-Schleifen bei Bulk** — `for _ in range(new_entries): increment_feature_usage()` — ineffizient; batch-Inkrement besser.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Modul-Inventar (Ist-Stand)
|
|
||||||
|
|
||||||
```
|
|
||||||
backend/
|
|
||||||
├── auth.py # get_effective_tier, check_feature_access, increment_feature_usage
|
|
||||||
├── feature_logger.py # JSON-Logging
|
|
||||||
├── dashboard_widget_entitlements.py # Widget allowed + Layout-Sanitisierung
|
|
||||||
├── widget_feature_requirements_db.py
|
|
||||||
└── routers/
|
|
||||||
├── subscription.py # /me, /usage, /limits (Nutzer)
|
|
||||||
├── features.py # Admin CRUD + /usage, /check-access
|
|
||||||
├── tiers_mgmt.py, tier_limits.py
|
|
||||||
├── coupons.py, access_grants.py
|
|
||||||
└── user_restrictions.py
|
|
||||||
|
|
||||||
frontend/src/components/
|
|
||||||
└── UsageBadge.jsx # Quota-Anzeige (Phase 3)
|
|
||||||
```
|
|
||||||
|
|
||||||
**DB (v9c):** `features`, `tiers`, `tier_limits`, `user_feature_restrictions`, `user_feature_usage`, `coupons`, `coupon_redemptions`, `access_grants`, `user_activity_log`
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Verwandte Dokumentation
|
|
||||||
|
|
||||||
- Membership-Detail: [MEMBERSHIP_SYSTEM.md](../../technical/MEMBERSHIP_SYSTEM.md)
|
|
||||||
- Enforcement-Howto: [FEATURE_ENFORCEMENT.md](../../architecture/FEATURE_ENFORCEMENT.md)
|
|
||||||
- Vision Produktfamilie: [CENTRAL_SUBSCRIPTION_SYSTEM.md](../../technical/CENTRAL_SUBSCRIPTION_SYSTEM.md)
|
|
||||||
- Widget-Gating: [DASHBOARD_WIDGETS_AGENT_GUIDE.md](../../technical/DASHBOARD_WIDGETS_AGENT_GUIDE.md) §0
|
|
||||||
- Prompt Engine (Enforcement-Lücke): [PROMPT_ENGINE_DESIGN_PRINCIPLES.md](./PROMPT_ENGINE_DESIGN_PRINCIPLES.md)
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Geplante Folgedokumente (Serie)
|
|
||||||
|
|
||||||
| # | Modul | Status |
|
|
||||||
|---|-------|--------|
|
|
||||||
| 1 | Prompt Engine | ✅ |
|
|
||||||
| 2 | Data Layer | ✅ |
|
|
||||||
| 3 | Feature & Entitlement | ✅ dieses Dokument |
|
|
||||||
| 4 | Registry-/Plugin-Muster | ✅ |
|
|
||||||
| 5 | Auth & Session | ✅ |
|
|
||||||
| 6 | Universal Import | ✅ |
|
|
||||||
| 7 | Dashboard Widgets | ✅ |
|
|
||||||
| 8 | Navigation / IA | ✅ |
|
|
||||||
| 9 | Migration & Deploy | ✅ `MIGRATION_DEPLOY_DESIGN_PRINCIPLES.md` |
|
|
||||||
-369
@@ -1,369 +0,0 @@
|
|||||||
# Migration & Deploy – Designprinzipien (Extraktion)
|
|
||||||
|
|
||||||
**Status:** Analyse / Arbeitspapier
|
|
||||||
**Stand:** 2026-07-04
|
|
||||||
**Geltungsbereich:** DB-Migrationen, Container-Startup, CI/CD-Deploy — keine Anwendungsdomäne
|
|
||||||
|
|
||||||
**Serie:** Designprinzipien für Produktfamilie · Dokument 9 von n (Abschluss)
|
|
||||||
**Vorgänger:** [NAVIGATION_IA_DESIGN_PRINCIPLES.md](./NAVIGATION_IA_DESIGN_PRINCIPLES.md)
|
|
||||||
|
|
||||||
**Kernkomponenten:**
|
|
||||||
|
|
||||||
| Bereich | Pfade |
|
|
||||||
|---------|-------|
|
|
||||||
| DB-Init & Migrationen | `backend/db_init.py`, `backend/startup.sh` |
|
|
||||||
| Migrationen | `backend/migrations/XXX_*.sql` |
|
|
||||||
| Basis-Schema (Greenfield) | `backend/schema.sql` |
|
|
||||||
| Tracking | Tabelle `schema_migrations` |
|
|
||||||
| Compose Prod/Dev | `docker-compose.yml`, `docker-compose.dev-env.yml` |
|
|
||||||
| CI/CD | `.gitea/workflows/deploy-dev.yml`, `deploy-prod.yml`, `test.yml` |
|
|
||||||
| Versionierung | `backend/version.py` (`APP_VERSION`, `DB_SCHEMA_VERSION`, `MODULE_VERSIONS`) |
|
|
||||||
| Doku (operativ) | `MIGRATIONS.md` |
|
|
||||||
| Architektur-Regeln | `.claude/rules/ARCHITECTURE.md` §2, §7 |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Modul
|
|
||||||
|
|
||||||
**Migration & Deploy**
|
|
||||||
|
|
||||||
Automatische **PostgreSQL-Schema-Evolution** beim Container-Start plus **Git-getriebene Deploy-Pipeline** (develop → Dev, main → Prod) auf selbst-gehosteter Infrastruktur (Docker auf Raspberry Pi).
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Fachliche Verantwortung
|
|
||||||
|
|
||||||
Das Modul übernimmt:
|
|
||||||
|
|
||||||
1. **Schema-Migrationen** — Nummerierte SQL-Dateien, idempotent wo möglich, getrackt in `schema_migrations`.
|
|
||||||
2. **Startup-Orchestrierung** — Postgres ready → Schema/Migrationen → optional SQLite-Import → Uvicorn.
|
|
||||||
3. **Umgebungstrennung** — Dev (`3099`/`8099`) vs. Prod (`3002`/`8002`), getrennte DBs/Volumes.
|
|
||||||
4. **Deploy-Automatisierung** — Push auf Branch → Runner → `git reset --hard` → `docker compose build --no-cache` → Health-Check.
|
|
||||||
5. **Post-Deploy-Tests** — Pytest/Lint/Frontend-Build gegen **deployed** Container auf dem Runner.
|
|
||||||
6. **Versions-Metadaten** — App-/Modul-Version und dokumentierte `DB_SCHEMA_VERSION`.
|
|
||||||
|
|
||||||
Es übernimmt **nicht**:
|
|
||||||
|
|
||||||
- Fachliche Datenberechnungen (→ Data Layer)
|
|
||||||
- Automatisches Downgrade/Rollback von Schema
|
|
||||||
- Blue-Green oder Multi-Region-Deploy
|
|
||||||
|
|
||||||
### Deploy-Pipeline (Happy Path)
|
|
||||||
|
|
||||||
```
|
|
||||||
Entwickler: commit → push develop
|
|
||||||
→ Gitea Runner: deploy-dev.yml
|
|
||||||
→ cd /home/lars/docker/bodytrack-dev
|
|
||||||
→ git fetch + reset --hard origin/develop
|
|
||||||
→ docker compose -f docker-compose.dev-env.yml build --no-cache && up -d
|
|
||||||
→ backend startup.sh → db_init.py (Migrationen)
|
|
||||||
→ curl localhost:8099/api/auth/status
|
|
||||||
→ test.yml (push + nach Deploy): pytest im Container, py_compile, npm run build
|
|
||||||
|
|
||||||
Prod: PR develop → main → deploy-prod.yml (Port 8002, bodytrack/)
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Administrierte vs. code-definierte Konfiguration
|
|
||||||
|
|
||||||
| Konfiguration | Speicherort | Wer pflegt? |
|
|
||||||
|---------------|-------------|-------------|
|
|
||||||
| Migration-SQL | `backend/migrations/` | Entwickler |
|
|
||||||
| Welche Migrationen angewendet | `schema_migrations` (DB) | Automatisch |
|
|
||||||
| Greenfield-Basis | `schema.sql` | Entwickler (selten) |
|
|
||||||
| Compose/Ports/Env | `docker-compose*.yml`, `.env` auf Server | Betrieb |
|
|
||||||
| Deploy-Workflow | `.gitea/workflows/*.yml` | Entwickler |
|
|
||||||
| App-Version / Changelog | `backend/version.py` | Entwickler (pro Release) |
|
|
||||||
| Prod-Geheimnisse | Server-`.env`, nicht im Repo | Betrieb |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Designprinzipien
|
|
||||||
|
|
||||||
### 1. Migrationen beim Container-Start ( nicht manuell in Prod)
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `startup.sh` ruft `db_init.py` auf **bevor** Uvicorn startet; pending Migrationen werden automatisch angewendet. |
|
|
||||||
| **Begründung** | Kein vergessenes Schema-Update; Deploy und DB-Stand bleiben gekoppelt. |
|
|
||||||
| **Quelle** | `startup.sh`, `db_init.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Fehlgeschlagene Migration blockiert API-Start (`sys.exit(1)`). |
|
|
||||||
|
|
||||||
### 2. Nummeriertes Datei-Pattern als Gate
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Nur `\d{3}_*.sql` wird ausgeführt (z. B. `054_activity_session_metrics_eav.sql`); alles andere wird ignoriert. |
|
|
||||||
| **Begründung** | Sortierbare Reihenfolge; Ad-hoc-Skripte (`check_features.sql`, `v9c_*.sql`) verunreinigen nicht den Lauf. |
|
|
||||||
| **Quelle** | `run_migrations()` Regex |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Legacy-Dateien ohne Nummer liegen noch im Ordner (historischer Ballast). |
|
|
||||||
|
|
||||||
### 3. Tracking-Tabelle als Single Source of „applied“
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `schema_migrations(filename)` — jede erfolgreiche Datei genau einmal eingetragen; Pending = Dateien minus Applied. |
|
|
||||||
| **Begründung** | Idempotenter Startup; wiederholter Container-Start wendet nichts doppelt an. |
|
|
||||||
| **Quelle** | `ensure_migration_table`, `apply_migration` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Kein checksum — geänderte Datei nach Apply wird nicht erneut ausgeführt. |
|
|
||||||
|
|
||||||
### 4. Alphabetische Reihenfolge = Migrations-Reihenfolge
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `sorted(glob)` — dreistellige Präfixe (`001`, `054`, `061`) definieren die Apply-Order. |
|
|
||||||
| **Begründung** | Einfach, git-freundlich, keine separate Migrations-Registry. |
|
|
||||||
| **Quelle** | `run_migrations()` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Nummern-Kollisionen oder nachträgliches Einfügen erfordern Disziplin (immer nächste freie Nummer). |
|
|
||||||
|
|
||||||
### 5. Fail-Fast bei Migrationsfehler
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Schlägt eine Migration fehl → kein Commit in Tracking (bei Exception vor INSERT), Prozess exit 1, Container unhealthy. |
|
|
||||||
| **Begründung** | API läuft nicht mit halb angewendetem Schema. |
|
|
||||||
| **Quelle** | `apply_migration`, `main` in `db_init.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Manueller Recovery-Prozess nötig (siehe MIGRATIONS.md Rollback). |
|
|
||||||
|
|
||||||
### 6. Greenfield: schema.sql, Bestand: nur Migrationen
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Existiert `profiles` nicht → einmalig `schema.sql` laden; danach nur noch nummerierte Migrationen. |
|
|
||||||
| **Begründung** | Frische Instanz schnell bootstrapped; langlebige DBs evolvieren incremental. |
|
|
||||||
| **Quelle** | `check_table_exists`, `load_schema` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | `schema.sql` kann hinter Migrationen zurückfallen wenn nicht gepflegt. |
|
|
||||||
|
|
||||||
### 7. Idempotente DDL bevorzugen
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `ADD COLUMN IF NOT EXISTS`, `CREATE INDEX IF NOT EXISTS`, defensive UPDATEs — Migration soll mehrfach ausführbar sein ohne Schaden. |
|
|
||||||
| **Begründung** | Recovery nach partiellem Apply; manuelles Re-Run sicherer. |
|
|
||||||
| **Quelle** | `MIGRATIONS.md` Best Practices |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Nicht alle Änderungen sind idempotent (DROP, irreversible Datenmigration). |
|
|
||||||
|
|
||||||
### 8. Kein psql-Meta in Migrationsdateien
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Nur SQL — kein `\echo`, `\i`, `\connect`; Ausführung via psycopg2, nicht interaktiv. |
|
|
||||||
| **Begründung** | Parser/Runner versteht nur SQL-Statements. |
|
|
||||||
| **Quelle** | `MIGRATIONS.md`, `apply_migration` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | — |
|
|
||||||
|
|
||||||
### 9. Schema-Änderung = nummerierte Migration (nie ad-hoc in Prod)
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Neue Tabellen/Spalten **nur** via `backend/migrations/XXX_*.sql`; nicht direkt in laufender Prod-DB editieren. |
|
|
||||||
| **Begründung** | Reproduzierbarkeit Dev→Prod; Review im Git-Diff. |
|
|
||||||
| **Quelle** | ARCHITECTURE.md, CLAUDE.md |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Agent-Regel — technisch nicht erzwungen. |
|
|
||||||
|
|
||||||
### 10. DB_SCHEMA_VERSION als dokumentierter Marker
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `backend/version.py` → `DB_SCHEMA_VERSION` bei Schema-Änderung manuell bumpen (Format z. B. `YYYYMMDD` + Suffix). |
|
|
||||||
| **Begründung** | API `/api/version` und Changelog zeigen Schema-Stand unabhängig von App-Minor. |
|
|
||||||
| **Quelle** | ARCHITECTURE.md §2.6 |
|
|
||||||
| **Tragfähigkeit** | **mittel–hoch** |
|
|
||||||
| **Einschränkung** | Nicht automatisch aus `schema_migrations` abgeleitet — Drift möglich. |
|
|
||||||
|
|
||||||
### 11. Branch → Umgebung (develop / main)
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `develop` → Dev-Deploy automatisch; `main` → Prod-Deploy automatisch; Prod nur nach expliziter Freigabe/Merge. |
|
|
||||||
| **Begründung** | Klare Promotion; Dev als Integrationsumgebung. |
|
|
||||||
| **Quelle** | Workflows, CLAUDE.md Deployment |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Kein Staging-Branch zwischen Dev und Prod. |
|
|
||||||
|
|
||||||
### 12. Deploy-Arbeitskopie = exakt Remote-Branch
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Runner: `git fetch` + `git reset --hard origin/<branch>` — keine `pull`-Merge-Konflikte, kein schmutziger `package-lock` auf dem Pi. |
|
|
||||||
| **Begründung** | Reproduzierbarer Deploy-Baum; Fix aus GUI-IA-Abnahme 2026-04-05. |
|
|
||||||
| **Quelle** | `deploy-prod.yml`, `deploy-dev.yml` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Lokale Hotfixes auf dem Server werden beim Deploy überschrieben. |
|
|
||||||
|
|
||||||
### 13. Immutabler Build pro Deploy (`--no-cache`)
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `docker compose build --no-cache` bei jedem Deploy — frisches Image aus Dockerfile + Repo-Stand. |
|
|
||||||
| **Begründung** | Keine veralteten Layer; Migrationen und Code garantiert im Image. |
|
|
||||||
| **Quelle** | Deploy-Workflows |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Langsamere Deploys; kein Registry-basiertes Image-Promotion. |
|
|
||||||
|
|
||||||
### 14. Health-Check nach Deploy
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Nach `up -d`: kurz warten, dann `curl -sf …/api/auth/status` (8099 Dev / 8002 Prod). |
|
|
||||||
| **Begründung** | Minimale Smoke-Verification dass API antwortet (inkl. DB-Init durchlaufen). |
|
|
||||||
| **Quelle** | Deploy-Workflows |
|
|
||||||
| **Tragfähigkeit** | **mittel–hoch** |
|
|
||||||
| **Einschränkung** | Prüft nicht fachliche Endpoints oder Migration-Inhalt. |
|
|
||||||
|
|
||||||
### 15. Persistente Volumes für Daten und Fotos
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Postgres-Daten, `/app/data`, `/app/photos` in benannten/external Volumes — überleben Container-Rebuild. |
|
|
||||||
| **Begründung** | Deploy = neues Image, nicht Datenverlust. |
|
|
||||||
| **Quelle** | `docker-compose*.yml` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Volume-Backup/Restore ist Betriebsaufgabe außerhalb Repo. |
|
|
||||||
|
|
||||||
### 16. Postgres Healthcheck vor Backend-Start
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `depends_on: condition: service_healthy` — Backend startet erst wenn DB `pg_isready`. |
|
|
||||||
| **Begründung** | `wait_for_postgres` in db_init ist zweite Absicherung; reduziert Race beim ersten Start. |
|
|
||||||
| **Quelle** | Compose-Files |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | — |
|
|
||||||
|
|
||||||
### 17. Tests gegen deployed Stack (Self-Hosted Runner)
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `test.yml` führt pytest **im laufenden Backend-Container** auf dem Pi aus, nicht in isolierter GitHub-Cloud. |
|
|
||||||
| **Begründung** | Tests laufen gegen echte Dev/Prod-Compose-Umgebung des Projekts. |
|
|
||||||
| **Quelle** | `.gitea/workflows/test.yml` |
|
|
||||||
| **Tragfähigkeit** | **mittel** |
|
|
||||||
| **Einschränkung** | Prod-Deploy triggert Tests auf Prod-Pfad — Risiko wenn Tests schreibend; `-m 'not slow'` begrenzt Laufzeit. |
|
|
||||||
|
|
||||||
### 18. Feste Ports pro Umgebung
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Dev `3099/8099`, Prod `3002/8002` — nicht ändern (Reverse Proxy/Fritz!Box hängen daran). |
|
|
||||||
| **Begründung** | Externe URLs (`dev.mitai.jinkendo.de`, `mitai.jinkendo.de`) stabil. |
|
|
||||||
| **Quelle** | CLAUDE.md, Compose |
|
|
||||||
| **Tragfähigkeit** | **hoch** (betriebsspezifisch) |
|
|
||||||
| **Einschränkung** | Andere Projekte brauchen eigene Port-Matrix. |
|
|
||||||
|
|
||||||
### 19. Prod-Schutz: Deploy nur über Git
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Keine direkten Prod-Container-/DB-Schreibzugriffe für Automation; Prod-Änderung = Merge `main` → Workflow. |
|
|
||||||
| **Begründung** | Audit-Trail, Review, keine Drift. |
|
|
||||||
| **Quelle** | ARCHITECTURE.md §7.1, `/deploy` Command |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Menschlicher SSH-Zugriff bleibt möglich — Prozess, nicht Technik. |
|
|
||||||
|
|
||||||
### 20. Versions-Bump als Release-Disziplin
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Jede lieferbare Änderung: `APP_VERSION`, betroffene `MODULE_VERSIONS`, `CHANGELOG` in `version.py`; bei Schema auch `DB_SCHEMA_VERSION`. |
|
|
||||||
| **Begründung** | `/api/version`, Support, Korrelation Deploy ↔ Code. |
|
|
||||||
| **Quelle** | ARCHITECTURE.md §2.5, `deploy.md` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Frontend-`version.js` in Spec erwähnt, im Repo teils nicht vorhanden — Dual-Bump unvollständig. |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Nicht übernehmen
|
|
||||||
|
|
||||||
1. **Unnummerierte Migrationsdateien** — `v9c_*.sql`, `check_*.sql` werden nicht auto-applied; nicht als Vorbild.
|
|
||||||
|
|
||||||
2. **Migration-Datei nach Apply ändern** — Tracking verhindert Re-Run; neue Nummer statt Edit.
|
|
||||||
|
|
||||||
3. **Automatischer Downgrade** — nicht implementiert; Rollback manuell + Tracking-Eintrag löschen.
|
|
||||||
|
|
||||||
4. **Direktes Schema in Prod** — immer Git-Migration + Deploy.
|
|
||||||
|
|
||||||
5. **Breaking DROP ohne Koordination** — App-Code und Migration in einem Release.
|
|
||||||
|
|
||||||
6. **psql-Metacommands in `.sql`** — bricht Python-Runner.
|
|
||||||
|
|
||||||
7. **`git pull` auf Deploy-Server** — Merge-Schmutz; `reset --hard` ist das Muster.
|
|
||||||
|
|
||||||
8. **Prod-Deploy ohne Dev-Validierung** — develop-First ist implizite Policy.
|
|
||||||
|
|
||||||
9. **Schema-Drift ohne `DB_SCHEMA_VERSION`-Bump** — dokumentarische Lücke.
|
|
||||||
|
|
||||||
10. **Cached Docker-Build als Default** — Mitai wählt Reproduzierbarkeit über Geschwindigkeit.
|
|
||||||
|
|
||||||
11. **Migrationen außerhalb Container-Start vergessen** — manuelles psql in Prod als Normalfall.
|
|
||||||
|
|
||||||
12. **Hardcoded Seed-Daten in Migration** — produktive User/Secrets nicht in SQL.
|
|
||||||
|
|
||||||
13. **Port-Änderung „nebenbei“** — Infrastruktur-Kopplung.
|
|
||||||
|
|
||||||
14. **Tests nur lokal, nie auf Runner-Stack** — Mitai testet bewusst post-deploy im Pi-Container (Trade-off verstehen).
|
|
||||||
|
|
||||||
15. **Transaktionssteuerung in SQL-Datei** — Runner committet pro Datei; komplexe multi-step Rollbacks nicht eingebaut.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Modul-Inventar (Ist-Stand)
|
|
||||||
|
|
||||||
```
|
|
||||||
backend/
|
|
||||||
├── db_init.py # wait, schema, run_migrations, sqlite import
|
|
||||||
├── startup.sh # db_init → uvicorn
|
|
||||||
├── schema.sql # Greenfield
|
|
||||||
├── migrations/ # 001–061+ nummeriert (+ Legacy ohne Nummer)
|
|
||||||
└── version.py # APP_VERSION, DB_SCHEMA_VERSION, MODULE_VERSIONS
|
|
||||||
|
|
||||||
docker-compose.yml # Prod: 3002/8002
|
|
||||||
docker-compose.dev-env.yml # Dev: 3099/8099
|
|
||||||
|
|
||||||
.gitea/workflows/
|
|
||||||
├── deploy-dev.yml # push develop
|
|
||||||
├── deploy-prod.yml # push main
|
|
||||||
└── test.yml # pytest, lint, npm build on Pi
|
|
||||||
|
|
||||||
Server (Pi):
|
|
||||||
/home/lars/docker/bodytrack-dev/ # develop
|
|
||||||
/home/lars/docker/bodytrack/ # main
|
|
||||||
```
|
|
||||||
|
|
||||||
**Migrationen (Stand):** 60+ nummerierte Dateien (`001` … `061`); höchste Nummer im Repo prüfen vor neuer Migration.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Verwandte Dokumentation
|
|
||||||
|
|
||||||
- Operativ: [MIGRATIONS.md](../../technical/MIGRATIONS.md)
|
|
||||||
- Architektur: `.claude/rules/ARCHITECTURE.md` §2 (Versionierung), §7 (Prod-Schutz)
|
|
||||||
- Deploy-Command: `.claude/commands/deploy.md`, `merge-to-prod.md`
|
|
||||||
- Import/Migration-Grenze: [UNIVERSAL_IMPORT_DESIGN_PRINCIPLES.md](./UNIVERSAL_IMPORT_DESIGN_PRINCIPLES.md)
|
|
||||||
- Auth auf Prod: [AUTH_SESSION_DESIGN_PRINCIPLES.md](./AUTH_SESSION_DESIGN_PRINCIPLES.md)
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Serie – Übersicht (abgeschlossen)
|
|
||||||
|
|
||||||
| # | Modul | Status |
|
|
||||||
|---|-------|--------|
|
|
||||||
| 1 | Prompt Engine | ✅ `PROMPT_ENGINE_DESIGN_PRINCIPLES.md` |
|
|
||||||
| 2 | Data Layer | ✅ `DATA_LAYER_DESIGN_PRINCIPLES.md` |
|
|
||||||
| 3 | Feature & Entitlement | ✅ `FEATURE_ENTITLEMENT_DESIGN_PRINCIPLES.md` |
|
|
||||||
| 4 | Registry / Plugin (Meta) | ✅ `REGISTRY_PLUGIN_DESIGN_PRINCIPLES.md` |
|
|
||||||
| 5 | Auth & Session | ✅ `AUTH_SESSION_DESIGN_PRINCIPLES.md` |
|
|
||||||
| 6 | Universal Import | ✅ `UNIVERSAL_IMPORT_DESIGN_PRINCIPLES.md` |
|
|
||||||
| 7 | Dashboard Widgets | ✅ `DASHBOARD_WIDGETS_DESIGN_PRINCIPLES.md` |
|
|
||||||
| 8 | Navigation / IA | ✅ `NAVIGATION_IA_DESIGN_PRINCIPLES.md` |
|
|
||||||
| 9 | Migration & Deploy | ✅ dieses Dokument |
|
|
||||||
-346
@@ -1,346 +0,0 @@
|
|||||||
# Navigation & Informationsarchitektur – Designprinzipien (Extraktion)
|
|
||||||
|
|
||||||
**Status:** Analyse / Arbeitspapier
|
|
||||||
**Stand:** 2026-07-04
|
|
||||||
**Geltungsbereich:** App-Navigation, Bereichs-Shells, Admin-IA, Responsive Shell — keine Seiteninhalte oder Domänenlogik
|
|
||||||
|
|
||||||
**Serie:** Designprinzipien für Produktfamilie · Dokument 8 von n
|
|
||||||
**Vorgänger:** [DASHBOARD_WIDGETS_DESIGN_PRINCIPLES.md](./DASHBOARD_WIDGETS_DESIGN_PRINCIPLES.md)
|
|
||||||
|
|
||||||
**Kernkomponenten:**
|
|
||||||
|
|
||||||
| Bereich | Pfade |
|
|
||||||
|---------|-------|
|
|
||||||
| Hauptnavigation | `frontend/src/config/appNav.js` |
|
|
||||||
| Erfassung | `frontend/src/config/captureNav.js`, `layouts/CaptureShell.jsx` |
|
|
||||||
| Einstellungen | `frontend/src/config/settingsNav.js`, `layouts/SettingsShell.jsx` |
|
|
||||||
| Admin | `frontend/src/config/adminNav.js`, `layouts/AdminShell.jsx`, `RequireAdmin.jsx` |
|
|
||||||
| KI-Analyse (Kategorien) | `frontend/src/config/analysisCategories.js`, `pages/Analysis.jsx` |
|
|
||||||
| Routing | `frontend/src/App.jsx` |
|
|
||||||
| Desktop-Sidebar | `frontend/src/components/DesktopSidebar.jsx` |
|
|
||||||
| Responsive CSS | `frontend/src/app.css` (`--nav-h`, `.bottom-nav`, `.analysis-split`, `.desktop-sidebar`) |
|
|
||||||
| Abnahme-Doku | `docs/issues/GUI_IA_ADMIN_NAV_2026-04-05.md` |
|
|
||||||
| Responsive-Spec | `.claude/docs/functional/RESPONSIVE_UI.md` |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Modul
|
|
||||||
|
|
||||||
**Navigation & Informationsarchitektur (IA)**
|
|
||||||
|
|
||||||
Schichtenmodell für die PWA: **eine primäre Hauptnavigation** (6–7 Bereiche), darunter **Bereichs-Shells** mit eigener Sub-Navigation, getrennte **Admin-Realm**, **Auth-Gates** und **ein Breakpoint** für Mobile vs. Desktop.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Fachliche Verantwortung
|
|
||||||
|
|
||||||
Das Modul übernimmt:
|
|
||||||
|
|
||||||
1. **Hauptnav-SSoT** — Reihenfolge, Labels, Icons, Admin-Sichtbarkeit (`getMainNavItems`).
|
|
||||||
2. **Routing-Struktur** — Welche URL gehört zu welchem Bereich (Übersicht, Erfassen, Verlauf, Ziele, Analyse, Einstellungen, Admin).
|
|
||||||
3. **Sub-Navigation pro Bereich** — Capture-Hub, Settings-Tabs, Admin-Gruppen, Analyse-Kategorien.
|
|
||||||
4. **Layout-Muster** — Bottom-Nav (mobil), Sidebar (Desktop), `analysis-split` für tiefe Bereiche.
|
|
||||||
5. **Zugriffskontrolle (UI)** — `RequireAdmin`, Admin-Link nur bei `role === 'admin'`.
|
|
||||||
6. **Active-State** — Nested Routes (Erfassung unter `/capture`, Admin unter `/admin/*`).
|
|
||||||
7. **PWA-Tauglichkeit** — Safe Area, Scrollbare Bottom-Nav, Content-Padding.
|
|
||||||
|
|
||||||
Es übernimmt **nicht**:
|
|
||||||
|
|
||||||
- Backend-Autorisierung (→ [AUTH_SESSION_DESIGN_PRINCIPLES.md](./AUTH_SESSION_DESIGN_PRINCIPLES.md))
|
|
||||||
- Feature-Entitlements in der Nav (Tier-Gates an Endpoints/Widgets, nicht an jedem NavLink)
|
|
||||||
- Inhaltliche Tab-Logik innerhalb von Verlauf/Analyse (Seiten concern)
|
|
||||||
|
|
||||||
### IA-Modell (Nutzerperspektive)
|
|
||||||
|
|
||||||
| Ebene | Mental Model | Beispiel-Routen |
|
|
||||||
|-------|--------------|-----------------|
|
|
||||||
| **Primär** | Wo bin ich in der App? | `/`, `/capture`, `/history`, `/goals`, `/analysis`, `/settings` |
|
|
||||||
| **Sekundär (Shell)** | Was mache ich in diesem Bereich? | `/weight`, `/admin/g/features`, `/settings/dashboard-layout` |
|
|
||||||
| **Tertiär (Seite)** | Tabs/Filter innerhalb einer Maske | Verlauf-Tabs, Analyse-Kategorien |
|
|
||||||
|
|
||||||
### Strategisch vs. taktisch (Ziele)
|
|
||||||
|
|
||||||
| Ebene | Ort | Zweck |
|
|
||||||
|-------|-----|-------|
|
|
||||||
| **Strategisch** | `/goals` (Hauptnav) | Ziele definieren, Prioritäten, Focus Areas |
|
|
||||||
| **Taktisch** | `/custom-goals` (Erfassung) | Tägliche Ist-Werte für eigene Ziele |
|
|
||||||
| **Auswertung** | `/history` | Trends, Charts, Vergleiche |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Administrierte vs. code-definierte Konfiguration
|
|
||||||
|
|
||||||
| Konfiguration | Speicherort | Wer pflegt? |
|
|
||||||
|---------------|-------------|-------------|
|
|
||||||
| Hauptnav-Reihenfolge & Labels | `appNav.js` | Entwickler |
|
|
||||||
| Erfassungs-Kacheln & Shell-Nav | `captureNav.js` | Entwickler |
|
|
||||||
| Admin-Gruppen & Hub-Karten | `adminNav.js` | Entwickler |
|
|
||||||
| Settings-Subnav | `settingsNav.js` | Entwickler |
|
|
||||||
| Analyse-Kategorie-Reihenfolge | `analysisCategories.js` | Entwickler |
|
|
||||||
| KI-Prompt-Kategorien (Runtime) | DB `ai_prompts.category` | Admin (Prompts) |
|
|
||||||
| React-Routes | `App.jsx` | Entwickler (muss zu Nav-Configs passen) |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Designprinzipien
|
|
||||||
|
|
||||||
### 1. Eine Quelle für die Hauptnavigation
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `getMainNavItems(isAdmin)` liefert dieselbe Item-Liste für **Bottom-Nav** und **Desktop-Sidebar** — keine parallelen Hardcodings. |
|
|
||||||
| **Begründung** | Reihenfolge und Labels bleiben synchron; Admin-Conditional an einer Stelle. |
|
|
||||||
| **Quelle** | `appNav.js`, `App.jsx`, `DesktopSidebar.jsx` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Active-State-Logik ist in zwei Dateien dupliziert (`navItemActive` / `sidebarLinkActive`). |
|
|
||||||
|
|
||||||
### 2. Feste primäre IA-Reihenfolge (Produkt-Story)
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Übersicht → Erfassen → Verlauf → **Ziele** → Analyse → Einstellungen → [Admin] — spiegelt Nutzerfluss: sehen → eingeben → auswerten → steuern → interpretieren → konfigurieren. |
|
|
||||||
| **Begründung** | Ziele als eigener Hauptpunkt (nicht unter Analyse versteckt); klare Trennung Capture vs. History vs. Analysis. |
|
|
||||||
| **Quelle** | `GUI_IA_ADMIN_NAV_2026-04-05.md`; `appNav.js` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Produkt-spezifisch; andere Apps können andere Reihenfolge brauchen. |
|
|
||||||
|
|
||||||
### 3. Config-Dateien pro Bereich (Nav-as-Data)
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Sub-Navigation lebt in dedizierten Config-Modulen (`captureNav`, `adminNav`, `settingsNav`, `analysisCategories`) — Shell-Komponenten iterieren nur. |
|
|
||||||
| **Begründung** | Neue Erfassungsmaske = Eintrag in Config + Route; kein Nav-HTML in jeder Page. |
|
|
||||||
| **Quelle** | `captureNav.js` Kommentar „Pfade müssen mit Routes übereinstimmen“ |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Kein Build-Time-Check Config ↔ Routes. |
|
|
||||||
|
|
||||||
### 4. Bereichs-Shells für tiefe Navigation
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Capture, Settings und Admin nutzen **Shell-Layouts** mit `<Outlet />`; Nutzer wechselt Sub-Bereiche ohne Hauptnav zu verlassen. |
|
|
||||||
| **Begründung** | Erfassung hat 12+ Masken — wären als Hauptnav-Einträge unbrauchbar. |
|
|
||||||
| **Quelle** | `CaptureShell`, `SettingsShell`, `AdminShell` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Verlauf und Analyse haben eigene Tab-Muster (kein gemeinsames Shell-Config). |
|
|
||||||
|
|
||||||
### 5. Wiederverwendbares `analysis-split`-Layout
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Admin, Settings und KI-Analyse teilen CSS-Muster: mobil horizontale Chips, Desktop linke Spalte + `__main` für Inhalt. |
|
|
||||||
| **Begründung** | Ein visuelles Muster für „Kategorie links, Arbeit rechts“; weniger UI-Drift. |
|
|
||||||
| **Quelle** | `AdminShell.jsx`, `SettingsShell.jsx`, `Analysis.jsx`, `app.css` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Capture nutzt eigenes `capture-shell` (Emoji-Icons, Hub-Kacheln). |
|
|
||||||
|
|
||||||
### 6. Admin: Gruppen in der Shell, Seiten über Hub
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Shell-Nav zeigt nur **Admin-Gruppen** (+ Übersicht); konkrete Seiten als Karten auf `/admin/g/:groupId`. |
|
|
||||||
| **Begründung** | Skaliert bei wachsender Admin-Oberfläche; keine 20er-Sidebar. |
|
|
||||||
| **Quelle** | `adminNav.js` (`ADMIN_GROUPS`, `getAdminShellNavEntries`) |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Ein Klick mehr als flache Nav; bewusster Trade-off. |
|
|
||||||
|
|
||||||
### 7. Admin als eigener Realm
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `/admin/*` hinter `RequireAdmin`; kein Admin-Block mehr in Einstellungen; Profil-Anlage nur Admin → Benutzerverwaltung. |
|
|
||||||
| **Begründung** | Trennung Nutzer- vs. Betreiber-Kontext; weniger Verwechslung. |
|
|
||||||
| **Quelle** | `RequireAdmin.jsx`, `GUI_IA_ADMIN_NAV_2026-04-05.md` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | UI-Guard ersetzt nicht Backend-`require_admin` auf APIs. |
|
|
||||||
|
|
||||||
### 8. Route-Guard mit Nutzer-Feedback
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Nicht-Admin auf `/admin` → Redirect `/` mit `state.adminDenied`; Dashboard zeigt Hinweis. |
|
|
||||||
| **Begründung** | Stilles Scheitern vermeiden; klare Erwartung. |
|
|
||||||
| **Quelle** | `RequireAdmin.jsx`, `Dashboard.jsx` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | — |
|
|
||||||
|
|
||||||
### 9. Nested Active-State für Section-Prefixes
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Custom Active-Logik: `/capture` aktiv bei allen Erfassungs-Pfaden; `/admin` bei gesamten Admin-Baum; `/goals` mit `end: true` (exakt). |
|
|
||||||
| **Begründung** | React-Router `end` allein reicht für Section-Gruppen nicht. |
|
|
||||||
| **Quelle** | `navItemActive`, `adminShellEntryIsActive` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Neue Section-Prefixes brauchen explizite Regel. |
|
|
||||||
|
|
||||||
### 10. Erfassungs-Hub + direkte Deep-Links
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `/capture` = Kachel-Hub; jede Maske auch direkt erreichbar (`/weight`, …); Shell-Nav immer sichtbar. |
|
|
||||||
| **Begründung** | Onboarding über Hub; Power-User/Dashboard-Links springen direkt. |
|
|
||||||
| **Quelle** | `CaptureHub`, `CAPTURE_HUB_TILES` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Hub und Shell-Nav listen dieselben Ziele (Doppelpflege). |
|
|
||||||
|
|
||||||
### 11. Einstellungen: nur aktives Profil
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Settings = Self-Service für **aktives** Profil (Name, E-Mail, Avatar, Quality-Filter); keine Profil-Liste für Endnutzer. |
|
|
||||||
| **Begründung** | Multi-Profil-Verwaltung ist Admin-Aufgabe; reduziert Komplexität. |
|
|
||||||
| **Quelle** | `SettingsPage.jsx`, IA-Doku |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Session-bound Profile-Id-Schwäche bleibt Backend-Thema. |
|
|
||||||
|
|
||||||
### 12. Settings-Subnav für Layout & Export
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Konfiguration schwerer Features (Dashboard-Layout, PDF-Berichte, Referenzwerte) als eigene Settings-Routen unter Shell — nicht in „Allgemein“ verstecken. |
|
|
||||||
| **Begründung** | Entspricht [DASHBOARD_WIDGETS_DESIGN_PRINCIPLES.md](./DASHBOARD_WIDGETS_DESIGN_PRINCIPLES.md) (Nutzer-Konfigurator). |
|
|
||||||
| **Quelle** | `settingsNav.js` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Admin-Dashboard-Default liegt unter `/admin/...` (getrennte Rolle). |
|
|
||||||
|
|
||||||
### 13. KI-Analyse: Ergebnis im Hauptspalt
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Neue Analyse-Ergebnisse rendern in `analysis-split__main`, nicht in der Kategorie-Nav — Nav bleibt wählbar. |
|
|
||||||
| **Begründung** | Lange Ergebnisse verdrängen sonst die Prompt-Auswahl (Mobile). |
|
|
||||||
| **Quelle** | `GUI_IA_ADMIN_NAV_2026-04-05.md`, `Analysis.jsx` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | — |
|
|
||||||
|
|
||||||
### 14. Ein Breakpoint Mobile / Desktop (1024px)
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `< 1024px`: Bottom-Nav + Mobile-Header; `≥ 1024px`: Desktop-Sidebar, Bottom-Nav ausgeblendet, breiterer Content. **Kein** separates Tablet-Layout. |
|
|
||||||
| **Begründung** | Einfache Spec, PWA-first; iPad im Portrait = Mobile-Verhalten. |
|
|
||||||
| **Quelle** | `RESPONSIVE_UI.md`, `app.css` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Große Phones und kleine Tablets identisch behandelt. |
|
|
||||||
|
|
||||||
### 15. PWA Safe Area für Bottom-Navigation
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `--nav-h`, `--nav-pad-top`, `env(safe-area-inset-bottom)` auf `.bottom-nav`; Content-`padding-bottom` inkl. Nav-Höhe; horizontal scrollbare Nav bei vielen Items. |
|
|
||||||
| **Begründung** | iPhone Home-Indicator und Notch — kein Clipping, kein verdeckter Content. |
|
|
||||||
| **Quelle** | `app.css`, IA-Doku |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Safe Area nur auf Nav/Content-Padding, nicht global überall. |
|
|
||||||
|
|
||||||
### 16. Auth-Routen außerhalb der App-Shell
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Login, Register, Verify, Reset-Password rendern **ohne** Bottom-Nav/Sidebar — minimale Vollbild-Cards. |
|
|
||||||
| **Begründung** | Keine Navigation ohne Session; klarer Fokus. |
|
|
||||||
| **Quelle** | `App.jsx` (early returns vor `AppShell`) |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Public Routes nicht zentral in einer Route-Config. |
|
|
||||||
|
|
||||||
### 17. Rollen-sichtbare Nav-Einträge (UI only)
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Admin-Link erscheint nur wenn `isAdmin`; Backend schützt APIs separat. |
|
|
||||||
| **Begründung** | Progressive disclosure; normale Nutzer sehen keinen toten Link. |
|
|
||||||
| **Quelle** | `getMainNavItems(isAdmin)` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Security nicht durch Ausblenden ersetzt. |
|
|
||||||
|
|
||||||
### 18. Deep-Link-State für Verlauf
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Nav zu `/history` setzt optional `state: { tab: 'overview' }` — konsistenter Einstieg von Hauptnav. |
|
|
||||||
| **Begründung** | Verlauf merkt sich Tabs; Hauptnav soll nicht zufälligen alten Tab öffnen. |
|
|
||||||
| **Quelle** | `App.jsx`, `DesktopSidebar.jsx`, `History.jsx` |
|
|
||||||
| **Tragfähigkeit** | **mittel** |
|
|
||||||
| **Einschränkung** | Nur für History implementiert, nicht app-weit. |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Nicht übernehmen
|
|
||||||
|
|
||||||
1. **Hauptnav an mehreren Stellen hardcoden** — immer `appNav.js`.
|
|
||||||
|
|
||||||
2. **Admin-Funktionen in Einstellungen** — eigener `/admin`-Bereich.
|
|
||||||
|
|
||||||
3. **Alle Erfassungsmasken in die Bottom-Nav** — Shell + Hub skaliert.
|
|
||||||
|
|
||||||
4. **Alle Admin-Seiten in der Shell-Sidebar** — Hub-Gruppen-Muster beibehalten.
|
|
||||||
|
|
||||||
5. **Nav-Config ohne Route-Pflege** — jeder neue Pfad: Config + `App.jsx` + ggf. Active-State.
|
|
||||||
|
|
||||||
6. **UI-Admin-Guard ohne Backend-Guard** — `RequireAdmin` ist UX, APIs brauchen `require_admin`.
|
|
||||||
|
|
||||||
7. **Zwei Tablet-/Desktop-Breakpoints** — Mitai: ein Cut bei 1024px.
|
|
||||||
|
|
||||||
8. **Safe Area ignorieren** — PWA auf iOS bricht sonst an Bottom-Nav.
|
|
||||||
|
|
||||||
9. **Profil-Liste für Endnutzer in Settings** — Multi-Profil = Admin.
|
|
||||||
|
|
||||||
10. **Feature-Tier-Logik in Nav-Komponenten** — Entitlements an Widgets/APIs ([FEATURE_ENTITLEMENT_DESIGN_PRINCIPLES.md](./FEATURE_ENTITLEMENT_DESIGN_PRINCIPLES.md)).
|
|
||||||
|
|
||||||
11. **Inkonsistente Layout-Muster pro Bereich** — wo `analysis-split` passt, nicht neues Ad-hoc-Layout erfinden.
|
|
||||||
|
|
||||||
12. **Orphan-Routes ohne Nav-Ergänzung** — z. B. `/subscription`, `/workflow-editor/:id` existieren außerhalb Haupt-IA; bewusst dokumentieren, nicht unkontrolliert multiply.
|
|
||||||
|
|
||||||
13. **Active-State nur per Router-Default** — Section-Prefixes (`/capture/*`, `/admin/*`) brauchen explizite Regeln.
|
|
||||||
|
|
||||||
14. **Analyse-Ergebnisse in der Nav-Spalte** — verdrängt Prompt-Auswahl auf Mobile.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Modul-Inventar (Ist-Stand)
|
|
||||||
|
|
||||||
```
|
|
||||||
frontend/src/config/
|
|
||||||
├── appNav.js # Hauptnav (6 + Admin)
|
|
||||||
├── captureNav.js # Erfassungs-Hub + Shell
|
|
||||||
├── settingsNav.js # Settings-Subnav
|
|
||||||
├── adminNav.js # ADMIN_GROUPS, Shell-Entries
|
|
||||||
└── analysisCategories.js # KI-Analyse-Gruppen
|
|
||||||
|
|
||||||
frontend/src/layouts/
|
|
||||||
├── CaptureShell.jsx
|
|
||||||
├── SettingsShell.jsx
|
|
||||||
├── AdminShell.jsx
|
|
||||||
└── RequireAdmin.jsx
|
|
||||||
|
|
||||||
frontend/src/components/
|
|
||||||
└── DesktopSidebar.jsx
|
|
||||||
|
|
||||||
frontend/src/App.jsx # Routes + Bottom-Nav + Auth-Gates
|
|
||||||
frontend/src/app.css # Shell, split, safe-area, 1024px breakpoint
|
|
||||||
```
|
|
||||||
|
|
||||||
**Hauptnav (7 Einträge mit Admin):** Übersicht · Erfassen · Verlauf · Ziele · Analyse · Einstellungen · Admin
|
|
||||||
|
|
||||||
**Admin-Gruppen (8):** users · features · subscription · training · goals · prompts · system
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Verwandte Dokumentation
|
|
||||||
|
|
||||||
- Abnahme-Stand: [GUI_IA_ADMIN_NAV_2026-04-05.md](../../../../docs/issues/GUI_IA_ADMIN_NAV_2026-04-05.md)
|
|
||||||
- Responsive-Spec: [RESPONSIVE_UI.md](../../functional/RESPONSIVE_UI.md)
|
|
||||||
- Auth/Session: [AUTH_SESSION_DESIGN_PRINCIPLES.md](./AUTH_SESSION_DESIGN_PRINCIPLES.md)
|
|
||||||
- Dashboard-Konfigurator: [DASHBOARD_WIDGETS_DESIGN_PRINCIPLES.md](./DASHBOARD_WIDGETS_DESIGN_PRINCIPLES.md)
|
|
||||||
- Gitea #30 (Responsive UI, teilweise erledigt)
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Geplante Folgedokumente (Serie)
|
|
||||||
|
|
||||||
| # | Modul | Status |
|
|
||||||
|---|-------|--------|
|
|
||||||
| 1–7 | … | ✅ |
|
|
||||||
| 8 | Navigation / IA | ✅ dieses Dokument |
|
|
||||||
| 9 | Migration & Deploy | ✅ |
|
|
||||||
-305
@@ -1,305 +0,0 @@
|
|||||||
# Prompt Engine – Designprinzipien (Extraktion)
|
|
||||||
|
|
||||||
**Status:** Analyse / Arbeitspapier
|
|
||||||
**Stand:** 2026-07-04
|
|
||||||
**Geltungsbereich:** Modul „Prompt Engine“ (Unified Prompt System, Issue #28) — keine Mitai-Gesamtarchitektur, keine Domänenlogik (Gesundheit, Ernährung, Messwerte)
|
|
||||||
|
|
||||||
**Serie:** Designprinzipien für Produktfamilie · Dokument 1 von n
|
|
||||||
|
|
||||||
**Kernkomponenten:**
|
|
||||||
|
|
||||||
| Bereich | Pfade |
|
|
||||||
|---------|-------|
|
|
||||||
| Executor | `backend/prompt_executor.py`, `backend/workflow_executor.py` |
|
|
||||||
| Platzhalter | `backend/placeholder_resolver.py`, `backend/placeholder_registry.py`, `backend/placeholder_registrations/` |
|
|
||||||
| API | `backend/routers/prompts.py`, `backend/routers/workflows.py` |
|
|
||||||
| Admin-UI | `frontend/src/pages/AdminPromptsPage.jsx`, `UnifiedPromptModal.jsx`, `WorkflowEditorPage.jsx` |
|
|
||||||
| Fachliche Spec | `.claude/docs/functional/AI_PROMPTS.md` |
|
|
||||||
| Platzhalter-Governance | `.claude/docs/technical/PLACEHOLDER_REGISTRY_FRAMEWORK.md`, `docs/PLACEHOLDER_GOVERNANCE.md` |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Modul
|
|
||||||
|
|
||||||
**Prompt Engine** (Unified Prompt System, Issue #28)
|
|
||||||
|
|
||||||
Backend-Kern: `prompt_executor.py`, `placeholder_resolver.py`, `placeholder_registry` / `placeholder_registrations/`, `workflow_executor.py`
|
|
||||||
API: `routers/prompts.py`
|
|
||||||
Admin-UI: `AdminPromptsPage`, `UnifiedPromptModal`, `WorkflowEditorPage`
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Fachliche Verantwortung
|
|
||||||
|
|
||||||
Die Prompt Engine ist die **zentrale Ausführungs- und Konfigurationsschicht für KI-Analysen**. Sie übernimmt:
|
|
||||||
|
|
||||||
1. **Prompt-Orchestrierung** — Laden aktiver Prompt-Definitionen aus der DB und Ausführung als `base` (Einzelprompt), `pipeline` (mehrstufig) oder `workflow` (Graph).
|
|
||||||
2. **Kontextaufbereitung** — Befüllen von Platzhaltern mit profilbezogenen Daten (Resolver → Data Layer).
|
|
||||||
3. **LLM-Aufruf** — Einheitlicher OpenRouter-Call über injizierte Callback-Funktion.
|
|
||||||
4. **Ergebnisbehandlung** — JSON-Validierung, strukturierte Container (Fragenergänzungen), Debug-Metadaten, optionales Speichern in `ai_insights`.
|
|
||||||
5. **Admin-Konfiguration** — CRUD für Prompts/Workflows, Import/Export, Vorschau und Test ohne Produktions-Ausführung.
|
|
||||||
|
|
||||||
### Administrierte Konfigurationen
|
|
||||||
|
|
||||||
| Konfiguration | Speicherort | Inhalt |
|
|
||||||
|---------------|-------------|--------|
|
|
||||||
| Prompt-Metadaten | `ai_prompts` | `name`, `slug`, `category`, `active`, `sort_order`, `display_name` |
|
|
||||||
| Templates | `ai_prompts` | `template` |
|
|
||||||
| Pipeline-Stages | `ai_prompts.stages` (JSONB) | Stages mit `inline` / `reference` |
|
|
||||||
| Workflow-Graphen | `ai_prompts.graph_data` | Knoten, Kanten, Metadaten |
|
|
||||||
| Output-Regeln | `ai_prompts` | `output_format`, `output_schema` |
|
|
||||||
| Fragenergänzungen | `ai_prompts.question_augmentations` | Optionale Standard-Fragen (Hybridmodell: Knoten > Prompt) |
|
|
||||||
| System-Reset | `ai_prompts` | `is_system_default`, `default_template` |
|
|
||||||
| Legacy-Pipeline-Configs | `pipeline_configs` | Module, Zeiträume, Stage-Slugs (parallel zum Unified System) |
|
|
||||||
| Workflow-Fragenkatalog | `workflow_question_catalog` | Fragetypen, Templates, Normalisierung |
|
|
||||||
|
|
||||||
### Bewusst nicht hardcodiert
|
|
||||||
|
|
||||||
- Prompt-Texte, Pipeline-Zusammensetzung, Workflow-Topologie
|
|
||||||
- Kategorie, Sichtbarkeit (`active`), Sortierung
|
|
||||||
- Output-Format und Schema pro Prompt
|
|
||||||
- Referenz vs. Inline in Pipeline-Stages
|
|
||||||
|
|
||||||
### Hardcodiert (Code / Env)
|
|
||||||
|
|
||||||
- Platzhalter-Definitionen und Resolver (`PLACEHOLDER_MAP`, Registry)
|
|
||||||
- LLM-Modell (`OPENROUTER_MODEL`)
|
|
||||||
- Default-Module und -Zeiträume in `/prompts/execute`
|
|
||||||
- Domänen-Kategorien im Frontend (`analysisCategories.js`)
|
|
||||||
- Meta-Prompts für Generate/Optimize (Admin-Tooling)
|
|
||||||
|
|
||||||
### Trennung: Template · Platzhalter · Kontext · Workflow
|
|
||||||
|
|
||||||
| Schicht | Ort | Rolle |
|
|
||||||
|---------|-----|-------|
|
|
||||||
| **Templates** | `ai_prompts.template`, `stages`, Knoten-Templates im Graph | Was an die KI geht |
|
|
||||||
| **Platzhalter** | `placeholder_resolver` + Registry | Semantische API-Keys `{{key}}`, Resolver-Funktionen |
|
|
||||||
| **Kontextdaten** | `execute_prompt_with_data` + Resolver → `data_layer/` | Werte für Platzhalter |
|
|
||||||
| **Workflows** | `graph_data` + `workflow_executor` | Ausführungsgraph, Verzweigung, Join, Aggregation |
|
|
||||||
|
|
||||||
### Durchsetzung: keine Sonderlogik außerhalb der Engine
|
|
||||||
|
|
||||||
**Konzeptionell:** Ein Executor (`execute_prompt` → `execute_prompt_with_data`) als Single Entry Point.
|
|
||||||
|
|
||||||
**Praktisch unvollständig:** Legacy-Pfade in `insights.py` (`/insights/run`, `/insights/pipeline`) mit eigener Template-Logik (`_prepare_template_vars`, `_render_template`) und direkten LLM-Calls; `History.jsx` nutzt noch `runInsight`. Kein technischer Guard (Lint/Policy), nur Konvention.
|
|
||||||
|
|
||||||
### Rollen und Berechtigungen
|
|
||||||
|
|
||||||
| Rolle | Darf |
|
|
||||||
|-------|------|
|
|
||||||
| **Admin** (`require_admin`) | Prompts/Workflows/Pipeline-Configs CRUD, Import/Export, Reset-to-default, Generate/Optimize, Platzhalter-Metadaten-ZIP |
|
|
||||||
| **Nutzer** (`require_auth`) | Aktive Prompts listen (ohne Pipeline-Slugs), ausführen (`/prompts/execute`), Preview, Platzhalter-Katalog, eigene Werte exportieren |
|
|
||||||
|
|
||||||
Workflow-Editor-Route (`/workflow-editor/:id`) ist nicht hinter `RequireAdmin`; Schreib-APIs sind admin-geschützt.
|
|
||||||
|
|
||||||
### Versionierung, Freigabe, Test
|
|
||||||
|
|
||||||
| Mechanismus | Status |
|
|
||||||
|-------------|--------|
|
|
||||||
| Prompt-Versionsverlauf in DB | ❌ Overwrite |
|
|
||||||
| Reset-to-default für System-Prompts | ✅ `is_system_default` + `default_template` |
|
|
||||||
| JSON Import/Export (Dev→Prod) | ✅ `/export-all`, `/import` |
|
|
||||||
| Admin-Test mit Debug | ✅ `debug=true`, UnifiedPromptModal |
|
|
||||||
| Preview ohne LLM | ✅ `POST /preview` |
|
|
||||||
| Platzhalter-Deprecation-Prozess | 📄 dokumentiert, nicht runtime-erzwungen |
|
|
||||||
| Formales Freigabe-Workflow | ❌ |
|
|
||||||
| Executor-E2E-Tests | ⚠️ punktuell (Modifier, Output-Compact) |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Designprinzipien
|
|
||||||
|
|
||||||
### 1. Single Executor für Prompt-Ausführung
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Alle KI-Analysen laufen über `execute_prompt` / `execute_prompt_with_data`. |
|
|
||||||
| **Begründung** | Einheitliche Platzhalter-Auflösung, Debug, JSON-Validierung, Speicher-Metadaten. |
|
|
||||||
| **Quelle** | `backend/prompt_executor.py`; `POST /api/prompts/execute`; `Analysis.jsx` → `executeUnifiedPromptStream` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Legacy `insights.py` und teils `History.jsx` umgehen den Executor noch. |
|
|
||||||
|
|
||||||
### 2. Konfigurierbare Prompt-Bibliothek statt fest verdrahteter Texte
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Prompt-Inhalte und Workflows liegen in `ai_prompts`, nicht im Anwendungscode. |
|
|
||||||
| **Begründung** | Admins können Analysen anpassen, duplizieren, deaktivieren, ohne Deploy. |
|
|
||||||
| **Quelle** | Migration 020; `UnifiedPromptCreate`/`Update` in `models.py`; `AdminPromptsPage.jsx` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Parallel existieren noch `pipeline_configs` und hardcodierte Default-Module/Zeiträume. |
|
|
||||||
|
|
||||||
### 3. Drei Prompt-Typen mit klarer Verantwortung
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `base` = wiederverwendbarer Baustein; `pipeline` = sequenzielle Stages; `workflow` = Graph mit Verzweigung. |
|
|
||||||
| **Begründung** | Komposition ohne Copy-Paste; Reference-Prompts in Pipelines (`source: 'reference'`). |
|
|
||||||
| **Quelle** | `execute_prompt()` Typ-Verzweigung; `StagePromptCreate` in `models.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Pipeline-Stages laufen sequentiell, obwohl konzeptionell „parallel“; `workflow_definitions` und `ai_prompts.graph_data` doppelt. |
|
|
||||||
|
|
||||||
### 4. Platzhalter als API-Verträge (Registry)
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Platzhalter sind registrierte, dokumentierte Verträge — keine freien Prompt-Hilfsvariablen. |
|
|
||||||
| **Begründung** | Konsistenz für Injektion, GUI-Picker, Export, Validierung. |
|
|
||||||
| **Quelle** | `PLACEHOLDER_REGISTRY_FRAMEWORK.md`; `docs/PLACEHOLDER_GOVERNANCE.md`; `import placeholder_registrations` in `main.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Duplikat `PLACEHOLDER_MAP` in `placeholder_resolver.py` neben Registry; Metadaten teils noch Legacy. |
|
|
||||||
|
|
||||||
### 5. Trennung Template (Was) vs. Resolver (Daten)
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Templates enthalten nur `{{keys}}`; Berechnung liegt in Resolver/Data Layer. |
|
|
||||||
| **Begründung** | Prompt-Autoren ändern Text, nicht Berechnungslogik. |
|
|
||||||
| **Quelle** | `resolve_placeholders()` in `prompt_executor.py`; Registry-Felder `resolver_function`, `data_layer_function` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | `execute_prompt_with_data` lädt zusätzlich Roh-SQL pro Modul — zweite Kontext-Schicht. |
|
|
||||||
|
|
||||||
### 6. Layer-1-Daten vs. Layer-2a-Prompt-Injektion
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Berechnungen in `data_layer/`; Prompt Engine konsumiert nur formatierte Werte. |
|
|
||||||
| **Begründung** | Single Source of Truth für Charts, Platzhalter, KI. |
|
|
||||||
| **Quelle** | Phase-0c-Architektur; Registry-Felder `data_layer_module` / `layer_1_decision` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Legacy `_prepare_template_vars` in `insights.py` umgeht Data Layer. |
|
|
||||||
|
|
||||||
### 7. Transparenz durch Debug- und Preview-Modus
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Aufgelöste/unaufgelöste Platzhalter, Final-Prompt und Stage-Outputs sind inspizierbar; Preview ohne LLM. |
|
|
||||||
| **Begründung** | Admin kann Prompts testen und Wertetabelle/Expertenmodus speisen. |
|
|
||||||
| **Quelle** | `debug`-Parameter; `/preview`; `UnifiedPromptModal` Test-Button; `ai_insights.metadata` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Debug-Daten in Responses können groß/sensibel sein; kein separates Staging. |
|
|
||||||
|
|
||||||
### 8. Wiederverwendbare Base-Prompts via Reference
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Pipeline-Stages referenzieren Slugs statt Templates zu duplizieren. |
|
|
||||||
| **Begründung** | Ein Baustein, mehrere Workflows; zentral wartbar. |
|
|
||||||
| **Quelle** | `source == 'reference'` in `execute_pipeline_prompt()` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Keine Referenz-Versionierung; Änderung am Base-Prompt wirkt sofort auf alle Referenzen. |
|
|
||||||
|
|
||||||
### 9. Strukturierte LLM-Ausgaben per Output-Format
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Pro Prompt/Prompt-Def: `output_format: text\|json`, optional `output_schema`; Pipeline-Outputs als Stage-Keys im Kontext. |
|
|
||||||
| **Begründung** | Maschinenlesbare Zwischenergebnisse für Multi-Stage und Wertetabelle. |
|
|
||||||
| **Quelle** | `validate_json_output()`; Stage `output_key` in Pipeline |
|
|
||||||
| **Tragfähigkeit** | **mittel** |
|
|
||||||
| **Einschränkung** | JSON-Schema-Validierung ist TODO (`jsonschema`); Markdown-Unwrap als Heuristik. |
|
|
||||||
|
|
||||||
### 10. Admin-only Konfiguration, User-only Ausführung
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Schreibende Prompt-/Workflow-Operationen nur mit `require_admin`. |
|
|
||||||
| **Begründung** | Produktions-Prompts sind Systemkonfiguration, nicht Nutzerdaten. |
|
|
||||||
| **Quelle** | `require_admin` in `routers/prompts.py`; `RequireAdmin` für `/admin/prompts` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | `/workflow-editor/:id` ohne Frontend-Admin-Gate; `/prompts/execute` ohne `check_feature_access` (Legacy-Pfad in `insights.py` hat Enforcement). |
|
|
||||||
|
|
||||||
### 11. Import/Export als Umgebungs-Sync
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Prompt-Sätze als JSON exportierbar/importierbar (Dev→Prod). |
|
|
||||||
| **Begründung** | Konfiguration versionierbar in Git, nicht in der App-DB. |
|
|
||||||
| **Quelle** | `GET /export-all`, `POST /import` in `routers/prompts.py`; Admin-UI Buttons |
|
|
||||||
| **Tragfähigkeit** | **mittel** |
|
|
||||||
| **Einschränkung** | Kein Diff, keine Merge-Strategie, kein Rollback; Overwrite-Flag manuell. |
|
|
||||||
|
|
||||||
### 12. Workflow-Erweiterung: Graph + Fragenergänzungen + Signale
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Workflows als Knoten/Kanten-Graph; optionale Fragen am Knoten; Normalisierung/Logic/Join als Engine-Schicht. |
|
|
||||||
| **Begründung** | Bedingte, verzweigte Analysen jenseits linearer Pipelines. |
|
|
||||||
| **Quelle** | `workflow_executor.py`; Migration 034; `question_augmenter.py` |
|
|
||||||
| **Tragfähigkeit** | **mittel** |
|
|
||||||
| **Einschränkung** | Hohe Komplexität; zwei Speicherorte (`graph_data` vs. `workflow_definitions`); Jinja2 im Workflow-Pfad zusätzlich zu `{{}}`-Resolver. |
|
|
||||||
|
|
||||||
### 13. Platzhalter-Modifier für KI-Kontext
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `{{key\|d}}` (Wert + Beschreibung), `{{key\|x}}` (Erklärung ohne Zahl) über Katalog-Metadaten. |
|
|
||||||
| **Begründung** | Prompts können Kontext für das Modell reichhaltiger machen ohne Template-Duplikate. |
|
|
||||||
| **Quelle** | `resolve_placeholders()` Modifier-Logik; `get_placeholder_catalog()` |
|
|
||||||
| **Tragfähigkeit** | **mittel** |
|
|
||||||
| **Einschränkung** | Modifier-Syntax ad hoc; Katalog-Pflicht für sinnvolle `\|x`-Nutzung. |
|
|
||||||
|
|
||||||
### 14. System-Prompt-Reset statt DB-Versionierung
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Shipped Prompts mit `is_system_default` + `default_template`; Admin-Reset auf Original. |
|
|
||||||
| **Begründung** | Schutz vor irreversiblen Fehlkonfigurationen ohne vollständiges Versionsmodell. |
|
|
||||||
| **Quelle** | Migration 019; `POST /{prompt_id}/reset-to-default` |
|
|
||||||
| **Tragfähigkeit** | **mittel** |
|
|
||||||
| **Einschränkung** | Nur ein Default-Snapshot; keine Historie benutzerdefinierter Änderungen. |
|
|
||||||
|
|
||||||
### 15. Governance für Platzhalter-Änderungen
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Breaking Changes nur über Deprecation + Replacement; semantische Verträge dokumentiert. |
|
|
||||||
| **Begründung** | Prompts in Produktion brechen nicht still. |
|
|
||||||
| **Quelle** | `docs/PLACEHOLDER_GOVERNANCE.md` §4 |
|
|
||||||
| **Tragfähigkeit** | **mittel** (prozessual) |
|
|
||||||
| **Einschränkung** | Prozess in Doku, nicht im Runtime erzwungen; Checkliste verweist noch auf Legacy-Dateien. |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Nicht übernehmen
|
|
||||||
|
|
||||||
Muster, die sich nicht bewährt haben oder zu produktspezifisch sind — bei Neuentwicklung vermeiden:
|
|
||||||
|
|
||||||
1. **Parallele Ausführungspfade** — Legacy `insights.py` (`/insights/run`, `/insights/pipeline`) mit eigener Template-Engine und LLM-Calls neben `prompt_executor`; Frontend-Split (`Analysis` vs. `History`).
|
|
||||||
|
|
||||||
2. **Doppelte Metadaten für Platzhalter** — `PLACEHOLDER_MAP`, Registry, `placeholder_metadata_complete.py` und Katalog-Fallbacks parallel; erzeugt Sync-Risiko (114 Keys müssen deckungsgleich bleiben).
|
|
||||||
|
|
||||||
3. **Zwei Pipeline-Modelle gleichzeitig** — `pipeline_configs` (3 fixe Stages) und Unified `type=pipeline` in `ai_prompts`; Migration 020 migriert, Tabelle bleibt aktiv.
|
|
||||||
|
|
||||||
4. **Zwei Workflow-Speicher** — `workflow_definitions.graph` und `ai_prompts.graph_data`; unklare Single Source of Truth.
|
|
||||||
|
|
||||||
5. **Roh-SQL-Kontextladung im Executor** — `execute_prompt_with_data` lädt Modul-Rohdaten per SQL, obwohl Resolver/Data Layer existieren; Domänenwissen in der Engine.
|
|
||||||
|
|
||||||
6. **Hardcodierte Execute-Defaults** — Module/Zeiträume in `/execute` fest verdrahtet statt aus Prompt-/Pipeline-Konfiguration.
|
|
||||||
|
|
||||||
7. **Fehlende Feature-Enforcement-Konsistenz** — `check_feature_access` auf Legacy-Insights, nicht auf `/prompts/execute`.
|
|
||||||
|
|
||||||
8. **„Parallel“ als sequentiell implementiert** — Pipeline-Stages kommentiert als parallel, Code sequentiell; irreführendes Modell.
|
|
||||||
|
|
||||||
9. **Unvollständige Output-Validierung** — JSON-Parse + Markdown-Unwrap, Schema-Check auskommentiert/TODO.
|
|
||||||
|
|
||||||
10. **Workflow-Editor ohne klares Admin-Gate in Routing** — Schreib-API geschützt, UI-Route für alle Authentifizierten erreichbar.
|
|
||||||
|
|
||||||
11. **Domänen-spezifische Hardcodings in der Engine** — Kategorien (`körper`, `ernährung`, …), deutsche Meta-Prompts für Generate/Optimize, Fitness-Kontext in Prompt-Generator.
|
|
||||||
|
|
||||||
12. **Kein integriertes Prompt-Versions- und Freigabemodell** — Overwrite + JSON-Export ersetzt keine Revision/Review/Publish-Pipeline.
|
|
||||||
|
|
||||||
13. **Issue #51 (Seitenzuordnung) nicht umgesetzt** — Prompt-Verfügbarkeit kontextuell nicht konfigurierbar; alles über zentrale Analyse-Seite.
|
|
||||||
|
|
||||||
14. **Globales LLM-Modell per Env** — `workflow_executor` übergibt Modell pro Call, `call_openrouter` ignoriert es; keine prompt-spezifische Modellwahl trotz API-Shape.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Verwandte Dokumentation
|
|
||||||
|
|
||||||
- Fachliche Spec: [AI_PROMPTS.md](../../functional/AI_PROMPTS.md)
|
|
||||||
- Platzhalter-Registry: [PLACEHOLDER_REGISTRY_FRAMEWORK.md](../../technical/PLACEHOLDER_REGISTRY_FRAMEWORK.md)
|
|
||||||
- Platzhalter-Governance: [PLACEHOLDER_GOVERNANCE.md](../../../../docs/PLACEHOLDER_GOVERNANCE.md)
|
|
||||||
- Issue #28 (Unified Prompt System): abgeschlossen, siehe `CLAUDE.md`
|
|
||||||
- Issue #51 (Prompt-Seitenzuordnung): [issue-51-prompt-page-assignment.md](../../../../docs/issues/issue-51-prompt-page-assignment.md)
|
|
||||||
- **Serie (abgeschlossen):** [Index](./README.md) · [Jinkendo Foundation](../README.md) · Dokumente #1–#9
|
|
||||||
@@ -1,136 +0,0 @@
|
|||||||
# Designprinzipien – Index (Jinkendo Foundation)
|
|
||||||
|
|
||||||
**Teil von:** [Jinkendo Foundation](../README.md)
|
|
||||||
|
|
||||||
**Status:** Arbeitspapier / Übergabe
|
|
||||||
**Stand:** 2026-07-04
|
|
||||||
**Zweck:** Zentraler Einstieg für die aus Mitai extrahierte Serie **tragfähiger Designprinzipien** — für neue Apps der Jinkendo-Produktfamilie oder vergleichbare Self-Hosted-PWAs.
|
|
||||||
|
|
||||||
**Nicht enthalten:** Mitai-Gesamtarchitektur, Domänenlogik (Gesundheit/Ernährung), Kairo-/Framework-Empfehlungen.
|
|
||||||
|
|
||||||
**Ablage:** `.claude/docs/jinkendo-foundation/design-principles/` · Regeln: [DOCUMENTATION.md](../../../rules/DOCUMENTATION.md)
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Wofür diese Serie?
|
|
||||||
|
|
||||||
Mitai Jinkendo implementiert wiederkehrende **Querschnittsmuster** (Prompt-Ausführung, Data Layer, Entitlements, Registries, Auth, Import, Dashboard, Navigation, Deploy). Die neun Dokumente destillieren daraus:
|
|
||||||
|
|
||||||
- **Was** übertragbar ist (Prinzip + Begründung + Tragfähigkeit)
|
|
||||||
- **Was** bewusst nicht kopiert werden soll („Nicht übernehmen“)
|
|
||||||
- **Wo** im Code nachgeschaut werden kann (Pfade, Agent-Guides)
|
|
||||||
|
|
||||||
Jedes Dokument ist **eigenständig lesbar**; dieser Index ordnet Abhängigkeiten und Lese-Reihenfolge.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Dokumente (9/9)
|
|
||||||
|
|
||||||
| # | Modul | Datei | Kernidee (1 Satz) |
|
|
||||||
|---|-------|-------|-------------------|
|
|
||||||
| 1 | Prompt Engine | [PROMPT_ENGINE_DESIGN_PRINCIPLES.md](./PROMPT_ENGINE_DESIGN_PRINCIPLES.md) | Ein Executor, Pipeline/Workflow-Typen, Platzhalter über Registry — keine Raw-Template-Ausführung. |
|
|
||||||
| 2 | Data Layer | [DATA_LAYER_DESIGN_PRINCIPLES.md](./DATA_LAYER_DESIGN_PRINCIPLES.md) | Layer 0→1→2: Single Source of Truth für Berechnungen; Charts und KI nutzen dieselbe Schicht. |
|
|
||||||
| 3 | Feature & Entitlement | [FEATURE_ENTITLEMENT_DESIGN_PRINCIPLES.md](./FEATURE_ENTITLEMENT_DESIGN_PRINCIPLES.md) | Zentrale `check_feature_access`, DB-Registry, 4-Phasen-Rollout — Enforcement an der API. |
|
|
||||||
| 4 | Registry / Plugin (Meta) | [REGISTRY_PLUGIN_DESIGN_PRINCIPLES.md](./REGISTRY_PLUGIN_DESIGN_PRINCIPLES.md) | Drei Registry-Muster (Platzhalter, Widgets, CSV): SSoT, Validierung an der Grenze, Runtime-Registrierung. |
|
|
||||||
| 5 | Auth & Session | [AUTH_SESSION_DESIGN_PRINCIPLES.md](./AUTH_SESSION_DESIGN_PRINCIPLES.md) | Server-Sessions, `require_auth` als Depends — bekannte Schwäche: Profile-Header ohne Session-Bindung. |
|
|
||||||
| 6 | Universal Import | [UNIVERSAL_IMPORT_DESIGN_PRINCIPLES.md](./UNIVERSAL_IMPORT_DESIGN_PRINCIPLES.md) | Modul-Registry + Executor + Vorlagen; Ingest ≠ Interpretation; SAVEPOINT pro Zeile. |
|
|
||||||
| 7 | Dashboard Widgets | [DASHBOARD_WIDGETS_DESIGN_PRINCIPLES.md](./DASHBOARD_WIDGETS_DESIGN_PRINCIPLES.md) | Backend-Katalog, Profil-Layout, Config-Whitelist, Entitlements — Dual Registry mit Frontend. |
|
|
||||||
| 8 | Navigation / IA | [NAVIGATION_IA_DESIGN_PRINCIPLES.md](./NAVIGATION_IA_DESIGN_PRINCIPLES.md) | `appNav` als SSoT, Shells für tiefe Bereiche, Admin-Hub, ein Breakpoint 1024px. |
|
|
||||||
| 9 | Migration & Deploy | [MIGRATION_DEPLOY_DESIGN_PRINCIPLES.md](./MIGRATION_DEPLOY_DESIGN_PRINCIPLES.md) | `XXX_*.sql` beim Container-Start; develop/main → Dev/Prod; fail-fast, kein Auto-Rollback. |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Empfohlene Lesereihenfolge
|
|
||||||
|
|
||||||
### Schnellüberblick (30 Min)
|
|
||||||
|
|
||||||
1. [REGISTRY_PLUGIN_DESIGN_PRINCIPLES.md](./REGISTRY_PLUGIN_DESIGN_PRINCIPLES.md) — Meta-Muster für viele Module
|
|
||||||
2. [DATA_LAYER_DESIGN_PRINCIPLES.md](./DATA_LAYER_DESIGN_PRINCIPLES.md) — Daten vs. Darstellung
|
|
||||||
3. [MIGRATION_DEPLOY_DESIGN_PRINCIPLES.md](./MIGRATION_DEPLOY_DESIGN_PRINCIPLES.md) — Betrieb & Schema-Evolution
|
|
||||||
|
|
||||||
### Vollständige Implementierung (neues Produkt)
|
|
||||||
|
|
||||||
```
|
|
||||||
Foundation: (9) Migration & Deploy → (5) Auth → (3) Feature & Entitlement
|
|
||||||
Daten: (2) Data Layer → (6) Universal Import
|
|
||||||
Erweiterung: (4) Registry Meta → (1) Prompt Engine → (7) Dashboard Widgets
|
|
||||||
Oberfläche: (8) Navigation / IA
|
|
||||||
```
|
|
||||||
|
|
||||||
### Nur ein Modul nachbauen
|
|
||||||
|
|
||||||
| Ziel | Lese zuerst | Dann |
|
|
||||||
|------|-------------|------|
|
|
||||||
| KI-Analysen | #1 Prompt Engine | #2 Data Layer, #4 Registry |
|
|
||||||
| Charts & KPIs | #2 Data Layer | #7 Dashboard Widgets |
|
|
||||||
| Freemium / Limits | #3 Feature & Entitlement | #7 Widgets (Gating) |
|
|
||||||
| CSV-Import | #6 Universal Import | #2 Data Layer, #4 Registry |
|
|
||||||
| Admin-PWA | #8 Navigation / IA | #3, #5 |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Querschnittsthemen (über alle Docs)
|
|
||||||
|
|
||||||
| Thema | Primär | Ergänzend |
|
|
||||||
|-------|--------|-----------|
|
|
||||||
| Single Source of Truth | #2 Data Layer | #4 Registry, #7 Widget-Katalog, #6 Modul-Registry |
|
|
||||||
| Validierung an der Grenze | #4 Registry | #7 Layout-Pydantic, #6 Template-Validator |
|
|
||||||
| Feature-Gating | #3 Entitlement | #7 Widget `allowed`, #6 Import-Limits |
|
|
||||||
| Dual Registry (Backend + Frontend) | #4 Registry | #7 `registerDashboardWidgets`, Platzhalter-UI |
|
|
||||||
| Idempotenz / Recovery | #9 Migration | #6 SAVEPOINT, Migration `IF NOT EXISTS` |
|
|
||||||
| Bekannte Lücken dokumentieren | alle „Nicht übernehmen“ | z. B. IDOR Profile-Header (#5), fehlender Cross-Check Widget-IDs (#7) |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Verwandte normative Docs (Mitai-spezifisch)
|
|
||||||
|
|
||||||
Diese Agent-Guides sind **Implementierungsdetail**; die Designprinzipien-Serie ist **extrahiertes Muster**:
|
|
||||||
|
|
||||||
| Thema | Agent-Guide / Spec |
|
|
||||||
|-------|-------------------|
|
|
||||||
| Platzhalter | [PLACEHOLDER_REGISTRY_FRAMEWORK.md](../../technical/PLACEHOLDER_REGISTRY_FRAMEWORK.md) |
|
|
||||||
| Data Layer erweitern | [DATA_LAYER_EXTENSION_GUIDE.md](../../technical/DATA_LAYER_EXTENSION_GUIDE.md) |
|
|
||||||
| CSV Import | [UNIVERSAL_CSV_IMPORT_AGENT_GUIDE.md](../../technical/UNIVERSAL_CSV_IMPORT_AGENT_GUIDE.md) |
|
|
||||||
| Dashboard Widgets | [DASHBOARD_WIDGETS_AGENT_GUIDE.md](../../technical/DASHBOARD_WIDGETS_AGENT_GUIDE.md) |
|
|
||||||
| GUI / Admin / Nav | [GUI_IA_ADMIN_NAV_2026-04-05.md](../../../../docs/issues/GUI_IA_ADMIN_NAV_2026-04-05.md) |
|
|
||||||
| Migrationen (operativ) | [MIGRATIONS.md](../../technical/MIGRATIONS.md) |
|
|
||||||
| Membership | [MEMBERSHIP_SYSTEM.md](../../technical/MEMBERSHIP_SYSTEM.md), [FEATURE_ENFORCEMENT.md](../../architecture/FEATURE_ENFORCEMENT.md) |
|
|
||||||
| Architektur-Regeln | [ARCHITECTURE.md](../../../rules/ARCHITECTURE.md) |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Übergabe-Checkliste (neues Produkt in der Familie)
|
|
||||||
|
|
||||||
Nutze diese Liste beim Start eines Schwester-Projekts — **Prinzipien ja, Mitai-Pfade nein**:
|
|
||||||
|
|
||||||
```
|
|
||||||
[ ] Deploy: nummerierte SQL-Migrationen + Tracking-Tabelle + Startup vor App
|
|
||||||
[ ] Auth: Session server-side; Depends(require_auth); Admin-Route-Guard
|
|
||||||
[ ] Entitlements: eine check_feature_access-Funktion; keine Tier-Logik in UI-Widgets
|
|
||||||
[ ] Data Layer: Berechnungen nicht in Router/React duplizieren
|
|
||||||
[ ] Registry: neue erweiterbare IDs nur über zentralen Katalog + Validierung
|
|
||||||
[ ] Import (falls CSV): Modul-Registry + Vorlagen + Import-Grenze (keine Scores beim Insert)
|
|
||||||
[ ] Dashboard (falls konfigurierbar): Backend-Katalog + Layout-JSON + allowed-Flag
|
|
||||||
[ ] Navigation: eine appNav-SSoT; Shells für >6 Top-Level-Bereiche
|
|
||||||
[ ] Prompt/KI (falls): ein Executor; Platzhalter-Registry; kein Raw-Template an LLM
|
|
||||||
[ ] Dokumentieren: pro Modul „Nicht übernehmen“ aus Mitai-Lücken mitnehmen
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Pflege
|
|
||||||
|
|
||||||
| Aktion | Wo |
|
|
||||||
|--------|-----|
|
|
||||||
| Neues Querschnittsmodul extrahiert | Neues `*_DESIGN_PRINCIPLES.md` + Zeile in Tabelle oben |
|
|
||||||
| Mitai-Implementierung ändert Muster | Betroffenes Einzeldokument + ggf. Querschnittstabelle hier |
|
|
||||||
| Nur Mitai-Bugfix ohne Muster-Änderung | Agent-Guides / Code; Designprinzipien optional |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Changelog Index
|
|
||||||
|
|
||||||
| Datum | Änderung |
|
|
||||||
|-------|----------|
|
|
||||||
| 2026-07-04 | Index angelegt; Serie 1–9 abgeschlossen |
|
|
||||||
| 2026-07-04 | Nach `jinkendo-foundation/design-principles/` verschoben |
|
|
||||||
-315
@@ -1,315 +0,0 @@
|
|||||||
# Registry- & Plugin-Muster – Designprinzipien (Extraktion)
|
|
||||||
|
|
||||||
**Status:** Analyse / Arbeitspapier
|
|
||||||
**Stand:** 2026-07-04
|
|
||||||
**Geltungsbereich:** Querschnittsmuster für erweiterbare Registries — drei Implementierungen in Mitai (Platzhalter, Dashboard-Widgets, CSV-Import)
|
|
||||||
|
|
||||||
**Serie:** Designprinzipien für Produktfamilie · Dokument 4 von n
|
|
||||||
**Vorgänger:** [FEATURE_ENTITLEMENT_DESIGN_PRINCIPLES.md](./FEATURE_ENTITLEMENT_DESIGN_PRINCIPLES.md)
|
|
||||||
|
|
||||||
**Die drei Registries:**
|
|
||||||
|
|
||||||
| Registry | Backend-Kanon | Frontend-/Runtime-Registry | Leitfaden |
|
|
||||||
|----------|---------------|----------------------------|-----------|
|
|
||||||
| **Platzhalter** | `placeholder_registry.py` + `placeholder_registrations/` | `PLACEHOLDER_MAP` in `placeholder_resolver.py` | `PLACEHOLDER_REGISTRY_FRAMEWORK.md` |
|
|
||||||
| **Dashboard-Widgets** | `widget_catalog.py` | `registerDashboardWidgets.js` → `dashboardWidgetRegistry.jsx` | `DASHBOARD_WIDGETS_AGENT_GUIDE.md` |
|
|
||||||
| **CSV-Import-Module** | `csv_parser/module_registry.py` | — (Executor + Admin-UI konsumieren API) | `UNIVERSAL_CSV_IMPORT_AGENT_GUIDE.md` |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Modul
|
|
||||||
|
|
||||||
**Registry- & Plugin-Muster** (Meta-Schicht)
|
|
||||||
|
|
||||||
Wiederkehrendes Architekturmuster: **Zentral registrierte, ID-basierte Erweiterungspunkte** mit Metadaten, Validierung und getrennten Konsumenten (GUI, API, Batch). Kein einzelnes Runtime-Modul — ein **Familien-Designpattern**, das in Mitai dreimal konkret umgesetzt ist.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Fachliche Verantwortung
|
|
||||||
|
|
||||||
Registries übernehmen:
|
|
||||||
|
|
||||||
1. **Autoritative ID-Liste** — Was existiert, was ist erlaubt, in welcher Reihenfolge (optional).
|
|
||||||
2. **Metadaten für Mensch & Maschine** — Titel, Beschreibung, Typen, Abhängigkeiten, semantische Verträge.
|
|
||||||
3. **Validierung** — Unbekannte IDs werden abgelehnt; Konfigurationen gegen Kanon geprüft.
|
|
||||||
4. **Entkopplung** — Implementierung (Resolver, React-Komponente, Import-Executor) registriert sich an den Kanon, nicht umgekehrt.
|
|
||||||
5. **Erweiterbarkeit ohne Schema-Explosion** — Neue Einträge über Code-Registrierung (+ ggf. DB-Overrides), nicht über neue DB-Spalten pro Feature.
|
|
||||||
|
|
||||||
Registries übernehmen **nicht**:
|
|
||||||
|
|
||||||
- Fachliche Berechnung (→ Data Layer)
|
|
||||||
- Entitlement-Auflösung (→ Feature System; Widgets *referenzieren* Features)
|
|
||||||
- Auth / Mandanten
|
|
||||||
|
|
||||||
### Gemeinsames Strukturschema
|
|
||||||
|
|
||||||
```
|
|
||||||
┌─────────────────────────────────────────────────────────┐
|
|
||||||
│ REGISTRY (Kanon) │
|
|
||||||
│ ID + Metadaten + optionale Policies/Abhängigkeiten │
|
|
||||||
└────────────────────┬────────────────────────────────────┘
|
|
||||||
│
|
|
||||||
┌───────────────┼───────────────┐
|
|
||||||
▼ ▼ ▼
|
|
||||||
Implementierung Validierung Konsumenten
|
|
||||||
(Resolver/ (Schema/ (GUI-Picker,
|
|
||||||
Component/ Tests) API, Executor)
|
|
||||||
Executor)
|
|
||||||
```
|
|
||||||
|
|
||||||
### Vergleich der drei Implementierungen
|
|
||||||
|
|
||||||
| Aspekt | Platzhalter | Dashboard-Widgets | CSV-Import |
|
|
||||||
|--------|-------------|-------------------|------------|
|
|
||||||
| **Primär-ID** | `key` (snake_case) | `id` (snake_case) | Modul-Name (`nutrition`, `activity`, …) |
|
|
||||||
| **Kanon-Speicher** | Python Singleton + Cluster-Module | Python-Liste `WIDGET_CATALOG` | Python-Dict `MODULE_DEFINITIONS` |
|
|
||||||
| **Metadaten-Tiefe** | Sehr hoch (22+ Felder, Evidence) | Mittel (title, description, requires_feature) | Hoch (fields, types, duplicate_key, aggregates) |
|
|
||||||
| **Runtime-Registry** | `register_placeholder()` beim Import | `registerDashboardWidget()` idempotent | Keine — Executor liest Dict |
|
|
||||||
| **Frontend-Spiegel** | PlaceholderPicker, Admin-Prompt-Modal | `ensureDashboardWidgetsRegistered()` | Admin CSV Template Editor |
|
|
||||||
| **Validierung** | `metadata.validate()`, Governance-Docs | Pydantic Layout + `validate_widget_entry_config` | `validate_field_mappings`, `validate_csv_template` |
|
|
||||||
| **Tests** | `test_placeholder_metadata.py`, … | `test_widget_catalog.py` | `test_template_validator.py`, … |
|
|
||||||
| **DB-Override** | Nein (nur Code) | Ja (`widget_feature_requirements`, Layout pro Profil) | Ja (Vorlagen, Nutzer-Mappings) |
|
|
||||||
| **Entitlements** | Indirekt (Data/Features) | `requires_feature` → `check_feature_access` | Feature-Limits beim Import |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Designprinzipien (übergreifend)
|
|
||||||
|
|
||||||
### 1. Single Source of Truth für erlaubte IDs
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Jede erweiterbare Einheit hat **eine** autoritative ID-Liste; Router und UI duplizieren keine Feld-/Widget-/Platzhalter-Listen. |
|
|
||||||
| **Begründung** | Verhindert „funktioniert in der UI, scheitert in der API“ und umgekehrt. |
|
|
||||||
| **Quelle** | `module_registry.py` Kommentar; `widget_catalog.py`; `PLACEHOLDER_REGISTRY_FRAMEWORK.md` §2.3 |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Platzhalter: paralleles `PLACEHOLDER_MAP` neben Registry. |
|
|
||||||
|
|
||||||
### 2. ID-Stabilität als Vertrag
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | IDs/Keys sind stabile API-Verträge; Umbenennung = neuer Key + Deprecation, nicht stilles Rename. |
|
|
||||||
| **Begründung** | Prompts, Layouts und CSV-Vorlagen referenzieren IDs persistent. |
|
|
||||||
| **Quelle** | `docs/PLACEHOLDER_GOVERNANCE.md` §4.2–4.3; Widget-Layout in `profiles.dashboard_layout` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Nicht überall runtime-erzwungen (Platzhalter-Governance prozessual). |
|
|
||||||
|
|
||||||
### 3. Metadaten getrennt von Implementierung
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Registry speichert **Was** (ID, Beschreibung, Typ, Policies); Implementierung lebt in separaten Modulen. |
|
|
||||||
| **Begründung** | GUI, Export, Validierung und Docs können Metadaten nutzen ohne Resolver/Component zu laden. |
|
|
||||||
| **Quelle** | `PlaceholderMetadata` Dataclass; `WidgetCatalogEntry`; `MODULE_DEFINITIONS.fields` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Platzhalter bindet `_resolver_func` optional an Metadata-Objekt. |
|
|
||||||
|
|
||||||
### 4. Zwei-Phasen-Registrierung (Backend-Kanon + Runtime-Binding)
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Phase A: Kanon definiert IDs und Metadaten. Phase B: Implementierung registriert sich (Platzhalter-Cluster-Import, `registerDashboardWidget`, Executor nutzt Modul-Def). |
|
|
||||||
| **Begründung** | Backend bleibt autoritativ; Frontend/plugins können nachziehen, Tests können Lücken finden. |
|
|
||||||
| **Quelle** | `import placeholder_registrations` in `main.py`; `ensureDashboardWidgetsRegistered()` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Fehlende Frontend-Registrierung zeigt „Unbekanntes Widget“ — kein Build-Time-Fail. |
|
|
||||||
|
|
||||||
### 5. Auto-Registration via Package-Import
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Side-Effect-Import eines Packages triggert vollständige Registrierung (`placeholder_registrations/__init__.py`). |
|
|
||||||
| **Begründung** | Keine vergessene manuelle Registrierungsliste in `main.py` pro Eintrag. |
|
|
||||||
| **Quelle** | `placeholder_registrations/__init__.py`; `main.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Import-Reihenfolge und zirkuläre Imports beachten. |
|
|
||||||
|
|
||||||
### 6. Validierung am Registry-Rand
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Unbekannte Keys/Widget-IDs/Feld-Mappings werden an Registry-Grenzen abgewiesen, nicht erst in der Business-Logik. |
|
|
||||||
| **Begründung** | Frühes, klares Fehlerbild für Admins und Entwickler. |
|
|
||||||
| **Quelle** | `DashboardWidgetEntry` + `ALLOWED_WIDGET_IDS`; `validate_field_mappings()`; `get_unknown_placeholders()` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Prompt-Templates können unbekannte Platzhalter erst zur Laufzeit offenbaren. |
|
|
||||||
|
|
||||||
### 7. Konsumenten-Agnostik
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Dieselbe Registry bedient mehrere Konsumenten (KI, Charts, Export, Admin-Browser) ohne duplizierte Metadaten. |
|
|
||||||
| **Begründung** | DRY für Beschreibungen, Kategorien, Beispielwerte. |
|
|
||||||
| **Quelle** | `PLACEHOLDER_REGISTRY_FRAMEWORK.md` §2.2; `get_placeholder_catalog()` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Legacy-Export-Pfade mergen noch „Registry + Legacy“. |
|
|
||||||
|
|
||||||
### 8. Erweiterungs-Checkliste statt Ad-hoc
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Jedes Registry hat dokumentierte Schritte A→G (Katalog-Eintrag, Validierung, Tests, Version-Bump). |
|
|
||||||
| **Begründung** | Agenten und Menschen erweitern konsistent; Review an Checkliste. |
|
|
||||||
| **Quelle** | `DASHBOARD_WIDGETS_AGENT_GUIDE.md` §2; `UNIVERSAL_CSV_IMPORT_AGENT_GUIDE.md` §2; `PLACEHOLDER_DEVELOPMENT_GUIDE.md` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Drei separate Guides — kein unified „Registry Agent Guide“. |
|
|
||||||
|
|
||||||
### 9. Tests auf Katalog-Konsistenz
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Automatisierte Tests prüfen Eindeutigkeit, Reihenfolge, Payload-Shape, ID-Abgleich Kanon ↔ abgeleitete Sets. |
|
|
||||||
| **Begründung** | Regression wenn Katalog wächst but Registry/Layout nicht mitzieht. |
|
|
||||||
| **Quelle** | `test_widget_catalog.py`; Placeholder-Metadata-Tests |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Kein Cross-Registry-Test „Frontend widget IDs == backend catalog“. |
|
|
||||||
|
|
||||||
### 10. Optionale Entitlement-Referenz im Kanon
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Registry-Einträge **referenzieren** Feature-IDs (`requires_feature`), lösen Entitlements aber nicht selbst auf. |
|
|
||||||
| **Begründung** | Tier-Logik bleibt in `check_feature_access`; Katalog bleibt deklarativ. |
|
|
||||||
| **Quelle** | `WidgetCatalogEntry.requires_feature`; `dashboard_widget_entitlements.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Platzhalter haben kein direktes `requires_feature` — Gating nur indirekt. |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Designprinzipien (spezifisch pro Registry)
|
|
||||||
|
|
||||||
### Platzhalter-Registry
|
|
||||||
|
|
||||||
| # | Prinzip | Tragfähigkeit | Schwäche |
|
|
||||||
|---|---------|---------------|----------|
|
|
||||||
| P1 | **Semantischer Vertrag** (`semantic_contract`) pro Key — Platzhalter sind API, nicht Prompt-Hilfe | hoch | Viele Legacy-Keys mit schwachem Vertrag |
|
|
||||||
| P2 | **Evidence-Tagging** — Herkunft jedes Metadatenfelds nachvollziehbar | mittel | Pflegeaufwand |
|
|
||||||
| P3 | **Cluster-Module** — Registrierung nach Domäne (`nutrition_part_a`, `body_metrics`, …) | hoch | 114 Keys Sync mit `PLACEHOLDER_MAP` |
|
|
||||||
| P4 | **Data-Layer-Referenz** in Metadata (`data_layer_function`) — Bindung an Layer 1 | hoch | Nicht alle Keys vollständig verknüpft |
|
|
||||||
| P5 | **Singleton** `get_registry()` — globaler Kanon | hoch | Test-Isolation braucht Disziplin |
|
|
||||||
|
|
||||||
### Dashboard-Widget-Registry
|
|
||||||
|
|
||||||
| # | Prinzip | Tragfähigkeit | Schwäche |
|
|
||||||
|---|---------|---------------|----------|
|
|
||||||
| W1 | **Backend-Katalog = ALLOWED_WIDGET_IDS** — Layout-Schema leitet ab | hoch | Frontend-Registry manuell parallel |
|
|
||||||
| W2 | **`merge_missing_catalog_widgets`** — neue Katalog-IDs erscheinen im Layout ohne User-Reset | hoch | — |
|
|
||||||
| W3 | **Strikte `config`-Validierung** nur für whitelisted Widgets (`WIDGETS_ALLOWING_CONFIG`) | hoch | Config-Schemas pro Widget heterogen |
|
|
||||||
| W4 | **Graceful Degradation** — unregistrierte ID → Fehler-Karte, nicht Crash | mittel | Maskiert Deploy-Fehler |
|
|
||||||
| W5 | **WidgetErrorBoundary** pro Instanz | hoch | — |
|
|
||||||
|
|
||||||
### CSV-Modul-Registry
|
|
||||||
|
|
||||||
| # | Prinzip | Tragfähigkeit | Schwäche |
|
|
||||||
|---|---------|---------------|----------|
|
|
||||||
| C1 | **`MODULE_DEFINITIONS` = einzige Feldliste** — Router duplizieren nicht | hoch | Activity erweitert dynamisch um `training_parameters` |
|
|
||||||
| C2 | **Deklarative Duplikat-Strategie** (`duplicate_key`, `update`/`skip`) | hoch | Modul-spezifische Executor-Sonderfälle |
|
|
||||||
| C3 | **`import_row_processing`** — Aggregation in Registry, nicht im Router | hoch | Legacy-Defaults in Modul-Def |
|
|
||||||
| C4 | **`validate_field_mappings`** vor Persistenz | hoch | Nutzer-Mappings teils ohne volle Validator-Parität (#71) |
|
|
||||||
| C5 | **Persistenz-Orchestrator** liest Registry-Felder (`activity_persistence_orchestrator`) | hoch | Nur Activity voll ausgebaut |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Anti-Pattern: Doppel-Registry
|
|
||||||
|
|
||||||
Mitai zeigt an **Platzhaltern** das Risiko explizit:
|
|
||||||
|
|
||||||
```
|
|
||||||
placeholder_registrations/ ──register──► PlaceholderRegistry (Metadata)
|
|
||||||
│ ▲
|
|
||||||
└── resolver in code ──► PLACEHOLDER_MAP (Runtime, 114 Keys)
|
|
||||||
```
|
|
||||||
|
|
||||||
**Regel für Produktfamilie:** Runtime-Auflösung soll Metadata-Registry **lesen**, nicht spiegeln.
|
|
||||||
|
|
||||||
Widgets sind näher am Ideal: Backend `WIDGET_CATALOG` ist Kanon; Frontend muss IDs manuell in `registerDashboardWidgets.js` binden — akzeptabel, aber testbar machen (Cross-Check).
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Nicht übernehmen
|
|
||||||
|
|
||||||
1. **Parallele Runtime-Maps** — `PLACEHOLDER_MAP` + Registry; eine Quelle für Keys und Resolver-Pfad.
|
|
||||||
|
|
||||||
2. **Frontend-Registry ohne Build-/Test-Gate** — fehlende `registerDashboardWidget`-Einträge erst zur Laufzeit sichtbar.
|
|
||||||
|
|
||||||
3. **Metadaten-Duplikation in Export-Code** — hardcodierte Beschreibungen außerhalb der Registry.
|
|
||||||
|
|
||||||
4. **Registry-Einträge ohne Validierungs-Tests** — besonders bei 100+ Platzhaltern.
|
|
||||||
|
|
||||||
5. **Import-Feldlisten in Routern** — alles über `module_registry` (Mitai-Zielbild, noch nicht überall).
|
|
||||||
|
|
||||||
6. **Evidence-/Metadata-Pflicht für einfache Plugins** — 22 Felder für Widgets wären Overkill; **Metadaten-Tiefe an Risiko anpassen**.
|
|
||||||
|
|
||||||
7. **Dynamische Registry aus DB ohne Versionierung** — Widget-Feature-Overrides OK; kompletter Kanon nur in DB wäre schwer testbar.
|
|
||||||
|
|
||||||
8. **Registry ohne Deprecation-Pfad** — Breaking Key-Changes still (Platzhalter-Governance existiert, durchsetzen).
|
|
||||||
|
|
||||||
9. **Entitlements in Registry auflösen** — Widgets richtig: referenzieren; nicht Tier-Logik im Katalog.
|
|
||||||
|
|
||||||
10. **Schlaf-Modul leeres `fields: {}`** — Sondermodus (`import_mode`) statt sauberem Registry-Eintrag; technische Schuld.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Entscheidungsmatrix: Welche Registry-Tiefe?
|
|
||||||
|
|
||||||
| Wenn … | dann Metadaten-Tiefe … | Beispiel |
|
|
||||||
|--------|------------------------|----------|
|
|
||||||
| Externe Verträge / KI / Compliance | Hoch (Vertrag, Evidence, Missing-Policy) | Platzhalter |
|
|
||||||
| UI-Plugin mit optionaler Config | Mittel (ID, title, config schema, feature ref) | Widgets |
|
|
||||||
| Daten-Ingest / Schema-Mapping | Hoch (Typen, keys, constraints) | CSV-Module |
|
|
||||||
| Internes Hilfsmodul | Minimal (ID + Handler-Ref) | — |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Modul-Inventar (Querschnitt)
|
|
||||||
|
|
||||||
```
|
|
||||||
backend/
|
|
||||||
├── placeholder_registry.py
|
|
||||||
├── placeholder_registrations/ # Auto-import Cluster
|
|
||||||
├── placeholder_resolver.py # PLACEHOLDER_MAP (Legacy-Spiegel)
|
|
||||||
├── placeholder_registry_export.py
|
|
||||||
├── widget_catalog.py
|
|
||||||
├── dashboard_layout_schema.py
|
|
||||||
├── dashboard_widget_config.py
|
|
||||||
├── dashboard_widget_entitlements.py
|
|
||||||
├── widget_feature_requirements_db.py
|
|
||||||
└── csv_parser/
|
|
||||||
└── module_registry.py
|
|
||||||
|
|
||||||
frontend/src/
|
|
||||||
├── widgetSystem/
|
|
||||||
│ ├── registerDashboardWidgets.js
|
|
||||||
│ └── dashboardWidgetRegistry.jsx
|
|
||||||
└── components/workflow/panels/PlaceholderPicker.jsx
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Verwandte Dokumentation
|
|
||||||
|
|
||||||
- Platzhalter: [PLACEHOLDER_REGISTRY_FRAMEWORK.md](../../technical/PLACEHOLDER_REGISTRY_FRAMEWORK.md), [PLACEHOLDER_GOVERNANCE.md](../../../../docs/PLACEHOLDER_GOVERNANCE.md)
|
|
||||||
- Widgets: [DASHBOARD_WIDGETS_AGENT_GUIDE.md](../../technical/DASHBOARD_WIDGETS_AGENT_GUIDE.md)
|
|
||||||
- Import: [UNIVERSAL_CSV_IMPORT_AGENT_GUIDE.md](../../technical/UNIVERSAL_CSV_IMPORT_AGENT_GUIDE.md)
|
|
||||||
- Entitlements (Widget-Gating): [FEATURE_ENTITLEMENT_DESIGN_PRINCIPLES.md](./FEATURE_ENTITLEMENT_DESIGN_PRINCIPLES.md)
|
|
||||||
- Data Layer (Platzhalter-Berechnung): [DATA_LAYER_DESIGN_PRINCIPLES.md](./DATA_LAYER_DESIGN_PRINCIPLES.md)
|
|
||||||
- Prompt Engine (Platzhalter-Konsument): [PROMPT_ENGINE_DESIGN_PRINCIPLES.md](./PROMPT_ENGINE_DESIGN_PRINCIPLES.md)
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Geplante Folgedokumente (Serie)
|
|
||||||
|
|
||||||
| # | Modul | Status |
|
|
||||||
|---|-------|--------|
|
|
||||||
| 1 | Prompt Engine | ✅ |
|
|
||||||
| 2 | Data Layer | ✅ |
|
|
||||||
| 3 | Feature & Entitlement | ✅ |
|
|
||||||
| 4 | Registry-/Plugin-Muster | ✅ dieses Dokument |
|
|
||||||
| 5 | Auth & Session | ✅ `AUTH_SESSION_DESIGN_PRINCIPLES.md` |
|
|
||||||
| 6 | Universal Import | ✅ |
|
|
||||||
| 7 | Dashboard Widgets | ✅ |
|
|
||||||
| 8 | Navigation / IA | ✅ |
|
|
||||||
| 9 | Migration & Deploy | ✅ |
|
|
||||||
|
|
||||||
*Hinweis:* Dokumente 6 und 7 vertiefen Einzel-Registries; dieses Meta-Dokument ist die übergreifende Extraktion.
|
|
||||||
-325
@@ -1,325 +0,0 @@
|
|||||||
# Universal CSV Import – Designprinzipien (Extraktion)
|
|
||||||
|
|
||||||
**Status:** Analyse / Arbeitspapier
|
|
||||||
**Stand:** 2026-07-04
|
|
||||||
**Geltungsbereich:** Universal CSV Import (Issue #21) — Ingest/Mapping/Persistenz, keine Auswertungslogik
|
|
||||||
|
|
||||||
**Serie:** Designprinzipien für Produktfamilie · Dokument 6 von n
|
|
||||||
**Vorgänger:** [AUTH_SESSION_DESIGN_PRINCIPLES.md](./AUTH_SESSION_DESIGN_PRINCIPLES.md)
|
|
||||||
|
|
||||||
**Kernkomponenten:**
|
|
||||||
|
|
||||||
| Bereich | Pfade |
|
|
||||||
|---------|-------|
|
|
||||||
| Modul-Kanon | `backend/csv_parser/module_registry.py` |
|
|
||||||
| Ausführung | `backend/csv_parser/executor.py` |
|
|
||||||
| Parsing/Typen | `core.py`, `type_converter.py`, `field_units.py` |
|
|
||||||
| Aggregation | `import_row_processing.py` |
|
|
||||||
| Validierung | `template_validator.py` |
|
|
||||||
| Fehler-Hints | `import_errors.py` |
|
|
||||||
| Mapping-Vorschläge | `mapping_suggest.py` |
|
|
||||||
| Nutzer-API | `backend/routers/csv_import.py` |
|
|
||||||
| Admin-Vorlagen | `backend/routers/admin_csv_templates.py` |
|
|
||||||
| Persistenz-Orchestrator | `data_layer/activity_persistence_orchestrator.py` |
|
|
||||||
| Leitfaden | `UNIVERSAL_CSV_IMPORT_AGENT_GUIDE.md` |
|
|
||||||
| Import-Grenze | `.claude/rules/ARCHITECTURE.md` §8 |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Modul
|
|
||||||
|
|
||||||
**Universal CSV Import**
|
|
||||||
|
|
||||||
Konfigurierbare Pipeline: **CSV-Datei → Feld-Mapping → Typkonvertierung → (optional Aggregation) → DB-Upsert** — mit Vorlagen, Audit-Log und row-level Fehlertoleranz.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Fachliche Verantwortung
|
|
||||||
|
|
||||||
Das Modul übernimmt:
|
|
||||||
|
|
||||||
1. **Modul-Registry** — Welche Zieltabellen/Felder importierbar sind (Typen, Duplikat-Keys, Strategien).
|
|
||||||
2. **Vorlagen (Mappings)** — System-Templates (Admin) + Nutzer-Kopien (`csv_field_mappings`).
|
|
||||||
3. **Analyse** — Delimiter-Erkennung, Spalten-Signatur, Mapping-Vorschläge, Diagnose einzelner Zeilen.
|
|
||||||
4. **Ausführung** — Upsert pro Modul, `source=csv`, Statistik, `affected_ids`.
|
|
||||||
5. **Fehlertransparenz** — Row-level Errors mit `code`/`hint`; kein Silent-Fail der ganzen Transaktion.
|
|
||||||
6. **Audit** — `csv_import_log` mit Status, Counts, betroffenen IDs.
|
|
||||||
7. **Limits** — Dateigröße/Zeilen aus `system_config`; Feature-Entitlements pro Modul.
|
|
||||||
|
|
||||||
Es übernimmt **nicht**:
|
|
||||||
|
|
||||||
- Fachliche Metriken / Scores (→ Data Layer, siehe [DATA_LAYER_DESIGN_PRINCIPLES.md](./DATA_LAYER_DESIGN_PRINCIPLES.md))
|
|
||||||
- Prompt-/KI-Logik
|
|
||||||
- Vollständiger Ersatz aller Legacy-Import-Endpoints (noch parallel)
|
|
||||||
|
|
||||||
### Pipeline (Happy Path)
|
|
||||||
|
|
||||||
```
|
|
||||||
Upload CSV
|
|
||||||
→ decode_raw_bytes + resolve_effective_csv_delimiter
|
|
||||||
→ Vorlage laden (csv_field_mappings)
|
|
||||||
→ validate (optional Admin) / feature check
|
|
||||||
→ run_universal_csv_import(cur, …) // eine Transaktion
|
|
||||||
→ build_row_after_mapping (type_converter)
|
|
||||||
→ aggregate_mapped_rows (import_row_processing)
|
|
||||||
→ UPSERT / activity_persistence_orchestrator
|
|
||||||
→ csv_import_log UPDATE + increment_feature_usage
|
|
||||||
```
|
|
||||||
|
|
||||||
### Unterstützte Module (Registry)
|
|
||||||
|
|
||||||
| Modul | Zieltabelle | Besonderheit |
|
|
||||||
|-------|-------------|--------------|
|
|
||||||
| `nutrition` | `nutrition_log` | Tages-Aggregation |
|
|
||||||
| `weight` | `weight_log` | Duplikat: profile + date |
|
|
||||||
| `activity` | `activity_log` | SAVEPOINT pro Zeile; EAV via Orchestrator |
|
|
||||||
| `vitals_baseline` | `vitals_baseline` | Tages-Aggregation |
|
|
||||||
| `blood_pressure` | `blood_pressure_log` | Composite `measured_at` |
|
|
||||||
| `sleep` | `sleep_log` | Legacy-Adapter `import_mode: apple_sleep_aggregate` |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Administrierte vs. code-definierte Konfiguration
|
|
||||||
|
|
||||||
| Konfiguration | Speicherort | Wer pflegt? |
|
|
||||||
|---------------|-------------|-------------|
|
|
||||||
| Zielfelder, Typen, Duplikat-Keys | `MODULE_DEFINITIONS` | Entwickler (Code) |
|
|
||||||
| System-Vorlagen | `csv_field_mappings` (`is_system=true`) | Admin (+ Migration Seeds) |
|
|
||||||
| Nutzer-Mappings | `csv_field_mappings` (`profile_id`) | Nutzer (Kopie/Anpassung) |
|
|
||||||
| `field_mappings`, `type_conversions`, `import_row_processing` | JSONB in Vorlage | Admin/Nutzer |
|
|
||||||
| Import-Limits | `system_config.csv_import` | Admin |
|
|
||||||
| Delimiter-Sniffing-Heuristik | `core.py` | Code |
|
|
||||||
| Header-Aliases (Vorschläge) | `mapping_suggest.py` | Code |
|
|
||||||
|
|
||||||
**Bewusst nicht in Routern hardcodiert:** Feldlisten, Duplikat-Logik — nur Registry + Executor.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Designprinzipien
|
|
||||||
|
|
||||||
### 1. Module Registry als Single Source of Truth
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Alle erlaubten Zielfelder, Typen und Duplikat-Keys leben in `MODULE_DEFINITIONS` — Router duplizieren nicht. |
|
|
||||||
| **Begründung** | Admin-UI, Validator, Executor und `/api/csv/modules` bleiben synchron. |
|
|
||||||
| **Quelle** | `module_registry.py`; Agent-Guide §1 |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Activity erweitert Felder dynamisch aus `training_parameters` (DB). |
|
|
||||||
|
|
||||||
### 2. Ingest vs. Interpretation (Import-Grenze)
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Import: Mapping + Typ/Einheit + Duplikat/Upsert. **Keine** fachliche Auswertung beim Insert. |
|
|
||||||
| **Begründung** | Semantik gehört in Data Layer; Import bleibt austauschbar und testbar. |
|
|
||||||
| **Quelle** | `ARCHITECTURE.md` §8; [DATA_LAYER_DESIGN_PRINCIPLES.md](./DATA_LAYER_DESIGN_PRINCIPLES.md) |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | `sleep_apple_import.py` ist Legacy-Adapter mit quellenspezifischer Logik. |
|
|
||||||
|
|
||||||
### 3. Vorlagen trennen Struktur von Datei
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `csv_field_mappings` speichert Modul, Delimiter, Header-Flag, Mappings, Conversions, Row-Processing — unabhängig vom Upload. |
|
|
||||||
| **Begründung** | Wiederverwendung (Apple Health, Omron, …); Nutzer wählt Vorlage statt jedes Mal neu zu mappen. |
|
|
||||||
| **Quelle** | Migration 042; Admin + User APIs |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Nutzer-Kopien nicht immer durch `validate_csv_template` (#71). |
|
|
||||||
|
|
||||||
### 4. Effektives Trennzeichen aus Datei, nicht blind aus Vorlage
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `resolve_effective_csv_delimiter` — DE-Export (`;`) vs. EN-Vorlage (`,`) wird aus Header-Feldanzahl erkannt. |
|
|
||||||
| **Begründung** | Regionale CSV-Exporte brechen sonst das gesamte Mapping (eine Spalte). |
|
|
||||||
| **Quelle** | `core.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Heuristik, kein 100%-Garant für exotische Formate. |
|
|
||||||
|
|
||||||
### 5. Typ- und Einheiten-Konvertierung deklarativ
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `type_conversions` + `source_unit` in Vorlage; Logik in `type_converter` / `field_units`. |
|
|
||||||
| **Begründung** | kJ→kcal, Datumsformate, Dezimal-Komma ohne Code pro Quelle. |
|
|
||||||
| **Quelle** | `type_converter.py`, `field_units.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Falsche `source_unit` → DB-Overflow; `enrich_row_error` hilft nachträglich. |
|
|
||||||
|
|
||||||
### 6. Zeilen-Aggregation vor Upsert
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `import_row_processing` (group_by + aggregates) fasst mehrere CSV-Zeilen pro logischem Tag/Datensatz zusammen. |
|
|
||||||
| **Begründung** | Ernährung/Vitals: viele Rohzeilen → ein Tageseintrag. |
|
|
||||||
| **Quelle** | `import_row_processing.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Modul-Default als Legacy-Fallback wenn Vorlage leer; Admin „Format prüfen“ kann Processing auslassen. |
|
|
||||||
|
|
||||||
### 7. Ein Cursor, eine Transaktion, SAVEPOINT pro Zeile
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `run_universal_csv_import(cur, …)` nutzt **bestehenden** Cursor; bei Row-Fehlern SAVEPOINT + ROLLBACK TO, nicht ganze Xact abbrechen. |
|
|
||||||
| **Begründung** | PostgreSQL „transaction aborted“; partielle Imports mit Fehlerliste. |
|
|
||||||
| **Quelle** | `executor.py` (activity, vitals); `csv_import.py` SAVEPOINT `csv_import_exec` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Nicht alle Module gleich implementiert; Disziplin pro Modul. |
|
|
||||||
|
|
||||||
### 8. Kein verschachteltes get_db im Importpfad
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | FK-Auflösung (z. B. Trainingstyp) und Activity-Persistenz mit **demselben** `cur` wie der Import. |
|
|
||||||
| **Begründung** | Pool-Deadlocks, konsistente Transaktion. |
|
|
||||||
| **Quelle** | `_resolve_training_type_for_activity`; Agent-Guide §2 |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Lazy-Import aus Router in Executor (Kopplung). |
|
|
||||||
|
|
||||||
### 9. Strukturierte Fehler mit Hints
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `enrich_row_error()` mappt DB-/Parse-Fehler auf `code` + menschenlesbaren `hint`. |
|
|
||||||
| **Begründung** | Nutzer/Admin können Vorlagen korrigieren ohne PostgreSQL-Kenntnis. |
|
|
||||||
| **Quelle** | `import_errors.py`; Import-Response `error_details` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Heuristische String-Matches, nicht vollständig. |
|
|
||||||
|
|
||||||
### 10. Vorlagen-Validierung vor Persistenz (Admin)
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `validate_csv_template` → `{ valid, errors[], warnings[] }`; Admin Create/Update → HTTP 422 bei Fehlern. |
|
|
||||||
| **Begründung** | Fehler früh, nicht erst beim Nutzer-Import. |
|
|
||||||
| **Quelle** | `template_validator.py`; `admin_csv_templates.py` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Dry-Run / User-Mappings Lücken (#71). |
|
|
||||||
|
|
||||||
### 11. System- vs. User-Mappings (Permissions)
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `is_system=true`: nur Admin editierbar; Nutzer kopiert und passt eigene Zeile an. |
|
|
||||||
| **Begründung** | Shipped Templates schützen; Individualisierung erlauben. |
|
|
||||||
| **Quelle** | `permissions.py`; DB CHECK + Unique Indexes |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | — |
|
|
||||||
|
|
||||||
### 12. Import-Audit und Rollback-Vorbereitung
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Jeder Lauf schreibt `csv_import_log` mit Counts, `error_details`, `affected_ids` (PKs pro Tabelle). |
|
|
||||||
| **Begründung** | Nachvollziehbarkeit, spätere Bereinigung/Rollback, Erfolgsrate pro Vorlage. |
|
|
||||||
| **Quelle** | Migration 042; `csv_import_execute` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Automatischer Rollback-Button nicht überall umgesetzt. |
|
|
||||||
|
|
||||||
### 13. Feature-Entitlements an Import gebunden
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `data_import` global + modulspezifisch (`nutrition_entries`, …); Increment nur für **neue** Zeilen. |
|
|
||||||
| **Begründung** | Konsistent mit Membership-System. |
|
|
||||||
| **Quelle** | `csv_import.py` `_check_module_feature_access`, `increment_feature_usage` |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Bulk-Increment-Schleife ineffizient (wie Feature-Doc). |
|
|
||||||
|
|
||||||
### 14. Mapping-Vorschläge (Heuristik, nicht Autorität)
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | `mapping_suggest.py` schlägt Spalten-Zuordnung aus Header-Aliases vor — Admin bestätigt. |
|
|
||||||
| **Begründung** | Schneller Editor-Start; Kanon bleibt menschlich/administrativ freigegeben. |
|
|
||||||
| **Quelle** | `_MODULE_HEADER_ALIASES` |
|
|
||||||
| **Tragfähigkeit** | **mittel–hoch** |
|
|
||||||
| **Einschränkung** | Domänenspezifische Aliases hardcodiert (DE/EN). |
|
|
||||||
|
|
||||||
### 15. Persistenz-Orchestrator für komplexe Domänen
|
|
||||||
|
|
||||||
| | |
|
|
||||||
|---|---|
|
|
||||||
| **Prinzip** | Activity: nach Registry-Mapping → `activity_persistence_orchestrator` (Upsert + EAV + Eval-Hook). |
|
|
||||||
| **Begründung** | Gleiche Schreiblogik wie REST-API; kein divergierender CSV-Pfad. |
|
|
||||||
| **Quelle** | `activity_persistence_orchestrator.py`; [DATA_LAYER_DESIGN_PRINCIPLES.md](./DATA_LAYER_DESIGN_PRINCIPLES.md) §9 |
|
|
||||||
| **Tragfähigkeit** | **hoch** |
|
|
||||||
| **Einschränkung** | Nur Activity vollständig; andere Module direkt im Executor. |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Nicht übernehmen
|
|
||||||
|
|
||||||
1. **Parallele Legacy-Import-Endpoints** — `/api/nutrition/import-csv`, `/api/activity/import-csv` neben Universal-Pfad; neue Quellen nur über Universal + Vorlage (ARCHITECTURE §8.2).
|
|
||||||
|
|
||||||
2. **Quellenspezifische Aggregat-Logik im Import** — `sleep_apple_import` als Dauerlösung; Ziel: mapping-nah + Layer 1 (Gitea #69).
|
|
||||||
|
|
||||||
3. **Feldlisten in Routern** — jede neue Spalte nur via `module_registry` + Migration.
|
|
||||||
|
|
||||||
4. **Verschachtelte DB-Connections im Executor** — Pool-Risiko; immer Caller-`cur` durchreichen.
|
|
||||||
|
|
||||||
5. **Transaktion ohne SAVEPOINT bei Multi-Row-Import** — ein Fehler killt gesamten Import + opaque „transaction aborted“.
|
|
||||||
|
|
||||||
6. **Blindes Vorlagen-Delimiter** — regionaler Export bricht Mapping.
|
|
||||||
|
|
||||||
7. **Nutzer-Mappings ohne Validierung** — #71; Copy-from-System muss durch Validator.
|
|
||||||
|
|
||||||
8. **Dry-Run ohne `import_row_processing`** — Admin „Format prüfen“ unvollständig vs. echter Import.
|
|
||||||
|
|
||||||
9. **`source`-CHECK in DB vergessen** — Import setzt `csv`, Constraint muss Migration sein.
|
|
||||||
|
|
||||||
10. **NUMERIC-Overflow durch falsche Einheit** — Schema + `source_unit` + Migrationbreite gemeinsam planen.
|
|
||||||
|
|
||||||
11. **Interpretation/Auswertung beim Import** — Scores, TDEE, Training-Quality nicht in `executor.py`.
|
|
||||||
|
|
||||||
12. **Executor-Monolith ohne Modul-Split** — `executor.py` wächst pro Modul; langfristig Executor-Strategie pro Registry-Key.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Modul-Inventar (Ist-Stand)
|
|
||||||
|
|
||||||
```
|
|
||||||
backend/csv_parser/
|
|
||||||
├── module_registry.py # MODULE_DEFINITIONS
|
|
||||||
├── executor.py # run_universal_csv_import
|
|
||||||
├── core.py # decode, delimiter, limits
|
|
||||||
├── type_converter.py
|
|
||||||
├── field_units.py
|
|
||||||
├── import_row_processing.py
|
|
||||||
├── template_validator.py
|
|
||||||
├── import_errors.py
|
|
||||||
├── mapping_suggest.py
|
|
||||||
├── permissions.py
|
|
||||||
└── sleep_apple_import.py # Legacy-Adapter
|
|
||||||
|
|
||||||
backend/routers/
|
|
||||||
├── csv_import.py # Nutzer: modules, analyze, import, mappings
|
|
||||||
└── admin_csv_templates.py # Admin: CRUD + validate
|
|
||||||
|
|
||||||
DB:
|
|
||||||
├── csv_field_mappings
|
|
||||||
└── csv_import_log
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Verwandte Dokumentation
|
|
||||||
|
|
||||||
- Agent-Guide (normativ): [UNIVERSAL_CSV_IMPORT_AGENT_GUIDE.md](../../technical/UNIVERSAL_CSV_IMPORT_AGENT_GUIDE.md)
|
|
||||||
- Registry-Meta: [REGISTRY_PLUGIN_DESIGN_PRINCIPLES.md](./REGISTRY_PLUGIN_DESIGN_PRINCIPLES.md)
|
|
||||||
- Import-Grenze: [ARCHITECTURE.md](../../../rules/ARCHITECTURE.md) §8
|
|
||||||
- Feature-Limits: [FEATURE_ENTITLEMENT_DESIGN_PRINCIPLES.md](./FEATURE_ENTITLEMENT_DESIGN_PRINCIPLES.md)
|
|
||||||
- Gitea #71: Dry-Run, User-Mapping-Validierung
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Geplante Folgedokumente (Serie)
|
|
||||||
|
|
||||||
| # | Modul | Status |
|
|
||||||
|---|-------|--------|
|
|
||||||
| 1–5 | … | ✅ |
|
|
||||||
| 6 | Universal Import | ✅ dieses Dokument |
|
|
||||||
| 7 | Dashboard Widgets | ✅ `DASHBOARD_WIDGETS_DESIGN_PRINCIPLES.md` |
|
|
||||||
| 8 | Navigation / IA | ✅ |
|
|
||||||
| 9 | Migration & Deploy | ✅ |
|
|
||||||
@@ -197,7 +197,7 @@ Phasen sind **sequentiell** wo „Abhängigkeit“ steht; Teile können parallel
|
|||||||
|
|
||||||
**Erledigt:** Phase A — [`ACTIVITY_SCALAR_KANON_TABLE.md`](./ACTIVITY_SCALAR_KANON_TABLE.md).
|
**Erledigt:** Phase A — [`ACTIVITY_SCALAR_KANON_TABLE.md`](./ACTIVITY_SCALAR_KANON_TABLE.md).
|
||||||
|
|
||||||
**Aktuell:** Phase B abgeschlossen (Consumer-Audit 2026-04-16). **Phase C** Schreibpfad entschlackt (Sync abgestellt, Orchestrator als SSoT; Review 2026-04-16 + Regression `test_activity_insert_sql.py`). Nächster Schritt: **Phase D** (Composite-MVP).
|
**Aktuell:** Phase B fortsetzen (weitere Consumer prüfen: Export, Import-Vorschau, ggf. zukünftige Chart-Metriken aus EAV), dann **Phase C** (Schreibpfad), dann **Phase D** (Composite-MVP).
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -1,52 +0,0 @@
|
|||||||
# BLS Food Reference – technische Spec
|
|
||||||
|
|
||||||
**Stand:** 2026-09-14 · Migration **062**–**069**
|
|
||||||
|
|
||||||
## Tabellen
|
|
||||||
|
|
||||||
- `food_attributes` — dynamischer Stoff-/Merkmalskatalog (`attr_key`, `data_type`, `origin`)
|
|
||||||
- `food_catalog` — Lebensmittel (`bls_code` UNIQUE bei official_bls; manuell ohne BLS-Code)
|
|
||||||
- `food_attribute_values` — typisiertes EAV
|
|
||||||
- `food_name_mappings` — FDDB-Name → `food_id` (User / global)
|
|
||||||
- `nutrition_items` — Tagebuchzeilen inkl. `logged_at`
|
|
||||||
- `nutrition_daily_nutrients` — Tages-Rollup numerischer Attribute
|
|
||||||
- `nutrition_day_marks` — `fasting` | `incomplete`
|
|
||||||
- `food_recipes` / `food_recipe_ingredients` — Mitai-Listen/Kombinationen (Rohmischung, kein Kochschwund); `nutrition_items.recipe_id`. Kein Tabellen-Rename. Gekochte Gerichte später Tandoor + `cooked_yield_g`, nicht hier.
|
|
||||||
|
|
||||||
## Layer 1
|
|
||||||
|
|
||||||
- `data_layer/food_mapping.py` — Normalisierung, Lookup, Learn, Apply, Delete
|
|
||||||
- `data_layer/nutrition_items.py` — Ingest, drei Makro-Summen, Policy, `resolve_*_attributes`
|
|
||||||
- `data_layer/food_recipes.py` — Listen-Upsert, Link auf Tagebuchzeilen, Listen-Makros (Skala: gegessen_g / Summe Zutaten, sonst 1/Portionen). Diese Skala gilt nur für Roh-Kombinationen, nicht für gekochte Gerichte.
|
|
||||||
- `bls/recipe_parser.py` — `produkte`-Feld: Split nur vor nächstem `\d+ (g|kg|ml|l)` (Kommas im Namen bleiben)
|
|
||||||
|
|
||||||
## Import
|
|
||||||
|
|
||||||
BLS: Admin-Upload Components + Daten-XLSX (`backend/bls/parser.py`). Upsert über `bls_code` / `attr_key`, nie Delete+Insert offizieller Zeilen.
|
|
||||||
|
|
||||||
FDDB: Items persistieren; `nutrition_log` nur bei leerem Tag oder laut Policy / Bestätigung.
|
|
||||||
|
|
||||||
## Router
|
|
||||||
|
|
||||||
- `/api/bls/*` — Suche, Attribute-Suche, eigene Foods (`attributes` + `serving_g`), eigene Mappings; `POST /bls/attributes` legt Extension-Stoff an (Name + Einheit), `GET /bls/attributes?keys=EPA,DHA`
|
|
||||||
- `GET /bls/foods/mine` — eigene `manual_user`-Einträge; `GET /bls/foods/shared` — zentrale `manual_admin`; `GET /bls/foods/{id}` — Editor-Payload inkl. `visibility` und `editable` (Owner persönlich; Admin auch zentral); `PUT /bls/foods/manual/{id}` — nur Owner. Zentrale Updates: `PUT /admin/bls/foods/manual/{id}` (Admin). Suche `?scope=global` blendet persönliche Foods aus (Admin-Mappings).
|
|
||||||
- Sichtbarkeit: `data_layer/food_visibility.py`. Lookup-Mapping: User-Zeile nur wenn Food sichtbar; globale Mapping-Zeile nur `owner_profile_id IS NULL`. `upsert_food_mapping` erzwingt dasselbe. Vorschläge sortieren persönlich → zentral → BLS.
|
|
||||||
- `GET /admin/bls/foods?kind=manual` — `manual_user`/`manual_admin`. `POST /admin/bls/foods/manual` — zentrale Foods. `PUT /admin/bls/foods/manual/{id}` — zentral **und** persönlich (`attributes` + `serving_g`). `GET /admin/bls/foods/{id}?editor=true` — Editor-Payload. Migration **069**: Owner-Check (persönlich hat Owner, BLS/Admin nicht).
|
|
||||||
- Migration **065** — Extension-Keys EPA/DHA/DPA/ALA/OMEGA3/OMEGA6 falls BLS sie nicht unter diesem Key hat
|
|
||||||
- `/api/admin/bls/*` — Import, Katalog, Attribute. `GET /admin/bls/attributes` liefert `group_key`/`group_label` (BLS-Gruppen, sortiert). `PUT /admin/bls/attributes/{id}` ändert Name/Einheit/Gruppe (nicht `attr_key`). `POST /admin/bls/attributes/fill-labels` füllt leere Namen und fehlende Gruppen aus BLS-4.0-Glossar (`bls/attribute_labels.py`). Lebensmittel-Import schreibt fehlende Namen aus Spaltenköpfen. Makro-Rollup: `catalog_macros_for_item` liest nur ENERCC/PROT625/FAT/CHO → `nutrition_log`; `rebuild_daily_nutrients` summiert alle `num_per_100g` nach `nutrition_daily_nutrients`.
|
|
||||||
- `/api/admin/food-mappings` — Admin-CRUD
|
|
||||||
- `/api/nutrition/*` — Items, Unmapped, Bulk-Map, Marken, Konflikt-Resolve
|
|
||||||
- `GET/POST /api/nutrition/recipes`, `PUT/DELETE /api/nutrition/recipes/{id}`, `POST …/import-fddb-lists`, `POST …/{id}/apply`
|
|
||||||
- `PUT /api/admin/food-mappings/{id}` — Ziel-Lebensmittel einer bestehenden Zuordnung wechseln
|
|
||||||
- Unmapped = Tagebuchzeilen ohne `food_id`/`recipe_id` **plus** Listenzutaten ohne Mapping
|
|
||||||
- Frontend: Inline-Vorschläge auf Zuordnen (`food_suggest.py`: Collapse-Key ohne Leerzeichen, Index 5 Min. Cache, Fett-%-Zahlen 9,5/10), `FoodSearchModal` nur noch Zusatzsuche (Abort + Debounce). Eigene Foods: Tab **Lebensmittel** (`MyFoodsPanel` + `FoodEditDialog`). Listen-UI: `FoodRecipeDialog` (Suche gelernt + Katalog; Katalogpick upsertet Mapping). Offene Zutaten: `kind=recipe_ingredient` (API-Wert unverändert, UI: Listenzutat).
|
|
||||||
- Tandoor (Modul `tandoor`): Migration **067** `profile_tandoor_settings`; `GET/PUT /api/tandoor/settings` (Token nur `token_set` / `token_hint`); `POST /api/tandoor/test` (10/min, kein Redirect-Follow). Client: `data_layer/tandoor_client.py`. Import und `cooked_yield_g` später.
|
|
||||||
- Migration **066** — `food_recipe_ingredients.quantity_amount` + `source_unit`; `quantity_g` = Umrechnung (`resolve_quantity` in `food_mapping.py`). `GET /bls/units` = Einheitenkatalog (EL/TL/Prise/Stück, default_g).
|
|
||||||
- `GET /nutrition/unmapped?since_days=28` — nur Namen mit `last_date` im Fenster; `count_only` liefert `{count, total, since_days}`; `meta=true` liefert `{items, count, total, since_days}`. Listenzutaten-Datum: letzte `recipe_id`-Nutzung oder `food_recipes.updated_at`/`created_at` — kein Join über den Listennamen auf alle Tagebuchzeilen. Migration **068**: Partial-Index unmapped-open + `(profile_id, recipe_id, date)`.
|
|
||||||
- `GET /nutrition/recipes?brief=true` — Listen ohne Zutaten, nur `ingredient_count`. `GET /nutrition/recipes/{id}` lädt eine Liste. FDDB-Listenimport antwortet nach Upsert; `rebuild_daily_nutrients` im Hintergrund (`rebuild_pending`).
|
|
||||||
- Listen-Verknüpfung (`apply` / `link_recipes_to_items`) matcht über `names_match_list` (Mengen-Prefix in gespeichertem `source_name_normalized` zählt). Unmapped-Zeilen bekommen `matching_recipe_id` + `matching_recipe_name`. Frontend: Tab Ernährung → Listen.
|
|
||||||
- `GET /nutrition/ingredient-names` — Distinct-Zutatennamen aller Listen inkl. Mapping-Status. Listendialog mappt manuell (`upsertMyFoodMapping`) an der Zeile.
|
|
||||||
- Mapping-Schreiben und Nährwert-Rebuild sind getrennte Transaktionen; Rebuild läuft nach der API-Antwort im Hintergrund (UI bleibt bedienbar)
|
|
||||||
- `food_name_mappings.grams_per_unit` / `source_unit` (Migration **064**)
|
|
||||||
- `GET /nutrition/items` liefert `recipe_name_de` + `recipe_complete`. `GET /nutrition/days/{date}/macros` vergleicht bestehende vs. Katalog-Tagesmakros; `POST …/apply-catalog-macros` schreibt die berechnete Summe nach `nutrition_log`. Listen-Save verknüpft passende Tagebuchzeilen (`items_linked`).
|
|
||||||
- `GET/POST /api/nutrition/food-knowledge` — portable JSON (`mitai-food-knowledge` v1): manuelle Foods, Mappings (über `bls_code` / Name, keine UUIDs), Listen. Import löst Katalog auf dem Zielsystem auf (BLS muss dort importiert sein).
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
# Dashboard-Widgets – Anleitung für Coding-Agenten
|
# Dashboard-Lab-Widgets – Anleitung für Coding-Agenten
|
||||||
|
|
||||||
Ziel: Ein neues Dashboard-Widget **end-to-end** korrekt einbinden (Backend-Katalog, Validierung, API-Layout, Frontend-Registrierung, optional Editor für `config` in **Übersicht anpassen**).
|
Ziel: Ein neues Dashboard-Widget **end-to-end** korrekt einbinden (Backend-Katalog, Validierung, API-Layout, Frontend-Registrierung, optional Lab-Editor für `config`).
|
||||||
Kontext: Geschützte Endpoints `GET/PUT /api/app/...` (siehe `backend/routers/app_dashboard.py`). Layout liegt pro Profil in `profiles.dashboard_layout` (JSON). Nutzer-Oberfläche: `frontend/src/pages/DashboardConfigurePage.jsx` (Route z. B. `/settings/dashboard-layout`).
|
Kontext: **Dashboard-Lab** unter geschützten Endpoints `GET/PUT /api/app/...` (siehe `backend/routers/app_dashboard.py`). Layout liegt pro Profil in `profiles.dashboard_layout` (JSON).
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -23,7 +23,7 @@ Kontext: Geschützte Endpoints `GET/PUT /api/app/...` (siehe `backend/routers/ap
|
|||||||
| Anforderung | Beschreibung |
|
| Anforderung | Beschreibung |
|
||||||
|-------------|--------------|
|
|-------------|--------------|
|
||||||
| **A1 – Zentrale Auflösung** | Backend ermittelt pro Profil (effektiver Tier + Restrictions), welche Widget-IDs **erlaubt** sind – idealerweise in **einer** Stelle (Erweiterung des Katalog-Endpoints oder dedizierter Entitlements-Teil der Response). Intern: `check_feature_access` und später ggf. Mapping Widget-ID → Feature-ID(n) / Cluster. |
|
| **A1 – Zentrale Auflösung** | Backend ermittelt pro Profil (effektiver Tier + Restrictions), welche Widget-IDs **erlaubt** sind – idealerweise in **einer** Stelle (Erweiterung des Katalog-Endpoints oder dedizierter Entitlements-Teil der Response). Intern: `check_feature_access` und später ggf. Mapping Widget-ID → Feature-ID(n) / Cluster. |
|
||||||
| **A2 – Nutzer-Konfigurator** | Im Layout-Konfigurator (**Übersicht anpassen**): Widgets **ohne Berechtigung nicht anbieten** (ausgeblendet oder gar nicht in der Liste). Alle **erlaubten** Widgets bleiben wie heute wählbar. |
|
| **A2 – Nutzer-Konfigurator** | Im Dashboard-Lab (und jedem späteren Layout-Konfigurator): Widgets **ohne Berechtigung nicht anbieten** (ausgeblendet oder gar nicht in der Liste). Alle **erlaubten** Widgets bleiben wie heute wählbar. |
|
||||||
| **A3 – Layout-Persistenz** | `PUT /api/app/dashboard-layout`: Layout darf **keine** nicht erlaubten Widgets dauerhaft speichern – entweder **ablehnen** (422) oder **beim Speichern entfernen/deaktivieren** (Policy festlegen und dokumentieren). Verhindert „gespeichert, aber nie sichtbar“-Zombies. |
|
| **A3 – Layout-Persistenz** | `PUT /api/app/dashboard-layout`: Layout darf **keine** nicht erlaubten Widgets dauerhaft speichern – entweder **ablehnen** (422) oder **beim Speichern entfernen/deaktivieren** (Policy festlegen und dokumentieren). Verhindert „gespeichert, aber nie sichtbar“-Zombies. |
|
||||||
| **A4 – API-/Datenschutz** | Sichtbarkeit im UI reicht nicht: Endpoints, die **Inhalte** für gated Widgets liefern (Charts, KI, …), müssen weiterhin wie heute **eigenständig** über Features abgesichert sein (`check_feature_access`, 403). |
|
| **A4 – API-/Datenschutz** | Sichtbarkeit im UI reicht nicht: Endpoints, die **Inhalte** für gated Widgets liefern (Charts, KI, …), müssen weiterhin wie heute **eigenständig** über Features abgesichert sein (`check_feature_access`, 403). |
|
||||||
|
|
||||||
@@ -42,8 +42,8 @@ Kontext: Geschützte Endpoints `GET/PUT /api/app/...` (siehe `backend/routers/ap
|
|||||||
1. **`backend/widget_catalog.py`** – `WIDGET_CATALOG`: erlaubte Widget-IDs, Reihenfolge, Titel/Beschreibung für API und Default-Layout.
|
1. **`backend/widget_catalog.py`** – `WIDGET_CATALOG`: erlaubte Widget-IDs, Reihenfolge, Titel/Beschreibung für API und Default-Layout.
|
||||||
2. **`backend/dashboard_layout_schema.py`** – `DashboardLayoutPayload`: jede Zeile hat `id`, `enabled`, optional `config`. IDs müssen in `ALLOWED_WIDGET_IDS` sein (aus dem Katalog abgeleitet).
|
2. **`backend/dashboard_layout_schema.py`** – `DashboardLayoutPayload`: jede Zeile hat `id`, `enabled`, optional `config`. IDs müssen in `ALLOWED_WIDGET_IDS` sein (aus dem Katalog abgeleitet).
|
||||||
3. **`backend/dashboard_widget_config.py`** – `validate_widget_entry_config`: **nur** Widgets in `WIDGETS_ALLOWING_CONFIG` dürfen **nicht-leere** `config` haben; Keys werden streng validiert (unbekannte Keys → Fehler).
|
3. **`backend/dashboard_widget_config.py`** – `validate_widget_entry_config`: **nur** Widgets in `WIDGETS_ALLOWING_CONFIG` dürfen **nicht-leere** `config` haben; Keys werden streng validiert (unbekannte Keys → Fehler).
|
||||||
4. **Frontend** – `ensureDashboardWidgetsRegistered()` in `frontend/src/widgetSystem/registerDashboardWidgets.js`: verbindet jede Katalog-ID mit einer React-Komponente und mappt `ctx.layoutEntry.config` auf Props.
|
4. **Frontend** – `ensurePilotLabWidgetsRegistered()` in `frontend/src/widgetSystem/registerPilotLabWidgets.js`: verbindet jede Katalog-ID mit einer React-Komponente und mappt `ctx.layoutEntry.config` auf Props.
|
||||||
5. **Layout-Editor (Produkt)** – `frontend/src/pages/DashboardConfigurePage.jsx`: Umsortieren, Ein/Aus, Speichern; **zusätzliche** UI nur nötig, wenn das Widget konfigurierbare Felder braucht.
|
5. **Dashboard-Lab-UI** – `frontend/src/pages/DashboardLabPage.jsx`: Umsortieren, Ein/Aus, Speichern; **zusätzliche** UI nur nötig, wenn das Widget konfigurierbare Felder braucht.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -52,9 +52,9 @@ Kontext: Geschützte Endpoints `GET/PUT /api/app/...` (siehe `backend/routers/ap
|
|||||||
| Schritt | Datei | Aktion |
|
| Schritt | Datei | Aktion |
|
||||||
|--------|--------|--------|
|
|--------|--------|--------|
|
||||||
| A | `backend/widget_catalog.py` | Neuen Eintrag `{ "id", "title", "description" }` in `WIDGET_CATALOG` einfügen (Reihenfolge = Default-Reihenfolge im Layout). Optional `"requires_feature": "<features.id>"` für Tarif-Gating (`dashboard_widget_entitlements`). |
|
| A | `backend/widget_catalog.py` | Neuen Eintrag `{ "id", "title", "description" }` in `WIDGET_CATALOG` einfügen (Reihenfolge = Default-Reihenfolge im Layout). Optional `"requires_feature": "<features.id>"` für Tarif-Gating (`dashboard_widget_entitlements`). |
|
||||||
| B | `backend/widget_catalog.py` | Optional: ID zu `DEFAULT_LAB_WIDGET_IDS` hinzufügen, wenn es im Server-Standardlayout **aktiv** sein soll (Feld `lab_default_layout` in der Layout-API). |
|
| B | `backend/widget_catalog.py` | Optional: ID zu `DEFAULT_LAB_WIDGET_IDS` hinzufügen, wenn es im Standard-Lab **aktiv** sein soll. |
|
||||||
| C | `frontend/src/components/dashboard-widgets/MyWidget.jsx` (oder Legacy-Widget unter `dashboard-widgets-legacy/`) | React-Komponente implementieren; typischerweise `refreshTick` aus `mapProps` nutzen, um Daten neu zu laden. |
|
| C | `frontend/src/components/dashboard-widgets/MyWidget.jsx` (oder Pilot-Komponente) | React-Komponente implementieren; typischerweise `refreshTick` aus `mapProps` nutzen, um Daten neu zu laden. |
|
||||||
| D | `frontend/src/widgetSystem/registerDashboardWidgets.js` | `import` + `registerDashboardWidget({ id, Component, mapProps })` – `id` **exakt** wie im Katalog. |
|
| D | `frontend/src/widgetSystem/registerPilotLabWidgets.js` | `import` + `registerDashboardWidget({ id, Component, mapProps })` – `id` **exakt** wie im Katalog. |
|
||||||
| E | `backend/tests/test_widget_catalog.py` | Läuft implizit mit; bei Strukturänderungen Katalog-Tests beachten. |
|
| E | `backend/tests/test_widget_catalog.py` | Läuft implizit mit; bei Strukturänderungen Katalog-Tests beachten. |
|
||||||
| F | `backend/version.py` | `MODULE_VERSIONS["app_dashboard"]` MINOR erhöhen und kurz kommentieren. |
|
| F | `backend/version.py` | `MODULE_VERSIONS["app_dashboard"]` MINOR erhöhen und kurz kommentieren. |
|
||||||
| G | Build/Tests | `pytest` (z. B. `tests/test_dashboard_layout_schema.py`, `test_widget_catalog.py`); `npm run build` im `frontend`. |
|
| G | Build/Tests | `pytest` (z. B. `tests/test_dashboard_layout_schema.py`, `test_widget_catalog.py`); `npm run build` im `frontend`. |
|
||||||
@@ -110,11 +110,11 @@ mapProps: (ctx) => ({
|
|||||||
|
|
||||||
**Abgleich mit Chart-Zeitraum:** Für `chart_days` existiert `frontend/src/widgetSystem/bodyChartDays.js` (`BODY_CHART_DAYS_MIN/MAX`, `normalizeBodyChartDays`). Entweder in `mapProps` normalisieren (wie `body_overview`) oder rohen Wert durchreichen und in der Widget-Komponente normalisieren (wie `nutrition_detail_charts` / `TrendKcalWeightWidget`) – **beides** ist im Projekt vertreten; wichtig ist Konsistenz mit der Backend-Grenze 7–90.
|
**Abgleich mit Chart-Zeitraum:** Für `chart_days` existiert `frontend/src/widgetSystem/bodyChartDays.js` (`BODY_CHART_DAYS_MIN/MAX`, `normalizeBodyChartDays`). Entweder in `mapProps` normalisieren (wie `body_overview`) oder rohen Wert durchreichen und in der Widget-Komponente normalisieren (wie `nutrition_detail_charts` / `TrendKcalWeightWidget`) – **beides** ist im Projekt vertreten; wichtig ist Konsistenz mit der Backend-Grenze 7–90.
|
||||||
|
|
||||||
### 3.4 Layout-Editor (`DashboardConfigurePage.jsx`)
|
### 3.4 Dashboard-Lab-Editor (`DashboardLabPage.jsx`)
|
||||||
|
|
||||||
Ohne UI-Änderung bleibt `config` beim Nutzer `{}` – konfigurierbare Widgets brauchen **Editor-Controls**:
|
Ohne UI-Änderung bleibt `config` beim Nutzer `{}` – konfigurierbare Widgets brauchen **Editor-Controls**:
|
||||||
|
|
||||||
- **Einfaches Zahlfeld `chart_days`:** Eintrag in `CHART_DAYS_WIDGET_IDS` (Set oben in `DashboardConfigurePage.jsx`) + bestehendes Label/`aria-label`-Pattern für die Zeitraum-Zeile erweitern (siehe `body_overview`, `nutrition_detail_charts`).
|
- **Einfaches Zahlfeld `chart_days`:** Eintrag in `CHART_DAYS_WIDGET_IDS` (Set oben in der Datei) + bestehendes Label/`aria-label`-Pattern für die Zeitraum-Zeile erweitern (siehe `body_overview`, `nutrition_detail_charts`).
|
||||||
- **Strukturierte Config (Listen, mehrere Booleans):** Eigenes Editor-Komponenten-File nach Vorbild `KpiBoardConfigEditor.jsx` / `QuickCaptureConfigEditor.jsx` einbinden und `setLayout` + `normalizeLayoutForEditor` wie bei den bestehenden Blöcken verwenden.
|
- **Strukturierte Config (Listen, mehrere Booleans):** Eigenes Editor-Komponenten-File nach Vorbild `KpiBoardConfigEditor.jsx` / `QuickCaptureConfigEditor.jsx` einbinden und `setLayout` + `normalizeLayoutForEditor` wie bei den bestehenden Blöcken verwenden.
|
||||||
|
|
||||||
Nach Speichern ruft die Seite `api.putAppDashboardLayout(layout)` auf; das Backend validiert über `DashboardLayoutPayload` → `validate_widget_entry_config`.
|
Nach Speichern ruft die Seite `api.putAppDashboardLayout(layout)` auf; das Backend validiert über `DashboardLayoutPayload` → `validate_widget_entry_config`.
|
||||||
@@ -137,7 +137,7 @@ Nach Speichern ruft die Seite `api.putAppDashboardLayout(layout)` auf; das Backe
|
|||||||
## 5. API zum Prüfen
|
## 5. API zum Prüfen
|
||||||
|
|
||||||
- `GET /api/app/widgets/catalog` – Katalog inkl. `allowed` je Widget (Auth + `X-Profile-Id` wie andere App-Endpoints).
|
- `GET /api/app/widgets/catalog` – Katalog inkl. `allowed` je Widget (Auth + `X-Profile-Id` wie andere App-Endpoints).
|
||||||
- `GET /api/app/dashboard-layout` – `layout` (effektiv, bereinigt), `custom`, `product_default_layout` (Übersichts-Standard), `lab_default_layout` (Servertemplate für Editor/Reset; Feldname historisch).
|
- `GET /api/app/dashboard-layout` – `layout` (effektiv, bereinigt), `custom`, `product_default_layout` (Übersichts-Standard), `lab_default_layout` (Dashboard-Lab-Standard).
|
||||||
- `PUT /api/app/dashboard-layout` – Body `{ "version": 1, "widgets": [ ... ] }` (unerlaubte Widgets werden auf `enabled: false` gesetzt).
|
- `PUT /api/app/dashboard-layout` – Body `{ "version": 1, "widgets": [ ... ] }` (unerlaubte Widgets werden auf `enabled: false` gesetzt).
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -159,5 +159,5 @@ Nach Speichern ruft die Seite `api.putAppDashboardLayout(layout)` auf; das Backe
|
|||||||
| Layout-Pydantic | `backend/dashboard_layout_schema.py` |
|
| Layout-Pydantic | `backend/dashboard_layout_schema.py` |
|
||||||
| HTTP | `backend/routers/app_dashboard.py` |
|
| HTTP | `backend/routers/app_dashboard.py` |
|
||||||
| Registry + Render | `frontend/src/widgetSystem/dashboardWidgetRegistry.jsx` |
|
| Registry + Render | `frontend/src/widgetSystem/dashboardWidgetRegistry.jsx` |
|
||||||
| Dashboard-Widget-Registrierung | `frontend/src/widgetSystem/registerDashboardWidgets.js` |
|
| Pilot/Lab-Registrierung | `frontend/src/widgetSystem/registerPilotLabWidgets.js` |
|
||||||
| Layout-Editor (Nutzer) | `frontend/src/pages/DashboardConfigurePage.jsx` |
|
| Lab-UI | `frontend/src/pages/DashboardLabPage.jsx` |
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
# Berichtsprofile & PDF (technisch)
|
|
||||||
|
|
||||||
**Stand:** 2026-04-29
|
|
||||||
|
|
||||||
## Begriffe
|
|
||||||
|
|
||||||
| Begriff | Bedeutung |
|
|
||||||
|--------|-----------|
|
|
||||||
| **Layout-Snapshot** | PDF aus gerasteter DOM-Übersicht (`html2canvas` + `jspdf`), optional Widget `report_export`. |
|
|
||||||
| **Strukturierter Bericht** | Profil mit Blöcken (`section`, `chart`, `ai_insight`), PDF serverseitig via Data Layer + Matplotlib + ReportLab. |
|
|
||||||
|
|
||||||
Die beiden Wege sind bewusst getrennt, damit das Dashboard nicht die einzige „Wahrheit“ für Dokumente wird.
|
|
||||||
|
|
||||||
## Datenbank
|
|
||||||
|
|
||||||
- Tabelle `report_profiles` (Migration `060_report_profiles.sql`): `profile_id` PK → `profiles`, `payload` JSONB, `updated_at`.
|
|
||||||
|
|
||||||
Ohne Zeile gilt ein **Code-Standard** (`default_report_profile_dict` in `report_profile_schema.py`).
|
|
||||||
|
|
||||||
## API (`/api/reports`)
|
|
||||||
|
|
||||||
| Methode | Pfad | Zweck |
|
|
||||||
|--------|------|--------|
|
|
||||||
| GET | `/catalog` | Diagramm-Katalog + Blocktypen für UI |
|
|
||||||
| GET | `/profile` | `{ stored, profile }` |
|
|
||||||
| PUT | `/profile` | Vollständiges Profil-JSON (Pydantic-validiert) |
|
|
||||||
| DELETE | `/profile` | DB-Zeile löschen → wieder Standard |
|
|
||||||
| POST | `/generate-pdf` | PDF-Download; `data_export`-Kontingent + `increment_feature_usage` |
|
|
||||||
|
|
||||||
## Schema v1 (`report_profile_schema.py`)
|
|
||||||
|
|
||||||
- `version`: nur `1`
|
|
||||||
- `document_title`: optional
|
|
||||||
- `blocks`: Liste mit Union:
|
|
||||||
- `section`: `title`
|
|
||||||
- `chart`: `chart_id` ∈ `ALLOWED_CHART_IDS`, `window_days` 7–365
|
|
||||||
- `ai_insight`: optional `insight_id` (UUID, `ai_insights.id`), optional `title`
|
|
||||||
|
|
||||||
## Diagrammdaten
|
|
||||||
|
|
||||||
`report_chart_fetch.fetch_chart_payload` ruft dieselben Bausteine auf wie `/api/charts` (ohne HTTP). Erweiterung: Eintrag in `ALLOWED_CHART_IDS`, Fetcher in `_CHART_FETCHERS`, Zeile in `CHART_CATALOG_FOR_API`.
|
|
||||||
|
|
||||||
## PDF-Rendering
|
|
||||||
|
|
||||||
`report_pdf_render.build_structured_report_pdf`: ReportLab-Flowable-Kette, Diagramme als PNG aus Chart-Payload (Matplotlib, Agg-Backend).
|
|
||||||
|
|
||||||
## Frontend
|
|
||||||
|
|
||||||
- **Einstellungen:** Karte „PDF-Bericht (strukturiert)“ — Blöcke bearbeiten, speichern, Standard, PDF erzeugen.
|
|
||||||
- **Dashboard:** Widget bleibt optionaler **Schnappschuss**; Hinweis verweist auf Einstellungen.
|
|
||||||
|
|
||||||
## Nächste sinnvolle Erweiterungen
|
|
||||||
|
|
||||||
- Dashboard-Layout → Berichtsprofil **einmalig importieren** (Mapping-Tabelle Widget-ID → chart_id).
|
|
||||||
- KI: Insights-Auswahl in der UI statt manueller UUID.
|
|
||||||
- Weitere `chart_id`-Werte / multipage Feintuning (Seitenumbrüche pro Block).
|
|
||||||
@@ -46,12 +46,10 @@ Dieses Dokument ist **normativ für Agenten**, die ein neues Import-Zielmodul an
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 4. Validierung & Dry-Run (Stand 2026-07-23, Gitea #71)
|
## 4. Bekannte Einschränkungen (Follow-up in Gitea)
|
||||||
|
|
||||||
- Admin **„Format prüfen“** sendet dieselbe `import_row_processing`-Spec wie Speichern (`AdminCsvTemplateEditorPage` → `POST /api/admin/csv-templates/validate`).
|
- Admin **„Format prüfen“** kann `import_row_processing` derzeit weglassen; volle Parität mit dem gespeicherten Template erst beim Speichern / echten Import.
|
||||||
- **Profil-Mappings:** `POST /api/csv/mappings/{id}/copy` und `POST /api/csv/import` prüfen vor dem Schreiben mit **`validate_csv_template`** (HTTP 422 bei Fehlern).
|
- Nutzer-Mappings (Copy aus Systemvorlage) laufen nicht automatisch durch **`validate_csv_template`** – Tracking: **Gitea #71** (http://192.168.2.144:3000/Lars/mitai-jinkendo/issues/71).
|
||||||
- **Diagnose:** `GET /api/csv/mappings/{id}/validate` — Strukturprüfung ohne Import.
|
|
||||||
- **Nutzer-UI:** `UniversalCsvImportPage` zeigt `error_details` mit Zeile, `code` und `hint` (`CsvImportErrorDetails`).
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large.
Load diff
@@ -1,10 +0,0 @@
|
|||||||
**Automatischer Audit (Code + Playwright, 2026-07-23)**
|
|
||||||
|
|
||||||
Ziele-System ist im Repo umgesetzt:
|
|
||||||
- Backend: `backend/routers/goals.py`, Focus Areas, Goal Types, Progress
|
|
||||||
- Frontend: `frontend/src/pages/GoalsPage.jsx`, Nav `/goals` in `config/appNav.js`
|
|
||||||
- Spec: `docs/issues/issue-50-phase-0a-goal-system.md`
|
|
||||||
|
|
||||||
Playwright auf dev.mitai.jinkendo.de: `/goals` rendert ohne Fehler.
|
|
||||||
|
|
||||||
Verbleibende KI-Goal-Erweiterungen ggf. als neues Issue.
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
**Automatischer Audit (Code, 2026-07-23)**
|
|
||||||
|
|
||||||
Deprecated Tabelle `subscriptions` ist aus dem aktiven Schema entfernt:
|
|
||||||
- Kein Treffer in `backend/schema.sql`, `backend/migrations/`, Backend-Python
|
|
||||||
- Membership nutzt `access_grants`, `tier_limits`, etc. (Migration v9c)
|
|
||||||
|
|
||||||
Doku-Hinweis deprecated: `.claude/docs/technical/DATABASE.md`
|
|
||||||
|
|
||||||
Optional Prod-Check: `\dt subscriptions` — sollte leer/nicht vorhanden sein.
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
**Automatischer Audit (Code, 2026-07-23)**
|
|
||||||
|
|
||||||
Bug behoben: JSONB-Insert für `abilities`/`profile` nutzt `psycopg2.extras.Json()`.
|
|
||||||
|
|
||||||
- Fix-Commit: `2977050` — wrap abilities dict with Json() for JSONB insert
|
|
||||||
- Aktuell: `backend/routers/admin_training_types.py` (`create_training_type`, Zeilen 111–112)
|
|
||||||
|
|
||||||
Admin-UI: `frontend/src/pages/AdminTrainingTypesPage.jsx`
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
**Umsetzung 2026-07-23 (nach Architektur-Abgleich)**
|
|
||||||
|
|
||||||
## Architektur-Check vor Implementierung
|
|
||||||
|
|
||||||
- **Phase C (Schreibpfad):** laut `ACTIVITY_PRODUCTION_ARCHITECTURE_AND_PHASES.md` bereits erledigt (Sync abgestellt, Orchestrator als SSoT, keine Aufrufer von `sync_column_backed_session_metrics`). Keine Doppel-Implementierung.
|
|
||||||
- **Feature-ID:** Issue-Vorgabe `activity_entries` (nicht separates `activity_import`) — konsistent mit `create_activity` und Universal-CSV-Modul-Check in `csv_import.py`.
|
|
||||||
- **Legacy-Endpoint bleibt:** Frontend nutzt weiterhin `POST /api/activity/import-csv` (`ActivityPage` Apple-Health-Panel); Universal-Pfad ist parallel (ARCH §8.2).
|
|
||||||
|
|
||||||
## Änderungen
|
|
||||||
|
|
||||||
**Backend** (`routers/activity.py`):
|
|
||||||
- `check_feature_access(pid, "activity_entries")` vor Legacy-Import
|
|
||||||
- HTTP 403 bei Limit (wie `nutrition/import-csv`)
|
|
||||||
- `increment_feature_usage` pro neu eingefügter Zeile (`inserted`)
|
|
||||||
|
|
||||||
**Frontend** (`ActivityPage.jsx` ImportPanel):
|
|
||||||
- `UsageBadge` am Import-Titel
|
|
||||||
- Drop-Zone deaktiviert bei Limit
|
|
||||||
- Usage-Reload nach Import
|
|
||||||
|
|
||||||
**Tests:** `tests/test_activity_import_feature_enforcement.py` (403 + Increment)
|
|
||||||
|
|
||||||
Regression INSERT-SQL: `tests/test_activity_insert_sql.py` (grün)
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
**Umsetzung 2026-07-23 (UI-Teil; Backend war bereits erledigt)**
|
|
||||||
|
|
||||||
## Ist-Zustand vorher
|
|
||||||
|
|
||||||
- Backend: `POST /api/nutrition/import-csv` mit `check_feature_access('nutrition_entries')` und HTTP 403 — **bereits implementiert**
|
|
||||||
- Frontend: FDDB-Import ohne Usage-Anzeige, Drop-Zone blieb bei Limit klickbar
|
|
||||||
|
|
||||||
## Änderungen
|
|
||||||
|
|
||||||
**Frontend** (`NutritionPage.jsx`, ImportPanel):
|
|
||||||
- `getFeatureUsage()` → `nutrition_entries`
|
|
||||||
- `UsageBadge` am Import-Titel
|
|
||||||
- Drop-Zone und Paste-Import deaktiviert bei Limit
|
|
||||||
- Usage-Reload nach erfolgreichem Import
|
|
||||||
|
|
||||||
Analog zu Activity #37 (geschlossen 2026-07-23).
|
|
||||||
|
|
||||||
**Playwright:** `tests/issue-audit.spec.js` — Badge am FDDB-Panel
|
|
||||||
|
|
||||||
## Hinweis
|
|
||||||
|
|
||||||
Universal-CSV (`/api/csv-import/import`) prüft zusätzlich `data_import` — Legacy FDDB-Pfad folgt dem Nutrition-Muster (nur `nutrition_entries`), konsistent mit Architektur §8.2.
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
**Automatischer Audit (Code + Playwright, 2026-07-23)**
|
|
||||||
|
|
||||||
Logout-Button im Mobile-Header neben Avatar implementiert:
|
|
||||||
- `frontend/src/App.jsx` — Button mit `title="Abmelden"`, Icon `LogOut`
|
|
||||||
- Zusätzlich Desktop: `frontend/src/components/DesktopSidebar.jsx`
|
|
||||||
|
|
||||||
Playwright auf dev.mitai.jinkendo.de: Button sichtbar und klickbar.
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
**Duplikat — geschlossen im Rahmen Issue-Audit 2026-07-23**
|
|
||||||
|
|
||||||
Inhalt identisch mit **#42** (Enhanced Debug/Prompt Analysis UI).
|
|
||||||
|
|
||||||
Teilumsetzung existiert bereits (`Analysis.jsx` Experten-Modus, `WorkflowDebugPanel`). Weiterverfolgung unter #42.
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
**Duplikat — geschlossen im Rahmen Issue-Audit 2026-07-23**
|
|
||||||
|
|
||||||
Inhalt identisch mit **#55** (Placeholder Registry: UNRESOLVED & TO_VERIFY Metadaten).
|
|
||||||
|
|
||||||
Bitte weiterverfolgen unter #55.
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
**Duplikat — geschlossen im Rahmen Issue-Audit 2026-07-23**
|
|
||||||
|
|
||||||
Inhalt identisch mit **#56** (Body Cluster — Restarbeiten & Metadaten-Verifizierung).
|
|
||||||
|
|
||||||
Bitte weiterverfolgen unter #56.
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
**Duplikat — geschlossen im Rahmen Issue-Audit 2026-07-23**
|
|
||||||
|
|
||||||
Inhalt identisch mit **#56** (Body Cluster — Restarbeiten & Metadaten-Verifizierung).
|
|
||||||
|
|
||||||
Bitte weiterverfolgen unter #56.
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
**Automatischer Audit (Code + Playwright, 2026-07-23)**
|
|
||||||
|
|
||||||
Nutzer-konfigurierbares Dashboard umgesetzt:
|
|
||||||
- Migration `039_dashboard_layout.sql` — `profiles.dashboard_layout`
|
|
||||||
- API: `backend/routers/app_dashboard.py` — GET/PUT/reset `/api/app/dashboard-layout`
|
|
||||||
- Frontend: `DashboardConfigurePage.jsx`, Widget-Registry `registerDashboardWidgets.js`
|
|
||||||
- Tests: `test_dashboard_layout_schema.py`, `test_widget_catalog.py`
|
|
||||||
|
|
||||||
Playwright: `/settings/dashboard-layout` erreichbar, Widget/Layout-UI sichtbar.
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
**Automatischer Audit (Code, 2026-07-23)**
|
|
||||||
|
|
||||||
Feature-Gate-Zuordnung aus Admin/DB implementiert:
|
|
||||||
- Migration `041_widget_feature_requirements.sql`
|
|
||||||
- Logik: `backend/widget_feature_requirements_db.py`, `dashboard_widget_entitlements.py`
|
|
||||||
- Admin-UI: `AdminWidgetFeatureAssignmentsPage.jsx`, Route `/admin/widget-features`
|
|
||||||
- Changelog: `backend/version.py` (Admin Widgets × Features)
|
|
||||||
|
|
||||||
Hardcodierter Katalog wird durch DB-Overrides ergänzt (Hybrid-Modell).
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
**Implementierung abgeschlossen (2026-07-24)**
|
|
||||||
|
|
||||||
### 1. Admin „Format prüfen“ inkl. `import_row_processing`
|
|
||||||
- `AdminCsvTemplateEditorPage`: `resolveEditorImportRowProcessing()` — dieselbe Spec wie beim Speichern
|
|
||||||
- Dry-Run/Format-Check nutzt konsistente Vorlage inkl. Aggregation (`group_by` / `aggregates`)
|
|
||||||
|
|
||||||
### 2. Profil-Mappings validieren
|
|
||||||
- `csv_import.py`: `_validate_mapping_config` / `_ensure_mapping_valid` bei **Copy** und **Import**
|
|
||||||
- Neuer Endpoint: `GET /api/csv/mappings/{id}/validate`
|
|
||||||
- `api.js`: `validateCsvMapping()`; `formatFastApiDetail` für verschachtelte Validierungsfehler
|
|
||||||
|
|
||||||
### 3. Nutzer-UI: strukturierte Fehler
|
|
||||||
- Neue Komponente `CsvImportErrorDetails.jsx` (Zeile, code, hint)
|
|
||||||
- `UniversalCsvImportPage` zeigt `error_details` statt JSON-Dump
|
|
||||||
|
|
||||||
### Tests & Doku
|
|
||||||
- `backend/tests/test_csv_mapping_validation.py` — pytest grün
|
|
||||||
- Agent-Guide: `.claude/docs/technical/UNIVERSAL_CSV_IMPORT_AGENT_GUIDE.md` §4 aktualisiert
|
|
||||||
|
|
||||||
**Betroffene Dateien:** `backend/routers/csv_import.py`, `frontend/src/pages/AdminCsvTemplateEditorPage.jsx`, `frontend/src/pages/UniversalCsvImportPage.jsx`, `frontend/src/components/CsvImportErrorDetails.jsx`, `frontend/src/utils/api.js`
|
|
||||||
@@ -455,15 +455,15 @@ NIEMALS gegen mitai.jinkendo.de
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 10. Dashboard-Widgets und Feature-System
|
## 10. Dashboard-Lab-Widgets und Feature-System
|
||||||
|
|
||||||
**Kontext:** Dashboard-Widgets (`backend/widget_catalog.py`, API unter `/api/app/...`) und das **Subscription-/Feature-Modell** (`features`, `tier_limits`, `check_feature_access` in `backend/auth.py`) sind **getrennte Schichten**, müssen aber bei tariffrelevanten Widgets **verknüpft** werden.
|
**Kontext:** Dashboard-Widgets (`backend/widget_catalog.py`, Lab unter `/api/app/...`) und das **Subscription-/Feature-Modell** (`features`, `tier_limits`, `check_feature_access` in `backend/auth.py`) sind **getrennte Schichten**, müssen aber bei tariffrelevanten Widgets **verknüpft** werden.
|
||||||
|
|
||||||
**Bindend:**
|
**Bindend:**
|
||||||
|
|
||||||
1. **Keine fest codierten Tier-Namen** für Widget-Rechte – Tiers und Limits kommen aus der DB.
|
1. **Keine fest codierten Tier-Namen** für Widget-Rechte – Tiers und Limits kommen aus der DB.
|
||||||
2. **Komplexität** (Module aus, Unter-Stufen, KI vs. Standard) liegt in der **Feature-/Subscription-Logik**, nicht verteilt in Widget-Komponenten.
|
2. **Komplexität** (Module aus, Unter-Stufen, KI vs. Standard) liegt in der **Feature-/Subscription-Logik**, nicht verteilt in Widget-Komponenten.
|
||||||
3. **Nutzer-Konfigurator** (**Übersicht anpassen** / `DashboardConfigurePage`): Widgets **ohne** passende Berechtigung **nicht anzeigen**; alle erlaubten Widgets bleiben verfügbar.
|
3. **Nutzer-Konfigurator** (z. B. Dashboard-Lab): Widgets **ohne** passende Berechtigung **nicht anzeigen**; alle erlaubten Widgets bleiben verfügbar.
|
||||||
4. **Backend** liefert die effektive Erlaubnis (z. B. über erweiterten Katalog oder Entitlements), und **validiert beim Speichern** des Layouts, dass keine unerlaubten Widget-IDs persistiert werden (Policy: ablehnen oder strippen – einheitlich halten).
|
4. **Backend** liefert die effektive Erlaubnis (z. B. über erweiterten Katalog oder Entitlements), und **validiert beim Speichern** des Layouts, dass keine unerlaubten Widget-IDs persistiert werden (Policy: ablehnen oder strippen – einheitlich halten).
|
||||||
5. **Daten/API:** Zusätzlich zur UI-Filterung müssen die **inhaltsliefernden Endpoints** weiterhin über `check_feature_access` geschützt sein (kein Leck über direkte API-Aufrufe).
|
5. **Daten/API:** Zusätzlich zur UI-Filterung müssen die **inhaltsliefernden Endpoints** weiterhin über `check_feature_access` geschützt sein (kein Leck über direkte API-Aufrufe).
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,6 @@
|
|||||||
|-----|------|----------------|
|
|-----|------|----------------|
|
||||||
| **Fachliche Spec (WAS)** | `.claude/docs/functional/` | Domäne, Use Cases, UX-Ziele, fachliche Datenarchitektur. **Keine** reine API-Parameterliste (→ technical). |
|
| **Fachliche Spec (WAS)** | `.claude/docs/functional/` | Domäne, Use Cases, UX-Ziele, fachliche Datenarchitektur. **Keine** reine API-Parameterliste (→ technical). |
|
||||||
| **Technische Spec (WIE)** | `.claude/docs/technical/` | API-, DB-, Implementierungsmuster, Agent-Guides, Migrationen. |
|
| **Technische Spec (WIE)** | `.claude/docs/technical/` | API-, DB-, Implementierungsmuster, Agent-Guides, Migrationen. |
|
||||||
| **Produktfamilie / Foundation** | `.claude/docs/jinkendo-foundation/` | Übertragbare Designprinzipien (nicht Mitai-Domäne); Einstieg `design-principles/README.md`. |
|
|
||||||
| **Architektur-Querschnitt** | `.claude/docs/architecture/` | Kurze Überblicke (z. B. Frontend-Baum), ergänzend zu technical. |
|
| **Architektur-Querschnitt** | `.claude/docs/architecture/` | Kurze Überblicke (z. B. Frontend-Baum), ergänzend zu technical. |
|
||||||
| **Arbeitspapier / Zwischenstand** | `.claude/docs/working/` | Analysen, Sessions, Migration-Notizen, **keine** langfristige Norm. Kann veraltet sein → Datum im Dokument. **Nicht** als alleinige „Wahrheit“ für Produkt zitieren. |
|
| **Arbeitspapier / Zwischenstand** | `.claude/docs/working/` | Analysen, Sessions, Migration-Notizen, **keine** langfristige Norm. Kann veraltet sein → Datum im Dokument. **Nicht** als alleinige „Wahrheit“ für Produkt zitieren. |
|
||||||
| **Audits & Matrizen** | `.claude/docs/audit/` | Zeitlich begrenzte Reviews, Reconciliation, Gitea-Vorlagen. |
|
| **Audits & Matrizen** | `.claude/docs/audit/` | Zeitlich begrenzte Reviews, Reconciliation, Gitea-Vorlagen. |
|
||||||
@@ -64,7 +63,6 @@ Diese Ordner sind **kein** Ersatz für `working/` oder `docs/issues/`, wenn das
|
|||||||
```
|
```
|
||||||
.claude/README.md ← Einstieg Agent/Human
|
.claude/README.md ← Einstieg Agent/Human
|
||||||
.claude/docs/README.md ← Spec-Katalog
|
.claude/docs/README.md ← Spec-Katalog
|
||||||
.claude/docs/jinkendo-foundation/ ← Foundation Produktfamilie (Designprinzipien)
|
|
||||||
.claude/docs/functional/ ← WAS
|
.claude/docs/functional/ ← WAS
|
||||||
.claude/docs/technical/ ← WIE
|
.claude/docs/technical/ ← WIE
|
||||||
.claude/docs/working/ ← Arbeitspapiere / Analysen
|
.claude/docs/working/ ← Arbeitspapiere / Analysen
|
||||||
|
|||||||
@@ -176,20 +176,6 @@ known_limitations=(
|
|||||||
**Problem:** Versuch, `.claude/task/` Files zu committen
|
**Problem:** Versuch, `.claude/task/` Files zu committen
|
||||||
**Lösung:** Nur `backend/` Code committen, `.claude/` ist local docs
|
**Lösung:** Nur `backend/` Code committen, `.claude/` ist local docs
|
||||||
|
|
||||||
## 9. Unmapped-Query: Kreuzjoin Liste × Tagebuch (12.09.2026)
|
|
||||||
|
|
||||||
**Was:** Offene Lebensmittel über `food_recipes` mit `nutrition_items` per `recipe_id OR name_normalized` verbinden, um Zutaten ein Datum zu geben.
|
|
||||||
**Problem:** Nach FDDB-Listenimport Minuten bis Timeout; Zutaten ohne `last_date` fielen im 28-Tage-Filter weg — leere Offene-Liste.
|
|
||||||
**Lösung:** Nur Items mit `recipe_id` gruppieren; Fallback `recipe.updated_at`. Listen-GET `brief=true`. Rebuild nach Import im Hintergrund.
|
|
||||||
|
|
||||||
**Regel:** Kein Join „Name der Liste gegen alle Tagebuchzeilen“. Datum aus der bereits gesetzten FK oder aus dem Eltern-Datensatz.
|
|
||||||
|
|
||||||
## 10. `from __future__ import annotations` + FastAPI-Limiter (13.09.2026)
|
|
||||||
|
|
||||||
**Was:** `tandoor.py` mit future-annotations und `body: TandoorProbeIn | None` unter `@limiter.limit`.
|
|
||||||
**Problem:** Pydantic löst den Typ nicht auf → Import-Crash → Backend startet nicht → Login bekommt HTML (`Unexpected token '<'`).
|
|
||||||
**Lösung:** Kein future-annotations in Routern; `Optional[TandoorProbeIn]`. Import-Test für den Router.
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
**Zusammenfassung Nutrition Cluster:**
|
**Zusammenfassung Nutrition Cluster:**
|
||||||
|
|||||||
@@ -58,9 +58,6 @@ coverage/
|
|||||||
# Temp
|
# Temp
|
||||||
tmp/
|
tmp/
|
||||||
*.tmp
|
*.tmp
|
||||||
test-results/
|
|
||||||
screenshots/
|
|
||||||
tests/.auth/
|
|
||||||
|
|
||||||
# Claude: nur ausgewählte Bereiche versionieren (siehe .claude/rules/DOCUMENTATION.md)
|
# Claude: nur ausgewählte Bereiche versionieren (siehe .claude/rules/DOCUMENTATION.md)
|
||||||
.claude/**
|
.claude/**
|
||||||
|
|||||||
@@ -10,8 +10,7 @@
|
|||||||
> | **Gitea-Landkarte (lokal gepflegt)** | **`.claude/docs/GITEA_ISSUES_INDEX.md`** |
|
> | **Gitea-Landkarte (lokal gepflegt)** | **`.claude/docs/GITEA_ISSUES_INDEX.md`** |
|
||||||
> | **Universal CSV Import** (neues Modul / Executor / Vorlagen) | **`.claude/docs/technical/UNIVERSAL_CSV_IMPORT_AGENT_GUIDE.md`** |
|
> | **Universal CSV Import** (neues Modul / Executor / Vorlagen) | **`.claude/docs/technical/UNIVERSAL_CSV_IMPORT_AGENT_GUIDE.md`** |
|
||||||
> | **GUI / IA / Admin / Nav / PWA-Leiste** | **`docs/issues/GUI_IA_ADMIN_NAV_2026-04-05.md`** |
|
> | **GUI / IA / Admin / Nav / PWA-Leiste** | **`docs/issues/GUI_IA_ADMIN_NAV_2026-04-05.md`** |
|
||||||
> | **Dashboard-Widgets** (Katalog, Registrierung, `config`) | **`.claude/docs/technical/DASHBOARD_WIDGETS_AGENT_GUIDE.md`** |
|
> | **Dashboard-Lab-Widgets** (Katalog, Registrierung, `config`) | **`.claude/docs/technical/DASHBOARD_WIDGETS_AGENT_GUIDE.md`** |
|
||||||
> | **Designprinzipien Produktfamilie** (Serie 1–9, Foundation) | **`.claude/docs/jinkendo-foundation/design-principles/README.md`** |
|
|
||||||
> | **Agent-Einstieg** | **`.claude/README.md`** |
|
> | **Agent-Einstieg** | **`.claude/README.md`** |
|
||||||
> | **Activity Session Metrics (EAV, Attributprofile)** | **`.claude/docs/technical/ACTIVITY_SESSION_METRICS_EAV_AGENT_GUIDE.md`** |
|
> | **Activity Session Metrics (EAV, Attributprofile)** | **`.claude/docs/technical/ACTIVITY_SESSION_METRICS_EAV_AGENT_GUIDE.md`** |
|
||||||
|
|
||||||
@@ -101,11 +100,6 @@ frontend/src/
|
|||||||
**Branch:** develop
|
**Branch:** develop
|
||||||
**Nächster Schritt:** Frontend Chart Integration → Testing → Prod Deploy v0.9i
|
**Nächster Schritt:** Frontend Chart Integration → Testing → Prod Deploy v0.9i
|
||||||
|
|
||||||
### Updates (23.04.2026 - Dashboard: veraltete Demo-Route entfernt, klare Produkt-Registry)
|
|
||||||
|
|
||||||
- **Frontend:** Veraltete Visualisierungs-Demo-Route und festes Demo-Layout entfernt; Widget-Registrierung in `frontend/src/widgetSystem/registerDashboardWidgets.js` (`ensureDashboardWidgetsRegistered`). Kern-Widgets unter `frontend/src/components/dashboard-widgets-legacy/`. Chart-Hilfen in `frontend/src/widgetSystem/dashboardChartUtils.js`. Experimentelles Layout-Lab entfernt; Konfiguration nur noch **Übersicht anpassen** (`DashboardConfigurePage`).
|
|
||||||
- **Doku:** `.claude/docs/technical/DASHBOARD_WIDGETS_AGENT_GUIDE.md` und Kommentar in `backend/widget_catalog.py` angepasst.
|
|
||||||
|
|
||||||
### Updates (09.04.2026 - Universal CSV Import, Prod-Migration abgeschlossen)
|
### Updates (09.04.2026 - Universal CSV Import, Prod-Migration abgeschlossen)
|
||||||
|
|
||||||
- **Agent-Leitfaden:** `.claude/docs/technical/UNIVERSAL_CSV_IMPORT_AGENT_GUIDE.md` (Checkliste für neue Import-Module, Executor, Vorlagen, `source=csv`, SAVEPOINT-/Cursor-Regeln)
|
- **Agent-Leitfaden:** `.claude/docs/technical/UNIVERSAL_CSV_IMPORT_AGENT_GUIDE.md` (Checkliste für neue Import-Module, Executor, Vorlagen, `source=csv`, SAVEPOINT-/Cursor-Regeln)
|
||||||
@@ -117,19 +111,6 @@ frontend/src/
|
|||||||
- **`main.py`:** `import placeholder_registrations` beim Start, damit die Registry (**114 Keys**, deckungsgleich `PLACEHOLDER_MAP`) und `get_placeholder_catalog()` ohne vorherigen Export-Request konsistent sind.
|
- **`main.py`:** `import placeholder_registrations` beim Start, damit die Registry (**114 Keys**, deckungsgleich `PLACEHOLDER_MAP`) und `get_placeholder_catalog()` ohne vorherigen Export-Request konsistent sind.
|
||||||
- **`placeholder_resolver.py`:** `{{top_goal_progress_pct}}` nutzt `_safe_int` statt `_safe_str` (Verdrahtung zu `scores.get_top_priority_goal` korrigiert).
|
- **`placeholder_resolver.py`:** `{{top_goal_progress_pct}}` nutzt `_safe_int` statt `_safe_str` (Verdrahtung zu `scores.get_top_priority_goal` korrigiert).
|
||||||
|
|
||||||
### Updates (12.09.2026 - BLS-Stammdaten, FDDB-Mapping, Item-Tagebuch)
|
|
||||||
|
|
||||||
- **Migration 062:** `food_catalog` (BLS-Code bleibt Identität), dynamische `food_attributes` + EAV, `food_name_mappings`, `nutrition_items`, `nutrition_daily_nutrients`, `nutrition_day_marks`, Import-Policy am Profil.
|
|
||||||
- **Admin:** Gruppe Ernährung — BLS-Import, Katalog, Attribute, Mappings.
|
|
||||||
- **Nutzer:** Einzelerfassung unverändert; Tab Zuordnen mit Namenssuche (Popup); FDDB-Listen/Kombinationen; JSON-Export/Import der Zuordnungen; Fasten/Lücke; Import-Abgleich.
|
|
||||||
- **Zuordnen-Performance:** Katalog-Index im Prozess (5 Min.), Vorschläge nur für sichtbare Zeilen (`POST /bls/foods/suggest-batch`), Suche mit Abort; nach Bestätigen kein Reload der ganzen Ernährungseite. Unmapped ohne Kreuzjoin Liste×Tagebuch; Listenzutaten-Datum aus `recipe.updated_at` oder letzter `recipe_id`-Nutzung. Listen-API `?brief=true`; FDDB-Listenimport rechnet Tageswerte im Hintergrund. Migration **068**.
|
|
||||||
- **Zuordnen-Zeitraum:** Standard letzte 4 Wochen (`since_days`); ältere ungemappte Namen (z. B. Getreide nach Glutenverzicht) bleiben unter „Alle“.
|
|
||||||
- **Katalogsuche:** Fettgehalt mitsuchen (`Joghurt 10%` / `9,5`); Dezimal-Komma bleibt erhalten.
|
|
||||||
- **Manuelle Foods:** Persönlich (`manual_user` + Owner, nur dieser Nutzer) oder zentral (`manual_admin`, alle Nutzer). Stoffe über EAV. Nutzer: Tab Ernährung → **Lebensmittel** (persönlich ändern; zentral nur Admin). Admin: Katalog → Manuell — Listen **Zentral** und **Persönlich**, beide änderbar. Stoffe & Attribute: Bezeichnung editierbar, Schlüssel fest; Makros nur ENERCC/PROT625/FAT/CHO → `nutrition_log`. Mapping: Nutzer-Mapping darf sichtbare Foods; globales Mapping nur BLS/zentral. Offizielle BLS-Zeilen nicht editierbar. Öl/Supplement = ein Lebensmittel, keine Liste.
|
|
||||||
- **Listen / Kombinationen (Mitai):** Tab **Listen** (anlegen, bearbeiten, FDDB-Import). Im Listendialog Zutaten direkt BLS-zuordnen (Status offen/zugeordnet); Suche zuerst Listen-Namen und eigene Foods, dann BLS. **Tandoor:** User-Einstellungen URL + Token, Verbindungstest (`/api/tandoor/*`). Gerichte bleiben in Tandoor; später Mapping + `cooked_yield_g`. Admin-Mappings: `PUT /admin/food-mappings/{id}`.
|
|
||||||
- **Gitea #106:** BLS-Stammdaten, FDDB-Mapping, Item-Tagebuch — http://192.168.2.144:3000/Lars/mitai-jinkendo/issues/106
|
|
||||||
- **Doku:** `.claude/docs/functional/BLS_FOOD_REFERENCE.md`, `.claude/docs/technical/BLS_FOOD_REFERENCE.md`, `docs/issues/issue-bls-food-mapping.md`. Folge #75.
|
|
||||||
|
|
||||||
### Updates (11.04.2026 - Gitea #75, nutrition_score Registry)
|
### Updates (11.04.2026 - Gitea #75, nutrition_score Registry)
|
||||||
|
|
||||||
- **Gitea #75** (offen): Zucker/Ballaststoffe/Lebensmittelqualität, automatisches Lebensmittelprofil, später Mahlzeiten-Timing/Abgleich mit Training — http://192.168.2.144:3000/Lars/mitai-jinkendo/issues/75
|
- **Gitea #75** (offen): Zucker/Ballaststoffe/Lebensmittelqualität, automatisches Lebensmittelprofil, später Mahlzeiten-Timing/Abgleich mit Training — http://192.168.2.144:3000/Lars/mitai-jinkendo/issues/75
|
||||||
@@ -910,7 +891,7 @@ Bottom-Padding Mobile: 80px (Navigation)
|
|||||||
|Auth-Flow|`.claude/library/AUTH.md`|Sicherheit + Sessions|
|
|Auth-Flow|`.claude/library/AUTH.md`|Sicherheit + Sessions|
|
||||||
|API-Referenz|`.claude/library/API\_REFERENCE.md`|Alle Endpoints|
|
|API-Referenz|`.claude/library/API\_REFERENCE.md`|Alle Endpoints|
|
||||||
|Datenbankschema|`.claude/library/DATABASE.md`|Tabellen + Beziehungen|
|
|Datenbankschema|`.claude/library/DATABASE.md`|Tabellen + Beziehungen|
|
||||||
|Dashboard-Widgets|`.claude/docs/technical/DASHBOARD_WIDGETS_AGENT_GUIDE.md`|Katalog, Validierung, Frontend-Registry, konfigurierbare `config`|
|
|Dashboard-Lab-Widgets|`.claude/docs/technical/DASHBOARD_WIDGETS_AGENT_GUIDE.md`|Katalog, Validierung, Frontend-Registry, konfigurierbare `config`|
|
||||||
|Projekt-Doku (Git)|`docs/README.md` + `docs/issues/`|Issue-Specs, Reviews, Platzhalter-Governance, Status-Snapshots|
|
|Projekt-Doku (Git)|`docs/README.md` + `docs/issues/`|Issue-Specs, Reviews, Platzhalter-Governance, Status-Snapshots|
|
||||||
|
|
||||||
> Library-Dateien werden mit `/document` generiert und nach größeren
|
> Library-Dateien werden mit `/document` generiert und nach größeren
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
"""BLS 4.0 ingest (official MRI XLSX)."""
|
|
||||||
@@ -1,321 +0,0 @@
|
|||||||
"""Official BLS 4.0 component labels (MRI documentation) and placeholder repair.
|
|
||||||
|
|
||||||
``attr_key`` stays the identity. These names only fill empty or key-equal ``name_de``.
|
|
||||||
"""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
# Keys that feed nutrition_log kcal / protein_g / fat_g / carbs_g
|
|
||||||
MACRO_KEYS = ("ENERCC", "PROT625", "FAT", "CHO")
|
|
||||||
|
|
||||||
GROUP_META = (
|
|
||||||
("energy", "Energie"),
|
|
||||||
("macro", "Makronährstoffe"),
|
|
||||||
("carbs", "Kohlenhydrate"),
|
|
||||||
("fiber", "Ballaststoffe"),
|
|
||||||
("fatty_acids", "Fettsäuren"),
|
|
||||||
("amino_acids", "Aminosäuren"),
|
|
||||||
("vitamins", "Vitamine"),
|
|
||||||
("minerals", "Mineralstoffe"),
|
|
||||||
("organic_acids", "Organische Säuren"),
|
|
||||||
("polyols", "Zuckeralkohole"),
|
|
||||||
("other", "Sonstige"),
|
|
||||||
("extension", "Eigene Erweiterungen"),
|
|
||||||
("unknown", "Ohne Gruppe"),
|
|
||||||
)
|
|
||||||
GROUP_LABELS = {key: label for key, label in GROUP_META}
|
|
||||||
GROUP_RANK = {key: i for i, (key, _) in enumerate(GROUP_META)}
|
|
||||||
|
|
||||||
# Code → (name_de, unit, category) from BLS 4.0 Referenzhandbuch (Jan 2026)
|
|
||||||
_OFFICIAL: dict[str, tuple[str, str, str]] = {
|
|
||||||
"ENERCJ": ("Energie (Kilojoule)", "kJ", "energy"),
|
|
||||||
"ENERCC": ("Energie (Kilokalorien)", "kcal", "energy"),
|
|
||||||
"WATER": ("Wasser", "g", "macro"),
|
|
||||||
"PROT625": ("Protein (N×6,25)", "g", "macro"),
|
|
||||||
"FAT": ("Fett", "g", "macro"),
|
|
||||||
"CHO": ("Kohlenhydrate, verfügbar", "g", "macro"),
|
|
||||||
"FIBT": ("Ballaststoffe, gesamt", "g", "fiber"),
|
|
||||||
"ALC": ("Alkohol (Ethanol)", "g", "macro"),
|
|
||||||
"OA": ("Organische Säuren, gesamt", "g", "organic_acids"),
|
|
||||||
"ASH": ("Rohasche", "g", "macro"),
|
|
||||||
"POLYL": ("Zuckeralkohole, gesamt", "g", "polyols"),
|
|
||||||
"GLUS": ("Glucose", "g", "carbs"),
|
|
||||||
"FRUS": ("Fructose", "g", "carbs"),
|
|
||||||
"GALS": ("Galactose", "g", "carbs"),
|
|
||||||
"MNSAC": ("Monosaccharide, gesamt", "g", "carbs"),
|
|
||||||
"SUCS": ("Saccharose", "g", "carbs"),
|
|
||||||
"MALS": ("Maltose", "g", "carbs"),
|
|
||||||
"LACS": ("Lactose", "g", "carbs"),
|
|
||||||
"DISAC": ("Disaccharide, gesamt", "g", "carbs"),
|
|
||||||
"SUGAR": ("Zucker, gesamt", "g", "carbs"),
|
|
||||||
"OLSAC": ("Oligosaccharide, verfügbar", "g", "carbs"),
|
|
||||||
"STARCH": ("Stärke", "g", "carbs"),
|
|
||||||
"FIBLMW": ("Ballaststoffe, niedermolekular", "g", "fiber"),
|
|
||||||
"FIBHMW": ("Ballaststoffe, hochmolekular", "g", "fiber"),
|
|
||||||
"FIBINS": ("Ballaststoffe, wasserunlöslich", "g", "fiber"),
|
|
||||||
"FIBSOL": ("Ballaststoffe, wasserlöslich", "g", "fiber"),
|
|
||||||
"FIBHMWS": ("Ballaststoffe, hochmolekular, wasserlöslich", "g", "fiber"),
|
|
||||||
"FIBHMWI": ("Ballaststoffe, hochmolekular, wasserunlöslich", "g", "fiber"),
|
|
||||||
"FASAT": ("Fettsäuren, gesättigt, gesamt", "g", "fatty_acids"),
|
|
||||||
"F4:0": ("Buttersäure (C4:0)", "g", "fatty_acids"),
|
|
||||||
"F6:0": ("Capronsäure (C6:0)", "g", "fatty_acids"),
|
|
||||||
"F8:0": ("Caprylsäure (C8:0)", "g", "fatty_acids"),
|
|
||||||
"F10:0": ("Caprinsäure (C10:0)", "g", "fatty_acids"),
|
|
||||||
"F12:0": ("Laurinsäure (C12:0)", "g", "fatty_acids"),
|
|
||||||
"F14:0": ("Myristinsäure (C14:0)", "g", "fatty_acids"),
|
|
||||||
"F15:0": ("Pentadecylsäure (C15:0)", "g", "fatty_acids"),
|
|
||||||
"F16:0": ("Palmitinsäure (C16:0)", "g", "fatty_acids"),
|
|
||||||
"F17:0": ("Margarinsäure (C17:0)", "g", "fatty_acids"),
|
|
||||||
"F18:0": ("Stearinsäure (C18:0)", "g", "fatty_acids"),
|
|
||||||
"F20:0": ("Arachinsäure (C20:0)", "g", "fatty_acids"),
|
|
||||||
"F22:0": ("Behensäure (C22:0)", "g", "fatty_acids"),
|
|
||||||
"F24:0": ("Lignocerinsäure (C24:0)", "g", "fatty_acids"),
|
|
||||||
"FAMS": ("Fettsäuren, einfach ungesättigt, gesamt", "g", "fatty_acids"),
|
|
||||||
"F14:1CN5": ("Myristoleinsäure (C14:1 n-5 cis)", "g", "fatty_acids"),
|
|
||||||
"F16:1CN7": ("Palmitoleinsäure (C16:1 n-7 cis)", "g", "fatty_acids"),
|
|
||||||
"F18:1CN7": ("Vaccensäure (C18:1 n-7 cis)", "g", "fatty_acids"),
|
|
||||||
"F18:1CN9": ("Ölsäure (C18:1 n-9 cis)", "g", "fatty_acids"),
|
|
||||||
"F20:1CN9": ("Gondosäure (C20:1 n-9 cis)", "g", "fatty_acids"),
|
|
||||||
"F22:1CN9": ("Erucasäure (C22:1 n-9 cis)", "g", "fatty_acids"),
|
|
||||||
"FAPU": ("Fettsäuren, mehrfach ungesättigt, gesamt", "g", "fatty_acids"),
|
|
||||||
"FAPUN3": ("Omega-3-Fettsäuren, gesamt", "g", "fatty_acids"),
|
|
||||||
"FAPUN6": ("Omega-6-Fettsäuren, gesamt", "g", "fatty_acids"),
|
|
||||||
"F18:2CN6": ("Linolsäure (C18:2 n-6 cis)", "g", "fatty_acids"),
|
|
||||||
"F18:2C9T11": ("Konjugierte Linolsäure (C18:2 cis-9, trans-11)", "g", "fatty_acids"),
|
|
||||||
"F18:3CN3": ("Alpha-Linolensäure (C18:3 n-3)", "g", "fatty_acids"),
|
|
||||||
"F18:3CN6": ("Gamma-Linolensäure (C18:3 n-6)", "g", "fatty_acids"),
|
|
||||||
"F18:4CN3": ("Stearidonsäure (C18:4 n-3)", "g", "fatty_acids"),
|
|
||||||
"F20:2CN6": ("Eicosadiensäure (C20:2 n-6)", "g", "fatty_acids"),
|
|
||||||
"F20:3CN6": ("Dihomo-Gamma-Linolensäure (C20:3 n-6)", "g", "fatty_acids"),
|
|
||||||
"F20:4CN6": ("Arachidonsäure (C20:4 n-6)", "g", "fatty_acids"),
|
|
||||||
"F20:5CN3": ("Eicosapentaensäure EPA (C20:5 n-3)", "g", "fatty_acids"),
|
|
||||||
"F22:5CN3": ("Docosapentaensäure DPA (C22:5 n-3)", "g", "fatty_acids"),
|
|
||||||
"F22:6CN3": ("Docosahexaensäure DHA (C22:6 n-3)", "g", "fatty_acids"),
|
|
||||||
"FAX": ("Fettsäuren, sonstige", "g", "fatty_acids"),
|
|
||||||
"AAE9": ("Aminosäuren, unentbehrlich, gesamt", "g", "amino_acids"),
|
|
||||||
"ILE": ("Isoleucin", "g", "amino_acids"),
|
|
||||||
"LEU": ("Leucin", "g", "amino_acids"),
|
|
||||||
"LYS": ("Lysin", "g", "amino_acids"),
|
|
||||||
"MET": ("Methionin", "g", "amino_acids"),
|
|
||||||
"PHE": ("Phenylalanin", "g", "amino_acids"),
|
|
||||||
"THR": ("Threonin", "g", "amino_acids"),
|
|
||||||
"TRP": ("Tryptophan", "g", "amino_acids"),
|
|
||||||
"VAL": ("Valin", "g", "amino_acids"),
|
|
||||||
"HIS": ("Histidin", "g", "amino_acids"),
|
|
||||||
"ALA": ("Alanin", "g", "amino_acids"),
|
|
||||||
"ARG": ("Arginin", "g", "amino_acids"),
|
|
||||||
"ASP": ("Asparaginsäure inkl. Asparagin", "g", "amino_acids"),
|
|
||||||
"CYS": ("Cystein", "g", "amino_acids"),
|
|
||||||
"GLU": ("Glutaminsäure inkl. Glutamin", "g", "amino_acids"),
|
|
||||||
"GLY": ("Glycin", "g", "amino_acids"),
|
|
||||||
"PRO": ("Prolin", "g", "amino_acids"),
|
|
||||||
"SER": ("Serin", "g", "amino_acids"),
|
|
||||||
"TYR": ("Tyrosin", "g", "amino_acids"),
|
|
||||||
"VITA": ("Vitamin A, Retinol-Äquivalent (RE)", "µg", "vitamins"),
|
|
||||||
"VITAA": ("Vitamin A, Retinol-Aktivitätsäquivalent (RAE)", "µg", "vitamins"),
|
|
||||||
"RETOL": ("Retinol", "µg", "vitamins"),
|
|
||||||
"CARTB": ("Beta-Carotin", "µg", "vitamins"),
|
|
||||||
"CAROTPAXB": ("Carotinoide außer Beta-Carotin", "µg", "vitamins"),
|
|
||||||
"VITD": ("Vitamin D", "µg", "vitamins"),
|
|
||||||
"ERGCAL": ("Vitamin D2 (Ergocalciferol)", "µg", "vitamins"),
|
|
||||||
"CHOCAL": ("Vitamin D3 (Cholecalciferol)", "µg", "vitamins"),
|
|
||||||
"VITE": ("Vitamin E (Alpha-Tocopherol)", "mg", "vitamins"),
|
|
||||||
"TOCPHA": ("Alpha-Tocopherol", "mg", "vitamins"),
|
|
||||||
"TOCPHB": ("Beta-Tocopherol", "mg", "vitamins"),
|
|
||||||
"TOCPHG": ("Gamma-Tocopherol", "mg", "vitamins"),
|
|
||||||
"TOCPHD": ("Delta-Tocopherol", "mg", "vitamins"),
|
|
||||||
"TOCTRA": ("Alpha-Tocotrienol", "mg", "vitamins"),
|
|
||||||
"VITK": ("Vitamin K", "µg", "vitamins"),
|
|
||||||
"VITK1": ("Vitamin K1 (Phyllochinon)", "µg", "vitamins"),
|
|
||||||
"VITK2": ("Vitamin K2 (Menachinone)", "µg", "vitamins"),
|
|
||||||
"THIA": ("Vitamin B1 (Thiamin)", "mg", "vitamins"),
|
|
||||||
"RIBF": ("Vitamin B2 (Riboflavin)", "mg", "vitamins"),
|
|
||||||
"NIA": ("Niacin", "mg", "vitamins"),
|
|
||||||
"NIAEQ": ("Niacin-Äquivalent", "mg", "vitamins"),
|
|
||||||
"PANTAC": ("Pantothensäure", "mg", "vitamins"),
|
|
||||||
"VITB6": ("Vitamin B6", "µg", "vitamins"),
|
|
||||||
"BIOT": ("Biotin", "µg", "vitamins"),
|
|
||||||
"FOL": ("Folat-Äquivalent", "µg", "vitamins"),
|
|
||||||
"FOLFD": ("Folat", "µg", "vitamins"),
|
|
||||||
"FOLAC": ("Folsäure, synthetisch", "µg", "vitamins"),
|
|
||||||
"VITB12": ("Vitamin B12 (Cobalamine)", "µg", "vitamins"),
|
|
||||||
"VITC": ("Vitamin C", "mg", "vitamins"),
|
|
||||||
"NACL": ("Salz (Natriumchlorid)", "g", "minerals"),
|
|
||||||
"NA": ("Natrium", "mg", "minerals"),
|
|
||||||
"CLD": ("Chlorid", "mg", "minerals"),
|
|
||||||
"K": ("Kalium", "mg", "minerals"),
|
|
||||||
"CA": ("Calcium", "mg", "minerals"),
|
|
||||||
"MG": ("Magnesium", "mg", "minerals"),
|
|
||||||
"P": ("Phosphor", "mg", "minerals"),
|
|
||||||
"S": ("Schwefel", "mg", "minerals"),
|
|
||||||
"FE": ("Eisen", "mg", "minerals"),
|
|
||||||
"ZN": ("Zink", "mg", "minerals"),
|
|
||||||
"ID": ("Iodid", "µg", "minerals"),
|
|
||||||
"CU": ("Kupfer", "µg", "minerals"),
|
|
||||||
"MN": ("Mangan", "µg", "minerals"),
|
|
||||||
"FD": ("Fluorid", "µg", "minerals"),
|
|
||||||
"CR": ("Chrom", "µg", "minerals"),
|
|
||||||
"MO": ("Molybdän", "µg", "minerals"),
|
|
||||||
"ACEAC": ("Essigsäure", "g", "organic_acids"),
|
|
||||||
"CITAC": ("Zitronensäure", "g", "organic_acids"),
|
|
||||||
"LACAC": ("Milchsäure", "g", "organic_acids"),
|
|
||||||
"MALAC": ("Äpfelsäure", "g", "organic_acids"),
|
|
||||||
"TARAC": ("Weinsäure", "g", "organic_acids"),
|
|
||||||
"MANTL": ("Mannit", "g", "polyols"),
|
|
||||||
"SORTL": ("Sorbit", "g", "polyols"),
|
|
||||||
"XYLTL": ("Xylit", "g", "polyols"),
|
|
||||||
"CHORL": ("Cholesterin", "mg", "other"),
|
|
||||||
"NT": ("Stickstoff, gesamt", "g", "other"),
|
|
||||||
"EPA": ("Eicosapentaensäure (EPA)", "g", "fatty_acids"),
|
|
||||||
"DHA": ("Docosahexaensäure (DHA)", "g", "fatty_acids"),
|
|
||||||
"DPA": ("Docosapentaensäure (DPA)", "g", "fatty_acids"),
|
|
||||||
"ALA": ("Alpha-Linolensäure (ALA)", "g", "fatty_acids"),
|
|
||||||
"OMEGA3": ("Omega-3-Fettsäuren gesamt", "g", "fatty_acids"),
|
|
||||||
"OMEGA6": ("Omega-6-Fettsäuren gesamt", "g", "fatty_acids"),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def lookup_key(raw: str | None) -> str:
|
|
||||||
return str(raw or "").strip().upper()
|
|
||||||
|
|
||||||
|
|
||||||
def resolve_group(attr_key: str | None, category: str | None = None, origin: str | None = None) -> tuple[str, str]:
|
|
||||||
cat = (category or "").strip().lower()
|
|
||||||
if cat in GROUP_LABELS:
|
|
||||||
return cat, GROUP_LABELS[cat]
|
|
||||||
lab = official_label(attr_key)
|
|
||||||
if lab and lab["category"] in GROUP_LABELS:
|
|
||||||
return lab["category"], GROUP_LABELS[lab["category"]]
|
|
||||||
if (origin or "") == "extension":
|
|
||||||
return "extension", GROUP_LABELS["extension"]
|
|
||||||
return "unknown", GROUP_LABELS["unknown"]
|
|
||||||
|
|
||||||
|
|
||||||
def annotate_attribute(row: dict[str, Any]) -> dict[str, Any]:
|
|
||||||
key = row.get("attr_key")
|
|
||||||
group_key, group_label = resolve_group(key, row.get("category"), row.get("origin"))
|
|
||||||
row["is_macro"] = key in MACRO_KEYS
|
|
||||||
row["needs_label"] = label_is_placeholder(row.get("name_de"), key)
|
|
||||||
row["group_key"] = group_key
|
|
||||||
row["group_label"] = group_label
|
|
||||||
row["group_rank"] = GROUP_RANK[group_key]
|
|
||||||
return row
|
|
||||||
|
|
||||||
|
|
||||||
def sort_attributes(rows: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
|
||||||
def key_of(row: dict[str, Any]):
|
|
||||||
return (
|
|
||||||
row.get("group_rank", 99),
|
|
||||||
0 if row.get("is_macro") else 1,
|
|
||||||
row.get("sort_order") if row.get("sort_order") is not None else 0,
|
|
||||||
(row.get("name_de") or row.get("attr_key") or "").casefold(),
|
|
||||||
row.get("attr_key") or "",
|
|
||||||
)
|
|
||||||
|
|
||||||
return sorted(rows, key=key_of)
|
|
||||||
|
|
||||||
|
|
||||||
def official_label(attr_key: str | None) -> dict[str, str] | None:
|
|
||||||
key = lookup_key(attr_key)
|
|
||||||
hit = _OFFICIAL.get(key)
|
|
||||||
if not hit:
|
|
||||||
hit = _OFFICIAL.get(key.replace("_", ":"))
|
|
||||||
if not hit:
|
|
||||||
return None
|
|
||||||
name_de, unit, category = hit
|
|
||||||
return {"name_de": name_de, "unit": unit, "category": category}
|
|
||||||
|
|
||||||
|
|
||||||
def label_is_placeholder(name_de: str | None, attr_key: str | None) -> bool:
|
|
||||||
name = (name_de or "").strip()
|
|
||||||
key = lookup_key(attr_key)
|
|
||||||
return (not name) or name.upper() == key
|
|
||||||
|
|
||||||
|
|
||||||
def apply_official_name(attr_key: str | None, name_de: str | None) -> str:
|
|
||||||
if not label_is_placeholder(name_de, attr_key):
|
|
||||||
return (name_de or "").strip()
|
|
||||||
lab = official_label(attr_key)
|
|
||||||
if lab:
|
|
||||||
return lab["name_de"]
|
|
||||||
return (name_de or "").strip() or lookup_key(attr_key)
|
|
||||||
|
|
||||||
|
|
||||||
def enrich_attributes_in_memory(attributes: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
|
||||||
for a in attributes:
|
|
||||||
key = a.get("attr_key")
|
|
||||||
lab = official_label(key)
|
|
||||||
if label_is_placeholder(a.get("name_de"), key) and lab:
|
|
||||||
a["name_de"] = lab["name_de"]
|
|
||||||
a["unit"] = a.get("unit") or lab["unit"]
|
|
||||||
a["category"] = a.get("category") or lab["category"]
|
|
||||||
return attributes
|
|
||||||
|
|
||||||
|
|
||||||
def fill_placeholder_labels(cur) -> dict[str, int]:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id, attr_key, name_de, unit, category
|
|
||||||
FROM food_attributes
|
|
||||||
WHERE is_active = true
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
updated = 0
|
|
||||||
for row in cur.fetchall():
|
|
||||||
rec = dict(row)
|
|
||||||
lab = official_label(rec.get("attr_key"))
|
|
||||||
if not lab:
|
|
||||||
continue
|
|
||||||
need_name = label_is_placeholder(rec.get("name_de"), rec.get("attr_key"))
|
|
||||||
need_cat = not (rec.get("category") or "").strip()
|
|
||||||
if not need_name and not need_cat:
|
|
||||||
continue
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
UPDATE food_attributes
|
|
||||||
SET name_de = %s,
|
|
||||||
unit = COALESCE(NULLIF(BTRIM(COALESCE(unit, '')), ''), %s),
|
|
||||||
category = COALESCE(NULLIF(BTRIM(COALESCE(category, '')), ''), %s),
|
|
||||||
updated_at = NOW()
|
|
||||||
WHERE id = %s
|
|
||||||
""",
|
|
||||||
(
|
|
||||||
lab["name_de"] if need_name else rec.get("name_de"),
|
|
||||||
lab["unit"],
|
|
||||||
lab["category"],
|
|
||||||
rec["id"],
|
|
||||||
),
|
|
||||||
)
|
|
||||||
updated += 1
|
|
||||||
return {"updated": updated}
|
|
||||||
|
|
||||||
|
|
||||||
def enrich_labels_from_food_headers(cur, headers: list[dict[str, Any]] | None) -> int:
|
|
||||||
updated = 0
|
|
||||||
for h in headers or []:
|
|
||||||
key = h.get("attr_key")
|
|
||||||
name = (h.get("name_de") or "").strip()
|
|
||||||
unit = (h.get("unit") or "").strip() or None
|
|
||||||
if not key or not name or label_is_placeholder(name, key):
|
|
||||||
lab = official_label(key)
|
|
||||||
if not lab:
|
|
||||||
continue
|
|
||||||
name = lab["name_de"]
|
|
||||||
unit = unit or lab["unit"]
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
UPDATE food_attributes
|
|
||||||
SET name_de = %s,
|
|
||||||
unit = COALESCE(%s, unit),
|
|
||||||
updated_at = NOW()
|
|
||||||
WHERE attr_key = %s
|
|
||||||
AND (
|
|
||||||
name_de IS NULL OR BTRIM(name_de) = '' OR name_de = attr_key
|
|
||||||
)
|
|
||||||
""",
|
|
||||||
(name, unit, key),
|
|
||||||
)
|
|
||||||
if cur.rowcount:
|
|
||||||
updated += cur.rowcount
|
|
||||||
return updated
|
|
||||||
@@ -1,153 +0,0 @@
|
|||||||
"""Apply parsed BLS 4.0 data: upsert attributes and foods, never delete official rows."""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from psycopg2.extras import execute_values
|
|
||||||
|
|
||||||
VALUE_PAGE = 2000
|
|
||||||
FOOD_PAGE = 500
|
|
||||||
|
|
||||||
|
|
||||||
def should_persist_value(val: dict[str, Any]) -> bool:
|
|
||||||
if val.get("is_trace"):
|
|
||||||
return True
|
|
||||||
return val.get("value_num") is not None
|
|
||||||
|
|
||||||
|
|
||||||
def upsert_attributes(cur, attributes: list[dict[str, Any]]) -> dict[str, int]:
|
|
||||||
from bls.attribute_labels import enrich_attributes_in_memory
|
|
||||||
|
|
||||||
attributes = enrich_attributes_in_memory(list(attributes))
|
|
||||||
inserted = updated = 0
|
|
||||||
for a in attributes:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO food_attributes
|
|
||||||
(attr_key, name_de, name_en, unit, category, data_type, origin, sort_order, updated_at)
|
|
||||||
VALUES (%s, %s, %s, %s, %s, %s, 'official_bls', %s, NOW())
|
|
||||||
ON CONFLICT (attr_key) DO UPDATE SET
|
|
||||||
name_de = EXCLUDED.name_de,
|
|
||||||
name_en = COALESCE(EXCLUDED.name_en, food_attributes.name_en),
|
|
||||||
unit = COALESCE(EXCLUDED.unit, food_attributes.unit),
|
|
||||||
category = COALESCE(EXCLUDED.category, food_attributes.category),
|
|
||||||
sort_order = EXCLUDED.sort_order,
|
|
||||||
updated_at = NOW()
|
|
||||||
WHERE food_attributes.origin = 'official_bls'
|
|
||||||
RETURNING (xmax = 0) AS inserted
|
|
||||||
""",
|
|
||||||
(
|
|
||||||
a["attr_key"], a["name_de"], a.get("name_en"), a.get("unit"),
|
|
||||||
a.get("category"), a.get("data_type") or "num_per_100g",
|
|
||||||
a.get("sort_order") or 0,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
row = cur.fetchone()
|
|
||||||
if row and row.get("inserted"):
|
|
||||||
inserted += 1
|
|
||||||
else:
|
|
||||||
updated += 1
|
|
||||||
return {"inserted": inserted, "updated": updated, "total": len(attributes)}
|
|
||||||
|
|
||||||
|
|
||||||
def upsert_foods(
|
|
||||||
cur,
|
|
||||||
foods: list[dict[str, Any]],
|
|
||||||
bls_version: str = "4.0",
|
|
||||||
attribute_headers: list[dict[str, Any]] | None = None,
|
|
||||||
) -> dict[str, int]:
|
|
||||||
if attribute_headers:
|
|
||||||
from bls.attribute_labels import enrich_labels_from_food_headers
|
|
||||||
enrich_labels_from_food_headers(cur, attribute_headers)
|
|
||||||
cur.execute("SELECT attr_key, id FROM food_attributes")
|
|
||||||
attr_ids = {r["attr_key"]: r["id"] for r in cur.fetchall()}
|
|
||||||
codes = [f["bls_code"] for f in foods if f.get("bls_code")]
|
|
||||||
existing: set[str] = set()
|
|
||||||
if codes:
|
|
||||||
cur.execute("SELECT bls_code FROM food_catalog WHERE bls_code = ANY(%s)", (codes,))
|
|
||||||
existing = {r["bls_code"] for r in cur.fetchall()}
|
|
||||||
inserted = sum(1 for c in codes if c not in existing)
|
|
||||||
updated = len(codes) - inserted
|
|
||||||
food_rows = [
|
|
||||||
(
|
|
||||||
f["bls_code"], f["name_de"], f.get("name_en"), f.get("food_group"),
|
|
||||||
"official_bls", bls_version, "bls_4.0",
|
|
||||||
)
|
|
||||||
for f in foods if f.get("bls_code")
|
|
||||||
]
|
|
||||||
if food_rows:
|
|
||||||
execute_values(
|
|
||||||
cur,
|
|
||||||
"""
|
|
||||||
INSERT INTO food_catalog
|
|
||||||
(bls_code, name_de, name_en, food_group, catalog_kind, bls_version, source)
|
|
||||||
VALUES %s
|
|
||||||
ON CONFLICT (bls_code) DO UPDATE SET
|
|
||||||
name_de = EXCLUDED.name_de,
|
|
||||||
name_en = EXCLUDED.name_en,
|
|
||||||
food_group = EXCLUDED.food_group,
|
|
||||||
bls_version = EXCLUDED.bls_version,
|
|
||||||
catalog_kind = 'official_bls',
|
|
||||||
source = 'bls_4.0',
|
|
||||||
is_active = true,
|
|
||||||
updated_at = NOW()
|
|
||||||
WHERE food_catalog.catalog_kind = 'official_bls'
|
|
||||||
""",
|
|
||||||
food_rows,
|
|
||||||
page_size=FOOD_PAGE,
|
|
||||||
)
|
|
||||||
|
|
||||||
food_ids: dict[str, Any] = {}
|
|
||||||
if codes:
|
|
||||||
cur.execute("SELECT bls_code, id FROM food_catalog WHERE bls_code = ANY(%s)", (codes,))
|
|
||||||
food_ids = {r["bls_code"]: r["id"] for r in cur.fetchall()}
|
|
||||||
|
|
||||||
value_rows = []
|
|
||||||
for f in foods:
|
|
||||||
food_id = food_ids.get(f.get("bls_code"))
|
|
||||||
if not food_id:
|
|
||||||
continue
|
|
||||||
for val in f.get("values") or []:
|
|
||||||
if not should_persist_value(val):
|
|
||||||
continue
|
|
||||||
aid = attr_ids.get(val["attr_key"])
|
|
||||||
if not aid:
|
|
||||||
continue
|
|
||||||
value_rows.append((
|
|
||||||
food_id,
|
|
||||||
aid,
|
|
||||||
None if val.get("is_trace") else val.get("value_num"),
|
|
||||||
bool(val.get("is_trace")),
|
|
||||||
val.get("origin_code"),
|
|
||||||
val.get("reference_text"),
|
|
||||||
))
|
|
||||||
|
|
||||||
if value_rows:
|
|
||||||
execute_values(
|
|
||||||
cur,
|
|
||||||
"""
|
|
||||||
INSERT INTO food_attribute_values
|
|
||||||
(food_id, attribute_id, value_num, is_trace, origin_code, reference_text, updated_at)
|
|
||||||
VALUES %s
|
|
||||||
ON CONFLICT (food_id, attribute_id) DO UPDATE SET
|
|
||||||
value_num = EXCLUDED.value_num,
|
|
||||||
is_trace = EXCLUDED.is_trace,
|
|
||||||
origin_code = EXCLUDED.origin_code,
|
|
||||||
reference_text = EXCLUDED.reference_text,
|
|
||||||
updated_at = NOW()
|
|
||||||
""",
|
|
||||||
value_rows,
|
|
||||||
template="(%s, %s, %s, %s, %s, %s, NOW())",
|
|
||||||
page_size=VALUE_PAGE,
|
|
||||||
)
|
|
||||||
|
|
||||||
from data_layer.food_suggest import invalidate_suggest_index
|
|
||||||
invalidate_suggest_index()
|
|
||||||
return {
|
|
||||||
"inserted": inserted,
|
|
||||||
"updated": updated,
|
|
||||||
"foods_inserted": inserted,
|
|
||||||
"foods_updated": updated,
|
|
||||||
"values_written": len(value_rows),
|
|
||||||
"foods_total": len(foods),
|
|
||||||
}
|
|
||||||
@@ -1,176 +0,0 @@
|
|||||||
"""In-memory BLS import jobs so HTTP requests stay short (avoid proxy 504)."""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import threading
|
|
||||||
import time
|
|
||||||
import uuid
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from bls.import_service import upsert_attributes, upsert_foods
|
|
||||||
from bls.parser import parse_components_xlsx, parse_foods_xlsx
|
|
||||||
from db import get_cursor, get_db
|
|
||||||
|
|
||||||
FOOD_CHUNK = 300
|
|
||||||
JOB_TTL_S = 2 * 60 * 60
|
|
||||||
MAX_JOBS = 12
|
|
||||||
|
|
||||||
_lock = threading.Lock()
|
|
||||||
_jobs: dict[str, dict[str, Any]] = {}
|
|
||||||
|
|
||||||
|
|
||||||
def _public(job: dict[str, Any]) -> dict[str, Any]:
|
|
||||||
return {
|
|
||||||
"id": job["id"],
|
|
||||||
"kind": job["kind"],
|
|
||||||
"status": job["status"],
|
|
||||||
"check": job.get("check"),
|
|
||||||
"progress": job.get("progress"),
|
|
||||||
"result": job.get("result"),
|
|
||||||
"error": job.get("error"),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def _purge_locked(now: float) -> None:
|
|
||||||
stale = [jid for jid, job in _jobs.items() if now - job["created_at"] > JOB_TTL_S]
|
|
||||||
for jid in stale:
|
|
||||||
_jobs.pop(jid, None)
|
|
||||||
if len(_jobs) <= MAX_JOBS:
|
|
||||||
return
|
|
||||||
oldest = sorted(_jobs.values(), key=lambda j: j["created_at"])
|
|
||||||
for job in oldest[: max(0, len(_jobs) - MAX_JOBS)]:
|
|
||||||
if job["status"] in ("checking", "applying"):
|
|
||||||
continue
|
|
||||||
_jobs.pop(job["id"], None)
|
|
||||||
|
|
||||||
|
|
||||||
def get_job(job_id: str) -> dict[str, Any] | None:
|
|
||||||
with _lock:
|
|
||||||
job = _jobs.get(job_id)
|
|
||||||
return _public(job) if job else None
|
|
||||||
|
|
||||||
|
|
||||||
def create_and_check(kind: str, raw: bytes) -> str:
|
|
||||||
if kind not in ("components", "foods"):
|
|
||||||
raise ValueError("kind muss components oder foods sein")
|
|
||||||
job_id = str(uuid.uuid4())
|
|
||||||
now = time.time()
|
|
||||||
with _lock:
|
|
||||||
_purge_locked(now)
|
|
||||||
_jobs[job_id] = {
|
|
||||||
"id": job_id,
|
|
||||||
"kind": kind,
|
|
||||||
"status": "checking",
|
|
||||||
"created_at": now,
|
|
||||||
"raw": raw,
|
|
||||||
"parsed": None,
|
|
||||||
"check": None,
|
|
||||||
"progress": None,
|
|
||||||
"result": None,
|
|
||||||
"error": None,
|
|
||||||
}
|
|
||||||
threading.Thread(target=_run_check, args=(job_id,), daemon=True).start()
|
|
||||||
return job_id
|
|
||||||
|
|
||||||
|
|
||||||
def start_apply(job_id: str) -> None:
|
|
||||||
with _lock:
|
|
||||||
job = _jobs.get(job_id)
|
|
||||||
if job is None:
|
|
||||||
raise KeyError(job_id)
|
|
||||||
if job["status"] != "checked":
|
|
||||||
raise ValueError("Zuerst die Prüfung abwarten")
|
|
||||||
if job.get("parsed") is None:
|
|
||||||
raise ValueError("Geparste Datei nicht mehr vorhanden — Datei neu wählen")
|
|
||||||
job["status"] = "applying"
|
|
||||||
job["error"] = None
|
|
||||||
job["progress"] = {"current": 0, "total": 0}
|
|
||||||
threading.Thread(target=_run_apply, args=(job_id,), daemon=True).start()
|
|
||||||
|
|
||||||
|
|
||||||
def _update(job_id: str, **fields: Any) -> None:
|
|
||||||
with _lock:
|
|
||||||
job = _jobs.get(job_id)
|
|
||||||
if not job:
|
|
||||||
return
|
|
||||||
job.update(fields)
|
|
||||||
|
|
||||||
|
|
||||||
def _run_check(job_id: str) -> None:
|
|
||||||
with _lock:
|
|
||||||
job = _jobs.get(job_id)
|
|
||||||
if not job:
|
|
||||||
return
|
|
||||||
kind = job["kind"]
|
|
||||||
raw = job["raw"]
|
|
||||||
try:
|
|
||||||
if kind == "components":
|
|
||||||
attrs = parse_components_xlsx(raw)
|
|
||||||
_update(
|
|
||||||
job_id,
|
|
||||||
parsed=attrs,
|
|
||||||
raw=None,
|
|
||||||
check={"attributes": len(attrs)},
|
|
||||||
status="checked",
|
|
||||||
)
|
|
||||||
return
|
|
||||||
parsed = parse_foods_xlsx(raw)
|
|
||||||
_update(
|
|
||||||
job_id,
|
|
||||||
parsed=parsed,
|
|
||||||
raw=None,
|
|
||||||
check={
|
|
||||||
"foods": len(parsed["foods"]),
|
|
||||||
"attribute_columns": len(parsed["attribute_headers"]),
|
|
||||||
},
|
|
||||||
status="checked",
|
|
||||||
)
|
|
||||||
except Exception as e:
|
|
||||||
_update(job_id, status="error", error=str(e), raw=None, parsed=None)
|
|
||||||
|
|
||||||
|
|
||||||
def _run_apply(job_id: str) -> None:
|
|
||||||
with _lock:
|
|
||||||
job = _jobs.get(job_id)
|
|
||||||
if not job:
|
|
||||||
return
|
|
||||||
kind = job["kind"]
|
|
||||||
parsed = job["parsed"]
|
|
||||||
try:
|
|
||||||
if kind == "components":
|
|
||||||
with get_db() as conn:
|
|
||||||
stats = upsert_attributes(get_cursor(conn), parsed)
|
|
||||||
_update(job_id, status="done", result=stats, parsed=None, progress={"current": stats.get("total", 0), "total": stats.get("total", 0)})
|
|
||||||
return
|
|
||||||
|
|
||||||
foods = parsed["foods"]
|
|
||||||
total = len(foods)
|
|
||||||
inserted = updated = values_written = 0
|
|
||||||
_update(job_id, progress={"current": 0, "total": total})
|
|
||||||
headers = parsed.get("attribute_headers")
|
|
||||||
for i in range(0, total, FOOD_CHUNK):
|
|
||||||
chunk = foods[i : i + FOOD_CHUNK]
|
|
||||||
with get_db() as conn:
|
|
||||||
stats = upsert_foods(
|
|
||||||
get_cursor(conn),
|
|
||||||
chunk,
|
|
||||||
attribute_headers=headers if i == 0 else None,
|
|
||||||
)
|
|
||||||
inserted += stats.get("inserted", 0)
|
|
||||||
updated += stats.get("updated", 0)
|
|
||||||
values_written += stats.get("values_written", 0)
|
|
||||||
_update(job_id, progress={"current": min(i + FOOD_CHUNK, total), "total": total})
|
|
||||||
_update(
|
|
||||||
job_id,
|
|
||||||
status="done",
|
|
||||||
parsed=None,
|
|
||||||
result={
|
|
||||||
"inserted": inserted,
|
|
||||||
"updated": updated,
|
|
||||||
"foods_inserted": inserted,
|
|
||||||
"foods_updated": updated,
|
|
||||||
"values_written": values_written,
|
|
||||||
"foods_total": total,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
except Exception as e:
|
|
||||||
_update(job_id, status="error", error=str(e))
|
|
||||||
@@ -1,168 +0,0 @@
|
|||||||
"""Parse official BLS 4.0 XLSX files without a hardcoded nutrient code list."""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import re
|
|
||||||
from io import BytesIO
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from openpyxl import load_workbook
|
|
||||||
|
|
||||||
# Header like: "ENERCJ Energie (Kilojoule) [kJ/100g]"
|
|
||||||
ATTR_HEADER_RE = re.compile(
|
|
||||||
r"^([A-Z][A-Z0-9:]{1,20})\s+(.+?)(?:\s*\[([^\]]+)\])?\s*$"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _cell(v: Any) -> str:
|
|
||||||
if v is None:
|
|
||||||
return ""
|
|
||||||
return str(v).strip()
|
|
||||||
|
|
||||||
|
|
||||||
def parse_components_xlsx(data: bytes) -> list[dict[str, Any]]:
|
|
||||||
wb = load_workbook(filename=BytesIO(data), read_only=True, data_only=True)
|
|
||||||
ws = wb.active
|
|
||||||
rows = ws.iter_rows(values_only=True)
|
|
||||||
header = [_cell(c) for c in next(rows)]
|
|
||||||
idx = {h.lower(): i for i, h in enumerate(header) if h}
|
|
||||||
|
|
||||||
def col(*names: str, exclude: tuple[str, ...] = ()) -> int | None:
|
|
||||||
for n in names:
|
|
||||||
if n.lower() in idx:
|
|
||||||
return idx[n.lower()]
|
|
||||||
for key, i in idx.items():
|
|
||||||
if any(ex in key for ex in exclude):
|
|
||||||
continue
|
|
||||||
for n in names:
|
|
||||||
if n.lower() in key:
|
|
||||||
return i
|
|
||||||
return None
|
|
||||||
|
|
||||||
i_code = col(
|
|
||||||
"component code", "komponentencode", "attr_key", "schlüssel", "schluessel",
|
|
||||||
"code", "komponente",
|
|
||||||
)
|
|
||||||
i_de = col(
|
|
||||||
"name_de", "bezeichnung_de", "bezeichnung deutsch", "name de",
|
|
||||||
"deutsch", "german", "bezeichnung",
|
|
||||||
exclude=("name_en", "bezeichnung_en", "english", "englisch", "name en"),
|
|
||||||
)
|
|
||||||
i_en = col("name_en", "bezeichnung_en", "name en", "english", "englisch")
|
|
||||||
i_unit = col("unit", "einheit")
|
|
||||||
i_cat = col("category", "kategorie", "gruppe")
|
|
||||||
if i_code is None:
|
|
||||||
i_code = 0
|
|
||||||
|
|
||||||
from bls.attribute_labels import apply_official_name
|
|
||||||
|
|
||||||
out = []
|
|
||||||
sort_order = 0
|
|
||||||
for raw in rows:
|
|
||||||
if not raw:
|
|
||||||
continue
|
|
||||||
code = _cell(raw[i_code] if i_code < len(raw) else "")
|
|
||||||
if not code or code.lower() in ("code", "schlüssel", "schluessel", "component code"):
|
|
||||||
continue
|
|
||||||
raw_de = _cell(raw[i_de] if i_de is not None and i_de < len(raw) else "")
|
|
||||||
name_de = apply_official_name(code, raw_de)
|
|
||||||
name_en = _cell(raw[i_en] if i_en is not None and i_en < len(raw) else "") or None
|
|
||||||
unit = _cell(raw[i_unit] if i_unit is not None and i_unit < len(raw) else "") or None
|
|
||||||
category = _cell(raw[i_cat] if i_cat is not None and i_cat < len(raw) else "") or None
|
|
||||||
sort_order += 1
|
|
||||||
out.append({
|
|
||||||
"attr_key": code,
|
|
||||||
"name_de": name_de,
|
|
||||||
"name_en": name_en,
|
|
||||||
"unit": unit,
|
|
||||||
"category": category,
|
|
||||||
"data_type": "num_per_100g",
|
|
||||||
"origin": "official_bls",
|
|
||||||
"sort_order": sort_order,
|
|
||||||
})
|
|
||||||
wb.close()
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
def _parse_value_header(title: str) -> tuple[str | None, str, str | None]:
|
|
||||||
t = title.strip()
|
|
||||||
m = ATTR_HEADER_RE.match(t)
|
|
||||||
if m:
|
|
||||||
return m.group(1), m.group(2).strip(), m.group(3)
|
|
||||||
# Fallback: first token
|
|
||||||
parts = t.split()
|
|
||||||
if parts and re.match(r"^[A-Z][A-Z0-9:]{1,20}$", parts[0]):
|
|
||||||
return parts[0], " ".join(parts[1:]) or parts[0], None
|
|
||||||
return None, t, None
|
|
||||||
|
|
||||||
|
|
||||||
def parse_foods_xlsx(data: bytes) -> dict[str, Any]:
|
|
||||||
wb = load_workbook(filename=BytesIO(data), read_only=True, data_only=True)
|
|
||||||
ws = wb.active
|
|
||||||
rows = ws.iter_rows(values_only=True)
|
|
||||||
header = [_cell(c) for c in next(rows)]
|
|
||||||
if len(header) < 3:
|
|
||||||
wb.close()
|
|
||||||
raise ValueError("BLS-Datendatei: erwartet mindestens BLS-Code, Name DE, Name EN")
|
|
||||||
|
|
||||||
triples: list[dict[str, Any]] = []
|
|
||||||
i = 3
|
|
||||||
while i < len(header):
|
|
||||||
code, name_de, unit = _parse_value_header(header[i])
|
|
||||||
origin_i = i + 1 if i + 1 < len(header) else None
|
|
||||||
ref_i = i + 2 if i + 2 < len(header) else None
|
|
||||||
triples.append({
|
|
||||||
"attr_key": code or f"COL{i}",
|
|
||||||
"name_de": name_de,
|
|
||||||
"unit": unit,
|
|
||||||
"value_col": i,
|
|
||||||
"origin_col": origin_i,
|
|
||||||
"ref_col": ref_i,
|
|
||||||
})
|
|
||||||
i += 3 if (origin_i is not None and ref_i is not None) else 1
|
|
||||||
|
|
||||||
foods = []
|
|
||||||
for raw in rows:
|
|
||||||
if not raw:
|
|
||||||
continue
|
|
||||||
code = _cell(raw[0] if len(raw) else "")
|
|
||||||
if not code:
|
|
||||||
continue
|
|
||||||
name_de = _cell(raw[1] if len(raw) > 1 else "") or code
|
|
||||||
name_en = _cell(raw[2] if len(raw) > 2 else "") or None
|
|
||||||
values = []
|
|
||||||
for t in triples:
|
|
||||||
vc = t["value_col"]
|
|
||||||
raw_v = raw[vc] if vc < len(raw) else None
|
|
||||||
is_trace = False
|
|
||||||
num = None
|
|
||||||
if raw_v is None or raw_v == "" or raw_v == "-":
|
|
||||||
num = None
|
|
||||||
elif str(raw_v).strip().upper() in ("TR", "TRACE", "SPUREN"):
|
|
||||||
is_trace = True
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
num = float(str(raw_v).replace(",", "."))
|
|
||||||
except (TypeError, ValueError):
|
|
||||||
num = None
|
|
||||||
origin = ""
|
|
||||||
if t["origin_col"] is not None and t["origin_col"] < len(raw):
|
|
||||||
origin = _cell(raw[t["origin_col"]])
|
|
||||||
ref = ""
|
|
||||||
if t["ref_col"] is not None and t["ref_col"] < len(raw):
|
|
||||||
ref = _cell(raw[t["ref_col"]])
|
|
||||||
values.append({
|
|
||||||
"attr_key": t["attr_key"],
|
|
||||||
"value_num": num,
|
|
||||||
"is_trace": is_trace,
|
|
||||||
"origin_code": origin or None,
|
|
||||||
"reference_text": ref or None,
|
|
||||||
})
|
|
||||||
foods.append({
|
|
||||||
"bls_code": code,
|
|
||||||
"name_de": name_de,
|
|
||||||
"name_en": name_en,
|
|
||||||
"food_group": code[0] if code else None,
|
|
||||||
"values": values,
|
|
||||||
})
|
|
||||||
wb.close()
|
|
||||||
return {"attribute_headers": triples, "foods": foods}
|
|
||||||
@@ -1,79 +0,0 @@
|
|||||||
"""Parse FDDB lists_*.csv (name;…;produkte) into recipe + ingredients."""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import csv
|
|
||||||
import io
|
|
||||||
import re
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from data_layer.food_mapping import normalize_food_name, parse_quantity_g
|
|
||||||
|
|
||||||
ING_RE = re.compile(
|
|
||||||
r"(?P<qty>\d+(?:[.,]\d+)?)\s*(?P<unit>g|kg|ml|l)\b\s*(?P<name>.+?)"
|
|
||||||
r"(?=,\s*\d+(?:[.,]\d+)?\s*(?:g|kg|ml|l)\b|$)",
|
|
||||||
re.IGNORECASE | re.DOTALL,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def parse_fddb_produkte(text: str) -> list[dict[str, Any]]:
|
|
||||||
raw = (text or "").strip().strip('"')
|
|
||||||
if not raw:
|
|
||||||
return []
|
|
||||||
out: list[dict[str, Any]] = []
|
|
||||||
for i, m in enumerate(ING_RE.finditer(raw)):
|
|
||||||
qty = float(m.group("qty").replace(",", "."))
|
|
||||||
unit = m.group("unit").lower()
|
|
||||||
name = re.sub(r"\s+", " ", m.group("name")).strip(" ,;")
|
|
||||||
if not name:
|
|
||||||
continue
|
|
||||||
grams = qty
|
|
||||||
if unit == "kg":
|
|
||||||
grams = qty * 1000.0
|
|
||||||
elif unit == "l":
|
|
||||||
grams = qty * 1000.0
|
|
||||||
elif unit == "ml":
|
|
||||||
grams = qty
|
|
||||||
out.append({
|
|
||||||
"source_name_raw": name,
|
|
||||||
"source_name_normalized": normalize_food_name(name),
|
|
||||||
"quantity_raw": f"{m.group('qty').replace(',', '.')} {unit}",
|
|
||||||
"quantity_g": round(grams, 3),
|
|
||||||
"sort_order": i,
|
|
||||||
})
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
def parse_fddb_lists_csv(text: str) -> list[dict[str, Any]]:
|
|
||||||
if text.startswith("\ufeff"):
|
|
||||||
text = text[1:]
|
|
||||||
reader = csv.DictReader(io.StringIO(text), delimiter=";")
|
|
||||||
recipes = []
|
|
||||||
for row in reader:
|
|
||||||
name = (row.get("name") or "").strip().strip('"')
|
|
||||||
if not name:
|
|
||||||
continue
|
|
||||||
try:
|
|
||||||
portions = float(str(row.get("anzahl_portionen") or "1").replace(",", "."))
|
|
||||||
except ValueError:
|
|
||||||
portions = 1.0
|
|
||||||
if portions <= 0:
|
|
||||||
portions = 1.0
|
|
||||||
ingredients = parse_fddb_produkte(row.get("produkte") or "")
|
|
||||||
if not ingredients:
|
|
||||||
leftover = (row.get("produkte") or "").strip().strip('"')
|
|
||||||
if leftover:
|
|
||||||
ingredients = [{
|
|
||||||
"source_name_raw": leftover,
|
|
||||||
"source_name_normalized": normalize_food_name(leftover),
|
|
||||||
"quantity_raw": None,
|
|
||||||
"quantity_g": parse_quantity_g(leftover),
|
|
||||||
"sort_order": 0,
|
|
||||||
}]
|
|
||||||
recipes.append({
|
|
||||||
"name_raw": name,
|
|
||||||
"name_normalized": normalize_food_name(name),
|
|
||||||
"portions": portions,
|
|
||||||
"description": (row.get("beschreibung") or "").strip() or None,
|
|
||||||
"ingredients": ingredients,
|
|
||||||
})
|
|
||||||
return recipes
|
|
||||||
@@ -11,31 +11,7 @@ from typing import Any
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from csv_parser.core import iter_csv_dict_rows, normalize_header_for_signature, resolve_effective_csv_delimiter
|
from csv_parser.core import iter_csv_dict_rows, resolve_effective_csv_delimiter
|
||||||
|
|
||||||
_FOOD_NAME_HEADERS = frozenset({
|
|
||||||
"bezeichnung", "lebensmittel", "food", "food_name", "name", "gericht",
|
|
||||||
})
|
|
||||||
_QTY_HEADERS = frozenset({
|
|
||||||
"menge", "quantity", "quantity_raw", "portion", "amount", "gramm",
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
def guess_nutrition_item_fields(csv_row: dict[str, Any] | None) -> tuple[str | None, str | None]:
|
|
||||||
"""FDDB-Spalten auch ohne Vorlagen-Mapping auf food_name / menge erkennen."""
|
|
||||||
name = qty = None
|
|
||||||
for key, val in (csv_row or {}).items():
|
|
||||||
if val is None:
|
|
||||||
continue
|
|
||||||
text = str(val).strip().strip('"')
|
|
||||||
if not text:
|
|
||||||
continue
|
|
||||||
norm = normalize_header_for_signature(str(key))
|
|
||||||
if name is None and (norm in _FOOD_NAME_HEADERS or "bezeichnung" in norm):
|
|
||||||
name = text
|
|
||||||
if qty is None and (norm in _QTY_HEADERS or norm.startswith("menge")):
|
|
||||||
qty = text
|
|
||||||
return name, qty
|
|
||||||
from csv_parser.import_row_processing import (
|
from csv_parser.import_row_processing import (
|
||||||
aggregate_mapped_rows,
|
aggregate_mapped_rows,
|
||||||
resolve_import_row_processing,
|
resolve_import_row_processing,
|
||||||
@@ -205,8 +181,6 @@ def run_universal_csv_import(
|
|||||||
"error_details": error_details[:50],
|
"error_details": error_details[:50],
|
||||||
"new_entries": stats.get("new_entries", stats.get("inserted", 0)),
|
"new_entries": stats.get("new_entries", stats.get("inserted", 0)),
|
||||||
"affected_ids": dict(affected_ids),
|
"affected_ids": dict(affected_ids),
|
||||||
"items_written": stats.get("items_written", 0),
|
|
||||||
"unmapped_names": stats.get("unmapped_names", 0),
|
|
||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
|
|
||||||
@@ -229,11 +203,6 @@ def _import_nutrition(
|
|||||||
for csv_row in iter_csv_dict_rows(text, delim, has_header=has_header):
|
for csv_row in iter_csv_dict_rows(text, delim, has_header=has_header):
|
||||||
rows_total += 1
|
rows_total += 1
|
||||||
mapped = build_row_after_mapping(csv_row, fm, tc, module="nutrition")
|
mapped = build_row_after_mapping(csv_row, fm, tc, module="nutrition")
|
||||||
guessed_name, guessed_qty = guess_nutrition_item_fields(csv_row)
|
|
||||||
if not mapped.get("food_name") and guessed_name:
|
|
||||||
mapped["food_name"] = guessed_name
|
|
||||||
if not mapped.get("quantity_raw") and guessed_qty:
|
|
||||||
mapped["quantity_raw"] = guessed_qty
|
|
||||||
d = coerce_date(mapped.get("date"))
|
d = coerce_date(mapped.get("date"))
|
||||||
if d is None:
|
if d is None:
|
||||||
error_details.append({"row": rows_total, "error": "Datum fehlt oder ungültig"})
|
error_details.append({"row": rows_total, "error": "Datum fehlt oder ungültig"})
|
||||||
@@ -254,50 +223,6 @@ def _import_nutrition(
|
|||||||
|
|
||||||
skipped_groups = sum(n.get("rows_in_group", 0) for n in (agg_notes or []) if n.get("error") == "mehrere_zeilen_pro_schluessel")
|
skipped_groups = sum(n.get("rows_in_group", 0) for n in (agg_notes or []) if n.get("error") == "mehrere_zeilen_pro_schluessel")
|
||||||
|
|
||||||
item_rows = []
|
|
||||||
for mapped in mapped_rows:
|
|
||||||
name = mapped.get("food_name")
|
|
||||||
if not name:
|
|
||||||
continue
|
|
||||||
d_item = coerce_date(mapped.get("date"))
|
|
||||||
if d_item is None:
|
|
||||||
continue
|
|
||||||
item_rows.append({
|
|
||||||
"date": d_item.isoformat(),
|
|
||||||
"logged_at": mapped.get("logged_at") or mapped.get("date"),
|
|
||||||
"food_name": str(name).strip(),
|
|
||||||
"quantity_raw": mapped.get("quantity_raw"),
|
|
||||||
"kcal": mapped.get("kcal"),
|
|
||||||
"protein_g": mapped.get("protein_g"),
|
|
||||||
"fat_g": mapped.get("fat_g"),
|
|
||||||
"carbs_g": mapped.get("carbs_g"),
|
|
||||||
})
|
|
||||||
if item_rows:
|
|
||||||
from data_layer.nutrition_items import get_import_policy, replace_csv_items_for_dates
|
|
||||||
|
|
||||||
policy = get_import_policy(cur, profile_id)
|
|
||||||
ingest = replace_csv_items_for_dates(cur, profile_id, item_rows, policy=policy)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT COUNT(DISTINCT source_name_normalized) AS n
|
|
||||||
FROM nutrition_items
|
|
||||||
WHERE profile_id = %s AND food_id IS NULL
|
|
||||||
""",
|
|
||||||
(profile_id,),
|
|
||||||
)
|
|
||||||
unmapped_row = cur.fetchone() or {}
|
|
||||||
return {
|
|
||||||
"rows_total": rows_total,
|
|
||||||
"inserted": ingest.get("new_log_days", 0),
|
|
||||||
"updated": max(0, ingest.get("days_written", 0) - ingest.get("new_log_days", 0)),
|
|
||||||
"skipped": skipped_groups,
|
|
||||||
"new_entries": ingest.get("new_log_days", 0),
|
|
||||||
"items_written": ingest.get("items_written", 0),
|
|
||||||
"unmapped_names": int(unmapped_row.get("n") or 0),
|
|
||||||
"conflicts": ingest.get("conflicts") or [],
|
|
||||||
"policy": ingest.get("policy"),
|
|
||||||
}
|
|
||||||
|
|
||||||
inserted = 0
|
inserted = 0
|
||||||
updated = 0
|
updated = 0
|
||||||
new_entries = 0
|
new_entries = 0
|
||||||
|
|||||||
@@ -20,8 +20,6 @@ _MODULE_HEADER_ALIASES: dict[str, dict[str, frozenset[str]]] = {
|
|||||||
"protein_g": frozenset({"protein", "eiwei", "eiweiss"}),
|
"protein_g": frozenset({"protein", "eiwei", "eiweiss"}),
|
||||||
"fat_g": frozenset({"fett", "fat", "lipid"}),
|
"fat_g": frozenset({"fett", "fat", "lipid"}),
|
||||||
"carbs_g": frozenset({"kh", "carb", "kohlenhydr", "carbs", "sugar", "zucker"}),
|
"carbs_g": frozenset({"kh", "carb", "kohlenhydr", "carbs", "sugar", "zucker"}),
|
||||||
"food_name": frozenset({"bezeichnung", "lebensmittel", "food", "gericht"}),
|
|
||||||
"quantity_raw": frozenset({"menge", "quantity", "portion", "gramm"}),
|
|
||||||
},
|
},
|
||||||
"weight": {
|
"weight": {
|
||||||
"date": frozenset({"datum", "date", "tag", "day", "zeit"}),
|
"date": frozenset({"datum", "date", "tag", "day", "zeit"}),
|
||||||
|
|||||||
@@ -20,8 +20,6 @@ MODULE_DEFINITIONS: Dict[str, Dict[str, Any]] = {
|
|||||||
"protein_g": {"type": "float", "required": False, "min": 0, "unit": "g"},
|
"protein_g": {"type": "float", "required": False, "min": 0, "unit": "g"},
|
||||||
"fat_g": {"type": "float", "required": False, "min": 0, "unit": "g"},
|
"fat_g": {"type": "float", "required": False, "min": 0, "unit": "g"},
|
||||||
"carbs_g": {"type": "float", "required": False, "min": 0, "unit": "g"},
|
"carbs_g": {"type": "float", "required": False, "min": 0, "unit": "g"},
|
||||||
"food_name": {"type": "string", "required": False, "label_de": "Lebensmittel"},
|
|
||||||
"quantity_raw": {"type": "string", "required": False, "label_de": "Menge"},
|
|
||||||
},
|
},
|
||||||
"duplicate_key": ["profile_id", "date"],
|
"duplicate_key": ["profile_id", "date"],
|
||||||
"duplicate_strategy": "update",
|
"duplicate_strategy": "update",
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
"""
|
"""
|
||||||
Dashboard-Layout v1: Validierung, Produkt-Standard (Übersicht) und Servertemplate (`lab_default_layout_dict`).
|
Dashboard-Layout v1: Validierung, Produkt-Standard (Übersicht) und Lab-Standard.
|
||||||
|
|
||||||
Erlaubte Widget-IDs und Reihenfolge: widget_catalog.WIDGET_CATALOG.
|
Erlaubte Widget-IDs und Reihenfolge: widget_catalog.WIDGET_CATALOG.
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import copy
|
|
||||||
from typing import Any, Literal
|
from typing import Any, Literal
|
||||||
|
|
||||||
from pydantic import BaseModel, Field, field_validator, model_validator
|
from pydantic import BaseModel, Field, field_validator, model_validator
|
||||||
@@ -26,13 +25,12 @@ __all__ = [
|
|||||||
"coalesce_effective_layout",
|
"coalesce_effective_layout",
|
||||||
"default_layout_dict",
|
"default_layout_dict",
|
||||||
"lab_default_layout_dict",
|
"lab_default_layout_dict",
|
||||||
"merge_missing_catalog_widgets",
|
|
||||||
"product_default_layout_dict",
|
"product_default_layout_dict",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def lab_default_layout_dict() -> dict[str, Any]:
|
def lab_default_layout_dict() -> dict[str, Any]:
|
||||||
"""Serverseitiges Standardlayout (DEFAULT_LAB_WIDGET_IDS); API-Feld `lab_default_layout`, u. a. für Editor/Reset."""
|
"""Standard für Dashboard-Lab (Experimentier-Widgets)."""
|
||||||
on = DEFAULT_LAB_WIDGET_IDS
|
on = DEFAULT_LAB_WIDGET_IDS
|
||||||
return {
|
return {
|
||||||
"version": 1,
|
"version": 1,
|
||||||
@@ -54,25 +52,6 @@ def default_layout_dict() -> dict[str, Any]:
|
|||||||
return product_default_layout_dict()
|
return product_default_layout_dict()
|
||||||
|
|
||||||
|
|
||||||
def merge_missing_catalog_widgets(layout: dict[str, Any]) -> dict[str, Any]:
|
|
||||||
"""
|
|
||||||
Hängt fehlende Widget-IDs aus WIDGET_CATALOG an (enabled=False, leere config).
|
|
||||||
Bestehende Reihenfolge bleibt erhalten — nötig, damit neue Katalog-Einträge in
|
|
||||||
„Übersicht anpassen“ / Lab erscheinen, ohne dass Nutzer:innen das Layout resetten müssen.
|
|
||||||
"""
|
|
||||||
out = copy.deepcopy(layout)
|
|
||||||
widgets: list[dict[str, Any]] = list(out.get("widgets") or [])
|
|
||||||
seen: set[str] = {str(w["id"]) for w in widgets if w.get("id")}
|
|
||||||
for e in WIDGET_CATALOG:
|
|
||||||
wid = e["id"]
|
|
||||||
if wid not in seen:
|
|
||||||
widgets.append({"id": wid, "enabled": False, "config": {}})
|
|
||||||
seen.add(wid)
|
|
||||||
out["version"] = out.get("version", 1)
|
|
||||||
out["widgets"] = widgets
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
class DashboardWidgetEntry(BaseModel):
|
class DashboardWidgetEntry(BaseModel):
|
||||||
id: str = Field(min_length=1, max_length=64)
|
id: str = Field(min_length=1, max_length=64)
|
||||||
enabled: bool = True
|
enabled: bool = True
|
||||||
|
|||||||
@@ -14,18 +14,12 @@ MAX_WIDGET_CONFIG_JSON_BYTES = 3072
|
|||||||
|
|
||||||
WIDGETS_ALLOWING_CONFIG: frozenset[str] = frozenset({
|
WIDGETS_ALLOWING_CONFIG: frozenset[str] = frozenset({
|
||||||
"body_overview",
|
"body_overview",
|
||||||
"body_history_viz",
|
|
||||||
"nutrition_history_viz",
|
|
||||||
"fitness_history_viz",
|
|
||||||
"recovery_history_viz",
|
|
||||||
"history_overview_viz",
|
|
||||||
"activity_overview",
|
"activity_overview",
|
||||||
"kpi_board",
|
"kpi_board",
|
||||||
"quick_capture",
|
"quick_capture",
|
||||||
"trend_kcal_weight",
|
"trend_kcal_weight",
|
||||||
"nutrition_detail_charts",
|
"nutrition_detail_charts",
|
||||||
"recovery_charts_panel",
|
"recovery_charts_panel",
|
||||||
"report_export",
|
|
||||||
})
|
})
|
||||||
|
|
||||||
_QUICK_CAPTURE_KEYS: frozenset[str] = frozenset({
|
_QUICK_CAPTURE_KEYS: frozenset[str] = frozenset({
|
||||||
@@ -38,141 +32,6 @@ _QUICK_CAPTURE_KEYS: frozenset[str] = frozenset({
|
|||||||
_KPI_TILE_FIXED: frozenset[str] = frozenset({"body_fat", "avg_kcal"})
|
_KPI_TILE_FIXED: frozenset[str] = frozenset({"body_fat", "avg_kcal"})
|
||||||
_KPI_REF_TILE_RE = re.compile(r"^ref:[a-z0-9_]{1,64}$")
|
_KPI_REF_TILE_RE = re.compile(r"^ref:[a-z0-9_]{1,64}$")
|
||||||
|
|
||||||
_BODY_HISTORY_VIZ_BOOL_KEYS: frozenset[str] = frozenset({
|
|
||||||
"show_goals_strip",
|
|
||||||
"show_intro_blurb",
|
|
||||||
"show_layer_meta",
|
|
||||||
"show_kpis",
|
|
||||||
"show_weight_chart",
|
|
||||||
"show_body_fat_chart",
|
|
||||||
"show_proportion_chart",
|
|
||||||
"show_circumference_index_chart",
|
|
||||||
"show_circumference_lines_chart",
|
|
||||||
})
|
|
||||||
|
|
||||||
_BODY_HISTORY_VIZ_DEFAULTS: dict[str, Any] = {
|
|
||||||
"chart_days": 30,
|
|
||||||
"show_goals_strip": False,
|
|
||||||
"show_intro_blurb": False,
|
|
||||||
"show_layer_meta": False,
|
|
||||||
"show_kpis": True,
|
|
||||||
"kpi_detail": "compact",
|
|
||||||
"show_weight_chart": True,
|
|
||||||
"show_body_fat_chart": False,
|
|
||||||
"show_proportion_chart": False,
|
|
||||||
"show_circumference_index_chart": False,
|
|
||||||
"show_circumference_lines_chart": False,
|
|
||||||
}
|
|
||||||
|
|
||||||
_NUTRITION_HISTORY_VIZ_BOOL_KEYS: frozenset[str] = frozenset({
|
|
||||||
"show_goals_strip",
|
|
||||||
"show_intro_blurb",
|
|
||||||
"show_kpis",
|
|
||||||
"show_kcal_vs_weight",
|
|
||||||
"show_calorie_balance_chart",
|
|
||||||
"show_protein_lean_chart",
|
|
||||||
"show_heuristics",
|
|
||||||
"show_macro_daily_bars",
|
|
||||||
"show_macro_distribution_pair",
|
|
||||||
"show_energy_protein_charts",
|
|
||||||
})
|
|
||||||
|
|
||||||
_NUTRITION_HISTORY_VIZ_DEFAULTS: dict[str, Any] = {
|
|
||||||
"chart_days": 30,
|
|
||||||
"show_goals_strip": False,
|
|
||||||
"show_intro_blurb": False,
|
|
||||||
"show_kpis": True,
|
|
||||||
"kpi_detail": "compact",
|
|
||||||
"show_kcal_vs_weight": True,
|
|
||||||
"show_calorie_balance_chart": False,
|
|
||||||
"show_protein_lean_chart": False,
|
|
||||||
"show_heuristics": False,
|
|
||||||
"show_macro_daily_bars": True,
|
|
||||||
"show_macro_distribution_pair": True,
|
|
||||||
"show_energy_protein_charts": False,
|
|
||||||
}
|
|
||||||
|
|
||||||
_FITNESS_HISTORY_VIZ_BOOL_KEYS: frozenset[str] = frozenset({
|
|
||||||
"show_layer_meta",
|
|
||||||
"show_kpis",
|
|
||||||
"show_progress_insights",
|
|
||||||
"show_chart_training_volume",
|
|
||||||
"show_chart_training_type_distribution",
|
|
||||||
"show_chart_quality_sessions",
|
|
||||||
"show_chart_load_monitoring",
|
|
||||||
})
|
|
||||||
|
|
||||||
_FITNESS_HISTORY_VIZ_DEFAULTS: dict[str, Any] = {
|
|
||||||
"chart_days": 30,
|
|
||||||
"show_layer_meta": False,
|
|
||||||
"show_kpis": True,
|
|
||||||
"kpi_detail": "compact",
|
|
||||||
"show_progress_insights": False,
|
|
||||||
"show_chart_training_volume": True,
|
|
||||||
"show_chart_training_type_distribution": True,
|
|
||||||
"show_chart_quality_sessions": False,
|
|
||||||
"show_chart_load_monitoring": False,
|
|
||||||
}
|
|
||||||
|
|
||||||
_RECOVERY_HISTORY_VIZ_BOOL_KEYS: frozenset[str] = frozenset({
|
|
||||||
"show_layer_meta",
|
|
||||||
"show_kpis",
|
|
||||||
"show_progress_insights",
|
|
||||||
"show_sleep_section_heading",
|
|
||||||
"show_chart_recovery_score",
|
|
||||||
"show_chart_sleep_quality",
|
|
||||||
"show_chart_sleep_debt",
|
|
||||||
"show_heart_section_heading",
|
|
||||||
"show_heart_context_card",
|
|
||||||
"show_chart_hrv_rhr",
|
|
||||||
"show_vitals_extra_heading",
|
|
||||||
"show_vitals_extra_trends",
|
|
||||||
})
|
|
||||||
|
|
||||||
_RECOVERY_HISTORY_VIZ_DEFAULTS: dict[str, Any] = {
|
|
||||||
"chart_days": 30,
|
|
||||||
"show_layer_meta": False,
|
|
||||||
"show_kpis": True,
|
|
||||||
"kpi_detail": "compact",
|
|
||||||
"show_progress_insights": False,
|
|
||||||
"show_sleep_section_heading": True,
|
|
||||||
"show_chart_recovery_score": True,
|
|
||||||
"show_chart_sleep_quality": True,
|
|
||||||
"show_chart_sleep_debt": False,
|
|
||||||
"show_heart_section_heading": True,
|
|
||||||
"show_heart_context_card": False,
|
|
||||||
"show_chart_hrv_rhr": True,
|
|
||||||
"show_vitals_extra_heading": False,
|
|
||||||
"show_vitals_extra_trends": False,
|
|
||||||
}
|
|
||||||
|
|
||||||
_HISTORY_OVERVIEW_VIZ_SECTION_KEYS: frozenset[str] = frozenset({
|
|
||||||
"show_section_body",
|
|
||||||
"show_section_nutrition",
|
|
||||||
"show_section_fitness",
|
|
||||||
"show_section_recovery",
|
|
||||||
})
|
|
||||||
|
|
||||||
_HISTORY_OVERVIEW_VIZ_BOOL_KEYS: frozenset[str] = frozenset({
|
|
||||||
"show_confidence_banner",
|
|
||||||
"show_intro_blurb",
|
|
||||||
*_HISTORY_OVERVIEW_VIZ_SECTION_KEYS,
|
|
||||||
"show_correlation_c1_c3",
|
|
||||||
"show_drivers_c4",
|
|
||||||
})
|
|
||||||
|
|
||||||
_HISTORY_OVERVIEW_VIZ_DEFAULTS: dict[str, Any] = {
|
|
||||||
"chart_days": 30,
|
|
||||||
"show_confidence_banner": True,
|
|
||||||
"show_intro_blurb": True,
|
|
||||||
"show_section_body": True,
|
|
||||||
"show_section_nutrition": True,
|
|
||||||
"show_section_fitness": True,
|
|
||||||
"show_section_recovery": True,
|
|
||||||
"show_correlation_c1_c3": True,
|
|
||||||
"show_drivers_c4": True,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def _config_json_size_bytes(config: dict[str, Any]) -> int:
|
def _config_json_size_bytes(config: dict[str, Any]) -> int:
|
||||||
return len(json.dumps(config, sort_keys=True, ensure_ascii=False).encode("utf-8"))
|
return len(json.dumps(config, sort_keys=True, ensure_ascii=False).encode("utf-8"))
|
||||||
@@ -180,44 +39,19 @@ def _config_json_size_bytes(config: dict[str, Any]) -> int:
|
|||||||
|
|
||||||
def validate_widget_entry_config(widget_id: str, raw: Any) -> dict[str, Any]:
|
def validate_widget_entry_config(widget_id: str, raw: Any) -> dict[str, Any]:
|
||||||
if raw is None:
|
if raw is None:
|
||||||
raw = {}
|
return {}
|
||||||
if not isinstance(raw, dict):
|
if not isinstance(raw, dict):
|
||||||
raise ValueError(f"Widget {widget_id}: config muss ein Objekt sein")
|
raise ValueError(f"Widget {widget_id}: config muss ein Objekt sein")
|
||||||
if _config_json_size_bytes(raw) > MAX_WIDGET_CONFIG_JSON_BYTES:
|
if _config_json_size_bytes(raw) > MAX_WIDGET_CONFIG_JSON_BYTES:
|
||||||
raise ValueError(f"Widget {widget_id}: config zu groß (max. {MAX_WIDGET_CONFIG_JSON_BYTES} Byte JSON)")
|
raise ValueError(f"Widget {widget_id}: config zu groß (max. {MAX_WIDGET_CONFIG_JSON_BYTES} Byte JSON)")
|
||||||
|
if not raw:
|
||||||
|
return {}
|
||||||
|
|
||||||
if widget_id not in WIDGETS_ALLOWING_CONFIG:
|
if widget_id not in WIDGETS_ALLOWING_CONFIG:
|
||||||
if raw:
|
|
||||||
raise ValueError(f"Widget {widget_id}: keine Konfiguration unterstützt")
|
raise ValueError(f"Widget {widget_id}: keine Konfiguration unterstützt")
|
||||||
return {}
|
|
||||||
|
|
||||||
if not raw:
|
|
||||||
if widget_id == "body_history_viz":
|
|
||||||
return _validate_body_history_viz_config({})
|
|
||||||
if widget_id == "nutrition_history_viz":
|
|
||||||
return _validate_nutrition_history_viz_config({})
|
|
||||||
if widget_id == "fitness_history_viz":
|
|
||||||
return _validate_fitness_history_viz_config({})
|
|
||||||
if widget_id == "recovery_history_viz":
|
|
||||||
return _validate_recovery_history_viz_config({})
|
|
||||||
if widget_id == "history_overview_viz":
|
|
||||||
return _validate_history_overview_viz_config({})
|
|
||||||
if widget_id == "report_export":
|
|
||||||
return _validate_report_export_config({})
|
|
||||||
return {}
|
|
||||||
|
|
||||||
if widget_id == "body_overview":
|
if widget_id == "body_overview":
|
||||||
return _validate_chart_days_only(raw, label="body_overview")
|
return _validate_chart_days_only(raw, label="body_overview")
|
||||||
if widget_id == "body_history_viz":
|
|
||||||
return _validate_body_history_viz_config(raw)
|
|
||||||
if widget_id == "nutrition_history_viz":
|
|
||||||
return _validate_nutrition_history_viz_config(raw)
|
|
||||||
if widget_id == "fitness_history_viz":
|
|
||||||
return _validate_fitness_history_viz_config(raw)
|
|
||||||
if widget_id == "recovery_history_viz":
|
|
||||||
return _validate_recovery_history_viz_config(raw)
|
|
||||||
if widget_id == "history_overview_viz":
|
|
||||||
return _validate_history_overview_viz_config(raw)
|
|
||||||
if widget_id == "activity_overview":
|
if widget_id == "activity_overview":
|
||||||
return _validate_chart_days_only(raw, label="activity_overview")
|
return _validate_chart_days_only(raw, label="activity_overview")
|
||||||
if widget_id == "kpi_board":
|
if widget_id == "kpi_board":
|
||||||
@@ -230,8 +64,6 @@ def validate_widget_entry_config(widget_id: str, raw: Any) -> dict[str, Any]:
|
|||||||
return _validate_chart_days_only(raw, label="nutrition_detail_charts")
|
return _validate_chart_days_only(raw, label="nutrition_detail_charts")
|
||||||
if widget_id == "recovery_charts_panel":
|
if widget_id == "recovery_charts_panel":
|
||||||
return _validate_chart_days_only(raw, label="recovery_charts_panel")
|
return _validate_chart_days_only(raw, label="recovery_charts_panel")
|
||||||
if widget_id == "report_export":
|
|
||||||
return _validate_report_export_config(raw)
|
|
||||||
|
|
||||||
raise ValueError(f"Widget {widget_id}: keine Konfiguration unterstützt")
|
raise ValueError(f"Widget {widget_id}: keine Konfiguration unterstützt")
|
||||||
|
|
||||||
@@ -318,210 +150,6 @@ def _parse_chart_days(v: Any, label: str) -> int:
|
|||||||
raise ValueError(f"{label}: chart_days muss ganze Zahl sein")
|
raise ValueError(f"{label}: chart_days muss ganze Zahl sein")
|
||||||
|
|
||||||
|
|
||||||
def _validate_body_history_viz_config(raw: dict[str, Any]) -> dict[str, Any]:
|
|
||||||
label = "body_history_viz"
|
|
||||||
allowed = _BODY_HISTORY_VIZ_BOOL_KEYS | frozenset({"chart_days", "kpi_detail"})
|
|
||||||
unknown = set(raw) - allowed
|
|
||||||
if unknown:
|
|
||||||
raise ValueError(f"{label}: unbekannte config-Felder: {sorted(unknown)}")
|
|
||||||
out: dict[str, Any] = dict(_BODY_HISTORY_VIZ_DEFAULTS)
|
|
||||||
for k in _BODY_HISTORY_VIZ_BOOL_KEYS:
|
|
||||||
if k not in raw:
|
|
||||||
continue
|
|
||||||
v = raw[k]
|
|
||||||
if not isinstance(v, bool):
|
|
||||||
raise ValueError(f"{label}: {k} muss boolean sein")
|
|
||||||
out[k] = v
|
|
||||||
if "kpi_detail" in raw:
|
|
||||||
kd = raw["kpi_detail"]
|
|
||||||
if kd not in ("compact", "full"):
|
|
||||||
raise ValueError(f"{label}: kpi_detail muss 'compact' oder 'full' sein")
|
|
||||||
out["kpi_detail"] = kd
|
|
||||||
if "chart_days" in raw:
|
|
||||||
v = _parse_chart_days(raw["chart_days"], label)
|
|
||||||
if v < 7 or v > 90:
|
|
||||||
raise ValueError(f"{label}: chart_days muss zwischen 7 und 90 liegen")
|
|
||||||
out["chart_days"] = v
|
|
||||||
if not out["show_kpis"] and not any(
|
|
||||||
out[k]
|
|
||||||
for k in (
|
|
||||||
"show_weight_chart",
|
|
||||||
"show_body_fat_chart",
|
|
||||||
"show_proportion_chart",
|
|
||||||
"show_circumference_index_chart",
|
|
||||||
"show_circumference_lines_chart",
|
|
||||||
)
|
|
||||||
):
|
|
||||||
raise ValueError(f"{label}: mindestens KPIs oder ein Chart muss sichtbar sein")
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
def _validate_nutrition_history_viz_config(raw: dict[str, Any]) -> dict[str, Any]:
|
|
||||||
label = "nutrition_history_viz"
|
|
||||||
allowed = _NUTRITION_HISTORY_VIZ_BOOL_KEYS | frozenset({"chart_days", "kpi_detail"})
|
|
||||||
unknown = set(raw) - allowed
|
|
||||||
if unknown:
|
|
||||||
raise ValueError(f"{label}: unbekannte config-Felder: {sorted(unknown)}")
|
|
||||||
out: dict[str, Any] = dict(_NUTRITION_HISTORY_VIZ_DEFAULTS)
|
|
||||||
for k in _NUTRITION_HISTORY_VIZ_BOOL_KEYS:
|
|
||||||
if k not in raw:
|
|
||||||
continue
|
|
||||||
v = raw[k]
|
|
||||||
if not isinstance(v, bool):
|
|
||||||
raise ValueError(f"{label}: {k} muss boolean sein")
|
|
||||||
out[k] = v
|
|
||||||
if "kpi_detail" in raw:
|
|
||||||
kd = raw["kpi_detail"]
|
|
||||||
if kd not in ("compact", "full"):
|
|
||||||
raise ValueError(f"{label}: kpi_detail muss 'compact' oder 'full' sein")
|
|
||||||
out["kpi_detail"] = kd
|
|
||||||
if "chart_days" in raw:
|
|
||||||
v = _parse_chart_days(raw["chart_days"], label)
|
|
||||||
if v < 7 or v > 90:
|
|
||||||
raise ValueError(f"{label}: chart_days muss zwischen 7 und 90 liegen")
|
|
||||||
out["chart_days"] = v
|
|
||||||
if not out["show_kpis"] and not any(
|
|
||||||
out[k]
|
|
||||||
for k in (
|
|
||||||
"show_kcal_vs_weight",
|
|
||||||
"show_calorie_balance_chart",
|
|
||||||
"show_protein_lean_chart",
|
|
||||||
"show_heuristics",
|
|
||||||
"show_macro_daily_bars",
|
|
||||||
"show_macro_distribution_pair",
|
|
||||||
"show_energy_protein_charts",
|
|
||||||
)
|
|
||||||
):
|
|
||||||
raise ValueError(f"{label}: mindestens KPIs oder ein Chart-Bereich muss sichtbar sein")
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
def _validate_fitness_history_viz_config(raw: dict[str, Any]) -> dict[str, Any]:
|
|
||||||
label = "fitness_history_viz"
|
|
||||||
allowed = _FITNESS_HISTORY_VIZ_BOOL_KEYS | frozenset({"chart_days", "kpi_detail"})
|
|
||||||
unknown = set(raw) - allowed
|
|
||||||
if unknown:
|
|
||||||
raise ValueError(f"{label}: unbekannte config-Felder: {sorted(unknown)}")
|
|
||||||
out: dict[str, Any] = dict(_FITNESS_HISTORY_VIZ_DEFAULTS)
|
|
||||||
for k in _FITNESS_HISTORY_VIZ_BOOL_KEYS:
|
|
||||||
if k not in raw:
|
|
||||||
continue
|
|
||||||
v = raw[k]
|
|
||||||
if not isinstance(v, bool):
|
|
||||||
raise ValueError(f"{label}: {k} muss boolean sein")
|
|
||||||
out[k] = v
|
|
||||||
if "kpi_detail" in raw:
|
|
||||||
kd = raw["kpi_detail"]
|
|
||||||
if kd not in ("compact", "full"):
|
|
||||||
raise ValueError(f"{label}: kpi_detail muss 'compact' oder 'full' sein")
|
|
||||||
out["kpi_detail"] = kd
|
|
||||||
if "chart_days" in raw:
|
|
||||||
v = _parse_chart_days(raw["chart_days"], label)
|
|
||||||
if v < 7 or v > 90:
|
|
||||||
raise ValueError(f"{label}: chart_days muss zwischen 7 und 90 liegen")
|
|
||||||
out["chart_days"] = v
|
|
||||||
if not out["show_kpis"] and not out["show_progress_insights"] and not any(
|
|
||||||
out[k]
|
|
||||||
for k in (
|
|
||||||
"show_chart_training_volume",
|
|
||||||
"show_chart_training_type_distribution",
|
|
||||||
"show_chart_quality_sessions",
|
|
||||||
"show_chart_load_monitoring",
|
|
||||||
)
|
|
||||||
):
|
|
||||||
raise ValueError(f"{label}: mindestens KPIs, Einschätzungen oder ein Chart muss sichtbar sein")
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
def _validate_recovery_history_viz_config(raw: dict[str, Any]) -> dict[str, Any]:
|
|
||||||
label = "recovery_history_viz"
|
|
||||||
allowed = _RECOVERY_HISTORY_VIZ_BOOL_KEYS | frozenset({"chart_days", "kpi_detail"})
|
|
||||||
unknown = set(raw) - allowed
|
|
||||||
if unknown:
|
|
||||||
raise ValueError(f"{label}: unbekannte config-Felder: {sorted(unknown)}")
|
|
||||||
out: dict[str, Any] = dict(_RECOVERY_HISTORY_VIZ_DEFAULTS)
|
|
||||||
for k in _RECOVERY_HISTORY_VIZ_BOOL_KEYS:
|
|
||||||
if k not in raw:
|
|
||||||
continue
|
|
||||||
v = raw[k]
|
|
||||||
if not isinstance(v, bool):
|
|
||||||
raise ValueError(f"{label}: {k} muss boolean sein")
|
|
||||||
out[k] = v
|
|
||||||
if "kpi_detail" in raw:
|
|
||||||
kd = raw["kpi_detail"]
|
|
||||||
if kd not in ("compact", "full"):
|
|
||||||
raise ValueError(f"{label}: kpi_detail muss 'compact' oder 'full' sein")
|
|
||||||
out["kpi_detail"] = kd
|
|
||||||
if "chart_days" in raw:
|
|
||||||
v = _parse_chart_days(raw["chart_days"], label)
|
|
||||||
if v < 7 or v > 90:
|
|
||||||
raise ValueError(f"{label}: chart_days muss zwischen 7 und 90 liegen")
|
|
||||||
out["chart_days"] = v
|
|
||||||
if not out["show_kpis"] and not out["show_progress_insights"] and not out["show_heart_context_card"] and not out[
|
|
||||||
"show_vitals_extra_trends"
|
|
||||||
] and not any(
|
|
||||||
out[k]
|
|
||||||
for k in (
|
|
||||||
"show_chart_recovery_score",
|
|
||||||
"show_chart_sleep_quality",
|
|
||||||
"show_chart_sleep_debt",
|
|
||||||
"show_chart_hrv_rhr",
|
|
||||||
)
|
|
||||||
):
|
|
||||||
raise ValueError(f"{label}: mindestens KPIs, Überblick, Kontextkarte, Extra-Vitals oder ein Chart muss sichtbar sein")
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
def _migrate_history_overview_viz_raw(raw: dict[str, Any]) -> dict[str, Any]:
|
|
||||||
"""Alt: show_area_summaries → vier show_section_* (nur wo keine expliziten Section-Keys gesetzt)."""
|
|
||||||
r = dict(raw)
|
|
||||||
if "show_area_summaries" not in r:
|
|
||||||
return r
|
|
||||||
leg = r.pop("show_area_summaries")
|
|
||||||
if not isinstance(leg, bool):
|
|
||||||
raise ValueError("history_overview_viz: show_area_summaries muss boolean sein (veraltet — nutze show_section_*)")
|
|
||||||
for k in _HISTORY_OVERVIEW_VIZ_SECTION_KEYS:
|
|
||||||
if k not in r:
|
|
||||||
r[k] = leg
|
|
||||||
return r
|
|
||||||
|
|
||||||
|
|
||||||
def _validate_history_overview_viz_config(raw: dict[str, Any]) -> dict[str, Any]:
|
|
||||||
label = "history_overview_viz"
|
|
||||||
raw_m = _migrate_history_overview_viz_raw(raw)
|
|
||||||
allowed = _HISTORY_OVERVIEW_VIZ_BOOL_KEYS | frozenset({"chart_days"})
|
|
||||||
unknown = set(raw_m) - allowed
|
|
||||||
if unknown:
|
|
||||||
raise ValueError(f"{label}: unbekannte config-Felder: {sorted(unknown)}")
|
|
||||||
out: dict[str, Any] = dict(_HISTORY_OVERVIEW_VIZ_DEFAULTS)
|
|
||||||
for k in _HISTORY_OVERVIEW_VIZ_BOOL_KEYS:
|
|
||||||
if k not in raw_m:
|
|
||||||
continue
|
|
||||||
v = raw_m[k]
|
|
||||||
if not isinstance(v, bool):
|
|
||||||
raise ValueError(f"{label}: {k} muss boolean sein")
|
|
||||||
out[k] = v
|
|
||||||
if "chart_days" in raw_m:
|
|
||||||
v = _parse_chart_days(raw_m["chart_days"], label)
|
|
||||||
if v < 7 or v > 90:
|
|
||||||
raise ValueError(f"{label}: chart_days muss zwischen 7 und 90 liegen")
|
|
||||||
out["chart_days"] = v
|
|
||||||
has_section = any(out[k] for k in _HISTORY_OVERVIEW_VIZ_SECTION_KEYS)
|
|
||||||
has_other = any(
|
|
||||||
out[k]
|
|
||||||
for k in (
|
|
||||||
"show_confidence_banner",
|
|
||||||
"show_correlation_c1_c3",
|
|
||||||
"show_drivers_c4",
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if not has_section and not has_other:
|
|
||||||
raise ValueError(
|
|
||||||
f"{label}: mindestens eine Bereichs-Kachel, das Datenlage-Banner, Lag-Korrelationen (C1–C3) oder Treiber (C4) muss sichtbar sein"
|
|
||||||
)
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
def _validate_chart_days_only(raw: dict[str, Any], *, label: str) -> dict[str, Any]:
|
def _validate_chart_days_only(raw: dict[str, Any], *, label: str) -> dict[str, Any]:
|
||||||
allowed = frozenset({"chart_days"})
|
allowed = frozenset({"chart_days"})
|
||||||
unknown = set(raw) - allowed
|
unknown = set(raw) - allowed
|
||||||
@@ -535,43 +163,3 @@ def _validate_chart_days_only(raw: dict[str, Any], *, label: str) -> dict[str, A
|
|||||||
return {"chart_days": v}
|
return {"chart_days": v}
|
||||||
|
|
||||||
|
|
||||||
def _validate_report_export_config(raw: dict[str, Any]) -> dict[str, Any]:
|
|
||||||
label = "report_export"
|
|
||||||
allowed = frozenset({"document_title", "subtitle", "capture_scale"})
|
|
||||||
unknown = set(raw) - allowed
|
|
||||||
if unknown:
|
|
||||||
raise ValueError(f"{label}: unbekannte config-Felder: {sorted(unknown)}")
|
|
||||||
out: dict[str, Any] = {"capture_scale": 2}
|
|
||||||
if "document_title" in raw:
|
|
||||||
t = raw["document_title"]
|
|
||||||
if t is not None and not isinstance(t, str):
|
|
||||||
raise ValueError(f"{label}: document_title muss Text sein")
|
|
||||||
s = (t or "").strip()
|
|
||||||
if len(s) > 120:
|
|
||||||
raise ValueError(f"{label}: document_title max. 120 Zeichen")
|
|
||||||
if s:
|
|
||||||
out["document_title"] = s
|
|
||||||
if "subtitle" in raw:
|
|
||||||
t = raw["subtitle"]
|
|
||||||
if t is not None and not isinstance(t, str):
|
|
||||||
raise ValueError(f"{label}: subtitle muss Text sein")
|
|
||||||
s = (t or "").strip()
|
|
||||||
if len(s) > 240:
|
|
||||||
raise ValueError(f"{label}: subtitle max. 240 Zeichen")
|
|
||||||
if s:
|
|
||||||
out["subtitle"] = s
|
|
||||||
if "capture_scale" in raw:
|
|
||||||
v = raw["capture_scale"]
|
|
||||||
if isinstance(v, bool) or isinstance(v, float):
|
|
||||||
if isinstance(v, float) and math.isfinite(v) and abs(v - round(v)) < 1e-9:
|
|
||||||
v = int(round(v))
|
|
||||||
else:
|
|
||||||
raise ValueError(f"{label}: capture_scale muss ganze Zahl 1–3 sein")
|
|
||||||
if not isinstance(v, int):
|
|
||||||
raise ValueError(f"{label}: capture_scale muss ganze Zahl 1–3 sein")
|
|
||||||
if v < 1 or v > 3:
|
|
||||||
raise ValueError(f"{label}: capture_scale muss zwischen 1 und 3 liegen")
|
|
||||||
out["capture_scale"] = v
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
@@ -135,7 +135,7 @@ def insert_activity_from_entry(cur, profile_id: str, eid: str, e: ActivityEntry)
|
|||||||
hr_avg,hr_max,hr_min,distance_km,pace_min_per_km,cadence,avg_power,elevation_gain,
|
hr_avg,hr_max,hr_min,distance_km,pace_min_per_km,cadence,avg_power,elevation_gain,
|
||||||
temperature_celsius,humidity_percent,avg_hr_percent,kcal_per_km,rpe,source,notes,
|
temperature_celsius,humidity_percent,avg_hr_percent,kcal_per_km,rpe,source,notes,
|
||||||
training_type_id,training_category,training_subcategory,created)
|
training_type_id,training_category,training_subcategory,created)
|
||||||
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,CURRENT_TIMESTAMP)""",
|
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,CURRENT_TIMESTAMP)""",
|
||||||
(
|
(
|
||||||
eid,
|
eid,
|
||||||
profile_id,
|
profile_id,
|
||||||
|
|||||||
@@ -48,31 +48,6 @@ def _iso(d: Any) -> Optional[str]:
|
|||||||
return str(d)[:10]
|
return str(d)[:10]
|
||||||
|
|
||||||
|
|
||||||
def _weight_trend_kpi(trend_periods: List[Dict[str, Any]]) -> Dict[str, str]:
|
|
||||||
"""
|
|
||||||
Kurzurteil Gewichtstrend (Schwelle ±0,25 kg, Priorität 90T → 30T → erste Periode).
|
|
||||||
Eine Quelle mit dem Verlauf-Bundle — kein paralleles Frontend-Routing mehr.
|
|
||||||
"""
|
|
||||||
if not trend_periods:
|
|
||||||
return {"verdict": "Stabil", "status": "good"}
|
|
||||||
t90 = next((t for t in trend_periods if t.get("label") == "90T"), None)
|
|
||||||
t30 = next((t for t in trend_periods if t.get("label") == "30T"), None)
|
|
||||||
d: Optional[float] = None
|
|
||||||
if t90 is not None and t90.get("diff_kg") is not None:
|
|
||||||
d = float(t90["diff_kg"])
|
|
||||||
elif t30 is not None and t30.get("diff_kg") is not None:
|
|
||||||
d = float(t30["diff_kg"])
|
|
||||||
elif trend_periods[0].get("diff_kg") is not None:
|
|
||||||
d = float(trend_periods[0]["diff_kg"])
|
|
||||||
else:
|
|
||||||
return {"verdict": "Stabil", "status": "good"}
|
|
||||||
if d < -0.25:
|
|
||||||
return {"verdict": "Trend ↓", "status": "good"}
|
|
||||||
if d > 0.25:
|
|
||||||
return {"verdict": "Trend ↑", "status": "warn"}
|
|
||||||
return {"verdict": "Stabil", "status": "good"}
|
|
||||||
|
|
||||||
|
|
||||||
def get_body_history_viz_bundle(profile_id: str, days: int) -> Dict[str, Any]:
|
def get_body_history_viz_bundle(profile_id: str, days: int) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
Returns chart-ready series and interpretation tiles for the body history tab.
|
Returns chart-ready series and interpretation tiles for the body history tab.
|
||||||
@@ -462,7 +437,6 @@ def get_body_history_viz_bundle(profile_id: str, days: int) -> Dict[str, Any]:
|
|||||||
"min_kg": min_w,
|
"min_kg": min_w,
|
||||||
"max_kg": max_w,
|
"max_kg": max_w,
|
||||||
"trend_periods": trend_periods,
|
"trend_periods": trend_periods,
|
||||||
"trend_kpi": _weight_trend_kpi(trend_periods),
|
|
||||||
"data_points": len(w_points),
|
"data_points": len(w_points),
|
||||||
"related_placeholder_keys": [
|
"related_placeholder_keys": [
|
||||||
"weight_aktuell",
|
"weight_aktuell",
|
||||||
|
|||||||
@@ -1,256 +0,0 @@
|
|||||||
"""
|
|
||||||
Chart.js-kompatible Payloads für Lag-Korrelationen C1–C3 und Treiber C4.
|
|
||||||
|
|
||||||
Gemeinsame Quelle für GET /charts/* und history_overview_viz.chart_payloads (Issue 53).
|
|
||||||
"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from typing import Any, Dict
|
|
||||||
|
|
||||||
from data_layer.correlations import calculate_lag_correlation, calculate_top_drivers
|
|
||||||
|
|
||||||
|
|
||||||
def build_weight_energy_correlation_chart_payload(profile_id: str, max_lag: int) -> Dict[str, Any]:
|
|
||||||
corr_data = calculate_lag_correlation(profile_id, "energy_balance", "weight", max_lag)
|
|
||||||
|
|
||||||
if not corr_data or corr_data.get("correlation") is None:
|
|
||||||
msg = "Nicht genug Daten für Korrelationsanalyse"
|
|
||||||
if isinstance(corr_data, dict):
|
|
||||||
msg = str(corr_data.get("interpretation") or corr_data.get("reason") or msg)
|
|
||||||
return {
|
|
||||||
"chart_type": "scatter",
|
|
||||||
"data": {"labels": [], "datasets": []},
|
|
||||||
"metadata": {
|
|
||||||
"confidence": "insufficient",
|
|
||||||
"data_points": corr_data.get("data_points", 0) if isinstance(corr_data, dict) else 0,
|
|
||||||
"message": msg,
|
|
||||||
"lag_details": corr_data.get("lag_details") if isinstance(corr_data, dict) else None,
|
|
||||||
"tdee_kcal_used": corr_data.get("tdee_kcal_used") if isinstance(corr_data, dict) else None,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
best_lag = corr_data.get("best_lag_days", corr_data.get("best_lag", 0))
|
|
||||||
correlation = corr_data.get("correlation", 0)
|
|
||||||
|
|
||||||
return {
|
|
||||||
"chart_type": "scatter",
|
|
||||||
"data": {
|
|
||||||
"labels": [f"Lag {best_lag} Tage"],
|
|
||||||
"datasets": [
|
|
||||||
{
|
|
||||||
"label": "Korrelation",
|
|
||||||
"data": [{"x": best_lag, "y": correlation}],
|
|
||||||
"backgroundColor": "#1D9E75",
|
|
||||||
"borderColor": "#085041",
|
|
||||||
"borderWidth": 2,
|
|
||||||
"pointRadius": 8,
|
|
||||||
}
|
|
||||||
],
|
|
||||||
},
|
|
||||||
"metadata": {
|
|
||||||
"confidence": corr_data.get("confidence", "low"),
|
|
||||||
"correlation": round(float(correlation), 3),
|
|
||||||
"best_lag_days": best_lag,
|
|
||||||
"interpretation": corr_data.get("interpretation", ""),
|
|
||||||
"data_points": corr_data.get("data_points", 0),
|
|
||||||
"lag_details": corr_data.get("lag_details"),
|
|
||||||
"tdee_kcal_used": corr_data.get("tdee_kcal_used"),
|
|
||||||
"layer_1": "correlations._correlate_energy_weight",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def build_lbm_protein_correlation_chart_payload(profile_id: str, max_lag: int) -> Dict[str, Any]:
|
|
||||||
corr_data = calculate_lag_correlation(profile_id, "protein", "lbm", max_lag)
|
|
||||||
|
|
||||||
if not corr_data or corr_data.get("correlation") is None:
|
|
||||||
msg = "Nicht genug Daten für LBM-Protein Korrelation"
|
|
||||||
if isinstance(corr_data, dict):
|
|
||||||
msg = str(corr_data.get("interpretation") or corr_data.get("reason") or msg)
|
|
||||||
return {
|
|
||||||
"chart_type": "scatter",
|
|
||||||
"data": {"labels": [], "datasets": []},
|
|
||||||
"metadata": {
|
|
||||||
"confidence": "insufficient",
|
|
||||||
"data_points": corr_data.get("data_points", 0) if isinstance(corr_data, dict) else 0,
|
|
||||||
"message": msg,
|
|
||||||
"lag_details": corr_data.get("lag_details") if isinstance(corr_data, dict) else None,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
best_lag = corr_data.get("best_lag_days", corr_data.get("best_lag", 0))
|
|
||||||
correlation = corr_data.get("correlation", 0)
|
|
||||||
|
|
||||||
return {
|
|
||||||
"chart_type": "scatter",
|
|
||||||
"data": {
|
|
||||||
"labels": [f"Lag {best_lag} Tage"],
|
|
||||||
"datasets": [
|
|
||||||
{
|
|
||||||
"label": "Korrelation",
|
|
||||||
"data": [{"x": best_lag, "y": correlation}],
|
|
||||||
"backgroundColor": "#3B82F6",
|
|
||||||
"borderColor": "#1E40AF",
|
|
||||||
"borderWidth": 2,
|
|
||||||
"pointRadius": 8,
|
|
||||||
}
|
|
||||||
],
|
|
||||||
},
|
|
||||||
"metadata": {
|
|
||||||
"confidence": corr_data.get("confidence", "low"),
|
|
||||||
"correlation": round(float(correlation), 3),
|
|
||||||
"best_lag_days": best_lag,
|
|
||||||
"interpretation": corr_data.get("interpretation", ""),
|
|
||||||
"data_points": corr_data.get("data_points", 0),
|
|
||||||
"lag_details": corr_data.get("lag_details"),
|
|
||||||
"layer_1": "correlations._correlate_protein_lbm",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def build_load_vitals_correlation_chart_payload(profile_id: str, max_lag: int) -> Dict[str, Any]:
|
|
||||||
corr_hrv = calculate_lag_correlation(profile_id, "load", "hrv", max_lag)
|
|
||||||
corr_rhr = calculate_lag_correlation(profile_id, "load", "rhr", max_lag)
|
|
||||||
|
|
||||||
def _abs_corr(c: Any) -> float:
|
|
||||||
if not c or c.get("correlation") is None:
|
|
||||||
return -1.0
|
|
||||||
try:
|
|
||||||
return abs(float(c["correlation"]))
|
|
||||||
except (TypeError, ValueError):
|
|
||||||
return -1.0
|
|
||||||
|
|
||||||
if _abs_corr(corr_hrv) < 0 and _abs_corr(corr_rhr) < 0:
|
|
||||||
msg = "Nicht genug Daten für Load-Vitals Korrelation"
|
|
||||||
h_msg = corr_hrv.get("interpretation") if isinstance(corr_hrv, dict) else None
|
|
||||||
r_msg = corr_rhr.get("interpretation") if isinstance(corr_rhr, dict) else None
|
|
||||||
if h_msg or r_msg:
|
|
||||||
msg = f"HRV: {h_msg or '—'} · RHR: {r_msg or '—'}"
|
|
||||||
return {
|
|
||||||
"chart_type": "scatter",
|
|
||||||
"data": {"labels": [], "datasets": []},
|
|
||||||
"metadata": {
|
|
||||||
"confidence": "insufficient",
|
|
||||||
"data_points": 0,
|
|
||||||
"message": msg,
|
|
||||||
"lag_details_hrv": corr_hrv.get("lag_details") if isinstance(corr_hrv, dict) else None,
|
|
||||||
"lag_details_rhr": corr_rhr.get("lag_details") if isinstance(corr_rhr, dict) else None,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
if _abs_corr(corr_hrv) >= _abs_corr(corr_rhr):
|
|
||||||
corr_data = corr_hrv
|
|
||||||
metric_name = "HRV"
|
|
||||||
else:
|
|
||||||
corr_data = corr_rhr
|
|
||||||
metric_name = "RHR"
|
|
||||||
|
|
||||||
if not corr_data or corr_data.get("correlation") is None:
|
|
||||||
return {
|
|
||||||
"chart_type": "scatter",
|
|
||||||
"data": {"labels": [], "datasets": []},
|
|
||||||
"metadata": {
|
|
||||||
"confidence": "insufficient",
|
|
||||||
"data_points": 0,
|
|
||||||
"message": str(corr_data.get("interpretation") or "Nicht genug Daten für Load-Vitals Korrelation"),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
best_lag = corr_data.get("best_lag_days", corr_data.get("best_lag", 0))
|
|
||||||
correlation = corr_data.get("correlation", 0)
|
|
||||||
|
|
||||||
return {
|
|
||||||
"chart_type": "scatter",
|
|
||||||
"data": {
|
|
||||||
"labels": [f"Load → {metric_name} (Lag {best_lag}d)"],
|
|
||||||
"datasets": [
|
|
||||||
{
|
|
||||||
"label": "Korrelation",
|
|
||||||
"data": [{"x": best_lag, "y": correlation}],
|
|
||||||
"backgroundColor": "#F59E0B",
|
|
||||||
"borderColor": "#D97706",
|
|
||||||
"borderWidth": 2,
|
|
||||||
"pointRadius": 8,
|
|
||||||
}
|
|
||||||
],
|
|
||||||
},
|
|
||||||
"metadata": {
|
|
||||||
"confidence": corr_data.get("confidence", "low"),
|
|
||||||
"correlation": round(float(correlation), 3),
|
|
||||||
"best_lag_days": best_lag,
|
|
||||||
"metric": metric_name,
|
|
||||||
"interpretation": corr_data.get("interpretation", ""),
|
|
||||||
"data_points": corr_data.get("data_points", 0),
|
|
||||||
"lag_details": corr_data.get("lag_details"),
|
|
||||||
"layer_1": "correlations._correlate_load_vitals",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def build_recovery_performance_chart_payload(profile_id: str) -> Dict[str, Any]:
|
|
||||||
drivers = calculate_top_drivers(profile_id)
|
|
||||||
|
|
||||||
if not drivers or len(drivers) == 0:
|
|
||||||
return {
|
|
||||||
"chart_type": "bar",
|
|
||||||
"data": {"labels": [], "datasets": []},
|
|
||||||
"metadata": {
|
|
||||||
"confidence": "insufficient",
|
|
||||||
"data_points": 0,
|
|
||||||
"message": "Nicht genug Daten für Driver-Analyse",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
hindering = [d for d in drivers if d.get("impact", "") == "hindering"]
|
|
||||||
helpful = [d for d in drivers if d.get("impact", "") == "helpful"]
|
|
||||||
|
|
||||||
top_hindering = hindering[:3]
|
|
||||||
top_helpful = helpful[:3]
|
|
||||||
|
|
||||||
labels = []
|
|
||||||
values = []
|
|
||||||
colors = []
|
|
||||||
|
|
||||||
for d in top_hindering:
|
|
||||||
labels.append(f"❌ {d.get('factor', '')}")
|
|
||||||
values.append(-abs(d.get("score", 0)))
|
|
||||||
colors.append("#EF4444")
|
|
||||||
|
|
||||||
for d in top_helpful:
|
|
||||||
labels.append(f"✅ {d.get('factor', '')}")
|
|
||||||
values.append(abs(d.get("score", 0)))
|
|
||||||
colors.append("#1D9E75")
|
|
||||||
|
|
||||||
if not labels:
|
|
||||||
return {
|
|
||||||
"chart_type": "bar",
|
|
||||||
"data": {"labels": [], "datasets": []},
|
|
||||||
"metadata": {
|
|
||||||
"confidence": "low",
|
|
||||||
"data_points": 0,
|
|
||||||
"message": "Keine signifikanten Treiber gefunden",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
"chart_type": "bar",
|
|
||||||
"data": {
|
|
||||||
"labels": labels,
|
|
||||||
"datasets": [
|
|
||||||
{
|
|
||||||
"label": "Impact Score",
|
|
||||||
"data": values,
|
|
||||||
"backgroundColor": colors,
|
|
||||||
"borderColor": "#085041",
|
|
||||||
"borderWidth": 1,
|
|
||||||
}
|
|
||||||
],
|
|
||||||
},
|
|
||||||
"metadata": {
|
|
||||||
"confidence": "medium",
|
|
||||||
"hindering_count": len(top_hindering),
|
|
||||||
"helpful_count": len(top_helpful),
|
|
||||||
"total_factors": len(drivers),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
@@ -17,29 +17,28 @@ Phase 0c: Multi-Layer Architecture
|
|||||||
Version: 1.0
|
Version: 1.0
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from typing import Any, Dict, List, Optional, Tuple
|
from typing import Dict, List, Optional, Tuple
|
||||||
|
|
||||||
from datetime import datetime, timedelta, date
|
from datetime import datetime, timedelta, date
|
||||||
from db import get_db, get_cursor, r2d
|
from db import get_db, get_cursor, r2d
|
||||||
import statistics
|
import statistics
|
||||||
|
|
||||||
from data_layer.nutrition_body_merge import build_merged_daily_nutrition_body_rows
|
|
||||||
from data_layer.nutrition_metrics import estimate_tdee_kcal_from_latest_weight
|
|
||||||
|
|
||||||
# Lag-Korrelation (Issue #53): gleiche TDEE-Logik wie nutrition_metrics / nutrition_viz
|
|
||||||
MIN_PAIRS_LAG_CORR = 15
|
|
||||||
LAG_CORR_LOOKBACK_DAYS = 120
|
|
||||||
|
|
||||||
def calculate_lag_correlation(profile_id: str, var1: str, var2: str, max_lag_days: int = 14) -> Optional[Dict]:
|
def calculate_lag_correlation(profile_id: str, var1: str, var2: str, max_lag_days: int = 14) -> Optional[Dict]:
|
||||||
"""
|
"""
|
||||||
Pearson-Korrelation mit Lag-Sweep (Issue 53, Data-Layer).
|
Calculate lagged correlation between two variables
|
||||||
|
|
||||||
C1: Tagesbilanz (kcal − TDEE wie ``estimate_tdee_kcal_from_latest_weight``) vs. ΔGewicht [t→t+L], L≥1.
|
Args:
|
||||||
C2: Protein (g) vs. ΔMager [t→t+L] aus ``build_merged_daily_nutrition_body_rows``, L≥1.
|
var1: 'energy', 'protein', 'training_load'
|
||||||
C3: Summe ``duration_min`` pro Tag vs. HRV oder Ruhepuls am Tag t+L (L≥0).
|
var2: 'weight', 'lbm', 'hrv', 'rhr'
|
||||||
|
max_lag_days: Maximum lag to test
|
||||||
|
|
||||||
Rückgabe enthält u. a. ``best_lag`` / ``best_lag_days``, ``correlation``, ``interpretation``,
|
Returns:
|
||||||
optional ``lag_details`` (r, n je Lag), mindestens ``MIN_PAIRS_LAG_CORR`` Paare am besten Lag.
|
{
|
||||||
|
'best_lag': X, # days
|
||||||
|
'correlation': 0.XX, # -1 to 1
|
||||||
|
'direction': 'positive'/'negative'/'none',
|
||||||
|
'confidence': 'high'/'medium'/'low',
|
||||||
|
'data_points': N
|
||||||
|
}
|
||||||
"""
|
"""
|
||||||
v1 = (var1 or "").strip().lower()
|
v1 = (var1 or "").strip().lower()
|
||||||
if v1 in ("energy", "energy_balance"):
|
if v1 in ("energy", "energy_balance"):
|
||||||
@@ -71,347 +70,83 @@ def _normalize_lag_payload(raw: Optional[Dict]) -> Optional[Dict]:
|
|||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
def _iso_date_key(d: Any) -> str:
|
|
||||||
if d is None:
|
|
||||||
return ""
|
|
||||||
if hasattr(d, "isoformat"):
|
|
||||||
return str(d.isoformat())[:10]
|
|
||||||
s = str(d)
|
|
||||||
return s[:10] if len(s) >= 10 else s
|
|
||||||
|
|
||||||
|
|
||||||
def _parse_iso_to_date(ds: str) -> Optional[date]:
|
|
||||||
if not ds or len(ds) < 10:
|
|
||||||
return None
|
|
||||||
try:
|
|
||||||
return date.fromisoformat(ds[:10])
|
|
||||||
except ValueError:
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def _pearson_r(xs: List[float], ys: List[float]) -> Optional[float]:
|
|
||||||
"""Pearson-Korrelation; mindestens ``MIN_PAIRS_LAG_CORR`` Paare."""
|
|
||||||
n = len(xs)
|
|
||||||
if n < MIN_PAIRS_LAG_CORR or n != len(ys):
|
|
||||||
return None
|
|
||||||
mx = sum(xs) / n
|
|
||||||
my = sum(ys) / n
|
|
||||||
num = sum((xs[i] - mx) * (ys[i] - my) for i in range(n))
|
|
||||||
dx = sum((xs[i] - mx) ** 2 for i in range(n))
|
|
||||||
dy = sum((ys[i] - my) ** 2 for i in range(n))
|
|
||||||
if dx <= 1e-12 or dy <= 1e-12:
|
|
||||||
return None
|
|
||||||
r = num / ((dx**0.5) * (dy**0.5))
|
|
||||||
return float(max(-1.0, min(1.0, r)))
|
|
||||||
|
|
||||||
|
|
||||||
def _direction_from_r(r: float) -> str:
|
|
||||||
if r > 0.05:
|
|
||||||
return "positive"
|
|
||||||
if r < -0.05:
|
|
||||||
return "negative"
|
|
||||||
return "none"
|
|
||||||
|
|
||||||
|
|
||||||
def _lag_confidence(n_pairs: int, r: float) -> str:
|
|
||||||
return calculate_correlation_confidence(n_pairs, abs(r))
|
|
||||||
|
|
||||||
|
|
||||||
def _correlate_energy_weight(profile_id: str, max_lag: int) -> Optional[Dict]:
|
def _correlate_energy_weight(profile_id: str, max_lag: int) -> Optional[Dict]:
|
||||||
"""
|
"""
|
||||||
Pearson: Tagesbilanz (kcal − TDEE wie nutrition_metrics) vs. Gewichtsdifferenz
|
Correlate energy balance with weight change
|
||||||
vom Tag t zu Tag t+L (L = 0 … max_lag). Bestes Lag nach maximalem |r|.
|
Test lags: 0, 3, 7, 10, 14 days
|
||||||
"""
|
"""
|
||||||
tdee = estimate_tdee_kcal_from_latest_weight(profile_id)
|
|
||||||
if tdee is None or float(tdee) <= 0:
|
|
||||||
return {
|
|
||||||
"best_lag": None,
|
|
||||||
"correlation": None,
|
|
||||||
"direction": "none",
|
|
||||||
"confidence": "insufficient",
|
|
||||||
"data_points": 0,
|
|
||||||
"interpretation": "Keine TDEE-Schätzung möglich (Gewicht/Demografie).",
|
|
||||||
"reason": "no_tdee",
|
|
||||||
}
|
|
||||||
|
|
||||||
tdee_f = float(tdee)
|
|
||||||
cutoff = (datetime.now() - timedelta(days=LAG_CORR_LOOKBACK_DAYS)).strftime("%Y-%m-%d")
|
|
||||||
|
|
||||||
with get_db() as conn:
|
with get_db() as conn:
|
||||||
cur = get_cursor(conn)
|
cur = get_cursor(conn)
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT date::date AS d, SUM(kcal)::float AS kcal
|
|
||||||
FROM nutrition_log
|
|
||||||
WHERE profile_id = %s AND date >= %s::date AND kcal IS NOT NULL
|
|
||||||
GROUP BY date
|
|
||||||
ORDER BY date
|
|
||||||
""",
|
|
||||||
(profile_id, cutoff),
|
|
||||||
)
|
|
||||||
kcal_rows = cur.fetchall()
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT date::date AS d, weight::float AS weight
|
|
||||||
FROM weight_log
|
|
||||||
WHERE profile_id = %s AND date >= %s::date AND weight IS NOT NULL
|
|
||||||
ORDER BY date
|
|
||||||
""",
|
|
||||||
(profile_id, cutoff),
|
|
||||||
)
|
|
||||||
w_rows = cur.fetchall()
|
|
||||||
|
|
||||||
kcal_by: Dict[str, float] = {}
|
# Get energy balance data (daily calories - estimated TDEE)
|
||||||
for r in kcal_rows:
|
cur.execute("""
|
||||||
kcal_by[_iso_date_key(r["d"])] = float(r["kcal"] or 0)
|
SELECT n.date, n.kcal, w.weight
|
||||||
weight_by: Dict[str, float] = {}
|
FROM nutrition_log n
|
||||||
for r in w_rows:
|
LEFT JOIN weight_log w ON w.profile_id = n.profile_id
|
||||||
weight_by[_iso_date_key(r["d"])] = float(r["weight"])
|
AND w.date = n.date
|
||||||
|
WHERE n.profile_id = %s
|
||||||
|
AND n.date >= CURRENT_DATE - INTERVAL '90 days'
|
||||||
|
ORDER BY n.date
|
||||||
|
""", (profile_id,))
|
||||||
|
|
||||||
balance_by = {d: kcal_by[d] - tdee_f for d in kcal_by}
|
data = cur.fetchall()
|
||||||
|
|
||||||
best: Optional[Tuple[int, float, int]] = None
|
if len(data) < 30:
|
||||||
lag_details: List[Dict[str, Any]] = []
|
|
||||||
|
|
||||||
max_l = max(0, min(int(max_lag), 28))
|
|
||||||
# Lag 0: ΔGewicht am selben Tag ist immer 0 → sinnvoll erst ab Tag 1
|
|
||||||
for lag in range(1, max_l + 1):
|
|
||||||
xs: List[float] = []
|
|
||||||
ys: List[float] = []
|
|
||||||
for ds in sorted(balance_by.keys()):
|
|
||||||
d0 = _parse_iso_to_date(ds)
|
|
||||||
if d0 is None:
|
|
||||||
continue
|
|
||||||
d1 = d0 + timedelta(days=lag)
|
|
||||||
ds1 = d1.isoformat()
|
|
||||||
w0 = weight_by.get(ds)
|
|
||||||
w1 = weight_by.get(ds1)
|
|
||||||
if w0 is None or w1 is None:
|
|
||||||
continue
|
|
||||||
xs.append(balance_by[ds])
|
|
||||||
ys.append(w1 - w0)
|
|
||||||
r = _pearson_r(xs, ys)
|
|
||||||
n_p = len(xs)
|
|
||||||
lag_details.append({"lag": lag, "n_pairs": n_p, "r": None if r is None else round(r, 4)})
|
|
||||||
if r is None:
|
|
||||||
continue
|
|
||||||
if best is None or abs(r) > abs(best[1]):
|
|
||||||
best = (lag, r, n_p)
|
|
||||||
|
|
||||||
if best is None:
|
|
||||||
return {
|
return {
|
||||||
"best_lag": None,
|
'best_lag': None,
|
||||||
"correlation": None,
|
'correlation': None,
|
||||||
"direction": "none",
|
'direction': 'none',
|
||||||
"confidence": "insufficient",
|
'confidence': 'low',
|
||||||
"data_points": 0,
|
'data_points': len(data),
|
||||||
"interpretation": "Zu wenige gepaarte Tage mit Ernährung, Gewicht und gewähltem Lag.",
|
'reason': 'Insufficient data (<30 days)'
|
||||||
"reason": "insufficient_pairs",
|
|
||||||
"lag_details": lag_details,
|
|
||||||
"tdee_kcal_used": round(tdee_f, 0),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
lag_b, r_b, n_b = best
|
# Calculate 7d rolling energy balance
|
||||||
direction = _direction_from_r(r_b)
|
# (Simplified - actual implementation would need TDEE estimation)
|
||||||
conf = _lag_confidence(n_b, r_b)
|
|
||||||
interp = (
|
|
||||||
f"Tagesbilanz (kcal − TDEE ~{tdee_f:.0f}) vs. Gewichtsänderung nach {lag_b} Tagen: "
|
|
||||||
f"r ≈ {r_b:.2f} ({direction}). "
|
|
||||||
f"Basierend auf {n_b} Kalendertagen mit vollständigen Paaren."
|
|
||||||
)
|
|
||||||
|
|
||||||
|
# For now, return placeholder
|
||||||
return {
|
return {
|
||||||
"best_lag": lag_b,
|
'best_lag': 7,
|
||||||
"correlation": round(r_b, 4),
|
'correlation': -0.45, # Placeholder
|
||||||
"direction": direction,
|
'direction': 'negative', # Higher deficit = lower weight (expected)
|
||||||
"confidence": conf,
|
'confidence': 'medium',
|
||||||
"data_points": n_b,
|
'data_points': len(data)
|
||||||
"interpretation": interp,
|
|
||||||
"lag_details": lag_details,
|
|
||||||
"tdee_kcal_used": round(tdee_f, 0),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def _correlate_protein_lbm(profile_id: str, max_lag: int) -> Optional[Dict]:
|
def _correlate_protein_lbm(profile_id: str, max_lag: int) -> Optional[Dict]:
|
||||||
"""
|
"""Correlate protein intake with LBM trend"""
|
||||||
Pearson: Protein (g/Tag) vs. Magermasse-Differenz (kg) vom Tag t zu t+L.
|
# TODO: Implement full correlation calculation
|
||||||
Datenbasis: nutrition_body_merge (Caliper-LBM forward-filled wie Ernährungs-Verlauf).
|
|
||||||
"""
|
|
||||||
merged = build_merged_daily_nutrition_body_rows(profile_id)
|
|
||||||
if not merged:
|
|
||||||
return {
|
return {
|
||||||
"best_lag": None,
|
'best_lag': 0,
|
||||||
"correlation": None,
|
'correlation': 0.32, # Placeholder
|
||||||
"direction": "none",
|
'direction': 'positive',
|
||||||
"confidence": "insufficient",
|
'confidence': 'medium',
|
||||||
"data_points": 0,
|
'data_points': 28
|
||||||
"interpretation": "Keine zusammengeführten Ernährungs-/Körperdaten.",
|
|
||||||
"reason": "no_merged_rows",
|
|
||||||
}
|
|
||||||
|
|
||||||
protein_by: Dict[str, float] = {}
|
|
||||||
lbm_by: Dict[str, float] = {}
|
|
||||||
for row in merged:
|
|
||||||
ds = _iso_date_key(row.get("date"))
|
|
||||||
if not ds:
|
|
||||||
continue
|
|
||||||
pg = row.get("protein_g")
|
|
||||||
lm = row.get("lean_mass")
|
|
||||||
if pg is not None:
|
|
||||||
protein_by[ds] = float(pg)
|
|
||||||
if lm is not None:
|
|
||||||
lbm_by[ds] = float(lm)
|
|
||||||
|
|
||||||
best: Optional[Tuple[int, float, int]] = None
|
|
||||||
lag_details: List[Dict[str, Any]] = []
|
|
||||||
max_l = max(0, min(int(max_lag), 28))
|
|
||||||
|
|
||||||
for lag in range(1, max_l + 1):
|
|
||||||
xs: List[float] = []
|
|
||||||
ys: List[float] = []
|
|
||||||
for ds in sorted(protein_by.keys()):
|
|
||||||
if ds not in lbm_by:
|
|
||||||
continue
|
|
||||||
d0 = _parse_iso_to_date(ds)
|
|
||||||
if d0 is None:
|
|
||||||
continue
|
|
||||||
d1 = d0 + timedelta(days=lag)
|
|
||||||
ds1 = d1.isoformat()
|
|
||||||
if ds1 not in lbm_by:
|
|
||||||
continue
|
|
||||||
xs.append(protein_by[ds])
|
|
||||||
ys.append(lbm_by[ds1] - lbm_by[ds])
|
|
||||||
r = _pearson_r(xs, ys)
|
|
||||||
n_p = len(xs)
|
|
||||||
lag_details.append({"lag": lag, "n_pairs": n_p, "r": None if r is None else round(r, 4)})
|
|
||||||
if r is None:
|
|
||||||
continue
|
|
||||||
if best is None or abs(r) > abs(best[1]):
|
|
||||||
best = (lag, r, n_p)
|
|
||||||
|
|
||||||
if best is None:
|
|
||||||
return {
|
|
||||||
"best_lag": None,
|
|
||||||
"correlation": None,
|
|
||||||
"direction": "none",
|
|
||||||
"confidence": "insufficient",
|
|
||||||
"data_points": 0,
|
|
||||||
"interpretation": "Zu wenige Tage mit Protein und Magermasse (Caliper) für die gewählten Lags.",
|
|
||||||
"reason": "insufficient_pairs",
|
|
||||||
"lag_details": lag_details,
|
|
||||||
}
|
|
||||||
|
|
||||||
lag_b, r_b, n_b = best
|
|
||||||
direction = _direction_from_r(r_b)
|
|
||||||
conf = _lag_confidence(n_b, r_b)
|
|
||||||
interp = (
|
|
||||||
f"Protein (g/Tag) vs. Magermasse-Änderung nach {lag_b} Tagen: r ≈ {r_b:.2f} ({direction}). "
|
|
||||||
f"{n_b} gepaarte Tage."
|
|
||||||
)
|
|
||||||
|
|
||||||
return {
|
|
||||||
"best_lag": lag_b,
|
|
||||||
"correlation": round(r_b, 4),
|
|
||||||
"direction": direction,
|
|
||||||
"confidence": conf,
|
|
||||||
"data_points": n_b,
|
|
||||||
"interpretation": interp,
|
|
||||||
"lag_details": lag_details,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def _correlate_load_vitals(profile_id: str, vital: str, max_lag: int) -> Optional[Dict]:
|
def _correlate_load_vitals(profile_id: str, vital: str, max_lag: int) -> Optional[Dict]:
|
||||||
"""
|
"""
|
||||||
Pearson: Tages-Trainingslast (Summe duration_min) vs. Vitals (HRV ms oder Ruhepuls)
|
Correlate training load with HRV or RHR
|
||||||
am Kalendertag t+Lag (typisch: Belastung am Vortag, Vitalwert am Folgetag bei Lag ≥ 1).
|
Test lags: 1, 2, 3 days
|
||||||
"""
|
"""
|
||||||
col = "hrv" if vital == "hrv" else "resting_hr"
|
# TODO: Implement full correlation calculation
|
||||||
cutoff = (datetime.now() - timedelta(days=LAG_CORR_LOOKBACK_DAYS)).strftime("%Y-%m-%d")
|
if vital == 'hrv':
|
||||||
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT date::text AS d, COALESCE(SUM(duration_min), 0)::float AS minutes
|
|
||||||
FROM activity_log
|
|
||||||
WHERE profile_id = %s AND date >= %s::date
|
|
||||||
AND duration_min IS NOT NULL AND duration_min > 0
|
|
||||||
GROUP BY date
|
|
||||||
ORDER BY date
|
|
||||||
""",
|
|
||||||
(profile_id, cutoff),
|
|
||||||
)
|
|
||||||
load_rows = cur.fetchall()
|
|
||||||
cur.execute(
|
|
||||||
f"""
|
|
||||||
SELECT date::text AS d, {col}::float AS v
|
|
||||||
FROM vitals_baseline
|
|
||||||
WHERE profile_id = %s AND date >= %s::date AND {col} IS NOT NULL
|
|
||||||
ORDER BY date
|
|
||||||
""",
|
|
||||||
(profile_id, cutoff),
|
|
||||||
)
|
|
||||||
vit_rows = cur.fetchall()
|
|
||||||
|
|
||||||
load_by = {str(r["d"])[:10]: float(r["minutes"] or 0) for r in load_rows}
|
|
||||||
vital_by = {str(r["d"])[:10]: float(r["v"]) for r in vit_rows}
|
|
||||||
|
|
||||||
best: Optional[Tuple[int, float, int]] = None
|
|
||||||
lag_details: List[Dict[str, Any]] = []
|
|
||||||
max_l = max(0, min(int(max_lag), 28))
|
|
||||||
vlabel = "HRV (ms)" if vital == "hrv" else "Ruhepuls (bpm)"
|
|
||||||
|
|
||||||
for lag in range(0, max_l + 1):
|
|
||||||
xs: List[float] = []
|
|
||||||
ys: List[float] = []
|
|
||||||
for ds in sorted(load_by.keys()):
|
|
||||||
d0 = _parse_iso_to_date(ds)
|
|
||||||
if d0 is None:
|
|
||||||
continue
|
|
||||||
d1 = d0 + timedelta(days=lag)
|
|
||||||
ds1 = d1.isoformat()
|
|
||||||
if ds1 not in vital_by:
|
|
||||||
continue
|
|
||||||
xs.append(load_by[ds])
|
|
||||||
ys.append(vital_by[ds1])
|
|
||||||
r = _pearson_r(xs, ys)
|
|
||||||
n_p = len(xs)
|
|
||||||
lag_details.append({"lag": lag, "n_pairs": n_p, "r": None if r is None else round(r, 4)})
|
|
||||||
if r is None:
|
|
||||||
continue
|
|
||||||
if best is None or abs(r) > abs(best[1]):
|
|
||||||
best = (lag, r, n_p)
|
|
||||||
|
|
||||||
if best is None:
|
|
||||||
return {
|
return {
|
||||||
"best_lag": None,
|
'best_lag': 1,
|
||||||
"correlation": None,
|
'correlation': -0.38, # Negative = high load reduces HRV (expected)
|
||||||
"direction": "none",
|
'direction': 'negative',
|
||||||
"confidence": "insufficient",
|
'confidence': 'medium',
|
||||||
"data_points": 0,
|
'data_points': 25
|
||||||
"interpretation": f"Zu wenige gepaarte Tage mit Training und {vlabel}.",
|
|
||||||
"reason": "insufficient_pairs",
|
|
||||||
"lag_details": lag_details,
|
|
||||||
"vital": vital,
|
|
||||||
}
|
}
|
||||||
|
else: # rhr
|
||||||
lag_b, r_b, n_b = best
|
|
||||||
direction = _direction_from_r(r_b)
|
|
||||||
conf = _lag_confidence(n_b, r_b)
|
|
||||||
interp = (
|
|
||||||
f"Trainingsminuten/Tag vs. {vlabel} nach {lag_b} Tagen Lag: r ≈ {r_b:.2f} ({direction}). "
|
|
||||||
f"{n_b} Paare."
|
|
||||||
)
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"best_lag": lag_b,
|
'best_lag': 1,
|
||||||
"correlation": round(r_b, 4),
|
'correlation': 0.42, # Positive = high load increases RHR (expected)
|
||||||
"direction": direction,
|
'direction': 'positive',
|
||||||
"confidence": conf,
|
'confidence': 'medium',
|
||||||
"data_points": n_b,
|
'data_points': 25
|
||||||
"interpretation": interp,
|
|
||||||
"lag_details": lag_details,
|
|
||||||
"vital": vital,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -59,15 +59,6 @@ def _last_activity_date(profile_id: str) -> Optional[str]:
|
|||||||
return _iso(row["d"])
|
return _iso(row["d"])
|
||||||
|
|
||||||
|
|
||||||
def get_activity_last_updated_iso(profile_id: str) -> Optional[str]:
|
|
||||||
"""
|
|
||||||
Leichtgewicht: letztes activity_log.date — identisch zu ``last_updated`` im Fitness-Viz-Bundle.
|
|
||||||
|
|
||||||
Für History-Header o. Ä. ohne vollständige Aktivitätsliste (Phase A, Issue-53-Pfad).
|
|
||||||
"""
|
|
||||||
return _last_activity_date(profile_id)
|
|
||||||
|
|
||||||
|
|
||||||
def get_fitness_dashboard_viz_bundle(profile_id: str, days: int) -> Dict[str, Any]:
|
def get_fitness_dashboard_viz_bundle(profile_id: str, days: int) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
Bundle für Fitness-Übersicht: KPI-Kacheln + eingebettete Chart-Payloads (Chart.js-Format).
|
Bundle für Fitness-Übersicht: KPI-Kacheln + eingebettete Chart-Payloads (Chart.js-Format).
|
||||||
|
|||||||
@@ -1,238 +0,0 @@
|
|||||||
"""Catalog attribute definitions and numeric values (BLS EAV + extensions)."""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import re
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
_ATTR_KEY_RE = re.compile(r"[^A-Z0-9]+")
|
|
||||||
_ALLOWED_UNITS = {"g": "g", "mg": "mg", "µg": "µg", "ug": "µg", "mcg": "µg", "kcal": "kcal", "kj": "kJ"}
|
|
||||||
|
|
||||||
MACRO_TO_KEY = {
|
|
||||||
"kcal": "ENERCC",
|
|
||||||
"protein_g": "PROT625",
|
|
||||||
"fat_g": "FAT",
|
|
||||||
"carbs_g": "CHO",
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def scale_to_per_100g(value: float, serving_g: float | None) -> float:
|
|
||||||
if not serving_g or serving_g <= 0 or serving_g == 100:
|
|
||||||
return float(value)
|
|
||||||
return float(value) * (100.0 / float(serving_g))
|
|
||||||
|
|
||||||
|
|
||||||
def list_numeric_attributes(
|
|
||||||
cur, query: str = "", limit: int = 40, keys: list[str] | None = None
|
|
||||||
) -> list[dict[str, Any]]:
|
|
||||||
lim = min(max(int(limit or 40), 1), 80)
|
|
||||||
wanted = [str(k).strip() for k in (keys or []) if str(k).strip()]
|
|
||||||
if wanted:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id, attr_key, name_de, name_en, unit, category, origin
|
|
||||||
FROM food_attributes
|
|
||||||
WHERE is_active = true AND data_type = 'num_per_100g'
|
|
||||||
AND attr_key = ANY(%s)
|
|
||||||
ORDER BY sort_order, attr_key
|
|
||||||
""",
|
|
||||||
(wanted,),
|
|
||||||
)
|
|
||||||
return [dict(r) for r in cur.fetchall()]
|
|
||||||
q = (query or "").strip()
|
|
||||||
if q:
|
|
||||||
like = f"%{q}%"
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id, attr_key, name_de, name_en, unit, category, origin
|
|
||||||
FROM food_attributes
|
|
||||||
WHERE is_active = true AND data_type = 'num_per_100g'
|
|
||||||
AND (
|
|
||||||
attr_key ILIKE %s OR name_de ILIKE %s
|
|
||||||
OR COALESCE(name_en, '') ILIKE %s
|
|
||||||
)
|
|
||||||
ORDER BY
|
|
||||||
CASE WHEN attr_key ILIKE %s THEN 0
|
|
||||||
WHEN name_de ILIKE %s THEN 1
|
|
||||||
ELSE 2 END,
|
|
||||||
sort_order, attr_key
|
|
||||||
LIMIT %s
|
|
||||||
""",
|
|
||||||
(like, like, like, q, f"{q}%", lim),
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id, attr_key, name_de, name_en, unit, category, origin
|
|
||||||
FROM food_attributes
|
|
||||||
WHERE is_active = true AND data_type = 'num_per_100g'
|
|
||||||
ORDER BY sort_order, attr_key
|
|
||||||
LIMIT %s
|
|
||||||
""",
|
|
||||||
(lim,),
|
|
||||||
)
|
|
||||||
return [dict(r) for r in cur.fetchall()]
|
|
||||||
|
|
||||||
|
|
||||||
def write_numeric_attributes(cur, food_id: str, values: dict[str, Any] | None) -> int:
|
|
||||||
if not values:
|
|
||||||
return 0
|
|
||||||
written = 0
|
|
||||||
for raw_key, raw_val in values.items():
|
|
||||||
key = str(raw_key or "").strip()
|
|
||||||
if not key or raw_val is None or raw_val == "":
|
|
||||||
continue
|
|
||||||
try:
|
|
||||||
num = float(raw_val)
|
|
||||||
except (TypeError, ValueError):
|
|
||||||
continue
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id FROM food_attributes
|
|
||||||
WHERE attr_key = %s AND is_active = true AND data_type = 'num_per_100g'
|
|
||||||
""",
|
|
||||||
(key,),
|
|
||||||
)
|
|
||||||
row = cur.fetchone()
|
|
||||||
if not row:
|
|
||||||
continue
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO food_attribute_values (food_id, attribute_id, value_num, is_trace)
|
|
||||||
VALUES (%s, %s, %s, false)
|
|
||||||
ON CONFLICT (food_id, attribute_id)
|
|
||||||
DO UPDATE SET value_num = EXCLUDED.value_num, updated_at = NOW()
|
|
||||||
""",
|
|
||||||
(food_id, row["id"], num),
|
|
||||||
)
|
|
||||||
written += 1
|
|
||||||
return written
|
|
||||||
|
|
||||||
|
|
||||||
def replace_numeric_attributes(cur, food_id: str, values: dict[str, Any] | None) -> int:
|
|
||||||
cur.execute("DELETE FROM food_attribute_values WHERE food_id = %s", (food_id,))
|
|
||||||
return write_numeric_attributes(cur, food_id, values)
|
|
||||||
|
|
||||||
|
|
||||||
KEY_TO_MACRO = {v: k for k, v in MACRO_TO_KEY.items()}
|
|
||||||
|
|
||||||
|
|
||||||
def editor_payload_for_food(cur, food: dict[str, Any], *, editable: bool) -> dict[str, Any]:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT a.attr_key, a.name_de, a.name_en, a.unit, a.category, a.origin, v.value_num
|
|
||||||
FROM food_attribute_values v
|
|
||||||
JOIN food_attributes a ON a.id = v.attribute_id
|
|
||||||
WHERE v.food_id = %s AND a.data_type = 'num_per_100g'
|
|
||||||
AND v.value_num IS NOT NULL AND v.is_trace = false
|
|
||||||
ORDER BY a.sort_order, a.attr_key
|
|
||||||
""",
|
|
||||||
(food["id"],),
|
|
||||||
)
|
|
||||||
macros = {"kcal": 0, "protein_g": 0, "fat_g": 0, "carbs_g": 0}
|
|
||||||
extras = []
|
|
||||||
for row in cur.fetchall():
|
|
||||||
rec = dict(row)
|
|
||||||
field = KEY_TO_MACRO.get(rec["attr_key"])
|
|
||||||
if field:
|
|
||||||
macros[field] = float(rec["value_num"])
|
|
||||||
else:
|
|
||||||
extras.append({
|
|
||||||
"attr": {
|
|
||||||
"attr_key": rec["attr_key"],
|
|
||||||
"name_de": rec["name_de"],
|
|
||||||
"name_en": rec["name_en"],
|
|
||||||
"unit": rec["unit"],
|
|
||||||
"category": rec["category"],
|
|
||||||
"origin": rec["origin"],
|
|
||||||
},
|
|
||||||
"value": float(rec["value_num"]),
|
|
||||||
})
|
|
||||||
return {
|
|
||||||
"id": str(food["id"]),
|
|
||||||
"name_de": food.get("name_de") or "",
|
|
||||||
"name_en": food.get("name_en"),
|
|
||||||
"catalog_kind": food.get("catalog_kind"),
|
|
||||||
"editable": editable,
|
|
||||||
"macros_per_100g": macros,
|
|
||||||
"attributes": extras,
|
|
||||||
"serving_g": 100,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def macros_and_attributes_to_values(
|
|
||||||
macros: dict[str, Any] | None,
|
|
||||||
attributes: dict[str, Any] | None,
|
|
||||||
serving_g: float | None = None,
|
|
||||||
) -> dict[str, float]:
|
|
||||||
out: dict[str, float] = {}
|
|
||||||
for field, key in MACRO_TO_KEY.items():
|
|
||||||
if not macros or field not in macros or macros[field] is None or macros[field] == "":
|
|
||||||
continue
|
|
||||||
try:
|
|
||||||
out[key] = scale_to_per_100g(float(macros[field]), serving_g)
|
|
||||||
except (TypeError, ValueError):
|
|
||||||
continue
|
|
||||||
for key, raw in (attributes or {}).items():
|
|
||||||
if raw is None or raw == "":
|
|
||||||
continue
|
|
||||||
try:
|
|
||||||
out[str(key)] = scale_to_per_100g(float(raw), serving_g)
|
|
||||||
except (TypeError, ValueError):
|
|
||||||
continue
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
def extra_attributes_for_food(cur, food_id: str) -> dict[str, float]:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT a.attr_key, v.value_num
|
|
||||||
FROM food_attribute_values v
|
|
||||||
JOIN food_attributes a ON a.id = v.attribute_id
|
|
||||||
WHERE v.food_id = %s AND a.data_type = 'num_per_100g'
|
|
||||||
AND v.value_num IS NOT NULL AND v.is_trace = false
|
|
||||||
AND NOT (a.attr_key = ANY(%s))
|
|
||||||
ORDER BY a.sort_order, a.attr_key
|
|
||||||
""",
|
|
||||||
(food_id, list(MACRO_TO_KEY.values())),
|
|
||||||
)
|
|
||||||
return {r["attr_key"]: float(r["value_num"]) for r in cur.fetchall()}
|
|
||||||
|
|
||||||
|
|
||||||
def normalize_attr_key(raw: str | None) -> str:
|
|
||||||
key = _ATTR_KEY_RE.sub("_", (raw or "").strip().upper()).strip("_")
|
|
||||||
return key[:64]
|
|
||||||
|
|
||||||
|
|
||||||
def normalize_attr_unit(raw: str | None) -> str:
|
|
||||||
return _ALLOWED_UNITS.get((raw or "g").strip().lower(), "g")
|
|
||||||
|
|
||||||
|
|
||||||
def create_extension_attribute(cur, name_de: str, unit: str = "g", attr_key: str | None = None) -> dict[str, Any]:
|
|
||||||
name = (name_de or "").strip()
|
|
||||||
if not name:
|
|
||||||
raise ValueError("Name fehlt")
|
|
||||||
key = normalize_attr_key(attr_key or name)
|
|
||||||
if not key:
|
|
||||||
raise ValueError("attr_key fehlt")
|
|
||||||
unit_n = normalize_attr_unit(unit)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id, attr_key, name_de, name_en, unit, category, origin
|
|
||||||
FROM food_attributes
|
|
||||||
WHERE attr_key = %s AND is_active = true
|
|
||||||
""",
|
|
||||||
(key,),
|
|
||||||
)
|
|
||||||
existing = cur.fetchone()
|
|
||||||
if existing:
|
|
||||||
return dict(existing)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO food_attributes
|
|
||||||
(attr_key, name_de, name_en, unit, category, data_type, origin, sort_order)
|
|
||||||
VALUES (%s, %s, %s, %s, 'extension', 'num_per_100g', 'extension', 9100)
|
|
||||||
RETURNING id, attr_key, name_de, name_en, unit, category, origin
|
|
||||||
""",
|
|
||||||
(key, name, None, unit_n),
|
|
||||||
)
|
|
||||||
return dict(cur.fetchone())
|
|
||||||
@@ -1,265 +0,0 @@
|
|||||||
"""Portable JSON for user food mappings, manual foods, and FDDB lists."""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from datetime import datetime, timezone
|
|
||||||
from decimal import Decimal
|
|
||||||
from typing import Any
|
|
||||||
from uuid import UUID
|
|
||||||
|
|
||||||
from data_layer.food_mapping import (
|
|
||||||
apply_mapping_to_items,
|
|
||||||
apply_quantities_to_items,
|
|
||||||
normalize_food_name,
|
|
||||||
upsert_food_mapping,
|
|
||||||
)
|
|
||||||
from data_layer.food_recipes import list_recipes, upsert_recipes
|
|
||||||
from data_layer.nutrition_items import catalog_macros_for_item, dates_for_normalized_name, rebuild_daily_nutrients
|
|
||||||
|
|
||||||
BUNDLE_FORMAT = "mitai-food-knowledge"
|
|
||||||
BUNDLE_VERSION = 1
|
|
||||||
|
|
||||||
|
|
||||||
def _jsonable(value: Any) -> Any:
|
|
||||||
if value is None:
|
|
||||||
return None
|
|
||||||
if isinstance(value, (str, int, float, bool)):
|
|
||||||
return value
|
|
||||||
if isinstance(value, Decimal):
|
|
||||||
return float(value)
|
|
||||||
if isinstance(value, UUID):
|
|
||||||
return str(value)
|
|
||||||
if isinstance(value, dict):
|
|
||||||
return {str(k): _jsonable(v) for k, v in value.items()}
|
|
||||||
if isinstance(value, (list, tuple)):
|
|
||||||
return [_jsonable(v) for v in value]
|
|
||||||
if hasattr(value, "isoformat"):
|
|
||||||
return value.isoformat()
|
|
||||||
return str(value)
|
|
||||||
|
|
||||||
|
|
||||||
def parse_food_knowledge_bundle(data: Any) -> dict[str, Any]:
|
|
||||||
if not isinstance(data, dict):
|
|
||||||
raise ValueError("Die Datei ist kein JSON-Objekt")
|
|
||||||
if data.get("format") != BUNDLE_FORMAT:
|
|
||||||
raise ValueError("Keine Mitai-Zuordnungsdatei — bitte die exportierte JSON verwenden")
|
|
||||||
try:
|
|
||||||
version = int(data.get("version") or 0)
|
|
||||||
except (TypeError, ValueError) as exc:
|
|
||||||
raise ValueError("Unbekannte Dateiversion") from exc
|
|
||||||
if version != BUNDLE_VERSION:
|
|
||||||
raise ValueError(f"Nicht unterstützte Dateiversion {version}")
|
|
||||||
return data
|
|
||||||
|
|
||||||
|
|
||||||
def portable_mapping(row: dict[str, Any]) -> dict[str, Any]:
|
|
||||||
return {
|
|
||||||
"source_system": row.get("source_system") or "fddb",
|
|
||||||
"source_name_raw": row.get("source_name_raw"),
|
|
||||||
"source_name_normalized": row.get("source_name_normalized"),
|
|
||||||
"bls_code": row.get("bls_code"),
|
|
||||||
"food_name_de": row.get("food_name_de") or row.get("name_de"),
|
|
||||||
"catalog_kind": row.get("catalog_kind"),
|
|
||||||
"external_key": row.get("external_key"),
|
|
||||||
"grams_per_unit": _jsonable(row.get("grams_per_unit")),
|
|
||||||
"source_unit": row.get("source_unit"),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def resolve_catalog_food(cur, profile_id: str, ref: dict[str, Any]) -> str | None:
|
|
||||||
bls = (ref.get("bls_code") or "").strip()
|
|
||||||
if bls:
|
|
||||||
cur.execute(
|
|
||||||
"SELECT id FROM food_catalog WHERE bls_code = %s AND is_active = true",
|
|
||||||
(bls,),
|
|
||||||
)
|
|
||||||
row = cur.fetchone()
|
|
||||||
return str(row["id"]) if row else None
|
|
||||||
kind = ref.get("catalog_kind") or "manual_user"
|
|
||||||
name = (ref.get("food_name_de") or ref.get("name_de") or "").strip()
|
|
||||||
key = (ref.get("external_key") or "").strip()
|
|
||||||
if kind == "manual_user":
|
|
||||||
if key:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id FROM food_catalog
|
|
||||||
WHERE owner_profile_id = %s AND external_key = %s AND is_active = true
|
|
||||||
LIMIT 1
|
|
||||||
""",
|
|
||||||
(profile_id, key),
|
|
||||||
)
|
|
||||||
row = cur.fetchone()
|
|
||||||
if row:
|
|
||||||
return str(row["id"])
|
|
||||||
if name:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id FROM food_catalog
|
|
||||||
WHERE owner_profile_id = %s AND catalog_kind = 'manual_user'
|
|
||||||
AND lower(name_de) = lower(%s) AND is_active = true
|
|
||||||
LIMIT 1
|
|
||||||
""",
|
|
||||||
(profile_id, name),
|
|
||||||
)
|
|
||||||
row = cur.fetchone()
|
|
||||||
if row:
|
|
||||||
return str(row["id"])
|
|
||||||
return None
|
|
||||||
if kind == "manual_admin" and name:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id FROM food_catalog
|
|
||||||
WHERE catalog_kind = 'manual_admin' AND lower(name_de) = lower(%s)
|
|
||||||
AND is_active = true
|
|
||||||
LIMIT 1
|
|
||||||
""",
|
|
||||||
(name,),
|
|
||||||
)
|
|
||||||
row = cur.fetchone()
|
|
||||||
return str(row["id"]) if row else None
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def export_food_knowledge(cur, profile_id: str) -> dict[str, Any]:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id, name_de, name_en, catalog_kind, external_key
|
|
||||||
FROM food_catalog
|
|
||||||
WHERE owner_profile_id = %s AND catalog_kind = 'manual_user' AND is_active = true
|
|
||||||
ORDER BY lower(name_de)
|
|
||||||
""",
|
|
||||||
(profile_id,),
|
|
||||||
)
|
|
||||||
manuals = []
|
|
||||||
for food in cur.fetchall():
|
|
||||||
macros = catalog_macros_for_item(cur, food["id"], 100.0)
|
|
||||||
from data_layer.food_attributes import extra_attributes_for_food
|
|
||||||
manuals.append({
|
|
||||||
"name_de": food["name_de"],
|
|
||||||
"name_en": food.get("name_en"),
|
|
||||||
"catalog_kind": food["catalog_kind"],
|
|
||||||
"external_key": food.get("external_key"),
|
|
||||||
"macros_per_100g": macros,
|
|
||||||
"attributes": extra_attributes_for_food(cur, food["id"]),
|
|
||||||
})
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT m.source_system, m.source_name_raw, m.source_name_normalized,
|
|
||||||
m.grams_per_unit, m.source_unit,
|
|
||||||
f.bls_code, f.name_de AS food_name_de, f.catalog_kind, f.external_key
|
|
||||||
FROM food_name_mappings m
|
|
||||||
JOIN food_catalog f ON f.id = m.food_id
|
|
||||||
WHERE m.profile_id = %s
|
|
||||||
ORDER BY m.source_name_normalized
|
|
||||||
""",
|
|
||||||
(profile_id,),
|
|
||||||
)
|
|
||||||
mappings = [portable_mapping(dict(r)) for r in cur.fetchall()]
|
|
||||||
recipes = []
|
|
||||||
for rec in list_recipes(cur, profile_id):
|
|
||||||
recipes.append({
|
|
||||||
"name_raw": rec.get("name_raw"),
|
|
||||||
"name_normalized": rec.get("name_normalized"),
|
|
||||||
"portions": _jsonable(rec.get("portions")),
|
|
||||||
"description": rec.get("description"),
|
|
||||||
"source": rec.get("source") or "fddb_list",
|
|
||||||
"ingredients": [
|
|
||||||
{
|
|
||||||
"source_name_raw": ing.get("source_name_raw"),
|
|
||||||
"source_name_normalized": ing.get("source_name_normalized"),
|
|
||||||
"quantity_raw": ing.get("quantity_raw"),
|
|
||||||
"quantity_g": _jsonable(ing.get("quantity_g")),
|
|
||||||
"quantity_amount": _jsonable(ing.get("quantity_amount")),
|
|
||||||
"source_unit": ing.get("source_unit"),
|
|
||||||
"sort_order": ing.get("sort_order") or 0,
|
|
||||||
}
|
|
||||||
for ing in rec.get("ingredients") or []
|
|
||||||
],
|
|
||||||
})
|
|
||||||
return _jsonable({
|
|
||||||
"format": BUNDLE_FORMAT,
|
|
||||||
"version": BUNDLE_VERSION,
|
|
||||||
"exported_at": datetime.now(timezone.utc).isoformat(),
|
|
||||||
"manual_foods": manuals,
|
|
||||||
"mappings": mappings,
|
|
||||||
"recipes": recipes,
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
def _upsert_manual_food(cur, profile_id: str, food: dict[str, Any]) -> str | None:
|
|
||||||
name = (food.get("name_de") or "").strip()
|
|
||||||
if not name:
|
|
||||||
return None
|
|
||||||
existing = resolve_catalog_food(cur, profile_id, {**food, "food_name_de": name, "catalog_kind": "manual_user"})
|
|
||||||
from data_layer.food_attributes import macros_and_attributes_to_values, write_numeric_attributes
|
|
||||||
values = macros_and_attributes_to_values(food.get("macros_per_100g"), food.get("attributes"))
|
|
||||||
if existing:
|
|
||||||
write_numeric_attributes(cur, existing, values)
|
|
||||||
return existing
|
|
||||||
key = (food.get("external_key") or "").strip() or f"man-user-{normalize_food_name(name)[:40]}"
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO food_catalog
|
|
||||||
(name_de, name_en, catalog_kind, owner_profile_id, source, external_key)
|
|
||||||
VALUES (%s, %s, 'manual_user', %s, 'import', %s)
|
|
||||||
RETURNING id
|
|
||||||
""",
|
|
||||||
(name, food.get("name_en"), profile_id, key),
|
|
||||||
)
|
|
||||||
food_id = str(cur.fetchone()["id"])
|
|
||||||
write_numeric_attributes(cur, food_id, values)
|
|
||||||
return food_id
|
|
||||||
|
|
||||||
|
|
||||||
def import_food_knowledge(cur, profile_id: str, data: dict[str, Any]) -> dict[str, Any]:
|
|
||||||
bundle = parse_food_knowledge_bundle(data)
|
|
||||||
foods_upserted = 0
|
|
||||||
for food in bundle.get("manual_foods") or []:
|
|
||||||
if _upsert_manual_food(cur, profile_id, food):
|
|
||||||
foods_upserted += 1
|
|
||||||
mappings_ok = mappings_skipped = items_updated = 0
|
|
||||||
skipped: list[str] = []
|
|
||||||
dates: set[str] = set()
|
|
||||||
for raw in bundle.get("mappings") or []:
|
|
||||||
name = (raw.get("source_name_raw") or "").strip()
|
|
||||||
if not name:
|
|
||||||
mappings_skipped += 1
|
|
||||||
continue
|
|
||||||
food_id = resolve_catalog_food(cur, profile_id, raw)
|
|
||||||
if not food_id:
|
|
||||||
mappings_skipped += 1
|
|
||||||
label = raw.get("bls_code") or raw.get("food_name_de") or name
|
|
||||||
skipped.append(str(label))
|
|
||||||
continue
|
|
||||||
mid = upsert_food_mapping(
|
|
||||||
cur,
|
|
||||||
source_name_raw=name,
|
|
||||||
food_id=food_id,
|
|
||||||
profile_id=profile_id,
|
|
||||||
source="import",
|
|
||||||
source_system=raw.get("source_system") or "fddb",
|
|
||||||
grams_per_unit=raw.get("grams_per_unit"),
|
|
||||||
source_unit=raw.get("source_unit"),
|
|
||||||
)
|
|
||||||
norm = normalize_food_name(name)
|
|
||||||
items_updated += apply_mapping_to_items(cur, profile_id, norm, food_id, mid)
|
|
||||||
apply_quantities_to_items(cur, profile_id, norm, raw.get("grams_per_unit"))
|
|
||||||
dates.update(dates_for_normalized_name(cur, profile_id, norm))
|
|
||||||
mappings_ok += 1
|
|
||||||
recipe_stats = {"inserted": 0, "updated": 0, "ingredients": 0, "items_linked": 0}
|
|
||||||
recipes = bundle.get("recipes") or []
|
|
||||||
if recipes:
|
|
||||||
recipe_stats = upsert_recipes(cur, profile_id, recipes)
|
|
||||||
dates.update(recipe_stats.pop("dates_linked", []) or [])
|
|
||||||
for day in dates:
|
|
||||||
rebuild_daily_nutrients(cur, profile_id, day)
|
|
||||||
from data_layer.food_suggest import invalidate_suggest_index
|
|
||||||
invalidate_suggest_index(profile_id)
|
|
||||||
return {
|
|
||||||
"ok": True,
|
|
||||||
"manual_foods": foods_upserted,
|
|
||||||
"mappings": mappings_ok,
|
|
||||||
"mappings_skipped": mappings_skipped,
|
|
||||||
"items_updated": items_updated,
|
|
||||||
"skipped_foods": skipped[:20],
|
|
||||||
**recipe_stats,
|
|
||||||
}
|
|
||||||
@@ -1,672 +0,0 @@
|
|||||||
"""FDDB → food_catalog mapping: normalize, lookup (user then global), learn, apply."""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import re
|
|
||||||
import unicodedata
|
|
||||||
from datetime import date, timedelta
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
LEADING_QTY_RE = re.compile(
|
|
||||||
r"^\s*\d+(?:[.,]\d+)?\s*(?:g|kg|ml|l|stück|stk|st\.?|portion(?:en)?)\b[\s,.:\-–]*",
|
|
||||||
re.IGNORECASE,
|
|
||||||
)
|
|
||||||
TRAILING_QTY_RE = re.compile(
|
|
||||||
r"[\s,.:\-–]+\d+(?:[.,]\d+)?\s*(?:g|kg|ml|l)\s*$",
|
|
||||||
re.IGNORECASE,
|
|
||||||
)
|
|
||||||
MULTISPACE_RE = re.compile(r"\s+")
|
|
||||||
DECIMAL_IN_NAME_RE = re.compile(r"(\d),(\d)")
|
|
||||||
|
|
||||||
|
|
||||||
def strip_leading_quantity(raw: str | None) -> str:
|
|
||||||
if not raw:
|
|
||||||
return ""
|
|
||||||
s = unicodedata.normalize("NFKC", str(raw)).strip().strip('"').strip("'")
|
|
||||||
s = s.lstrip("!")
|
|
||||||
s = LEADING_QTY_RE.sub("", s)
|
|
||||||
return MULTISPACE_RE.sub(" ", s).strip()
|
|
||||||
|
|
||||||
|
|
||||||
def normalize_food_name(raw: str | None) -> str:
|
|
||||||
s = strip_leading_quantity(raw)
|
|
||||||
if not s:
|
|
||||||
return ""
|
|
||||||
s = TRAILING_QTY_RE.sub("", s)
|
|
||||||
s = s.replace("-", " ")
|
|
||||||
s = DECIMAL_IN_NAME_RE.sub(r"\1.\2", s)
|
|
||||||
return MULTISPACE_RE.sub(" ", s).strip().lower()
|
|
||||||
|
|
||||||
|
|
||||||
def item_name_keys(raw: str | None, stored_norm: str | None = None) -> set[str]:
|
|
||||||
"""Compare diary names to list names despite stored quantity prefixes."""
|
|
||||||
keys: set[str] = set()
|
|
||||||
for v in (raw, stored_norm):
|
|
||||||
if not v:
|
|
||||||
continue
|
|
||||||
text = str(v).strip()
|
|
||||||
if text:
|
|
||||||
keys.add(text.lower())
|
|
||||||
norm = normalize_food_name(text)
|
|
||||||
if norm:
|
|
||||||
keys.add(norm)
|
|
||||||
return keys
|
|
||||||
|
|
||||||
|
|
||||||
def names_match_list(raw: str | None, stored_norm: str | None, list_norm: str | None) -> bool:
|
|
||||||
target = normalize_food_name(list_norm) if list_norm else ""
|
|
||||||
if not target:
|
|
||||||
return False
|
|
||||||
return target in item_name_keys(raw, stored_norm)
|
|
||||||
|
|
||||||
|
|
||||||
_LIST_COMMA_RE = re.compile(r",(?!\s*\d)")
|
|
||||||
|
|
||||||
|
|
||||||
def primary_search_query(query: str | None) -> str:
|
|
||||||
"""Use the name before a list-comma, but keep decimal commas (9,5 %)."""
|
|
||||||
q = (query or "").strip()
|
|
||||||
if not q:
|
|
||||||
return ""
|
|
||||||
return _LIST_COMMA_RE.split(q, maxsplit=1)[0].strip()
|
|
||||||
|
|
||||||
|
|
||||||
def merge_unmapped_rows(rows: list[dict]) -> list[dict]:
|
|
||||||
merged: dict[str, dict] = {}
|
|
||||||
for row in rows:
|
|
||||||
raw = row.get("source_name_raw") or ""
|
|
||||||
key = normalize_food_name(raw) or row.get("source_name_normalized") or raw.lower()
|
|
||||||
if not key:
|
|
||||||
continue
|
|
||||||
display = strip_leading_quantity(raw) or raw
|
|
||||||
count = int(row.get("count") or 0)
|
|
||||||
if key not in merged:
|
|
||||||
item = dict(row)
|
|
||||||
item["source_name_normalized"] = key
|
|
||||||
item["source_name_raw"] = display
|
|
||||||
item["count"] = count
|
|
||||||
item["variant_count"] = 1
|
|
||||||
merged[key] = item
|
|
||||||
continue
|
|
||||||
cur = merged[key]
|
|
||||||
cur["count"] = int(cur.get("count") or 0) + count
|
|
||||||
cur["variant_count"] = int(cur.get("variant_count") or 1) + 1
|
|
||||||
if display and (not cur.get("source_name_raw") or len(display) < len(cur["source_name_raw"])):
|
|
||||||
cur["source_name_raw"] = display
|
|
||||||
if row.get("matching_recipe_id") and not cur.get("matching_recipe_id"):
|
|
||||||
cur["matching_recipe_id"] = row["matching_recipe_id"]
|
|
||||||
if row.get("sample_quantity_raw") and not cur.get("sample_quantity_raw"):
|
|
||||||
cur["sample_quantity_raw"] = row["sample_quantity_raw"]
|
|
||||||
first, last = row.get("first_date"), row.get("last_date")
|
|
||||||
if first and (not cur.get("first_date") or str(first) < str(cur["first_date"])):
|
|
||||||
cur["first_date"] = first
|
|
||||||
if last and (not cur.get("last_date") or str(last) > str(cur["last_date"])):
|
|
||||||
cur["last_date"] = last
|
|
||||||
return list(merged.values())
|
|
||||||
|
|
||||||
|
|
||||||
def as_iso_date(value: Any) -> str | None:
|
|
||||||
if value is None or value == "":
|
|
||||||
return None
|
|
||||||
if hasattr(value, "isoformat"):
|
|
||||||
return str(value.isoformat())[:10]
|
|
||||||
text = str(value).strip()
|
|
||||||
return text[:10] if len(text) >= 10 else None
|
|
||||||
|
|
||||||
|
|
||||||
def filter_unmapped_since(rows: list[dict], since_days: int, today: date | None = None) -> list[dict]:
|
|
||||||
"""Keep names last eaten in the window. Rows without last_date drop out (old recipe leftovers)."""
|
|
||||||
days = int(since_days or 0)
|
|
||||||
if days <= 0:
|
|
||||||
return list(rows)
|
|
||||||
cutoff = ((today or date.today()) - timedelta(days=days)).isoformat()
|
|
||||||
out = []
|
|
||||||
for row in rows:
|
|
||||||
last = as_iso_date(row.get("last_date"))
|
|
||||||
if last and last >= cutoff:
|
|
||||||
out.append(row)
|
|
||||||
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 id, name_raw, name_normalized
|
|
||||||
FROM food_recipes
|
|
||||||
WHERE profile_id = %s
|
|
||||||
""",
|
|
||||||
(profile_id,),
|
|
||||||
)
|
|
||||||
lists = [dict(r) for r in cur.fetchall()]
|
|
||||||
for row in diary:
|
|
||||||
keys = item_name_keys(row.get("source_name_raw"), row.get("source_name_normalized"))
|
|
||||||
hit = next(
|
|
||||||
(
|
|
||||||
rec
|
|
||||||
for rec in lists
|
|
||||||
if rec.get("name_normalized") in keys
|
|
||||||
or normalize_food_name(rec.get("name_raw")) in keys
|
|
||||||
),
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
if hit:
|
|
||||||
row["matching_recipe_id"] = str(hit["id"])
|
|
||||||
row["matching_recipe_name"] = hit["name_raw"]
|
|
||||||
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:
|
|
||||||
rows.sort(
|
|
||||||
key=lambda x: (
|
|
||||||
as_iso_date(x.get("last_date")) or "",
|
|
||||||
int(x.get("count") or 0),
|
|
||||||
),
|
|
||||||
reverse=True,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
rows.sort(key=lambda x: (-int(x.get("count") or 0), x.get("source_name_normalized") or ""))
|
|
||||||
return rows
|
|
||||||
|
|
||||||
|
|
||||||
UNIT_ALIASES = {
|
|
||||||
"g": "g", "gr": "g", "gramm": "g",
|
|
||||||
"kg": "kg",
|
|
||||||
"ml": "ml",
|
|
||||||
"l": "l", "liter": "l", "lt": "l",
|
|
||||||
"stück": "stück", "stk": "stück", "st": "stück", "st.": "stück", "pcs": "stück",
|
|
||||||
"el": "el", "esslöffel": "el",
|
|
||||||
"tl": "tl", "teelöffel": "tl",
|
|
||||||
"prise": "prise",
|
|
||||||
"scheibe": "scheibe",
|
|
||||||
"portion": "portion", "portionen": "portion",
|
|
||||||
"becher": "becher",
|
|
||||||
"tasse": "tasse",
|
|
||||||
"msp": "msp", "msp.": "msp",
|
|
||||||
}
|
|
||||||
MASS_VOLUME_TO_G = {"g": 1.0, "kg": 1000.0, "ml": 1.0, "l": 1000.0}
|
|
||||||
DEFAULT_UNIT_G = {"el": 15.0, "tl": 5.0, "prise": 0.3, "msp": 1.0}
|
|
||||||
COUNT_UNITS = frozenset({"stück", "scheibe", "portion", "becher", "tasse"})
|
|
||||||
UNIT_LABELS = {
|
|
||||||
"g": "g", "kg": "kg", "ml": "ml", "l": "l",
|
|
||||||
"el": "EL", "tl": "TL", "prise": "Prise", "msp": "Msp.",
|
|
||||||
"stück": "Stück", "scheibe": "Scheibe", "portion": "Portion",
|
|
||||||
"becher": "Becher", "tasse": "Tasse",
|
|
||||||
}
|
|
||||||
UNIT_ORDER = (
|
|
||||||
"g", "ml", "el", "tl", "prise", "msp",
|
|
||||||
"stück", "scheibe", "portion", "becher", "tasse", "kg", "l",
|
|
||||||
)
|
|
||||||
QTY_PARSE_RE = re.compile(
|
|
||||||
r"^\s*(\d+(?:[.,]\d+)?)\s*([a-zA-ZäöüÄÖÜß.]+)?\s*$",
|
|
||||||
re.IGNORECASE,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _canon_unit(raw: str | None) -> str | None:
|
|
||||||
if not raw:
|
|
||||||
return None
|
|
||||||
return UNIT_ALIASES.get(raw.strip().lower().rstrip("."))
|
|
||||||
|
|
||||||
|
|
||||||
def parse_quantity(raw: str | None, grams_per_unit: float | None = None) -> dict[str, Any]:
|
|
||||||
empty = {"value": None, "unit": None, "quantity_g": None, "needs_unit_map": False}
|
|
||||||
if raw is None or str(raw).strip() == "":
|
|
||||||
return empty
|
|
||||||
text = str(raw).strip().replace(",", ".")
|
|
||||||
m = QTY_PARSE_RE.match(text)
|
|
||||||
if not m:
|
|
||||||
return empty
|
|
||||||
value = float(m.group(1))
|
|
||||||
unit = _canon_unit(m.group(2))
|
|
||||||
if unit is None:
|
|
||||||
return {"value": value, "unit": "g", "quantity_g": round(value, 3), "needs_unit_map": False}
|
|
||||||
if unit in MASS_VOLUME_TO_G:
|
|
||||||
return {
|
|
||||||
"value": value,
|
|
||||||
"unit": unit,
|
|
||||||
"quantity_g": round(value * MASS_VOLUME_TO_G[unit], 3),
|
|
||||||
"needs_unit_map": False,
|
|
||||||
}
|
|
||||||
factor = grams_per_unit if grams_per_unit is not None else DEFAULT_UNIT_G.get(unit)
|
|
||||||
if factor is not None:
|
|
||||||
return {
|
|
||||||
"value": value,
|
|
||||||
"unit": unit,
|
|
||||||
"quantity_g": round(value * float(factor), 3),
|
|
||||||
"needs_unit_map": unit in COUNT_UNITS,
|
|
||||||
}
|
|
||||||
return {"value": value, "unit": unit, "quantity_g": None, "needs_unit_map": True}
|
|
||||||
|
|
||||||
|
|
||||||
def parse_quantity_g(raw: str | None, grams_per_unit: float | None = None) -> float | None:
|
|
||||||
return parse_quantity(raw, grams_per_unit)["quantity_g"]
|
|
||||||
|
|
||||||
|
|
||||||
def detect_quantity_unit(*texts: str | None) -> str | None:
|
|
||||||
for text in texts:
|
|
||||||
unit = parse_quantity(text).get("unit")
|
|
||||||
if unit and unit not in MASS_VOLUME_TO_G:
|
|
||||||
return unit
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def format_quantity_raw(value: float | None, unit: str | None) -> str | None:
|
|
||||||
if value is None:
|
|
||||||
return None
|
|
||||||
label = UNIT_LABELS.get(unit or "g", unit or "g")
|
|
||||||
num = int(value) if float(value) == int(value) else value
|
|
||||||
return f"{num} {label}".replace(".", ",")
|
|
||||||
|
|
||||||
|
|
||||||
def list_quantity_units() -> list[dict[str, Any]]:
|
|
||||||
out = []
|
|
||||||
for uid in UNIT_ORDER:
|
|
||||||
default_g = MASS_VOLUME_TO_G.get(uid)
|
|
||||||
if default_g is None:
|
|
||||||
default_g = DEFAULT_UNIT_G.get(uid)
|
|
||||||
out.append({
|
|
||||||
"id": uid,
|
|
||||||
"label": UNIT_LABELS[uid],
|
|
||||||
"default_g": default_g,
|
|
||||||
"needs_unit_map": uid in COUNT_UNITS,
|
|
||||||
})
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
def resolve_quantity(
|
|
||||||
*,
|
|
||||||
quantity_raw: str | None = None,
|
|
||||||
quantity_amount: float | None = None,
|
|
||||||
source_unit: str | None = None,
|
|
||||||
quantity_g: float | None = None,
|
|
||||||
grams_per_unit: float | None = None,
|
|
||||||
) -> dict[str, Any]:
|
|
||||||
"""Amount + unit → quantity_g. Standard unit is grams; conversion uses mapping or defaults."""
|
|
||||||
amount = None
|
|
||||||
if quantity_amount not in (None, ""):
|
|
||||||
try:
|
|
||||||
amount = float(quantity_amount)
|
|
||||||
except (TypeError, ValueError):
|
|
||||||
amount = None
|
|
||||||
explicit_g = None
|
|
||||||
if quantity_g not in (None, ""):
|
|
||||||
try:
|
|
||||||
explicit_g = float(quantity_g)
|
|
||||||
except (TypeError, ValueError):
|
|
||||||
explicit_g = None
|
|
||||||
if amount is not None:
|
|
||||||
unit = _canon_unit(source_unit) or "g"
|
|
||||||
raw = format_quantity_raw(amount, unit)
|
|
||||||
parsed = parse_quantity(raw.replace(",", "."), grams_per_unit)
|
|
||||||
elif quantity_raw:
|
|
||||||
parsed = parse_quantity(quantity_raw, grams_per_unit)
|
|
||||||
elif explicit_g is not None:
|
|
||||||
parsed = {"value": explicit_g, "unit": "g", "quantity_g": explicit_g, "needs_unit_map": False}
|
|
||||||
else:
|
|
||||||
parsed = {"value": None, "unit": _canon_unit(source_unit), "quantity_g": None, "needs_unit_map": False}
|
|
||||||
qty_g = parsed.get("quantity_g")
|
|
||||||
if qty_g is None and explicit_g is not None:
|
|
||||||
qty_g = explicit_g
|
|
||||||
unit = parsed.get("unit")
|
|
||||||
value = parsed.get("value")
|
|
||||||
return {
|
|
||||||
"quantity_amount": value,
|
|
||||||
"source_unit": unit,
|
|
||||||
"quantity_raw": format_quantity_raw(value, unit) or (str(quantity_raw).strip() if quantity_raw else None),
|
|
||||||
"quantity_g": qty_g,
|
|
||||||
"needs_unit_map": bool(parsed.get("needs_unit_map")),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def get_food_mapping_with_cursor(
|
|
||||||
cur,
|
|
||||||
source_name: str,
|
|
||||||
profile_id: str | None = None,
|
|
||||||
source_system: str = "fddb",
|
|
||||||
) -> dict[str, Any] | None:
|
|
||||||
norm = normalize_food_name(source_name)
|
|
||||||
if not norm:
|
|
||||||
return None
|
|
||||||
if profile_id:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT m.id AS mapping_id, m.food_id, m.profile_id, m.source,
|
|
||||||
m.grams_per_unit, m.source_unit,
|
|
||||||
f.bls_code, f.name_de, f.catalog_kind, f.owner_profile_id
|
|
||||||
FROM food_name_mappings m
|
|
||||||
JOIN food_catalog f ON f.id = m.food_id
|
|
||||||
WHERE m.source_system = %s AND m.source_name_normalized = %s
|
|
||||||
AND m.profile_id = %s
|
|
||||||
AND f.is_active = true
|
|
||||||
AND (f.owner_profile_id IS NULL OR f.owner_profile_id = %s)
|
|
||||||
LIMIT 1
|
|
||||||
""",
|
|
||||||
(source_system, norm, profile_id, profile_id),
|
|
||||||
)
|
|
||||||
row = cur.fetchone()
|
|
||||||
if row:
|
|
||||||
return dict(row)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT m.id AS mapping_id, m.food_id, m.profile_id, m.source,
|
|
||||||
m.grams_per_unit, m.source_unit,
|
|
||||||
f.bls_code, f.name_de, f.catalog_kind, f.owner_profile_id
|
|
||||||
FROM food_name_mappings m
|
|
||||||
JOIN food_catalog f ON f.id = m.food_id
|
|
||||||
WHERE m.source_system = %s AND m.source_name_normalized = %s
|
|
||||||
AND m.profile_id IS NULL
|
|
||||||
AND f.is_active = true
|
|
||||||
AND f.owner_profile_id IS NULL
|
|
||||||
LIMIT 1
|
|
||||||
""",
|
|
||||||
(source_system, norm),
|
|
||||||
)
|
|
||||||
row = cur.fetchone()
|
|
||||||
return dict(row) if row else None
|
|
||||||
|
|
||||||
|
|
||||||
def upsert_food_mapping(
|
|
||||||
cur,
|
|
||||||
*,
|
|
||||||
source_name_raw: str,
|
|
||||||
food_id: str,
|
|
||||||
profile_id: str | None,
|
|
||||||
source: str = "bulk",
|
|
||||||
source_system: str = "fddb",
|
|
||||||
grams_per_unit: float | None = None,
|
|
||||||
source_unit: str | None = None,
|
|
||||||
) -> int:
|
|
||||||
from data_layer.food_visibility import require_mapping_target
|
|
||||||
|
|
||||||
require_mapping_target(cur, food_id, profile_id)
|
|
||||||
norm = normalize_food_name(source_name_raw)
|
|
||||||
if not norm:
|
|
||||||
raise ValueError("Leerer Lebensmittelname")
|
|
||||||
if profile_id:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id FROM food_name_mappings
|
|
||||||
WHERE source_system = %s AND source_name_normalized = %s AND profile_id = %s
|
|
||||||
""",
|
|
||||||
(source_system, norm, profile_id),
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id FROM food_name_mappings
|
|
||||||
WHERE source_system = %s AND source_name_normalized = %s AND profile_id IS NULL
|
|
||||||
""",
|
|
||||||
(source_system, norm),
|
|
||||||
)
|
|
||||||
existing = cur.fetchone()
|
|
||||||
raw = source_name_raw.strip()
|
|
||||||
unit = _canon_unit(source_unit) if source_unit else None
|
|
||||||
if existing:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
UPDATE food_name_mappings
|
|
||||||
SET food_id = %s, source_name_raw = %s, source = %s,
|
|
||||||
grams_per_unit = %s, source_unit = %s, updated_at = NOW()
|
|
||||||
WHERE id = %s
|
|
||||||
""",
|
|
||||||
(food_id, raw, source, grams_per_unit, unit, existing["id"]),
|
|
||||||
)
|
|
||||||
return int(existing["id"])
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO food_name_mappings
|
|
||||||
(source_system, source_name_raw, source_name_normalized, food_id, profile_id,
|
|
||||||
source, grams_per_unit, source_unit, updated_at)
|
|
||||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, NOW())
|
|
||||||
RETURNING id
|
|
||||||
""",
|
|
||||||
(source_system, raw, norm, food_id, profile_id, source, grams_per_unit, unit),
|
|
||||||
)
|
|
||||||
return int(cur.fetchone()["id"])
|
|
||||||
|
|
||||||
|
|
||||||
def apply_quantities_to_items(cur, profile_id: str, source_name_normalized: str, grams_per_unit: float | None) -> int:
|
|
||||||
if not grams_per_unit:
|
|
||||||
return 0
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id, quantity_raw, source_name_raw
|
|
||||||
FROM nutrition_items
|
|
||||||
WHERE profile_id = %s AND source_name_normalized = %s
|
|
||||||
""",
|
|
||||||
(profile_id, source_name_normalized),
|
|
||||||
)
|
|
||||||
n = 0
|
|
||||||
for row in cur.fetchall():
|
|
||||||
qty = parse_quantity_g(row.get("quantity_raw") or row.get("source_name_raw"), grams_per_unit)
|
|
||||||
if qty is None:
|
|
||||||
continue
|
|
||||||
cur.execute(
|
|
||||||
"UPDATE nutrition_items SET quantity_g = %s, updated_at = NOW() WHERE id = %s",
|
|
||||||
(qty, row["id"]),
|
|
||||||
)
|
|
||||||
n += 1
|
|
||||||
return n
|
|
||||||
|
|
||||||
|
|
||||||
def apply_mapping_to_items(cur, profile_id: str, source_name_normalized: str, food_id: str, mapping_id: int) -> int:
|
|
||||||
origin = _value_origin_for_food(cur, food_id)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
UPDATE nutrition_items
|
|
||||||
SET food_id = %s, mapping_id = %s, value_origin = %s, updated_at = NOW()
|
|
||||||
WHERE profile_id = %s AND source_name_normalized = %s
|
|
||||||
AND recipe_id IS NULL
|
|
||||||
""",
|
|
||||||
(food_id, mapping_id, origin, profile_id, source_name_normalized),
|
|
||||||
)
|
|
||||||
n = cur.rowcount or 0
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id, source_name_raw
|
|
||||||
FROM nutrition_items
|
|
||||||
WHERE profile_id = %s AND recipe_id IS NULL AND food_id IS NULL
|
|
||||||
""",
|
|
||||||
(profile_id,),
|
|
||||||
)
|
|
||||||
extra = [
|
|
||||||
row["id"] for row in cur.fetchall()
|
|
||||||
if normalize_food_name(row.get("source_name_raw")) == source_name_normalized
|
|
||||||
]
|
|
||||||
if extra:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
UPDATE nutrition_items
|
|
||||||
SET food_id = %s, mapping_id = %s, value_origin = %s, updated_at = NOW()
|
|
||||||
WHERE id = ANY(%s)
|
|
||||||
""",
|
|
||||||
(food_id, mapping_id, origin, extra),
|
|
||||||
)
|
|
||||||
n += cur.rowcount or 0
|
|
||||||
return n
|
|
||||||
|
|
||||||
|
|
||||||
def clear_mapping_from_items(cur, profile_id: str, source_name_normalized: str) -> int:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
UPDATE nutrition_items
|
|
||||||
SET food_id = NULL, mapping_id = NULL, value_origin = 'fddb', updated_at = NOW()
|
|
||||||
WHERE profile_id = %s AND source_name_normalized = %s
|
|
||||||
""",
|
|
||||||
(profile_id, source_name_normalized),
|
|
||||||
)
|
|
||||||
return cur.rowcount or 0
|
|
||||||
|
|
||||||
|
|
||||||
def _value_origin_for_food(cur, food_id: str) -> str:
|
|
||||||
cur.execute("SELECT catalog_kind FROM food_catalog WHERE id = %s", (food_id,))
|
|
||||||
row = cur.fetchone()
|
|
||||||
if not row:
|
|
||||||
return "fddb"
|
|
||||||
kind = row["catalog_kind"]
|
|
||||||
if kind == "official_bls":
|
|
||||||
return "bls"
|
|
||||||
return "manual_catalog"
|
|
||||||
|
|
||||||
|
|
||||||
_SEARCH_NUM_RE = re.compile(r"\d+(?:[.,]\d+)?")
|
|
||||||
|
|
||||||
|
|
||||||
def _catalog_must_tokens(query: str) -> list[list[str]]:
|
|
||||||
"""AND-groups for catalog search: first word plus each number (with 9,5/10 aliases)."""
|
|
||||||
text = normalize_food_name(primary_search_query(query))
|
|
||||||
nums = [m.replace(",", ".") for m in _SEARCH_NUM_RE.findall(text)]
|
|
||||||
words = [t for t in re.split(r"[^a-z0-9äöüß]+", _SEARCH_NUM_RE.sub(" ", text)) if len(t) >= 2]
|
|
||||||
groups: list[list[str]] = []
|
|
||||||
if words:
|
|
||||||
groups.append([words[0]])
|
|
||||||
for num in nums[:3]:
|
|
||||||
variants = {num, num.replace(".", ",")}
|
|
||||||
try:
|
|
||||||
value = float(num)
|
|
||||||
except ValueError:
|
|
||||||
value = None
|
|
||||||
if value is not None and (abs(value - 10) <= 0.6 or abs(value - 9.5) <= 0.6):
|
|
||||||
variants.update({"10", "9.5", "9,5"})
|
|
||||||
groups.append(list(variants))
|
|
||||||
return groups
|
|
||||||
|
|
||||||
|
|
||||||
def suggest_catalog_foods(cur, query: str, profile_id: str | None, limit: int = 8) -> list[dict]:
|
|
||||||
q = (query or "").strip()
|
|
||||||
if not q:
|
|
||||||
return []
|
|
||||||
primary = primary_search_query(q) or q
|
|
||||||
like_full = f"%{q}%"
|
|
||||||
like_primary = f"%{primary}%"
|
|
||||||
prefix = f"{primary}%"
|
|
||||||
norm = normalize_food_name(primary)
|
|
||||||
extra_sql = ""
|
|
||||||
extra_params: list[str] = []
|
|
||||||
must = _catalog_must_tokens(q)
|
|
||||||
if len(must) >= 2:
|
|
||||||
parts = []
|
|
||||||
for group in must:
|
|
||||||
ors = " OR ".join(["name_de ILIKE %s"] * len(group))
|
|
||||||
parts.append(f"({ors})")
|
|
||||||
extra_params.extend(f"%{v}%" for v in group)
|
|
||||||
extra_sql = " OR (" + " AND ".join(parts) + ")"
|
|
||||||
cur.execute(
|
|
||||||
f"""
|
|
||||||
SELECT id, bls_code, name_de, name_en, catalog_kind, food_group, owner_profile_id
|
|
||||||
FROM food_catalog
|
|
||||||
WHERE is_active = true
|
|
||||||
AND (
|
|
||||||
owner_profile_id IS NULL
|
|
||||||
OR owner_profile_id = %s
|
|
||||||
)
|
|
||||||
AND (
|
|
||||||
name_de ILIKE %s OR name_de ILIKE %s
|
|
||||||
OR COALESCE(name_en, '') ILIKE %s OR COALESCE(name_en, '') ILIKE %s
|
|
||||||
OR COALESCE(bls_code, '') ILIKE %s
|
|
||||||
OR lower(name_de) = %s
|
|
||||||
{extra_sql}
|
|
||||||
)
|
|
||||||
ORDER BY
|
|
||||||
CASE
|
|
||||||
WHEN lower(name_de) = %s THEN 0
|
|
||||||
WHEN name_de ILIKE %s THEN 1
|
|
||||||
WHEN COALESCE(bls_code, '') ILIKE %s THEN 2
|
|
||||||
ELSE 3 END,
|
|
||||||
CASE catalog_kind
|
|
||||||
WHEN 'manual_user' THEN 0
|
|
||||||
WHEN 'manual_admin' THEN 1
|
|
||||||
ELSE 2 END,
|
|
||||||
name_de
|
|
||||||
LIMIT %s
|
|
||||||
""",
|
|
||||||
(
|
|
||||||
profile_id,
|
|
||||||
like_full, like_primary, like_full, like_primary, like_full, norm,
|
|
||||||
*extra_params,
|
|
||||||
norm, prefix, q, limit,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
return [dict(r) for r in cur.fetchall()]
|
|
||||||
@@ -1,433 +0,0 @@
|
|||||||
"""FDDB recipe lists: upsert, link to diary items, catalog macros via ingredients."""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import uuid
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from data_layer.food_mapping import (
|
|
||||||
get_food_mapping_with_cursor,
|
|
||||||
names_match_list,
|
|
||||||
normalize_food_name,
|
|
||||||
resolve_quantity,
|
|
||||||
)
|
|
||||||
from data_layer.nutrition_items import catalog_macros_for_item, _f
|
|
||||||
|
|
||||||
|
|
||||||
def _resolved_ingredient_qty(cur, profile_id: str, source_name_raw: str, ing: dict[str, Any]) -> dict[str, Any]:
|
|
||||||
mapping = get_food_mapping_with_cursor(cur, source_name_raw, profile_id)
|
|
||||||
gpu = ing.get("grams_per_unit")
|
|
||||||
if gpu in (None, ""):
|
|
||||||
gpu = mapping.get("grams_per_unit") if mapping else None
|
|
||||||
try:
|
|
||||||
gpu = float(gpu) if gpu not in (None, "") else None
|
|
||||||
except (TypeError, ValueError):
|
|
||||||
gpu = None
|
|
||||||
return resolve_quantity(
|
|
||||||
quantity_raw=ing.get("quantity_raw"),
|
|
||||||
quantity_amount=ing.get("quantity_amount"),
|
|
||||||
source_unit=ing.get("source_unit"),
|
|
||||||
quantity_g=ing.get("quantity_g"),
|
|
||||||
grams_per_unit=gpu,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def upsert_recipes(cur, profile_id: str, recipes: list[dict[str, Any]]) -> dict[str, int]:
|
|
||||||
inserted = updated = ingredients = 0
|
|
||||||
for rec in recipes:
|
|
||||||
norm = rec.get("name_normalized") or normalize_food_name(rec.get("name_raw"))
|
|
||||||
if not norm:
|
|
||||||
continue
|
|
||||||
cur.execute(
|
|
||||||
"SELECT id FROM food_recipes WHERE profile_id = %s AND name_normalized = %s",
|
|
||||||
(profile_id, norm),
|
|
||||||
)
|
|
||||||
row = cur.fetchone()
|
|
||||||
if row:
|
|
||||||
rid = row["id"]
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
UPDATE food_recipes
|
|
||||||
SET name_raw=%s, portions=%s, description=%s, updated_at=NOW()
|
|
||||||
WHERE id=%s
|
|
||||||
""",
|
|
||||||
(rec["name_raw"], rec.get("portions") or 1, rec.get("description"), rid),
|
|
||||||
)
|
|
||||||
cur.execute("DELETE FROM food_recipe_ingredients WHERE recipe_id = %s", (rid,))
|
|
||||||
updated += 1
|
|
||||||
else:
|
|
||||||
rid = str(uuid.uuid4())
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO food_recipes
|
|
||||||
(id, profile_id, name_raw, name_normalized, portions, description, source)
|
|
||||||
VALUES (%s,%s,%s,%s,%s,%s,'fddb_list')
|
|
||||||
""",
|
|
||||||
(rid, profile_id, rec["name_raw"], norm, rec.get("portions") or 1, rec.get("description")),
|
|
||||||
)
|
|
||||||
inserted += 1
|
|
||||||
for ing in rec.get("ingredients") or []:
|
|
||||||
inorm = ing.get("source_name_normalized") or normalize_food_name(ing.get("source_name_raw"))
|
|
||||||
if not inorm:
|
|
||||||
continue
|
|
||||||
raw_name = ing.get("source_name_raw") or inorm
|
|
||||||
qty = resolve_quantity(
|
|
||||||
quantity_raw=ing.get("quantity_raw"),
|
|
||||||
quantity_amount=ing.get("quantity_amount"),
|
|
||||||
source_unit=ing.get("source_unit"),
|
|
||||||
quantity_g=ing.get("quantity_g"),
|
|
||||||
)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO food_recipe_ingredients
|
|
||||||
(id, recipe_id, source_name_raw, source_name_normalized,
|
|
||||||
quantity_raw, quantity_g, quantity_amount, source_unit, sort_order)
|
|
||||||
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)
|
|
||||||
""",
|
|
||||||
(
|
|
||||||
str(uuid.uuid4()), rid, raw_name, inorm,
|
|
||||||
qty["quantity_raw"], qty["quantity_g"], qty["quantity_amount"], qty["source_unit"],
|
|
||||||
ing.get("sort_order") or 0,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
ingredients += 1
|
|
||||||
linked, dates = link_recipes_to_items(cur, profile_id)
|
|
||||||
return {
|
|
||||||
"inserted": inserted,
|
|
||||||
"updated": updated,
|
|
||||||
"ingredients": ingredients,
|
|
||||||
"items_linked": linked,
|
|
||||||
"dates_linked": dates,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def link_recipes_to_items(cur, profile_id: str) -> tuple[int, list[str]]:
|
|
||||||
cur.execute(
|
|
||||||
"SELECT id, name_raw, name_normalized FROM food_recipes WHERE profile_id = %s",
|
|
||||||
(profile_id,),
|
|
||||||
)
|
|
||||||
recipes = [dict(r) for r in cur.fetchall()]
|
|
||||||
if not recipes:
|
|
||||||
return 0, []
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id, date::text AS date, source_name_raw, source_name_normalized
|
|
||||||
FROM nutrition_items
|
|
||||||
WHERE profile_id = %s AND food_id IS NULL AND recipe_id IS NULL
|
|
||||||
""",
|
|
||||||
(profile_id,),
|
|
||||||
)
|
|
||||||
by_rid: dict[str, list[str]] = {}
|
|
||||||
dates: set[str] = set()
|
|
||||||
for item in cur.fetchall():
|
|
||||||
rid = next(
|
|
||||||
(
|
|
||||||
str(rec["id"])
|
|
||||||
for rec in recipes
|
|
||||||
if names_match_list(item.get("source_name_raw"), item.get("source_name_normalized"), rec.get("name_normalized"))
|
|
||||||
),
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
if not rid:
|
|
||||||
continue
|
|
||||||
by_rid.setdefault(rid, []).append(str(item["id"]))
|
|
||||||
if item.get("date"):
|
|
||||||
dates.add(str(item["date"]))
|
|
||||||
linked = 0
|
|
||||||
for rid, ids in by_rid.items():
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
UPDATE nutrition_items
|
|
||||||
SET recipe_id = %s, updated_at = NOW()
|
|
||||||
WHERE id = ANY(%s::uuid[])
|
|
||||||
""",
|
|
||||||
(rid, ids),
|
|
||||||
)
|
|
||||||
linked += cur.rowcount or 0
|
|
||||||
return linked, list(dates)
|
|
||||||
|
|
||||||
|
|
||||||
def list_known_ingredient_names(cur, profile_id: str, limit: int = 2000) -> list[dict[str, Any]]:
|
|
||||||
"""Distinct list-ingredient names, with mapping if already learned."""
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT i.source_name_raw, i.source_name_normalized,
|
|
||||||
MIN(m.food_id::text) AS food_id,
|
|
||||||
MIN(f.name_de) AS food_name_de,
|
|
||||||
MIN(f.bls_code) AS bls_code,
|
|
||||||
BOOL_OR(m.id IS NOT NULL) AS mapped
|
|
||||||
FROM food_recipe_ingredients i
|
|
||||||
JOIN food_recipes r ON r.id = i.recipe_id
|
|
||||||
LEFT JOIN food_name_mappings m
|
|
||||||
ON m.profile_id = r.profile_id
|
|
||||||
AND m.source_name_normalized = i.source_name_normalized
|
|
||||||
LEFT JOIN food_catalog f ON f.id = m.food_id
|
|
||||||
WHERE r.profile_id = %s
|
|
||||||
GROUP BY i.source_name_raw, i.source_name_normalized
|
|
||||||
ORDER BY i.source_name_normalized
|
|
||||||
LIMIT %s
|
|
||||||
""",
|
|
||||||
(profile_id, limit),
|
|
||||||
)
|
|
||||||
out = []
|
|
||||||
for row in cur.fetchall():
|
|
||||||
rec = dict(row)
|
|
||||||
rec["mapped"] = bool(rec.get("mapped"))
|
|
||||||
out.append(rec)
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
def list_recipes(cur, profile_id: str, with_ingredients: bool = True) -> list[dict[str, Any]]:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id, name_raw, name_normalized, portions, description, source
|
|
||||||
FROM food_recipes
|
|
||||||
WHERE profile_id = %s
|
|
||||||
ORDER BY name_normalized
|
|
||||||
""",
|
|
||||||
(profile_id,),
|
|
||||||
)
|
|
||||||
recipes = [dict(r) for r in cur.fetchall()]
|
|
||||||
if not recipes:
|
|
||||||
return []
|
|
||||||
ids = [str(r["id"]) for r in recipes]
|
|
||||||
if not with_ingredients:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT recipe_id, COUNT(*) AS n
|
|
||||||
FROM food_recipe_ingredients
|
|
||||||
WHERE recipe_id = ANY(%s::uuid[])
|
|
||||||
GROUP BY recipe_id
|
|
||||||
""",
|
|
||||||
(ids,),
|
|
||||||
)
|
|
||||||
counts = {str(r["recipe_id"]): int(r["n"]) for r in cur.fetchall()}
|
|
||||||
for rec in recipes:
|
|
||||||
rec["id"] = str(rec["id"])
|
|
||||||
rec["ingredient_count"] = counts.get(rec["id"], 0)
|
|
||||||
rec["ingredients"] = []
|
|
||||||
return recipes
|
|
||||||
by_r = _ingredients_by_recipe(cur, ids)
|
|
||||||
for rec in recipes:
|
|
||||||
rec["id"] = str(rec["id"])
|
|
||||||
rec["ingredients"] = by_r.get(rec["id"], [])
|
|
||||||
rec["ingredient_count"] = len(rec["ingredients"])
|
|
||||||
return recipes
|
|
||||||
|
|
||||||
|
|
||||||
def _ingredients_by_recipe(cur, recipe_ids: list[str]) -> dict[str, list]:
|
|
||||||
if not recipe_ids:
|
|
||||||
return {}
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT recipe_id, source_name_raw, source_name_normalized,
|
|
||||||
quantity_raw, quantity_g, quantity_amount, source_unit, sort_order
|
|
||||||
FROM food_recipe_ingredients
|
|
||||||
WHERE recipe_id = ANY(%s::uuid[])
|
|
||||||
ORDER BY sort_order, source_name_raw
|
|
||||||
""",
|
|
||||||
(recipe_ids,),
|
|
||||||
)
|
|
||||||
by_r: dict[str, list] = {str(i): [] for i in recipe_ids}
|
|
||||||
for row in cur.fetchall():
|
|
||||||
by_r.setdefault(str(row["recipe_id"]), []).append(dict(row))
|
|
||||||
return by_r
|
|
||||||
|
|
||||||
|
|
||||||
def get_recipe(cur, profile_id: str, recipe_id: str) -> dict[str, Any] | None:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id, name_raw, name_normalized, portions, description, source
|
|
||||||
FROM food_recipes
|
|
||||||
WHERE profile_id = %s AND id = %s
|
|
||||||
""",
|
|
||||||
(profile_id, recipe_id),
|
|
||||||
)
|
|
||||||
rec = cur.fetchone()
|
|
||||||
if not rec:
|
|
||||||
return None
|
|
||||||
rec = dict(rec)
|
|
||||||
rec["id"] = str(rec["id"])
|
|
||||||
rec["ingredients"] = _ingredients_by_recipe(cur, [rec["id"]]).get(rec["id"], [])
|
|
||||||
rec["ingredient_count"] = len(rec["ingredients"])
|
|
||||||
return rec
|
|
||||||
|
|
||||||
|
|
||||||
def save_recipe(cur, profile_id: str, rec: dict[str, Any], recipe_id: str | None = None) -> dict[str, Any]:
|
|
||||||
name_raw = (rec.get("name_raw") or "").strip()
|
|
||||||
norm = rec.get("name_normalized") or normalize_food_name(name_raw)
|
|
||||||
if not name_raw or not norm:
|
|
||||||
raise ValueError("Rezeptname fehlt")
|
|
||||||
try:
|
|
||||||
portions = float(rec.get("portions") or 1)
|
|
||||||
except (TypeError, ValueError):
|
|
||||||
portions = 1.0
|
|
||||||
if portions <= 0:
|
|
||||||
portions = 1.0
|
|
||||||
description = rec.get("description")
|
|
||||||
source = (rec.get("source") or "manual").strip() or "manual"
|
|
||||||
if recipe_id:
|
|
||||||
cur.execute(
|
|
||||||
"SELECT id FROM food_recipes WHERE id = %s AND profile_id = %s",
|
|
||||||
(recipe_id, profile_id),
|
|
||||||
)
|
|
||||||
if not cur.fetchone():
|
|
||||||
raise KeyError("Rezept nicht gefunden")
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id FROM food_recipes
|
|
||||||
WHERE profile_id = %s AND name_normalized = %s AND id <> %s
|
|
||||||
""",
|
|
||||||
(profile_id, norm, recipe_id),
|
|
||||||
)
|
|
||||||
if cur.fetchone():
|
|
||||||
raise ValueError("Ein Rezept mit diesem Namen existiert bereits")
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
UPDATE food_recipes
|
|
||||||
SET name_raw=%s, name_normalized=%s, portions=%s, description=%s, updated_at=NOW()
|
|
||||||
WHERE id=%s AND profile_id=%s
|
|
||||||
""",
|
|
||||||
(name_raw, norm, portions, description, recipe_id, profile_id),
|
|
||||||
)
|
|
||||||
cur.execute("DELETE FROM food_recipe_ingredients WHERE recipe_id = %s", (recipe_id,))
|
|
||||||
rid = recipe_id
|
|
||||||
else:
|
|
||||||
cur.execute(
|
|
||||||
"SELECT id FROM food_recipes WHERE profile_id = %s AND name_normalized = %s",
|
|
||||||
(profile_id, norm),
|
|
||||||
)
|
|
||||||
existing = cur.fetchone()
|
|
||||||
if existing:
|
|
||||||
return save_recipe(cur, profile_id, rec, str(existing["id"]))
|
|
||||||
rid = str(uuid.uuid4())
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO food_recipes
|
|
||||||
(id, profile_id, name_raw, name_normalized, portions, description, source)
|
|
||||||
VALUES (%s,%s,%s,%s,%s,%s,%s)
|
|
||||||
""",
|
|
||||||
(rid, profile_id, name_raw, norm, portions, description, source),
|
|
||||||
)
|
|
||||||
for i, ing in enumerate(rec.get("ingredients") or []):
|
|
||||||
raw = (ing.get("source_name_raw") or "").strip()
|
|
||||||
inorm = ing.get("source_name_normalized") or normalize_food_name(raw)
|
|
||||||
if not inorm:
|
|
||||||
continue
|
|
||||||
qty = _resolved_ingredient_qty(cur, profile_id, raw or inorm, ing)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO food_recipe_ingredients
|
|
||||||
(id, recipe_id, source_name_raw, source_name_normalized,
|
|
||||||
quantity_raw, quantity_g, quantity_amount, source_unit, sort_order)
|
|
||||||
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)
|
|
||||||
""",
|
|
||||||
(
|
|
||||||
str(uuid.uuid4()), rid, raw or inorm, inorm,
|
|
||||||
qty["quantity_raw"], qty["quantity_g"], qty["quantity_amount"], qty["source_unit"], i,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
linked, dates = link_recipes_to_items(cur, profile_id)
|
|
||||||
saved = get_recipe(cur, profile_id, rid)
|
|
||||||
if not saved:
|
|
||||||
raise ValueError("Rezept konnte nicht gelesen werden")
|
|
||||||
saved["items_linked"] = linked
|
|
||||||
saved["dates_linked"] = dates
|
|
||||||
return saved
|
|
||||||
|
|
||||||
|
|
||||||
def delete_recipe(cur, profile_id: str, recipe_id: str) -> None:
|
|
||||||
cur.execute(
|
|
||||||
"DELETE FROM food_recipes WHERE id = %s AND profile_id = %s RETURNING id",
|
|
||||||
(recipe_id, profile_id),
|
|
||||||
)
|
|
||||||
if not cur.fetchone():
|
|
||||||
raise KeyError("Rezept nicht gefunden")
|
|
||||||
|
|
||||||
|
|
||||||
def apply_recipe_to_items(cur, profile_id: str, source_name_normalized: str, recipe_id: str) -> int:
|
|
||||||
target = normalize_food_name(source_name_normalized) or (source_name_normalized or "")
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id, source_name_raw, source_name_normalized
|
|
||||||
FROM nutrition_items
|
|
||||||
WHERE profile_id = %s AND food_id IS NULL
|
|
||||||
""",
|
|
||||||
(profile_id,),
|
|
||||||
)
|
|
||||||
ids = [
|
|
||||||
str(row["id"])
|
|
||||||
for row in cur.fetchall()
|
|
||||||
if names_match_list(row.get("source_name_raw"), row.get("source_name_normalized"), target)
|
|
||||||
]
|
|
||||||
if not ids:
|
|
||||||
return 0
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
UPDATE nutrition_items
|
|
||||||
SET recipe_id = %s, food_id = NULL, mapping_id = NULL, value_origin = 'fddb', updated_at = NOW()
|
|
||||||
WHERE id = ANY(%s::uuid[])
|
|
||||||
""",
|
|
||||||
(recipe_id, ids),
|
|
||||||
)
|
|
||||||
return cur.rowcount or 0
|
|
||||||
|
|
||||||
|
|
||||||
def mapped_ingredient_quantities(
|
|
||||||
cur, profile_id: str, recipe_id: str, eaten_qty_g: float | None
|
|
||||||
) -> list[dict[str, Any]] | None:
|
|
||||||
cur.execute(
|
|
||||||
"SELECT portions FROM food_recipes WHERE id = %s AND profile_id = %s",
|
|
||||||
(recipe_id, profile_id),
|
|
||||||
)
|
|
||||||
rec = cur.fetchone()
|
|
||||||
if not rec:
|
|
||||||
return None
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT source_name_raw, quantity_g, quantity_raw, quantity_amount, source_unit
|
|
||||||
FROM food_recipe_ingredients
|
|
||||||
WHERE recipe_id = %s
|
|
||||||
ORDER BY sort_order
|
|
||||||
""",
|
|
||||||
(recipe_id,),
|
|
||||||
)
|
|
||||||
ings = cur.fetchall()
|
|
||||||
if not ings:
|
|
||||||
return None
|
|
||||||
resolved_ings = []
|
|
||||||
for ing in ings:
|
|
||||||
mapping = get_food_mapping_with_cursor(cur, ing["source_name_raw"], profile_id)
|
|
||||||
if not mapping:
|
|
||||||
return None
|
|
||||||
qty = resolve_quantity(
|
|
||||||
quantity_raw=ing.get("quantity_raw"),
|
|
||||||
quantity_amount=ing.get("quantity_amount"),
|
|
||||||
source_unit=ing.get("source_unit"),
|
|
||||||
quantity_g=ing.get("quantity_g"),
|
|
||||||
grams_per_unit=mapping.get("grams_per_unit"),
|
|
||||||
)
|
|
||||||
grams = _f(qty.get("quantity_g"))
|
|
||||||
if grams <= 0:
|
|
||||||
return None
|
|
||||||
resolved_ings.append({"food_id": mapping["food_id"], "quantity_g": grams})
|
|
||||||
total_g = sum(i["quantity_g"] for i in resolved_ings)
|
|
||||||
if eaten_qty_g and eaten_qty_g > 0 and total_g > 0:
|
|
||||||
scale = float(eaten_qty_g) / total_g
|
|
||||||
else:
|
|
||||||
portions = float(rec.get("portions") or 1) or 1.0
|
|
||||||
scale = 1.0 / portions
|
|
||||||
return [{**i, "quantity_g": i["quantity_g"] * scale} for i in resolved_ings]
|
|
||||||
|
|
||||||
|
|
||||||
def catalog_macros_for_recipe(cur, profile_id: str, recipe_id: str, eaten_qty_g: float | None) -> dict[str, float] | None:
|
|
||||||
parts = mapped_ingredient_quantities(cur, profile_id, recipe_id, eaten_qty_g)
|
|
||||||
if not parts:
|
|
||||||
return None
|
|
||||||
acc = {"kcal": 0.0, "protein_g": 0.0, "fat_g": 0.0, "carbs_g": 0.0}
|
|
||||||
for part in parts:
|
|
||||||
cat = catalog_macros_for_item(cur, part["food_id"], part["quantity_g"])
|
|
||||||
if not cat:
|
|
||||||
return None
|
|
||||||
for k in acc:
|
|
||||||
acc[k] += cat[k]
|
|
||||||
return acc
|
|
||||||
@@ -1,319 +0,0 @@
|
|||||||
"""In-memory catalog suggestions: Haferflocken → Hafer Flocken, without opening a dialog."""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import re
|
|
||||||
import time
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from data_layer.food_mapping import normalize_food_name, primary_search_query
|
|
||||||
|
|
||||||
_INDEX_TTL_SEC = 300
|
|
||||||
_index_cache: dict[str, tuple[float, dict[str, Any]]] = {}
|
|
||||||
MAX_CANDIDATES = 60
|
|
||||||
MAX_BUCKET = 40
|
|
||||||
|
|
||||||
SPLIT_RE = re.compile(r"[^a-z0-9äöüß]+")
|
|
||||||
COLLAPSE_RE = re.compile(r"[^a-z0-9äöüß]")
|
|
||||||
NUM_RE = re.compile(r"\d+(?:[.,]\d+)?")
|
|
||||||
NUM_TOKEN_RE = re.compile(r"^\d+(?:\.\d+)?$")
|
|
||||||
MIN_SCORE = 45
|
|
||||||
FAT_CLOSE = 0.6
|
|
||||||
|
|
||||||
|
|
||||||
def collapse_key(raw: str | None) -> str:
|
|
||||||
return COLLAPSE_RE.sub("", normalize_food_name(raw))
|
|
||||||
|
|
||||||
|
|
||||||
def number_tokens(raw: str | None) -> list[str]:
|
|
||||||
return [m.replace(",", ".") for m in NUM_RE.findall(normalize_food_name(raw))]
|
|
||||||
|
|
||||||
|
|
||||||
def is_number_token(tok: str) -> bool:
|
|
||||||
return bool(NUM_TOKEN_RE.fullmatch(tok or ""))
|
|
||||||
|
|
||||||
|
|
||||||
def number_search_aliases(tok: str) -> list[str]:
|
|
||||||
"""9.5 and 10 are the same fat class in dairy; keep both searchable."""
|
|
||||||
if not is_number_token(tok):
|
|
||||||
return [tok]
|
|
||||||
aliases = {tok}
|
|
||||||
try:
|
|
||||||
value = float(tok)
|
|
||||||
except ValueError:
|
|
||||||
return [tok]
|
|
||||||
if abs(value - 10) <= FAT_CLOSE or abs(value - 9.5) <= FAT_CLOSE:
|
|
||||||
aliases.update({"9.5", "10"})
|
|
||||||
if value == int(value):
|
|
||||||
aliases.add(str(int(value)))
|
|
||||||
return list(aliases)
|
|
||||||
|
|
||||||
|
|
||||||
def numbers_compatible(query_nums: list[str], name_nums: list[str]) -> bool | None:
|
|
||||||
if not query_nums:
|
|
||||||
return None
|
|
||||||
if not name_nums:
|
|
||||||
return False
|
|
||||||
for qn in query_nums:
|
|
||||||
try:
|
|
||||||
qv = float(qn)
|
|
||||||
except ValueError:
|
|
||||||
continue
|
|
||||||
for nn in name_nums:
|
|
||||||
try:
|
|
||||||
if abs(qv - float(nn)) <= FAT_CLOSE:
|
|
||||||
return True
|
|
||||||
except ValueError:
|
|
||||||
if qn == nn:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def name_tokens(raw: str | None) -> list[str]:
|
|
||||||
text = normalize_food_name(raw)
|
|
||||||
nums = number_tokens(text)
|
|
||||||
words = [t for t in SPLIT_RE.split(NUM_RE.sub(" ", text)) if len(t) >= 2]
|
|
||||||
return words + nums
|
|
||||||
|
|
||||||
|
|
||||||
def score_name_match(query: str, name_de: str, name_en: str | None = None) -> int:
|
|
||||||
qn = normalize_food_name(primary_search_query(query))
|
|
||||||
nn = normalize_food_name(name_de)
|
|
||||||
if not qn or not nn:
|
|
||||||
return 0
|
|
||||||
qc, nc = collapse_key(qn), collapse_key(nn)
|
|
||||||
q_nums, n_nums = number_tokens(qn), number_tokens(nn)
|
|
||||||
fat_ok = numbers_compatible(q_nums, n_nums)
|
|
||||||
if qn == nn:
|
|
||||||
return 100
|
|
||||||
if qc and qc == nc:
|
|
||||||
return 95
|
|
||||||
qt, nt = set(name_tokens(qn)), set(name_tokens(nn))
|
|
||||||
q_words = {t for t in qt if not is_number_token(t)}
|
|
||||||
n_words = {t for t in nt if not is_number_token(t)}
|
|
||||||
words_overlap = bool(q_words and n_words and (q_words & n_words or q_words <= n_words))
|
|
||||||
if fat_ok and words_overlap:
|
|
||||||
return 96
|
|
||||||
if fat_ok is False:
|
|
||||||
base = 0
|
|
||||||
if qc and nc.startswith(qc) and len(qc) >= 4:
|
|
||||||
base = 82
|
|
||||||
elif nc and qc.startswith(nc) and len(nc) >= 4:
|
|
||||||
base = 78
|
|
||||||
elif qt and qt <= nt:
|
|
||||||
base = 72
|
|
||||||
elif nt and nt <= qt:
|
|
||||||
base = 68
|
|
||||||
elif qt and nt:
|
|
||||||
overlap = len(qt & nt) / len(qt | nt)
|
|
||||||
if overlap >= 0.5:
|
|
||||||
base = 50 + int(overlap * 20)
|
|
||||||
elif qc and nc and len(qc) >= 4 and (qc in nc or nc in qc):
|
|
||||||
base = 55 if abs(len(qc) - len(nc)) <= 8 else 46
|
|
||||||
return min(base, 52) if base else 0
|
|
||||||
if qc and nc.startswith(qc) and len(qc) >= 4:
|
|
||||||
return 82
|
|
||||||
if nc and qc.startswith(nc) and len(nc) >= 4:
|
|
||||||
return 78
|
|
||||||
if qt and qt <= nt:
|
|
||||||
return 72
|
|
||||||
if nt and nt <= qt:
|
|
||||||
return 68
|
|
||||||
if qt and nt:
|
|
||||||
overlap = len(qt & nt) / len(qt | nt)
|
|
||||||
if overlap >= 0.5:
|
|
||||||
return 50 + int(overlap * 20)
|
|
||||||
if qc and nc and len(qc) >= 4 and (qc in nc or nc in qc):
|
|
||||||
return 55 if abs(len(qc) - len(nc)) <= 8 else 46
|
|
||||||
en = normalize_food_name(name_en or "")
|
|
||||||
if en and (qn == en or collapse_key(en) == qc):
|
|
||||||
return 88
|
|
||||||
return 0
|
|
||||||
|
|
||||||
|
|
||||||
def _public(food: dict[str, Any], score: int) -> dict[str, Any]:
|
|
||||||
from data_layer.food_visibility import visibility_of
|
|
||||||
|
|
||||||
return {
|
|
||||||
"id": str(food["id"]),
|
|
||||||
"bls_code": food.get("bls_code"),
|
|
||||||
"name_de": food.get("name_de"),
|
|
||||||
"name_en": food.get("name_en"),
|
|
||||||
"catalog_kind": food.get("catalog_kind"),
|
|
||||||
"food_group": food.get("food_group"),
|
|
||||||
"visibility": visibility_of(food),
|
|
||||||
"score": score,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def invalidate_suggest_index(profile_id: str | None = None) -> None:
|
|
||||||
if profile_id is None:
|
|
||||||
_index_cache.clear()
|
|
||||||
return
|
|
||||||
_index_cache.pop(str(profile_id), None)
|
|
||||||
_index_cache.pop("global", None)
|
|
||||||
|
|
||||||
|
|
||||||
def get_suggest_index(cur, profile_id: str | None) -> dict[str, Any]:
|
|
||||||
key = str(profile_id or "global")
|
|
||||||
hit = _index_cache.get(key)
|
|
||||||
if hit and (time.monotonic() - hit[0]) < _INDEX_TTL_SEC:
|
|
||||||
return hit[1]
|
|
||||||
index = load_suggest_index(cur, profile_id)
|
|
||||||
_index_cache[key] = (time.monotonic(), index)
|
|
||||||
return index
|
|
||||||
|
|
||||||
|
|
||||||
def load_suggest_index(cur, profile_id: str | None) -> dict[str, Any]:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id, bls_code, name_de, name_en, catalog_kind, food_group, owner_profile_id
|
|
||||||
FROM food_catalog
|
|
||||||
WHERE is_active = true
|
|
||||||
AND (owner_profile_id IS NULL OR owner_profile_id = %s)
|
|
||||||
""",
|
|
||||||
(profile_id,),
|
|
||||||
)
|
|
||||||
foods = [dict(r) for r in cur.fetchall()]
|
|
||||||
by_collapse: dict[str, list] = {}
|
|
||||||
by_token: dict[str, list] = {}
|
|
||||||
by_prefix: dict[str, list] = {}
|
|
||||||
by_suffix: dict[str, list] = {}
|
|
||||||
for food in foods:
|
|
||||||
food["_c"] = collapse_key(food.get("name_de"))
|
|
||||||
food["_t"] = name_tokens(food.get("name_de"))
|
|
||||||
if food["_c"]:
|
|
||||||
by_collapse.setdefault(food["_c"], []).append(food)
|
|
||||||
by_prefix.setdefault(food["_c"][:4], []).append(food)
|
|
||||||
if len(food["_c"]) >= 4:
|
|
||||||
by_suffix.setdefault(food["_c"][-4:], []).append(food)
|
|
||||||
for tok in food["_t"]:
|
|
||||||
by_token.setdefault(tok, []).append(food)
|
|
||||||
return {
|
|
||||||
"foods": foods,
|
|
||||||
"by_collapse": by_collapse,
|
|
||||||
"by_token": by_token,
|
|
||||||
"by_prefix": by_prefix,
|
|
||||||
"by_suffix": by_suffix,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def _candidate_foods(index: dict[str, Any], query: str) -> list[dict[str, Any]]:
|
|
||||||
qc = collapse_key(query)
|
|
||||||
seen: set[str] = set()
|
|
||||||
out: list[dict[str, Any]] = []
|
|
||||||
|
|
||||||
def add(food: dict[str, Any]) -> None:
|
|
||||||
fid = str(food["id"])
|
|
||||||
if fid in seen:
|
|
||||||
return
|
|
||||||
seen.add(fid)
|
|
||||||
out.append(food)
|
|
||||||
|
|
||||||
if qc:
|
|
||||||
for food in index["by_collapse"].get(qc, []):
|
|
||||||
add(food)
|
|
||||||
if len(qc) >= 4:
|
|
||||||
prefix_hits = index["by_prefix"].get(qc[:4], [])
|
|
||||||
if len(prefix_hits) > MAX_BUCKET:
|
|
||||||
prefix_hits = [
|
|
||||||
food for food in prefix_hits
|
|
||||||
if (food.get("_c") or "").startswith(qc) or qc.startswith(food.get("_c") or "")
|
|
||||||
]
|
|
||||||
for food in prefix_hits[:MAX_CANDIDATES]:
|
|
||||||
add(food)
|
|
||||||
suffix_hits = index["by_suffix"].get(qc[-4:], [])
|
|
||||||
if len(suffix_hits) <= MAX_BUCKET:
|
|
||||||
for food in suffix_hits:
|
|
||||||
add(food)
|
|
||||||
q_words = [t for t in name_tokens(query) if not is_number_token(t)]
|
|
||||||
seen_tok: set[str] = set()
|
|
||||||
for tok in name_tokens(query):
|
|
||||||
probes = number_search_aliases(tok) if is_number_token(tok) else [tok]
|
|
||||||
for probe in probes:
|
|
||||||
if probe in seen_tok:
|
|
||||||
continue
|
|
||||||
seen_tok.add(probe)
|
|
||||||
token_hits = index["by_token"].get(probe, [])
|
|
||||||
if is_number_token(probe) and q_words:
|
|
||||||
token_hits = [
|
|
||||||
food for food in token_hits
|
|
||||||
if set(q_words) & set(food.get("_t") or [])
|
|
||||||
]
|
|
||||||
elif not is_number_token(probe) and len(token_hits) > MAX_BUCKET:
|
|
||||||
token_hits = sorted(token_hits, key=lambda f: len(f.get("name_de") or ""))[:MAX_BUCKET]
|
|
||||||
for food in token_hits:
|
|
||||||
add(food)
|
|
||||||
if len(out) >= MAX_CANDIDATES and not any(is_number_token(t) for t in name_tokens(query)):
|
|
||||||
break
|
|
||||||
return out[:MAX_CANDIDATES] if not number_tokens(query) else out
|
|
||||||
|
|
||||||
|
|
||||||
def _vis_rank(food: dict[str, Any]) -> int:
|
|
||||||
from data_layer.food_visibility import visibility_rank
|
|
||||||
return visibility_rank(food)
|
|
||||||
|
|
||||||
|
|
||||||
def suggest_for_name(index: dict[str, Any], query: str, limit: int = 3) -> dict[str, Any]:
|
|
||||||
q = (query or "").strip()
|
|
||||||
scored: list[tuple[int, int, dict]] = []
|
|
||||||
for food in _candidate_foods(index, q):
|
|
||||||
score = score_name_match(q, food.get("name_de") or "", food.get("name_en"))
|
|
||||||
if score < MIN_SCORE:
|
|
||||||
continue
|
|
||||||
scored.append((score, len(food.get("name_de") or ""), food))
|
|
||||||
scored.sort(key=lambda x: (-x[0], _vis_rank(x[2]), x[1], x[2].get("name_de") or ""))
|
|
||||||
top = [_public(food, score) for score, _nlen, food in scored[: max(limit, 3)]]
|
|
||||||
ambiguous = False
|
|
||||||
if len(top) >= 2 and top[0]["score"] - top[1]["score"] <= 8 and top[1]["score"] >= 60:
|
|
||||||
ambiguous = True
|
|
||||||
elif len(top) >= 2 and top[0]["score"] < 90:
|
|
||||||
ambiguous = True
|
|
||||||
return {
|
|
||||||
"suggestions": top[:limit],
|
|
||||||
"suggestion_count": len(top),
|
|
||||||
"ambiguous": ambiguous,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def attach_suggestions(index: dict[str, Any], rows: list[dict[str, Any]], limit: int = 3) -> list[dict[str, Any]]:
|
|
||||||
for row in rows:
|
|
||||||
q = row.get("source_name_raw") or row.get("source_name_normalized") or ""
|
|
||||||
packed = suggest_for_name(index, q, limit=limit)
|
|
||||||
row["suggestions"] = packed["suggestions"]
|
|
||||||
row["suggestion_count"] = packed["suggestion_count"]
|
|
||||||
row["ambiguous"] = packed["ambiguous"]
|
|
||||||
return rows
|
|
||||||
|
|
||||||
|
|
||||||
def suggest_catalog_foods_ranked(cur, query: str, profile_id: str | None, limit: int = 8) -> list[dict]:
|
|
||||||
q = (query or "").strip()
|
|
||||||
if len(q) < 2:
|
|
||||||
return []
|
|
||||||
index = get_suggest_index(cur, profile_id)
|
|
||||||
packed = suggest_for_name(index, q, limit=max(limit, 8))
|
|
||||||
hits = list(packed["suggestions"])
|
|
||||||
from data_layer.food_mapping import suggest_catalog_foods
|
|
||||||
if number_tokens(q) or not hits:
|
|
||||||
seen = {str(h["id"]) for h in hits}
|
|
||||||
for row in suggest_catalog_foods(cur, q, profile_id, limit=max(limit, 20)):
|
|
||||||
fid = str(row["id"])
|
|
||||||
if fid in seen:
|
|
||||||
continue
|
|
||||||
seen.add(fid)
|
|
||||||
score = score_name_match(q, row.get("name_de") or "", row.get("name_en"))
|
|
||||||
if score < MIN_SCORE and not number_tokens(q):
|
|
||||||
continue
|
|
||||||
hits.append(_public(row, score or 40))
|
|
||||||
hits.sort(key=lambda h: (-int(h.get("score") or 0), _vis_rank(h), h.get("name_de") or ""))
|
|
||||||
return hits[:limit]
|
|
||||||
|
|
||||||
|
|
||||||
def suggest_batch(cur, profile_id: str | None, names: list[str], limit: int = 3) -> dict[str, dict[str, Any]]:
|
|
||||||
index = get_suggest_index(cur, profile_id)
|
|
||||||
out = {}
|
|
||||||
for raw in names[:100]:
|
|
||||||
key = (raw or "").strip()
|
|
||||||
if not key or key in out:
|
|
||||||
continue
|
|
||||||
out[key] = suggest_for_name(index, key, limit=limit)
|
|
||||||
return out
|
|
||||||
@@ -1,121 +0,0 @@
|
|||||||
"""Who may see and map a catalog food.
|
|
||||||
|
|
||||||
Scopes:
|
|
||||||
- personal — ``manual_user`` with ``owner_profile_id`` (only that profile)
|
|
||||||
- global — ``manual_admin`` (all users; no owner)
|
|
||||||
- bls — official BLS (all users; no owner)
|
|
||||||
|
|
||||||
Name-mappings: a *user* mapping may target any food visible to that user.
|
|
||||||
A *global* mapping (``profile_id IS NULL``) may only target global/BLS foods,
|
|
||||||
never another user's private entry.
|
|
||||||
"""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
VIS_PERSONAL = "personal"
|
|
||||||
VIS_GLOBAL = "global"
|
|
||||||
VIS_BLS = "bls"
|
|
||||||
|
|
||||||
SQL_VISIBLE_TO_PROFILE = "(owner_profile_id IS NULL OR owner_profile_id = %s)"
|
|
||||||
SQL_GLOBAL_ONLY = "owner_profile_id IS NULL"
|
|
||||||
|
|
||||||
_KIND_VIS = {
|
|
||||||
"official_bls": VIS_BLS,
|
|
||||||
"manual_admin": VIS_GLOBAL,
|
|
||||||
"manual_user": VIS_PERSONAL,
|
|
||||||
}
|
|
||||||
|
|
||||||
_VIS_RANK = {VIS_PERSONAL: 0, VIS_GLOBAL: 1, VIS_BLS: 2}
|
|
||||||
|
|
||||||
|
|
||||||
def same_id(a, b) -> bool:
|
|
||||||
return str(a or "").replace("-", "").lower() == str(b or "").replace("-", "").lower()
|
|
||||||
|
|
||||||
|
|
||||||
def visibility_of(food: dict[str, Any] | None) -> str:
|
|
||||||
if not food:
|
|
||||||
return VIS_BLS
|
|
||||||
kind = food.get("catalog_kind")
|
|
||||||
if kind in _KIND_VIS:
|
|
||||||
return _KIND_VIS[kind]
|
|
||||||
if food.get("owner_profile_id"):
|
|
||||||
return VIS_PERSONAL
|
|
||||||
return VIS_GLOBAL
|
|
||||||
|
|
||||||
|
|
||||||
def visibility_rank(food: dict[str, Any] | None) -> int:
|
|
||||||
return _VIS_RANK.get(visibility_of(food), 3)
|
|
||||||
|
|
||||||
|
|
||||||
def is_active(food: dict[str, Any] | None) -> bool:
|
|
||||||
if not food:
|
|
||||||
return False
|
|
||||||
if "is_active" not in food:
|
|
||||||
return True
|
|
||||||
return bool(food.get("is_active"))
|
|
||||||
|
|
||||||
|
|
||||||
def is_visible_to(food: dict[str, Any] | None, profile_id: str | None) -> bool:
|
|
||||||
if not is_active(food):
|
|
||||||
return False
|
|
||||||
owner = food.get("owner_profile_id")
|
|
||||||
if owner is None:
|
|
||||||
return True
|
|
||||||
return profile_id is not None and same_id(owner, profile_id)
|
|
||||||
|
|
||||||
|
|
||||||
def is_global_mapping_target(food: dict[str, Any] | None) -> bool:
|
|
||||||
return is_active(food) and food.get("owner_profile_id") is None
|
|
||||||
|
|
||||||
|
|
||||||
def load_catalog_food(cur, food_id: str) -> dict[str, Any] | None:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id, name_de, catalog_kind, owner_profile_id, is_active, bls_code
|
|
||||||
FROM food_catalog
|
|
||||||
WHERE id = %s
|
|
||||||
""",
|
|
||||||
(food_id,),
|
|
||||||
)
|
|
||||||
row = cur.fetchone()
|
|
||||||
return dict(row) if row else None
|
|
||||||
|
|
||||||
|
|
||||||
def require_visible_food(cur, food_id: str, profile_id: str) -> dict[str, Any]:
|
|
||||||
food = load_catalog_food(cur, food_id)
|
|
||||||
if not is_visible_to(food, profile_id):
|
|
||||||
raise LookupError("Lebensmittel nicht gefunden")
|
|
||||||
return food
|
|
||||||
|
|
||||||
|
|
||||||
def require_global_mapping_target(cur, food_id: str) -> dict[str, Any]:
|
|
||||||
food = load_catalog_food(cur, food_id)
|
|
||||||
if not food or not is_active(food):
|
|
||||||
raise LookupError("Lebensmittel nicht gefunden")
|
|
||||||
if not is_global_mapping_target(food):
|
|
||||||
raise PermissionError("Globales Mapping darf nur auf zentrale oder BLS-Lebensmittel zeigen")
|
|
||||||
return food
|
|
||||||
|
|
||||||
|
|
||||||
def require_mapping_target(cur, food_id: str, mapping_profile_id: str | None) -> dict[str, Any]:
|
|
||||||
if mapping_profile_id:
|
|
||||||
return require_visible_food(cur, food_id, mapping_profile_id)
|
|
||||||
return require_global_mapping_target(cur, food_id)
|
|
||||||
|
|
||||||
|
|
||||||
def can_edit_food(food: dict[str, Any] | None, profile_id: str | None, *, is_admin: bool = False) -> bool:
|
|
||||||
"""Owner ändert persönlich; Admin ändert zentral (und im Admin-Katalog auch fremde Manuals)."""
|
|
||||||
if not is_active(food):
|
|
||||||
return False
|
|
||||||
kind = food.get("catalog_kind")
|
|
||||||
if kind == "manual_user":
|
|
||||||
return same_id(food.get("owner_profile_id"), profile_id) or bool(is_admin)
|
|
||||||
if kind == "manual_admin":
|
|
||||||
return bool(is_admin)
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def attach_visibility(row: dict[str, Any]) -> dict[str, Any]:
|
|
||||||
row["visibility"] = visibility_of(row)
|
|
||||||
return row
|
|
||||||
@@ -9,17 +9,10 @@ from __future__ import annotations
|
|||||||
from typing import Any, Dict, List, Optional
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
from data_layer.body_viz import get_body_history_viz_bundle
|
from data_layer.body_viz import get_body_history_viz_bundle
|
||||||
from data_layer.correlation_chart_payloads import (
|
|
||||||
build_lbm_protein_correlation_chart_payload,
|
|
||||||
build_load_vitals_correlation_chart_payload,
|
|
||||||
build_recovery_performance_chart_payload,
|
|
||||||
build_weight_energy_correlation_chart_payload,
|
|
||||||
)
|
|
||||||
from data_layer.correlations import calculate_lag_correlation, calculate_top_drivers
|
from data_layer.correlations import calculate_lag_correlation, calculate_top_drivers
|
||||||
from data_layer.fitness_viz import get_fitness_dashboard_viz_bundle
|
from data_layer.fitness_viz import get_fitness_dashboard_viz_bundle
|
||||||
from data_layer.nutrition_viz import get_nutrition_history_viz_bundle
|
from data_layer.nutrition_viz import get_nutrition_history_viz_bundle
|
||||||
from data_layer.recovery_viz import get_recovery_dashboard_viz_bundle
|
from data_layer.recovery_viz import get_recovery_dashboard_viz_bundle
|
||||||
from data_layer.utils import safe_float
|
|
||||||
|
|
||||||
|
|
||||||
def _take_kpis(tiles: Any, max_n: int = 4) -> List[Dict[str, Any]]:
|
def _take_kpis(tiles: Any, max_n: int = 4) -> List[Dict[str, Any]]:
|
||||||
@@ -97,9 +90,11 @@ def get_history_overview_viz_bundle(profile_id: str, days: int) -> Dict[str, Any
|
|||||||
c3_rhr = calculate_lag_correlation(profile_id, "load", "rhr", 14)
|
c3_rhr = calculate_lag_correlation(profile_id, "load", "rhr", 14)
|
||||||
c3 = None
|
c3 = None
|
||||||
if c3_hrv and c3_rhr:
|
if c3_hrv and c3_rhr:
|
||||||
a1 = abs(safe_float(c3_hrv.get("correlation"), 0.0))
|
c3 = (
|
||||||
a2 = abs(safe_float(c3_rhr.get("correlation"), 0.0))
|
c3_hrv
|
||||||
c3 = c3_hrv if a1 >= a2 else c3_rhr
|
if abs(float(c3_hrv.get("correlation") or 0)) >= abs(float(c3_rhr.get("correlation") or 0))
|
||||||
|
else c3_rhr
|
||||||
|
)
|
||||||
if c3 is c3_hrv:
|
if c3 is c3_hrv:
|
||||||
c3 = dict(c3)
|
c3 = dict(c3)
|
||||||
c3["metric"] = "HRV"
|
c3["metric"] = "HRV"
|
||||||
@@ -187,12 +182,6 @@ def get_history_overview_viz_bundle(profile_id: str, days: int) -> Dict[str, Any
|
|||||||
"drivers": drv_list[:8],
|
"drivers": drv_list[:8],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"chart_payloads": {
|
|
||||||
"c1_weight_energy": build_weight_energy_correlation_chart_payload(profile_id, 14),
|
|
||||||
"c2_protein_lbm": build_lbm_protein_correlation_chart_payload(profile_id, 14),
|
|
||||||
"c3_load_vitals": build_load_vitals_correlation_chart_payload(profile_id, 14),
|
|
||||||
"c4_recovery_performance": build_recovery_performance_chart_payload(profile_id),
|
|
||||||
},
|
|
||||||
"meta": {
|
"meta": {
|
||||||
"layer_1": "composed_metrics",
|
"layer_1": "composed_metrics",
|
||||||
"layer_2b": "history_overview_viz",
|
"layer_2b": "history_overview_viz",
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ from __future__ import annotations
|
|||||||
from typing import Any, Dict, List, Optional
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
from db import get_db, get_cursor, r2d
|
from db import get_db, get_cursor, r2d
|
||||||
from caliper_composition import as_date, compute_lean_fat_kg, nearest_weight_kg_from_map
|
from caliper_composition import compute_lean_fat_kg, nearest_weight_kg_from_map
|
||||||
|
|
||||||
|
|
||||||
def build_merged_daily_nutrition_body_rows(profile_id: str) -> List[Dict[str, Any]]:
|
def build_merged_daily_nutrition_body_rows(profile_id: str) -> List[Dict[str, Any]]:
|
||||||
@@ -20,42 +20,21 @@ def build_merged_daily_nutrition_body_rows(profile_id: str) -> List[Dict[str, An
|
|||||||
with get_db() as conn:
|
with get_db() as conn:
|
||||||
cur = get_cursor(conn)
|
cur = get_cursor(conn)
|
||||||
cur.execute("SELECT * FROM nutrition_log WHERE profile_id=%s ORDER BY date", (profile_id,))
|
cur.execute("SELECT * FROM nutrition_log WHERE profile_id=%s ORDER BY date", (profile_id,))
|
||||||
nutr: Dict[Any, Dict[str, Any]] = {}
|
nutr = {r["date"]: r2d(r) for r in cur.fetchall()}
|
||||||
for r in cur.fetchall():
|
|
||||||
rd = r2d(r)
|
|
||||||
dk = as_date(rd.get("date"))
|
|
||||||
if dk is not None:
|
|
||||||
nutr[dk] = rd
|
|
||||||
cur.execute("SELECT date, weight FROM weight_log WHERE profile_id=%s ORDER BY date", (profile_id,))
|
cur.execute("SELECT date, weight FROM weight_log WHERE profile_id=%s ORDER BY date", (profile_id,))
|
||||||
wlog: Dict[Any, Any] = {}
|
wlog = {r["date"]: r["weight"] for r in cur.fetchall()}
|
||||||
for r in cur.fetchall():
|
|
||||||
rd = r2d(r)
|
|
||||||
dk = as_date(rd.get("date"))
|
|
||||||
if dk is not None:
|
|
||||||
wlog[dk] = rd["weight"]
|
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"SELECT date, lean_mass, body_fat_pct FROM caliper_log WHERE profile_id=%s ORDER BY date",
|
"SELECT date, lean_mass, body_fat_pct FROM caliper_log WHERE profile_id=%s ORDER BY date",
|
||||||
(profile_id,),
|
(profile_id,),
|
||||||
)
|
)
|
||||||
cals = [r2d(r) for r in cur.fetchall()]
|
cals = sorted([r2d(r) for r in cur.fetchall()], key=lambda x: x["date"])
|
||||||
cals = sorted(
|
|
||||||
[c for c in cals if as_date(c.get("date")) is not None],
|
|
||||||
key=lambda x: as_date(x["date"]),
|
|
||||||
)
|
|
||||||
|
|
||||||
# Alle Keys sind datetime.date — vermeidet TypeError bei Vergleichen (str vs date)
|
all_dates = sorted(set(list(nutr.keys()) + list(wlog.keys())))
|
||||||
all_dates = sorted(set(nutr.keys()) | set(wlog.keys()))
|
|
||||||
mi = 0
|
mi = 0
|
||||||
last_cal: Dict[str, Any] = {}
|
last_cal: Dict[str, Any] = {}
|
||||||
cal_by_date: Dict[Any, Dict[str, Any]] = {}
|
cal_by_date: Dict[Any, Dict[str, Any]] = {}
|
||||||
for d in all_dates:
|
for d in all_dates:
|
||||||
while mi < len(cals):
|
while mi < len(cals) and cals[mi]["date"] <= d:
|
||||||
cd = as_date(cals[mi].get("date"))
|
|
||||||
if cd is None:
|
|
||||||
mi += 1
|
|
||||||
continue
|
|
||||||
if cd > d:
|
|
||||||
break
|
|
||||||
last_cal = cals[mi]
|
last_cal = cals[mi]
|
||||||
mi += 1
|
mi += 1
|
||||||
if last_cal:
|
if last_cal:
|
||||||
|
|||||||
@@ -1,404 +0,0 @@
|
|||||||
"""
|
|
||||||
Chart.js-kompatible Payloads für Ernährungs-Charts (E1, E2, E4).
|
|
||||||
|
|
||||||
Gleiche Logik wie ``routers/charts.py`` — hier zentral, damit ``nutrition_viz``
|
|
||||||
und die API dieselbe Berechnung nutzen (Phase C, Issue 53).
|
|
||||||
"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from datetime import datetime, timedelta
|
|
||||||
from typing import Any, Dict
|
|
||||||
|
|
||||||
from db import get_db, get_cursor
|
|
||||||
from data_layer.nutrition_metrics import (
|
|
||||||
get_energy_balance_data,
|
|
||||||
get_protein_adequacy_data,
|
|
||||||
get_protein_targets_data,
|
|
||||||
)
|
|
||||||
from data_layer.utils import calculate_confidence, safe_float, serialize_dates
|
|
||||||
|
|
||||||
|
|
||||||
def build_energy_balance_chart_payload(profile_id: str, days: int) -> Dict[str, Any]:
|
|
||||||
"""E1 Energiebilanz — identisch zu GET /api/charts/energy-balance."""
|
|
||||||
balance_meta = get_energy_balance_data(profile_id, days)
|
|
||||||
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cutoff = (datetime.now() - timedelta(days=days)).strftime("%Y-%m-%d")
|
|
||||||
|
|
||||||
cur.execute(
|
|
||||||
"""SELECT date, SUM(kcal)::float AS kcal
|
|
||||||
FROM nutrition_log
|
|
||||||
WHERE profile_id=%s AND date >= %s AND kcal IS NOT NULL
|
|
||||||
GROUP BY date
|
|
||||||
ORDER BY date""",
|
|
||||||
(profile_id, cutoff),
|
|
||||||
)
|
|
||||||
rows = cur.fetchall()
|
|
||||||
|
|
||||||
if not rows or len(rows) < 3:
|
|
||||||
return {
|
|
||||||
"chart_type": "line",
|
|
||||||
"data": {"labels": [], "datasets": []},
|
|
||||||
"metadata": {
|
|
||||||
"confidence": "insufficient",
|
|
||||||
"data_points": len(rows) if rows else 0,
|
|
||||||
"message": "Nicht genug Ernährungsdaten (min. 3 Tage)",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
estimated_tdee = balance_meta.get("estimated_tdee") or 0
|
|
||||||
if estimated_tdee <= 0:
|
|
||||||
return {
|
|
||||||
"chart_type": "line",
|
|
||||||
"data": {"labels": [], "datasets": []},
|
|
||||||
"metadata": {
|
|
||||||
"confidence": "insufficient",
|
|
||||||
"data_points": len(rows),
|
|
||||||
"message": "Kein Gewicht für TDEE-Schätzung (weight_log erforderlich)",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
labels = []
|
|
||||||
daily_values = []
|
|
||||||
avg_7d = []
|
|
||||||
avg_14d = []
|
|
||||||
|
|
||||||
for i, row in enumerate(rows):
|
|
||||||
labels.append(row["date"].isoformat())
|
|
||||||
daily_values.append(safe_float(row["kcal"]))
|
|
||||||
|
|
||||||
start_7d = max(0, i - 6)
|
|
||||||
window_7d = [safe_float(rows[j]["kcal"]) for j in range(start_7d, i + 1)]
|
|
||||||
avg_7d.append(round(sum(window_7d) / len(window_7d), 1) if window_7d else None)
|
|
||||||
|
|
||||||
start_14d = max(0, i - 13)
|
|
||||||
window_14d = [safe_float(rows[j]["kcal"]) for j in range(start_14d, i + 1)]
|
|
||||||
avg_14d.append(round(sum(window_14d) / len(window_14d), 1) if window_14d else None)
|
|
||||||
|
|
||||||
avg_intake = float(
|
|
||||||
balance_meta.get("avg_intake")
|
|
||||||
or (sum(daily_values) / len(daily_values) if daily_values else 0)
|
|
||||||
)
|
|
||||||
energy_balance = float(
|
|
||||||
balance_meta.get("energy_balance") or (avg_intake - estimated_tdee)
|
|
||||||
)
|
|
||||||
balance_status = balance_meta.get("status") or (
|
|
||||||
"deficit"
|
|
||||||
if energy_balance < -200
|
|
||||||
else "surplus"
|
|
||||||
if energy_balance > 200
|
|
||||||
else "maintenance"
|
|
||||||
)
|
|
||||||
|
|
||||||
datasets = [
|
|
||||||
{
|
|
||||||
"label": "Kalorien (täglich)",
|
|
||||||
"data": daily_values,
|
|
||||||
"borderColor": "#1D9E7599",
|
|
||||||
"backgroundColor": "rgba(29, 158, 117, 0.1)",
|
|
||||||
"borderWidth": 1.5,
|
|
||||||
"tension": 0.2,
|
|
||||||
"fill": False,
|
|
||||||
"pointRadius": 2,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"label": "Ø 7 Tage",
|
|
||||||
"data": avg_7d,
|
|
||||||
"borderColor": "#1D9E75",
|
|
||||||
"borderWidth": 2.5,
|
|
||||||
"tension": 0.3,
|
|
||||||
"fill": False,
|
|
||||||
"pointRadius": 0,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"label": "Ø 14 Tage",
|
|
||||||
"data": avg_14d,
|
|
||||||
"borderColor": "#085041",
|
|
||||||
"borderWidth": 2,
|
|
||||||
"tension": 0.3,
|
|
||||||
"fill": False,
|
|
||||||
"pointRadius": 0,
|
|
||||||
"borderDash": [6, 3],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"label": "TDEE (geschätzt)",
|
|
||||||
"data": [estimated_tdee] * len(labels),
|
|
||||||
"borderColor": "#888",
|
|
||||||
"borderWidth": 1,
|
|
||||||
"borderDash": [5, 5],
|
|
||||||
"fill": False,
|
|
||||||
"pointRadius": 0,
|
|
||||||
},
|
|
||||||
]
|
|
||||||
|
|
||||||
confidence = balance_meta.get("confidence") or "low"
|
|
||||||
|
|
||||||
return {
|
|
||||||
"chart_type": "line",
|
|
||||||
"data": {"labels": labels, "datasets": datasets},
|
|
||||||
"metadata": serialize_dates(
|
|
||||||
{
|
|
||||||
"confidence": confidence,
|
|
||||||
"data_points": len(rows),
|
|
||||||
"avg_kcal": round(avg_intake, 1),
|
|
||||||
"estimated_tdee": estimated_tdee,
|
|
||||||
"energy_balance": round(energy_balance, 1),
|
|
||||||
"balance_status": balance_status,
|
|
||||||
"first_date": rows[0]["date"],
|
|
||||||
"last_date": rows[-1]["date"],
|
|
||||||
}
|
|
||||||
),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def build_protein_adequacy_chart_payload(profile_id: str, days: int) -> Dict[str, Any]:
|
|
||||||
"""E2 Protein Adequacy — identisch zu GET /api/charts/protein-adequacy."""
|
|
||||||
targets = get_protein_targets_data(profile_id)
|
|
||||||
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cutoff = (datetime.now() - timedelta(days=days)).strftime("%Y-%m-%d")
|
|
||||||
|
|
||||||
cur.execute(
|
|
||||||
"""SELECT date, SUM(protein_g)::float AS protein_g
|
|
||||||
FROM nutrition_log
|
|
||||||
WHERE profile_id=%s AND date >= %s AND protein_g IS NOT NULL
|
|
||||||
GROUP BY date
|
|
||||||
ORDER BY date""",
|
|
||||||
(profile_id, cutoff),
|
|
||||||
)
|
|
||||||
rows = cur.fetchall()
|
|
||||||
|
|
||||||
if not rows or len(rows) < 3:
|
|
||||||
return {
|
|
||||||
"chart_type": "line",
|
|
||||||
"data": {"labels": [], "datasets": []},
|
|
||||||
"metadata": {
|
|
||||||
"confidence": "insufficient",
|
|
||||||
"data_points": len(rows) if rows else 0,
|
|
||||||
"message": "Nicht genug Protein-Daten (min. 3 Tage)",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
labels = []
|
|
||||||
daily_values = []
|
|
||||||
avg_7d = []
|
|
||||||
avg_28d = []
|
|
||||||
|
|
||||||
for i, row in enumerate(rows):
|
|
||||||
labels.append(row["date"].isoformat())
|
|
||||||
daily_values.append(safe_float(row["protein_g"]))
|
|
||||||
|
|
||||||
start_7d = max(0, i - 6)
|
|
||||||
window_7d = [safe_float(rows[j]["protein_g"]) for j in range(start_7d, i + 1)]
|
|
||||||
avg_7d.append(round(sum(window_7d) / len(window_7d), 1) if window_7d else None)
|
|
||||||
|
|
||||||
start_28d = max(0, i - 27)
|
|
||||||
window_28d = [safe_float(rows[j]["protein_g"]) for j in range(start_28d, i + 1)]
|
|
||||||
avg_28d.append(round(sum(window_28d) / len(window_28d), 1) if window_28d else None)
|
|
||||||
|
|
||||||
target_low = targets["protein_target_low"]
|
|
||||||
target_high = targets["protein_target_high"]
|
|
||||||
|
|
||||||
datasets = [
|
|
||||||
{
|
|
||||||
"label": "Protein (täglich)",
|
|
||||||
"data": daily_values,
|
|
||||||
"borderColor": "#1D9E7599",
|
|
||||||
"backgroundColor": "rgba(29, 158, 117, 0.1)",
|
|
||||||
"borderWidth": 1.5,
|
|
||||||
"tension": 0.2,
|
|
||||||
"fill": False,
|
|
||||||
"pointRadius": 2,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"label": "Ø 7 Tage",
|
|
||||||
"data": avg_7d,
|
|
||||||
"borderColor": "#1D9E75",
|
|
||||||
"borderWidth": 2.5,
|
|
||||||
"tension": 0.3,
|
|
||||||
"fill": False,
|
|
||||||
"pointRadius": 0,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"label": "Ø 28 Tage",
|
|
||||||
"data": avg_28d,
|
|
||||||
"borderColor": "#085041",
|
|
||||||
"borderWidth": 2,
|
|
||||||
"tension": 0.3,
|
|
||||||
"fill": False,
|
|
||||||
"pointRadius": 0,
|
|
||||||
"borderDash": [6, 3],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"label": "Ziel Min",
|
|
||||||
"data": [target_low] * len(labels),
|
|
||||||
"borderColor": "#888",
|
|
||||||
"borderWidth": 1,
|
|
||||||
"borderDash": [5, 5],
|
|
||||||
"fill": False,
|
|
||||||
"pointRadius": 0,
|
|
||||||
},
|
|
||||||
]
|
|
||||||
|
|
||||||
datasets.append(
|
|
||||||
{
|
|
||||||
"label": "Ziel Max",
|
|
||||||
"data": [target_high] * len(labels),
|
|
||||||
"borderColor": "#888",
|
|
||||||
"borderWidth": 1,
|
|
||||||
"borderDash": [5, 5],
|
|
||||||
"fill": False,
|
|
||||||
"pointRadius": 0,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
confidence = calculate_confidence(len(rows), days, "general")
|
|
||||||
|
|
||||||
days_in_target = sum(1 for v in daily_values if target_low <= v <= target_high)
|
|
||||||
|
|
||||||
return {
|
|
||||||
"chart_type": "line",
|
|
||||||
"data": {"labels": labels, "datasets": datasets},
|
|
||||||
"metadata": serialize_dates(
|
|
||||||
{
|
|
||||||
"confidence": confidence,
|
|
||||||
"data_points": len(rows),
|
|
||||||
"target_low": round(target_low, 1),
|
|
||||||
"target_high": round(target_high, 1),
|
|
||||||
"days_in_target": days_in_target,
|
|
||||||
"target_compliance_pct": round(
|
|
||||||
days_in_target / len(daily_values) * 100, 1
|
|
||||||
)
|
|
||||||
if daily_values
|
|
||||||
else 0,
|
|
||||||
"first_date": rows[0]["date"],
|
|
||||||
"last_date": rows[-1]["date"],
|
|
||||||
}
|
|
||||||
),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def build_nutrition_adherence_score_payload(profile_id: str, days: int) -> Dict[str, Any]:
|
|
||||||
"""E4 Adhärenz — identisch zu GET /api/charts/nutrition-adherence-score."""
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute("SELECT goal_mode FROM profiles WHERE id = %s", (profile_id,))
|
|
||||||
profile_row = cur.fetchone()
|
|
||||||
goal_mode = (
|
|
||||||
profile_row["goal_mode"]
|
|
||||||
if profile_row and profile_row["goal_mode"]
|
|
||||||
else "health"
|
|
||||||
)
|
|
||||||
|
|
||||||
cutoff = (datetime.now() - timedelta(days=days)).strftime("%Y-%m-%d")
|
|
||||||
|
|
||||||
cur.execute(
|
|
||||||
"""WITH daily AS (
|
|
||||||
SELECT date,
|
|
||||||
COALESCE(SUM(kcal), 0)::float AS dk,
|
|
||||||
COALESCE(SUM(protein_g), 0)::float AS dp,
|
|
||||||
COALESCE(SUM(carbs_g), 0)::float AS dc,
|
|
||||||
COALESCE(SUM(fat_g), 0)::float AS df FROM nutrition_log
|
|
||||||
WHERE profile_id=%s AND date >= %s AND kcal IS NOT NULL
|
|
||||||
GROUP BY date
|
|
||||||
)
|
|
||||||
SELECT COUNT(*)::int AS cnt,
|
|
||||||
AVG(dk) AS avg_kcal,
|
|
||||||
STDDEV(dk) AS std_kcal,
|
|
||||||
AVG(dp) AS avg_protein,
|
|
||||||
AVG(dc) AS avg_carbs,
|
|
||||||
AVG(df) AS avg_fat
|
|
||||||
FROM daily""",
|
|
||||||
(profile_id, cutoff),
|
|
||||||
)
|
|
||||||
stats = cur.fetchone()
|
|
||||||
|
|
||||||
if not stats or stats["cnt"] < 7:
|
|
||||||
return {
|
|
||||||
"score": 0,
|
|
||||||
"components": {},
|
|
||||||
"metadata": {
|
|
||||||
"confidence": "insufficient",
|
|
||||||
"message": "Nicht genug Daten (min. 7 Tage)",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
protein_data = get_protein_adequacy_data(profile_id, days)
|
|
||||||
|
|
||||||
calorie_adherence = 70.0
|
|
||||||
protein_adequacy_pct = protein_data.get("adequacy_score", 0)
|
|
||||||
protein_adherence = min(100, protein_adequacy_pct)
|
|
||||||
|
|
||||||
kcal_cv = (
|
|
||||||
(safe_float(stats["std_kcal"]) / safe_float(stats["avg_kcal"]) * 100)
|
|
||||||
if safe_float(stats["avg_kcal"]) > 0
|
|
||||||
else 100
|
|
||||||
)
|
|
||||||
intake_consistency = max(0, 100 - kcal_cv)
|
|
||||||
|
|
||||||
food_quality = 60.0
|
|
||||||
|
|
||||||
if goal_mode == "weight_loss":
|
|
||||||
weights = {
|
|
||||||
"calorie": 0.35,
|
|
||||||
"protein": 0.25,
|
|
||||||
"consistency": 0.20,
|
|
||||||
"quality": 0.20,
|
|
||||||
}
|
|
||||||
elif goal_mode == "strength":
|
|
||||||
weights = {
|
|
||||||
"calorie": 0.25,
|
|
||||||
"protein": 0.35,
|
|
||||||
"consistency": 0.20,
|
|
||||||
"quality": 0.20,
|
|
||||||
}
|
|
||||||
elif goal_mode == "endurance":
|
|
||||||
weights = {
|
|
||||||
"calorie": 0.30,
|
|
||||||
"protein": 0.20,
|
|
||||||
"consistency": 0.20,
|
|
||||||
"quality": 0.30,
|
|
||||||
}
|
|
||||||
else:
|
|
||||||
weights = {
|
|
||||||
"calorie": 0.25,
|
|
||||||
"protein": 0.25,
|
|
||||||
"consistency": 0.25,
|
|
||||||
"quality": 0.25,
|
|
||||||
}
|
|
||||||
|
|
||||||
final_score = (
|
|
||||||
calorie_adherence * weights["calorie"]
|
|
||||||
+ protein_adherence * weights["protein"]
|
|
||||||
+ intake_consistency * weights["consistency"]
|
|
||||||
+ food_quality * weights["quality"]
|
|
||||||
)
|
|
||||||
|
|
||||||
components = {
|
|
||||||
"calorie_adherence": round(calorie_adherence, 1),
|
|
||||||
"protein_adherence": round(protein_adherence, 1),
|
|
||||||
"intake_consistency": round(intake_consistency, 1),
|
|
||||||
"food_quality": round(food_quality, 1),
|
|
||||||
}
|
|
||||||
|
|
||||||
weak_areas = [k for k, v in components.items() if v < 60]
|
|
||||||
if weak_areas:
|
|
||||||
recommendation = f"Verbesserungspotenzial: {', '.join(weak_areas)}"
|
|
||||||
else:
|
|
||||||
recommendation = "Gute Adhärenz, weiter so!"
|
|
||||||
|
|
||||||
return {
|
|
||||||
"score": round(final_score, 1),
|
|
||||||
"components": components,
|
|
||||||
"goal_mode": goal_mode,
|
|
||||||
"weights": weights,
|
|
||||||
"recommendation": recommendation,
|
|
||||||
"metadata": {
|
|
||||||
"confidence": calculate_confidence(stats["cnt"], days, "general"),
|
|
||||||
"data_points": stats["cnt"],
|
|
||||||
"days_analyzed": days,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
@@ -1,590 +0,0 @@
|
|||||||
"""Nutrition diary items, three macro sums, import policy, attribute resolve."""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import logging
|
|
||||||
import threading
|
|
||||||
import uuid
|
|
||||||
from datetime import date, datetime
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
from data_layer.food_mapping import (
|
|
||||||
get_food_mapping_with_cursor,
|
|
||||||
normalize_food_name,
|
|
||||||
parse_quantity_g,
|
|
||||||
)
|
|
||||||
|
|
||||||
MACRO_ATTR_KEYS = {
|
|
||||||
"kcal": "ENERCC",
|
|
||||||
"protein_g": "PROT625",
|
|
||||||
"fat_g": "FAT",
|
|
||||||
"carbs_g": "CHO",
|
|
||||||
}
|
|
||||||
|
|
||||||
POLICIES = frozenset({"prompt", "overwrite_catalog", "overwrite_fddb", "keep_existing"})
|
|
||||||
|
|
||||||
|
|
||||||
def _f(v: Any) -> float:
|
|
||||||
if v is None or v == "":
|
|
||||||
return 0.0
|
|
||||||
try:
|
|
||||||
return float(v)
|
|
||||||
except (TypeError, ValueError):
|
|
||||||
return 0.0
|
|
||||||
|
|
||||||
|
|
||||||
def _round_macros(d: dict[str, float]) -> dict[str, float]:
|
|
||||||
return {
|
|
||||||
"kcal": round(_f(d.get("kcal")), 1),
|
|
||||||
"protein_g": round(_f(d.get("protein_g")), 1),
|
|
||||||
"fat_g": round(_f(d.get("fat_g")), 1),
|
|
||||||
"carbs_g": round(_f(d.get("carbs_g")), 1),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def macros_differ(a: dict[str, float], b: dict[str, float]) -> bool:
|
|
||||||
aa, bb = _round_macros(a), _round_macros(b)
|
|
||||||
return any(aa[k] != bb[k] for k in aa)
|
|
||||||
|
|
||||||
|
|
||||||
def get_import_policy(cur, profile_id: str) -> str:
|
|
||||||
cur.execute(
|
|
||||||
"SELECT nutrition_import_conflict_policy FROM profiles WHERE id = %s",
|
|
||||||
(profile_id,),
|
|
||||||
)
|
|
||||||
row = cur.fetchone()
|
|
||||||
if not row:
|
|
||||||
return "prompt"
|
|
||||||
pol = row.get("nutrition_import_conflict_policy") or "prompt"
|
|
||||||
return pol if pol in POLICIES else "prompt"
|
|
||||||
|
|
||||||
|
|
||||||
def catalog_macros_for_item(cur, food_id: str | None, quantity_g: float | None) -> dict[str, float] | None:
|
|
||||||
if not food_id or quantity_g is None or quantity_g <= 0:
|
|
||||||
return None
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT a.attr_key, v.value_num, v.is_trace
|
|
||||||
FROM food_attribute_values v
|
|
||||||
JOIN food_attributes a ON a.id = v.attribute_id
|
|
||||||
WHERE v.food_id = %s AND a.attr_key = ANY(%s) AND a.data_type = 'num_per_100g'
|
|
||||||
""",
|
|
||||||
(food_id, list(MACRO_ATTR_KEYS.values())),
|
|
||||||
)
|
|
||||||
by_key = {r["attr_key"]: r for r in cur.fetchall()}
|
|
||||||
if not by_key:
|
|
||||||
return None
|
|
||||||
out = {}
|
|
||||||
factor = float(quantity_g) / 100.0
|
|
||||||
missing = False
|
|
||||||
for field, key in MACRO_ATTR_KEYS.items():
|
|
||||||
row = by_key.get(key)
|
|
||||||
if not row or row.get("is_trace") or row.get("value_num") is None:
|
|
||||||
missing = True
|
|
||||||
break
|
|
||||||
out[field] = float(row["value_num"]) * factor
|
|
||||||
return None if missing else out
|
|
||||||
|
|
||||||
|
|
||||||
def compute_day_macro_sums(cur, profile_id: str, day: date | str) -> dict[str, Any]:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT kcal, protein_g, fat_g, carbs_g, macro_origin, has_items
|
|
||||||
FROM nutrition_log WHERE profile_id = %s AND date = %s
|
|
||||||
""",
|
|
||||||
(profile_id, day),
|
|
||||||
)
|
|
||||||
existing = cur.fetchone()
|
|
||||||
existing_macros = (
|
|
||||||
_round_macros(existing)
|
|
||||||
if existing
|
|
||||||
else None
|
|
||||||
)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT food_id, recipe_id, quantity_g, fddb_kcal, fddb_protein_g, fddb_fat_g, fddb_carbs_g, value_origin
|
|
||||||
FROM nutrition_items
|
|
||||||
WHERE profile_id = %s AND date = %s
|
|
||||||
""",
|
|
||||||
(profile_id, day),
|
|
||||||
)
|
|
||||||
items = cur.fetchall()
|
|
||||||
fddb = {"kcal": 0.0, "protein_g": 0.0, "fat_g": 0.0, "carbs_g": 0.0}
|
|
||||||
catalog = {"kcal": 0.0, "protein_g": 0.0, "fat_g": 0.0, "carbs_g": 0.0}
|
|
||||||
mapped = unmapped = 0
|
|
||||||
used_bls = used_fddb = False
|
|
||||||
for it in items:
|
|
||||||
fddb["kcal"] += _f(it.get("fddb_kcal"))
|
|
||||||
fddb["protein_g"] += _f(it.get("fddb_protein_g"))
|
|
||||||
fddb["fat_g"] += _f(it.get("fddb_fat_g"))
|
|
||||||
fddb["carbs_g"] += _f(it.get("fddb_carbs_g"))
|
|
||||||
if it.get("recipe_id") and not it.get("food_id"):
|
|
||||||
from data_layer.food_recipes import catalog_macros_for_recipe
|
|
||||||
cat = catalog_macros_for_recipe(cur, profile_id, it["recipe_id"], it.get("quantity_g"))
|
|
||||||
else:
|
|
||||||
cat = catalog_macros_for_item(cur, it.get("food_id"), it.get("quantity_g"))
|
|
||||||
if cat:
|
|
||||||
mapped += 1
|
|
||||||
used_bls = True
|
|
||||||
for k in catalog:
|
|
||||||
catalog[k] += cat[k]
|
|
||||||
else:
|
|
||||||
unmapped += 1
|
|
||||||
used_fddb = True
|
|
||||||
catalog["kcal"] += _f(it.get("fddb_kcal"))
|
|
||||||
catalog["protein_g"] += _f(it.get("fddb_protein_g"))
|
|
||||||
catalog["fat_g"] += _f(it.get("fddb_fat_g"))
|
|
||||||
catalog["carbs_g"] += _f(it.get("fddb_carbs_g"))
|
|
||||||
origin = "mixed"
|
|
||||||
if used_bls and not used_fddb:
|
|
||||||
origin = "bls"
|
|
||||||
elif used_fddb and not used_bls:
|
|
||||||
origin = "fddb"
|
|
||||||
if not items:
|
|
||||||
origin = "manual"
|
|
||||||
return {
|
|
||||||
"existing": existing_macros,
|
|
||||||
"fddb": _round_macros(fddb) if items else None,
|
|
||||||
"catalog": _round_macros(catalog) if items else None,
|
|
||||||
"mapped_item_count": mapped,
|
|
||||||
"unmapped_item_count": unmapped,
|
|
||||||
"has_items": bool(items),
|
|
||||||
"catalog_origin": origin,
|
|
||||||
"has_log": existing is not None,
|
|
||||||
"macro_origin": existing["macro_origin"] if existing else None,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def apply_nutrition_day_macros(
|
|
||||||
cur,
|
|
||||||
profile_id: str,
|
|
||||||
day: date | str,
|
|
||||||
macros: dict[str, float],
|
|
||||||
*,
|
|
||||||
macro_origin: str,
|
|
||||||
source: str = "csv",
|
|
||||||
confirm: bool = False,
|
|
||||||
) -> str:
|
|
||||||
m = _round_macros(macros)
|
|
||||||
cur.execute("SELECT id FROM nutrition_log WHERE profile_id = %s AND date = %s", (profile_id, day))
|
|
||||||
row = cur.fetchone()
|
|
||||||
counts = compute_day_macro_sums(cur, profile_id, day)
|
|
||||||
extra = (
|
|
||||||
counts["mapped_item_count"],
|
|
||||||
counts["unmapped_item_count"],
|
|
||||||
counts["has_items"],
|
|
||||||
)
|
|
||||||
confirmed = datetime.utcnow() if confirm else None
|
|
||||||
if row:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
UPDATE nutrition_log
|
|
||||||
SET kcal=%s, protein_g=%s, fat_g=%s, carbs_g=%s, source=%s,
|
|
||||||
macro_origin=%s, mapped_item_count=%s, unmapped_item_count=%s,
|
|
||||||
has_items=%s, last_import_at=NOW(), macros_confirmed_at=COALESCE(%s, macros_confirmed_at)
|
|
||||||
WHERE profile_id=%s AND date=%s
|
|
||||||
""",
|
|
||||||
(
|
|
||||||
m["kcal"], m["protein_g"], m["fat_g"], m["carbs_g"], source,
|
|
||||||
macro_origin, extra[0], extra[1], extra[2], confirmed, profile_id, day,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
return "updated"
|
|
||||||
eid = str(uuid.uuid4())
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO nutrition_log (
|
|
||||||
id, profile_id, date, kcal, protein_g, fat_g, carbs_g, source,
|
|
||||||
macro_origin, mapped_item_count, unmapped_item_count, has_items,
|
|
||||||
last_import_at, macros_confirmed_at, created
|
|
||||||
) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,NOW(),%s,CURRENT_TIMESTAMP)
|
|
||||||
""",
|
|
||||||
(
|
|
||||||
eid, profile_id, day, m["kcal"], m["protein_g"], m["fat_g"], m["carbs_g"], source,
|
|
||||||
macro_origin, extra[0], extra[1], extra[2], confirmed,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
return "created"
|
|
||||||
|
|
||||||
|
|
||||||
def _accumulate_food_qty(cur, acc: dict[int, list[float]], food_id: str, quantity_g: float) -> None:
|
|
||||||
if not food_id or not quantity_g or quantity_g <= 0:
|
|
||||||
return
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT v.attribute_id, v.value_num, v.is_trace, a.data_type
|
|
||||||
FROM food_attribute_values v
|
|
||||||
JOIN food_attributes a ON a.id = v.attribute_id
|
|
||||||
WHERE v.food_id = %s AND a.data_type = 'num_per_100g'
|
|
||||||
AND v.value_num IS NOT NULL AND v.is_trace = false
|
|
||||||
""",
|
|
||||||
(food_id,),
|
|
||||||
)
|
|
||||||
factor = float(quantity_g) / 100.0
|
|
||||||
for row in cur.fetchall():
|
|
||||||
acc.setdefault(row["attribute_id"], []).append(float(row["value_num"]) * factor)
|
|
||||||
|
|
||||||
|
|
||||||
def schedule_daily_nutrient_rebuild(profile_id: str, dates: list[str], context: str) -> None:
|
|
||||||
days = [str(d) for d in (dates or []) if d]
|
|
||||||
if not days:
|
|
||||||
return
|
|
||||||
|
|
||||||
def _run() -> None:
|
|
||||||
try:
|
|
||||||
from db import get_cursor, get_db
|
|
||||||
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
for day in days:
|
|
||||||
rebuild_daily_nutrients(cur, profile_id, day)
|
|
||||||
except Exception:
|
|
||||||
logger.exception("Nährwert-Rebuild nach %s fehlgeschlagen", context)
|
|
||||||
|
|
||||||
threading.Thread(target=_run, daemon=True, name="nutrition-rebuild").start()
|
|
||||||
|
|
||||||
|
|
||||||
def rebuild_daily_nutrients(cur, profile_id: str, day: date | str) -> None:
|
|
||||||
cur.execute(
|
|
||||||
"DELETE FROM nutrition_daily_nutrients WHERE profile_id = %s AND date = %s",
|
|
||||||
(profile_id, day),
|
|
||||||
)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT food_id, recipe_id, quantity_g
|
|
||||||
FROM nutrition_items
|
|
||||||
WHERE profile_id = %s AND date = %s
|
|
||||||
""",
|
|
||||||
(profile_id, day),
|
|
||||||
)
|
|
||||||
acc: dict[int, list[float]] = {}
|
|
||||||
from data_layer.food_recipes import mapped_ingredient_quantities
|
|
||||||
for it in cur.fetchall():
|
|
||||||
if it.get("food_id"):
|
|
||||||
_accumulate_food_qty(cur, acc, it["food_id"], _f(it.get("quantity_g")))
|
|
||||||
continue
|
|
||||||
if it.get("recipe_id"):
|
|
||||||
parts = mapped_ingredient_quantities(cur, profile_id, it["recipe_id"], it.get("quantity_g"))
|
|
||||||
if not parts:
|
|
||||||
continue
|
|
||||||
for part in parts:
|
|
||||||
_accumulate_food_qty(cur, acc, part["food_id"], part["quantity_g"])
|
|
||||||
for attr_id, vals in acc.items():
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO nutrition_daily_nutrients
|
|
||||||
(profile_id, date, attribute_id, value, contributing_item_count, updated_at)
|
|
||||||
VALUES (%s, %s, %s, %s, %s, NOW())
|
|
||||||
""",
|
|
||||||
(profile_id, day, attr_id, round(sum(vals), 6), len(vals)),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _item_value_origin(mapping: dict | None) -> str:
|
|
||||||
if not mapping:
|
|
||||||
return "fddb"
|
|
||||||
kind = mapping.get("catalog_kind")
|
|
||||||
if kind == "official_bls":
|
|
||||||
return "bls"
|
|
||||||
if kind in ("manual_admin", "manual_user"):
|
|
||||||
return "manual_catalog"
|
|
||||||
return "fddb"
|
|
||||||
|
|
||||||
|
|
||||||
def replace_csv_items_for_dates(
|
|
||||||
cur,
|
|
||||||
profile_id: str,
|
|
||||||
rows: list[dict[str, Any]],
|
|
||||||
*,
|
|
||||||
policy: str,
|
|
||||||
policy_override: str | None = None,
|
|
||||||
) -> dict[str, Any]:
|
|
||||||
"""
|
|
||||||
Replace csv-sourced items for the dates present in rows.
|
|
||||||
Returns conflicts when policy is prompt and existing macros differ.
|
|
||||||
"""
|
|
||||||
effective = policy_override if policy_override in POLICIES else policy
|
|
||||||
by_date: dict[str, list[dict]] = {}
|
|
||||||
for row in rows:
|
|
||||||
d = row.get("date")
|
|
||||||
if hasattr(d, "isoformat"):
|
|
||||||
iso = d.isoformat()
|
|
||||||
else:
|
|
||||||
iso = str(d)[:10]
|
|
||||||
if not iso:
|
|
||||||
continue
|
|
||||||
by_date.setdefault(iso, []).append(row)
|
|
||||||
|
|
||||||
conflicts = []
|
|
||||||
days_written = 0
|
|
||||||
items_written = 0
|
|
||||||
new_log_days = 0
|
|
||||||
|
|
||||||
for iso, day_rows in by_date.items():
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
DELETE FROM nutrition_items
|
|
||||||
WHERE profile_id = %s AND date = %s AND source = 'csv'
|
|
||||||
""",
|
|
||||||
(profile_id, iso),
|
|
||||||
)
|
|
||||||
for raw in day_rows:
|
|
||||||
name = (raw.get("food_name") or raw.get("source_name_raw") or "").strip()
|
|
||||||
if not name:
|
|
||||||
continue
|
|
||||||
qty_raw = raw.get("quantity_raw")
|
|
||||||
qty_g = parse_quantity_g(
|
|
||||||
qty_raw if qty_raw is not None else name,
|
|
||||||
mapping.get("grams_per_unit") if mapping else None,
|
|
||||||
)
|
|
||||||
mapping = get_food_mapping_with_cursor(cur, name, profile_id)
|
|
||||||
logged_at = raw.get("logged_at")
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO nutrition_items (
|
|
||||||
id, profile_id, date, logged_at, source_name_raw, source_name_normalized,
|
|
||||||
source_system, quantity_raw, quantity_g,
|
|
||||||
fddb_kcal, fddb_protein_g, fddb_fat_g, fddb_carbs_g,
|
|
||||||
food_id, mapping_id, value_origin, source, recipe_id
|
|
||||||
) VALUES (
|
|
||||||
%s,%s,%s,%s,%s,%s,'fddb',%s,%s,%s,%s,%s,%s,%s,%s,%s,'csv',%s
|
|
||||||
)
|
|
||||||
""",
|
|
||||||
(
|
|
||||||
str(uuid.uuid4()),
|
|
||||||
profile_id,
|
|
||||||
iso,
|
|
||||||
logged_at,
|
|
||||||
name,
|
|
||||||
normalize_food_name(name),
|
|
||||||
str(qty_raw) if qty_raw is not None else None,
|
|
||||||
qty_g,
|
|
||||||
_f(raw.get("fddb_kcal") if raw.get("fddb_kcal") is not None else raw.get("kcal")),
|
|
||||||
_f(raw.get("fddb_protein_g") if raw.get("fddb_protein_g") is not None else raw.get("protein_g")),
|
|
||||||
_f(raw.get("fddb_fat_g") if raw.get("fddb_fat_g") is not None else raw.get("fat_g")),
|
|
||||||
_f(raw.get("fddb_carbs_g") if raw.get("fddb_carbs_g") is not None else raw.get("carbs_g")),
|
|
||||||
mapping["food_id"] if mapping else None,
|
|
||||||
mapping["mapping_id"] if mapping else None,
|
|
||||||
_item_value_origin(mapping),
|
|
||||||
None,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
items_written += 1
|
|
||||||
from data_layer.food_recipes import link_recipes_to_items
|
|
||||||
link_recipes_to_items(cur, profile_id)
|
|
||||||
days_written += 1
|
|
||||||
sums = compute_day_macro_sums(cur, profile_id, iso)
|
|
||||||
rebuild_daily_nutrients(cur, profile_id, iso)
|
|
||||||
if not sums["has_items"]:
|
|
||||||
continue
|
|
||||||
if not sums["has_log"]:
|
|
||||||
apply_nutrition_day_macros(
|
|
||||||
cur, profile_id, iso, sums["catalog"],
|
|
||||||
macro_origin=sums["catalog_origin"], source="csv",
|
|
||||||
)
|
|
||||||
new_log_days += 1
|
|
||||||
continue
|
|
||||||
existing = sums["existing"]
|
|
||||||
catalog = sums["catalog"]
|
|
||||||
fddb = sums["fddb"]
|
|
||||||
differ = macros_differ(existing, catalog) or macros_differ(existing, fddb)
|
|
||||||
if effective == "keep_existing":
|
|
||||||
_touch_item_counts(cur, profile_id, iso, sums)
|
|
||||||
continue
|
|
||||||
if effective == "overwrite_catalog":
|
|
||||||
apply_nutrition_day_macros(
|
|
||||||
cur, profile_id, iso, catalog,
|
|
||||||
macro_origin=sums["catalog_origin"], source="csv",
|
|
||||||
)
|
|
||||||
continue
|
|
||||||
if effective == "overwrite_fddb":
|
|
||||||
apply_nutrition_day_macros(
|
|
||||||
cur, profile_id, iso, fddb, macro_origin="fddb", source="csv",
|
|
||||||
)
|
|
||||||
continue
|
|
||||||
# prompt
|
|
||||||
if differ:
|
|
||||||
conflicts.append({
|
|
||||||
"date": iso,
|
|
||||||
"existing": existing,
|
|
||||||
"fddb": fddb,
|
|
||||||
"catalog": catalog,
|
|
||||||
"catalog_origin": sums["catalog_origin"],
|
|
||||||
"mapped_item_count": sums["mapped_item_count"],
|
|
||||||
"unmapped_item_count": sums["unmapped_item_count"],
|
|
||||||
})
|
|
||||||
else:
|
|
||||||
apply_nutrition_day_macros(
|
|
||||||
cur, profile_id, iso, catalog,
|
|
||||||
macro_origin=sums["catalog_origin"], source="csv",
|
|
||||||
)
|
|
||||||
|
|
||||||
return {
|
|
||||||
"days_written": days_written,
|
|
||||||
"items_written": items_written,
|
|
||||||
"new_log_days": new_log_days,
|
|
||||||
"conflicts": conflicts,
|
|
||||||
"policy": effective,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def _touch_item_counts(cur, profile_id: str, day: str, sums: dict) -> None:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
UPDATE nutrition_log
|
|
||||||
SET mapped_item_count=%s, unmapped_item_count=%s, has_items=%s, last_import_at=NOW()
|
|
||||||
WHERE profile_id=%s AND date=%s
|
|
||||||
""",
|
|
||||||
(
|
|
||||||
sums["mapped_item_count"],
|
|
||||||
sums["unmapped_item_count"],
|
|
||||||
sums["has_items"],
|
|
||||||
profile_id,
|
|
||||||
day,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def day_macro_preview(cur, profile_id: str, day: date | str) -> dict[str, Any]:
|
|
||||||
sums = compute_day_macro_sums(cur, profile_id, day)
|
|
||||||
catalog = sums.get("catalog")
|
|
||||||
existing = sums.get("existing")
|
|
||||||
mapped = int(sums.get("mapped_item_count") or 0)
|
|
||||||
return {
|
|
||||||
"date": str(day)[:10],
|
|
||||||
"existing": existing,
|
|
||||||
"catalog": catalog,
|
|
||||||
"fddb": sums.get("fddb"),
|
|
||||||
"mapped_item_count": mapped,
|
|
||||||
"unmapped_item_count": int(sums.get("unmapped_item_count") or 0),
|
|
||||||
"catalog_origin": sums.get("catalog_origin"),
|
|
||||||
"can_apply": bool(catalog) and mapped > 0,
|
|
||||||
"differs": bool(existing and catalog and macros_differ(existing, catalog)),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def apply_day_catalog_macros(cur, profile_id: str, day: date | str) -> dict[str, Any]:
|
|
||||||
preview = day_macro_preview(cur, profile_id, day)
|
|
||||||
if not preview["can_apply"]:
|
|
||||||
raise ValueError("Keine aus Zuordnungen berechenbaren Tagesmakros")
|
|
||||||
cur.execute(
|
|
||||||
"SELECT source FROM nutrition_log WHERE profile_id = %s AND date = %s",
|
|
||||||
(profile_id, day),
|
|
||||||
)
|
|
||||||
row = cur.fetchone()
|
|
||||||
source = (row.get("source") if row else None) or "csv"
|
|
||||||
apply_nutrition_day_macros(
|
|
||||||
cur,
|
|
||||||
profile_id,
|
|
||||||
day,
|
|
||||||
preview["catalog"],
|
|
||||||
macro_origin=preview.get("catalog_origin") or "bls",
|
|
||||||
source=source,
|
|
||||||
confirm=True,
|
|
||||||
)
|
|
||||||
rebuild_daily_nutrients(cur, profile_id, day)
|
|
||||||
preview = day_macro_preview(cur, profile_id, day)
|
|
||||||
preview["applied"] = True
|
|
||||||
return preview
|
|
||||||
|
|
||||||
|
|
||||||
def annotate_nutrition_item(cur, profile_id: str, row: dict[str, Any]) -> dict[str, Any]:
|
|
||||||
item = dict(row)
|
|
||||||
item["id"] = str(item["id"])
|
|
||||||
if item.get("food_id"):
|
|
||||||
item["food_id"] = str(item["food_id"])
|
|
||||||
if item.get("recipe_id"):
|
|
||||||
item["recipe_id"] = str(item["recipe_id"])
|
|
||||||
from data_layer.food_recipes import catalog_macros_for_recipe
|
|
||||||
item["recipe_complete"] = catalog_macros_for_recipe(
|
|
||||||
cur, profile_id, item["recipe_id"], item.get("quantity_g")
|
|
||||||
) is not None
|
|
||||||
else:
|
|
||||||
item["recipe_complete"] = False
|
|
||||||
return item
|
|
||||||
|
|
||||||
|
|
||||||
def resolve_choice_macros(sums: dict[str, Any], choice: str) -> tuple[dict[str, float], str]:
|
|
||||||
if choice == "existing":
|
|
||||||
return sums["existing"], "user_confirmed"
|
|
||||||
if choice == "fddb":
|
|
||||||
return sums["fddb"], "fddb"
|
|
||||||
if choice == "catalog":
|
|
||||||
return sums["catalog"], sums.get("catalog_origin") or "mixed"
|
|
||||||
raise ValueError("Ungültige Wahl (existing|fddb|catalog)")
|
|
||||||
|
|
||||||
|
|
||||||
def resolve_food_attributes(cur, food_id: str) -> list[dict]:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT a.attr_key, a.name_de, a.unit, a.category, a.data_type, a.origin AS attr_origin,
|
|
||||||
v.value_num, v.value_bool, v.value_text, v.is_trace, v.origin_code
|
|
||||||
FROM food_attributes a
|
|
||||||
LEFT JOIN food_attribute_values v
|
|
||||||
ON v.attribute_id = a.id AND v.food_id = %s
|
|
||||||
WHERE a.is_active = true
|
|
||||||
ORDER BY a.sort_order, a.attr_key
|
|
||||||
""",
|
|
||||||
(food_id,),
|
|
||||||
)
|
|
||||||
return [dict(r) for r in cur.fetchall()]
|
|
||||||
|
|
||||||
|
|
||||||
def dates_for_normalized_name(cur, profile_id: str, source_name_normalized: str) -> list[str]:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT DISTINCT date::text AS date
|
|
||||||
FROM nutrition_items
|
|
||||||
WHERE profile_id = %s AND source_name_normalized = %s
|
|
||||||
UNION
|
|
||||||
SELECT DISTINCT i.date::text AS date
|
|
||||||
FROM nutrition_items i
|
|
||||||
JOIN food_recipe_ingredients ri ON ri.recipe_id = i.recipe_id
|
|
||||||
WHERE i.profile_id = %s AND ri.source_name_normalized = %s
|
|
||||||
""",
|
|
||||||
(profile_id, source_name_normalized, profile_id, source_name_normalized),
|
|
||||||
)
|
|
||||||
return [r["date"] for r in cur.fetchall()]
|
|
||||||
|
|
||||||
|
|
||||||
def dates_using_food(cur, profile_id: str, food_id: str) -> list[str]:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT DISTINCT date::text AS date
|
|
||||||
FROM nutrition_items
|
|
||||||
WHERE profile_id = %s AND food_id = %s
|
|
||||||
UNION
|
|
||||||
SELECT DISTINCT i.date::text AS date
|
|
||||||
FROM nutrition_items i
|
|
||||||
JOIN food_recipe_ingredients ri ON ri.recipe_id = i.recipe_id
|
|
||||||
JOIN food_name_mappings m
|
|
||||||
ON m.profile_id = i.profile_id
|
|
||||||
AND m.source_name_normalized = ri.source_name_normalized
|
|
||||||
AND m.food_id = %s
|
|
||||||
WHERE i.profile_id = %s AND i.recipe_id IS NOT NULL
|
|
||||||
""",
|
|
||||||
(profile_id, food_id, food_id, profile_id),
|
|
||||||
)
|
|
||||||
return [r["date"] for r in cur.fetchall() if r.get("date")]
|
|
||||||
|
|
||||||
|
|
||||||
def profiles_dates_using_food(cur, food_id: str) -> list[tuple[str, str]]:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT DISTINCT profile_id::text AS profile_id, date::text AS date
|
|
||||||
FROM nutrition_items
|
|
||||||
WHERE food_id = %s
|
|
||||||
UNION
|
|
||||||
SELECT DISTINCT i.profile_id::text AS profile_id, i.date::text AS date
|
|
||||||
FROM nutrition_items i
|
|
||||||
JOIN food_recipe_ingredients ri ON ri.recipe_id = i.recipe_id
|
|
||||||
JOIN food_name_mappings m
|
|
||||||
ON m.profile_id = i.profile_id
|
|
||||||
AND m.source_name_normalized = ri.source_name_normalized
|
|
||||||
AND m.food_id = %s
|
|
||||||
WHERE i.recipe_id IS NOT NULL
|
|
||||||
""",
|
|
||||||
(food_id, food_id),
|
|
||||||
)
|
|
||||||
return [(r["profile_id"], r["date"]) for r in cur.fetchall() if r.get("profile_id") and r.get("date")]
|
|
||||||
@@ -17,11 +17,6 @@ from data_layer.nutrition_interpretation import (
|
|||||||
build_nutrition_correlation_heuristic_items,
|
build_nutrition_correlation_heuristic_items,
|
||||||
build_nutrition_history_kpi_tiles,
|
build_nutrition_history_kpi_tiles,
|
||||||
)
|
)
|
||||||
from data_layer.nutrition_chart_payloads import (
|
|
||||||
build_energy_balance_chart_payload,
|
|
||||||
build_nutrition_adherence_score_payload,
|
|
||||||
build_protein_adequacy_chart_payload,
|
|
||||||
)
|
|
||||||
from data_layer.nutrition_metrics import (
|
from data_layer.nutrition_metrics import (
|
||||||
estimate_tdee_kcal_from_latest_weight,
|
estimate_tdee_kcal_from_latest_weight,
|
||||||
get_energy_availability_warning_payload,
|
get_energy_availability_warning_payload,
|
||||||
@@ -249,8 +244,6 @@ def get_nutrition_history_viz_bundle(profile_id: str, days: int) -> Dict[str, An
|
|||||||
"calorie_balance_daily": [],
|
"calorie_balance_daily": [],
|
||||||
"protein_vs_lean_mass": {"points": [], "protein_target_low_g": None},
|
"protein_vs_lean_mass": {"points": [], "protein_target_low_g": None},
|
||||||
"nutrition_correlation_heuristics": [],
|
"nutrition_correlation_heuristics": [],
|
||||||
"chart_payloads": {},
|
|
||||||
"chart_payloads_days": None,
|
|
||||||
"meta": {"layer_1": "nutrition_metrics", "layer_2b": "nutrition_viz"},
|
"meta": {"layer_1": "nutrition_metrics", "layer_2b": "nutrition_viz"},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -319,20 +312,6 @@ def get_nutrition_history_viz_bundle(profile_id: str, days: int) -> Dict[str, An
|
|||||||
weeks_for_weekly = max(4, min(52, (chart_days_for_pipeline + 6) // 7))
|
weeks_for_weekly = max(4, min(52, (chart_days_for_pipeline + 6) // 7))
|
||||||
weekly_chart = get_weekly_macro_distribution_chart_data(profile_id, weeks_for_weekly)
|
weekly_chart = get_weekly_macro_distribution_chart_data(profile_id, weeks_for_weekly)
|
||||||
|
|
||||||
# E1/E2/E4 Chart.js-Payloads — gleiche Funktionen wie /api/charts/* (kein zweiter HTTP-Roundtrip im Verlauf)
|
|
||||||
days_for_embedded_charts = max(7, min(int(chart_days_for_pipeline), 90))
|
|
||||||
chart_payloads = {
|
|
||||||
"energy_balance": build_energy_balance_chart_payload(
|
|
||||||
profile_id, days_for_embedded_charts
|
|
||||||
),
|
|
||||||
"protein_adequacy": build_protein_adequacy_chart_payload(
|
|
||||||
profile_id, days_for_embedded_charts
|
|
||||||
),
|
|
||||||
"nutrition_adherence": build_nutrition_adherence_score_payload(
|
|
||||||
profile_id, days_for_embedded_charts
|
|
||||||
),
|
|
||||||
}
|
|
||||||
|
|
||||||
conf = navg.get("confidence") or "medium"
|
conf = navg.get("confidence") or "medium"
|
||||||
if targets.get("confidence") == "insufficient":
|
if targets.get("confidence") == "insufficient":
|
||||||
conf = "insufficient"
|
conf = "insufficient"
|
||||||
@@ -383,8 +362,6 @@ def get_nutrition_history_viz_bundle(profile_id: str, days: int) -> Dict[str, An
|
|||||||
"protein_target_low_g": pt_low if pt_low > 0 else None,
|
"protein_target_low_g": pt_low if pt_low > 0 else None,
|
||||||
},
|
},
|
||||||
"nutrition_correlation_heuristics": nutrition_correlation_heuristics,
|
"nutrition_correlation_heuristics": nutrition_correlation_heuristics,
|
||||||
"chart_payloads": chart_payloads,
|
|
||||||
"chart_payloads_days": days_for_embedded_charts,
|
|
||||||
"meta": {
|
"meta": {
|
||||||
"layer_1": "nutrition_metrics",
|
"layer_1": "nutrition_metrics",
|
||||||
"layer_2b": "nutrition_viz",
|
"layer_2b": "nutrition_viz",
|
||||||
|
|||||||
@@ -1,120 +0,0 @@
|
|||||||
"""HTTP client for a user's Tandoor instance. Never logs the token."""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import json
|
|
||||||
import ssl
|
|
||||||
import urllib.error
|
|
||||||
import urllib.request
|
|
||||||
from typing import Any
|
|
||||||
from urllib.parse import urljoin, urlparse
|
|
||||||
|
|
||||||
|
|
||||||
class TandoorError(ValueError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def normalize_base_url(raw: str | None) -> str:
|
|
||||||
url = (raw or "").strip().rstrip("/")
|
|
||||||
if not url:
|
|
||||||
raise TandoorError("Tandoor-URL fehlt")
|
|
||||||
parsed = urlparse(url)
|
|
||||||
if parsed.scheme not in ("https", "http") or not parsed.netloc:
|
|
||||||
raise TandoorError("Tandoor-URL muss mit https:// oder http:// beginnen")
|
|
||||||
if parsed.scheme == "http" and parsed.hostname not in ("localhost", "127.0.0.1"):
|
|
||||||
raise TandoorError("Tandoor-URL muss https verwenden")
|
|
||||||
return url
|
|
||||||
|
|
||||||
|
|
||||||
def token_hint(token: str | None) -> str | None:
|
|
||||||
t = (token or "").strip()
|
|
||||||
if len(t) < 4:
|
|
||||||
return "gesetzt" if t else None
|
|
||||||
return f"…{t[-4:]}"
|
|
||||||
|
|
||||||
|
|
||||||
def public_settings(row: dict[str, Any] | None) -> dict[str, Any]:
|
|
||||||
if not row:
|
|
||||||
return {
|
|
||||||
"configured": False,
|
|
||||||
"base_url": "",
|
|
||||||
"token_set": False,
|
|
||||||
"token_hint": None,
|
|
||||||
"last_ok_at": None,
|
|
||||||
"last_error": None,
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
"configured": True,
|
|
||||||
"base_url": row.get("base_url") or "",
|
|
||||||
"token_set": bool((row.get("api_token") or "").strip()),
|
|
||||||
"token_hint": token_hint(row.get("api_token")),
|
|
||||||
"last_ok_at": row.get("last_ok_at").isoformat() if row.get("last_ok_at") else None,
|
|
||||||
"last_error": row.get("last_error"),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def _request(base_url: str, token: str, path: str, timeout: float = 10.0) -> tuple[int, str, str]:
|
|
||||||
url = urljoin(base_url.rstrip("/") + "/", path.lstrip("/"))
|
|
||||||
req = urllib.request.Request(
|
|
||||||
url,
|
|
||||||
headers={
|
|
||||||
"Authorization": f"Token {token.strip()}",
|
|
||||||
"Accept": "application/json",
|
|
||||||
"User-Agent": "Mitai-Jinkendo/tandoor-connector",
|
|
||||||
},
|
|
||||||
method="GET",
|
|
||||||
)
|
|
||||||
ctx = ssl.create_default_context()
|
|
||||||
|
|
||||||
class _NoRedirect(urllib.request.HTTPRedirectHandler):
|
|
||||||
def redirect_request(self, req, fp, code, msg, headers, newurl): # noqa: ARG002
|
|
||||||
return None
|
|
||||||
|
|
||||||
opener = urllib.request.build_opener(_NoRedirect, urllib.request.HTTPSHandler(context=ctx))
|
|
||||||
try:
|
|
||||||
with opener.open(req, timeout=timeout) as resp:
|
|
||||||
body = resp.read().decode("utf-8", errors="replace")
|
|
||||||
ctype = resp.headers.get("Content-Type", "")
|
|
||||||
return resp.status, body, ctype
|
|
||||||
except urllib.error.HTTPError as e:
|
|
||||||
body = e.read().decode("utf-8", errors="replace") if e.fp else ""
|
|
||||||
ctype = e.headers.get("Content-Type", "") if e.headers else ""
|
|
||||||
return e.code, body, ctype
|
|
||||||
except TimeoutError as e:
|
|
||||||
raise TandoorError("Tandoor antwortet nicht (Zeitüberschreitung)") from e
|
|
||||||
except urllib.error.URLError as e:
|
|
||||||
raise TandoorError(f"Tandoor nicht erreichbar: {e.reason}") from e
|
|
||||||
|
|
||||||
|
|
||||||
def probe_connection(base_url: str, token: str) -> dict[str, Any]:
|
|
||||||
url = normalize_base_url(base_url)
|
|
||||||
tok = (token or "").strip()
|
|
||||||
if not tok:
|
|
||||||
raise TandoorError("API-Token fehlt")
|
|
||||||
status, body, ctype = _request(url, tok, "api/user/")
|
|
||||||
if status in (301, 302, 303, 307, 308) or "text/html" in (ctype or "") and status < 400:
|
|
||||||
raise TandoorError("Tandoor hat nicht mit JSON geantwortet — URL oder Token prüfen")
|
|
||||||
if status in (401, 403):
|
|
||||||
raise TandoorError("Token ungültig oder ohne Rechte")
|
|
||||||
if status == 404:
|
|
||||||
status, body, ctype = _request(url, tok, "api/recipe/?page=1&page_size=1")
|
|
||||||
if status in (401, 403):
|
|
||||||
raise TandoorError("Token ungültig oder ohne Rechte")
|
|
||||||
if status >= 400:
|
|
||||||
raise TandoorError(f"Tandoor antwortete mit HTTP {status}")
|
|
||||||
if "json" not in (ctype or "") and not (body or "").lstrip().startswith(("{", "[")):
|
|
||||||
raise TandoorError("Tandoor hat nicht mit JSON geantwortet — URL oder Token prüfen")
|
|
||||||
try:
|
|
||||||
data = json.loads(body) if body else {}
|
|
||||||
except json.JSONDecodeError as e:
|
|
||||||
raise TandoorError("Tandoor-Antwort war kein JSON") from e
|
|
||||||
name = None
|
|
||||||
if isinstance(data, dict):
|
|
||||||
results = data.get("results")
|
|
||||||
if isinstance(results, list) and results and isinstance(results[0], dict):
|
|
||||||
name = results[0].get("display_name") or results[0].get("username") or results[0].get("name")
|
|
||||||
else:
|
|
||||||
name = data.get("display_name") or data.get("username") or data.get("name")
|
|
||||||
count = data.get("count")
|
|
||||||
else:
|
|
||||||
count = None
|
|
||||||
return {"ok": True, "user": name, "recipe_count": count}
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
"""Persist per-profile Tandoor credentials. Token never returned in full."""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from data_layer.tandoor_client import public_settings
|
|
||||||
|
|
||||||
|
|
||||||
def get_settings(cur, profile_id: str) -> dict[str, Any] | None:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT profile_id, base_url, api_token, last_ok_at, last_error, updated_at
|
|
||||||
FROM profile_tandoor_settings
|
|
||||||
WHERE profile_id = %s
|
|
||||||
""",
|
|
||||||
(profile_id,),
|
|
||||||
)
|
|
||||||
row = cur.fetchone()
|
|
||||||
return dict(row) if row else None
|
|
||||||
|
|
||||||
|
|
||||||
def get_public_settings(cur, profile_id: str) -> dict[str, Any]:
|
|
||||||
return public_settings(get_settings(cur, profile_id))
|
|
||||||
|
|
||||||
|
|
||||||
def upsert_settings(cur, profile_id: str, base_url: str, token: str | None, *, clear_token: bool = False) -> dict[str, Any]:
|
|
||||||
existing = get_settings(cur, profile_id)
|
|
||||||
if clear_token:
|
|
||||||
cur.execute("DELETE FROM profile_tandoor_settings WHERE profile_id = %s", (profile_id,))
|
|
||||||
return public_settings(None)
|
|
||||||
stored = (token or "").strip() or ((existing or {}).get("api_token") or "")
|
|
||||||
if not stored:
|
|
||||||
raise ValueError("API-Token fehlt")
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO profile_tandoor_settings (profile_id, base_url, api_token, last_error, updated_at)
|
|
||||||
VALUES (%s, %s, %s, NULL, NOW())
|
|
||||||
ON CONFLICT (profile_id) DO UPDATE SET
|
|
||||||
base_url = EXCLUDED.base_url,
|
|
||||||
api_token = EXCLUDED.api_token,
|
|
||||||
last_error = NULL,
|
|
||||||
updated_at = NOW()
|
|
||||||
""",
|
|
||||||
(profile_id, base_url, stored),
|
|
||||||
)
|
|
||||||
return get_public_settings(cur, profile_id)
|
|
||||||
|
|
||||||
|
|
||||||
def record_probe(cur, profile_id: str, ok: bool, error: str | None = None) -> None:
|
|
||||||
if ok:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
UPDATE profile_tandoor_settings
|
|
||||||
SET last_ok_at = NOW(), last_error = NULL, updated_at = NOW()
|
|
||||||
WHERE profile_id = %s
|
|
||||||
""",
|
|
||||||
(profile_id,),
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
UPDATE profile_tandoor_settings
|
|
||||||
SET last_error = %s, updated_at = NOW()
|
|
||||||
WHERE profile_id = %s
|
|
||||||
""",
|
|
||||||
((error or "")[:300], profile_id),
|
|
||||||
)
|
|
||||||
+1
-9
@@ -34,12 +34,9 @@ from routers import workflow_questions # Phase 1 Workflow Engine - Question Cat
|
|||||||
from routers import workflows # Phase 2 Workflow Engine - Execution
|
from routers import workflows # Phase 2 Workflow Engine - Execution
|
||||||
from routers import reference_values # Persönliche Referenzwerte (Profil)
|
from routers import reference_values # Persönliche Referenzwerte (Profil)
|
||||||
from routers import admin_reference_value_types # Admin: Referenzwert-Typen
|
from routers import admin_reference_value_types # Admin: Referenzwert-Typen
|
||||||
from routers import app_dashboard # Geschützter App-Bereich: Dashboard-Layout + Widget-Katalog
|
from routers import app_dashboard # Geschützter App-Bereich: Dashboard-Lab Layout
|
||||||
from routers import reports # Strukturierter PDF-Bericht (Profil v1)
|
|
||||||
from routers import csv_import, admin_csv_templates # Issue #21 Universal CSV Parser
|
from routers import csv_import, admin_csv_templates # Issue #21 Universal CSV Parser
|
||||||
from routers import admin_training_parameters, admin_activity_attribute_profiles # EAV session metrics
|
from routers import admin_training_parameters, admin_activity_attribute_profiles # EAV session metrics
|
||||||
from routers import bls, admin_bls, admin_food_mappings # BLS catalog + FDDB mapping
|
|
||||||
from routers import tandoor # per-user Tandoor connection
|
|
||||||
|
|
||||||
# ── App Configuration ─────────────────────────────────────────────────────────
|
# ── App Configuration ─────────────────────────────────────────────────────────
|
||||||
DATA_DIR = Path(os.getenv("DATA_DIR", "./data"))
|
DATA_DIR = Path(os.getenv("DATA_DIR", "./data"))
|
||||||
@@ -130,15 +127,10 @@ app.include_router(workflows.router) # /api/workflows/* (Phase 2 Exec
|
|||||||
app.include_router(reference_values.router) # /api/reference-value-types, /api/profile-reference-values
|
app.include_router(reference_values.router) # /api/reference-value-types, /api/profile-reference-values
|
||||||
app.include_router(admin_reference_value_types.router) # /api/admin/reference-value-types
|
app.include_router(admin_reference_value_types.router) # /api/admin/reference-value-types
|
||||||
app.include_router(app_dashboard.router) # /api/app/dashboard-layout
|
app.include_router(app_dashboard.router) # /api/app/dashboard-layout
|
||||||
app.include_router(reports.router) # /api/reports/* (Berichtsprofil + PDF)
|
|
||||||
app.include_router(csv_import.router) # /api/csv/* (Issue #21)
|
app.include_router(csv_import.router) # /api/csv/* (Issue #21)
|
||||||
app.include_router(admin_csv_templates.router) # /api/admin/csv-templates/* (Issue #21)
|
app.include_router(admin_csv_templates.router) # /api/admin/csv-templates/* (Issue #21)
|
||||||
app.include_router(admin_training_parameters.router) # /api/admin/training-parameters
|
app.include_router(admin_training_parameters.router) # /api/admin/training-parameters
|
||||||
app.include_router(admin_activity_attribute_profiles.router) # /api/admin/training-*-parameters
|
app.include_router(admin_activity_attribute_profiles.router) # /api/admin/training-*-parameters
|
||||||
app.include_router(bls.router) # /api/bls/*
|
|
||||||
app.include_router(admin_bls.router) # /api/admin/bls/*
|
|
||||||
app.include_router(admin_food_mappings.router) # /api/admin/food-mappings
|
|
||||||
app.include_router(tandoor.router) # /api/tandoor/*
|
|
||||||
|
|
||||||
# ── Health Check ──────────────────────────────────────────────────────────────
|
# ── Health Check ──────────────────────────────────────────────────────────────
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
-- Migration 060: Strukturierter Bericht (Profil JSON pro Nutzerprofil, unabhängig vom Dashboard-Layout)
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS report_profiles (
|
|
||||||
profile_id UUID PRIMARY KEY REFERENCES profiles(id) ON DELETE CASCADE,
|
|
||||||
payload JSONB NOT NULL DEFAULT '{}'::jsonb,
|
|
||||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_report_profiles_updated ON report_profiles(updated_at);
|
|
||||||
|
|
||||||
COMMENT ON TABLE report_profiles IS 'Konfigurierbarer PDF-Bericht v1 (Blöcke: section, chart, ai_insight); Rendering serverseitig aus Datenlayer';
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
-- Migration 061: Mehrere benannte PDF-Berichte pro Nutzerprofil; Daten von report_profiles übernehmen.
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS report_definitions (
|
|
||||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
||||||
profile_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
|
||||||
name TEXT NOT NULL DEFAULT 'Bericht',
|
|
||||||
payload JSONB NOT NULL DEFAULT '{}'::jsonb,
|
|
||||||
sort_order INT NOT NULL DEFAULT 0,
|
|
||||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_report_definitions_profile_sort
|
|
||||||
ON report_definitions (profile_id, sort_order);
|
|
||||||
|
|
||||||
COMMENT ON TABLE report_definitions IS 'Mehrere strukturierte PDF-Berichte pro Profil (payload = ReportProfilePayload v1)';
|
|
||||||
|
|
||||||
INSERT INTO report_definitions (profile_id, name, payload, sort_order)
|
|
||||||
SELECT rp.profile_id, 'Standard', rp.payload, 0
|
|
||||||
FROM report_profiles rp
|
|
||||||
WHERE NOT EXISTS (
|
|
||||||
SELECT 1 FROM report_definitions rd WHERE rd.profile_id = rp.profile_id
|
|
||||||
);
|
|
||||||
|
|
||||||
DROP TABLE IF EXISTS report_profiles;
|
|
||||||
@@ -1,205 +0,0 @@
|
|||||||
-- Migration 062: BLS/food catalog, typed attributes, FDDB mapping, nutrition items, day marks
|
|
||||||
-- Additive only. Official BLS rows upsert by bls_code / attr_key — never delete+reinsert.
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS food_attributes (
|
|
||||||
id SERIAL PRIMARY KEY,
|
|
||||||
attr_key VARCHAR(64) NOT NULL,
|
|
||||||
name_de VARCHAR(255) NOT NULL,
|
|
||||||
name_en VARCHAR(255),
|
|
||||||
unit VARCHAR(40),
|
|
||||||
category VARCHAR(80),
|
|
||||||
data_type VARCHAR(20) NOT NULL DEFAULT 'num_per_100g'
|
|
||||||
CHECK (data_type IN ('num_per_100g', 'boolean', 'text', 'enum')),
|
|
||||||
enum_values JSONB,
|
|
||||||
origin VARCHAR(20) NOT NULL DEFAULT 'official_bls'
|
|
||||||
CHECK (origin IN ('official_bls', 'extension')),
|
|
||||||
sort_order INT NOT NULL DEFAULT 0,
|
|
||||||
is_active BOOLEAN NOT NULL DEFAULT true,
|
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
||||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
||||||
CONSTRAINT uq_food_attributes_key UNIQUE (attr_key)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_food_attributes_origin ON food_attributes (origin);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS food_catalog (
|
|
||||||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
|
||||||
bls_code VARCHAR(16),
|
|
||||||
external_key VARCHAR(80),
|
|
||||||
name_de VARCHAR(500) NOT NULL,
|
|
||||||
name_en VARCHAR(500),
|
|
||||||
food_group VARCHAR(8),
|
|
||||||
catalog_kind VARCHAR(20) NOT NULL DEFAULT 'official_bls'
|
|
||||||
CHECK (catalog_kind IN ('official_bls', 'manual_admin', 'manual_user')),
|
|
||||||
owner_profile_id UUID REFERENCES profiles(id) ON DELETE CASCADE,
|
|
||||||
bls_version VARCHAR(16),
|
|
||||||
source VARCHAR(40) NOT NULL DEFAULT 'bls_4.0',
|
|
||||||
is_active BOOLEAN NOT NULL DEFAULT true,
|
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
||||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
||||||
CONSTRAINT uq_food_catalog_bls_code UNIQUE (bls_code),
|
|
||||||
CONSTRAINT chk_food_catalog_bls_code CHECK (
|
|
||||||
(catalog_kind = 'official_bls' AND bls_code IS NOT NULL)
|
|
||||||
OR (catalog_kind <> 'official_bls' AND bls_code IS NULL)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_food_catalog_name_de ON food_catalog (lower(name_de));
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_food_catalog_kind ON food_catalog (catalog_kind);
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_food_catalog_owner ON food_catalog (owner_profile_id)
|
|
||||||
WHERE owner_profile_id IS NOT NULL;
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS food_attribute_values (
|
|
||||||
id BIGSERIAL PRIMARY KEY,
|
|
||||||
food_id UUID NOT NULL REFERENCES food_catalog(id) ON DELETE CASCADE,
|
|
||||||
attribute_id INT NOT NULL REFERENCES food_attributes(id) ON DELETE CASCADE,
|
|
||||||
value_num DOUBLE PRECISION,
|
|
||||||
value_bool BOOLEAN,
|
|
||||||
value_text TEXT,
|
|
||||||
is_trace BOOLEAN NOT NULL DEFAULT false,
|
|
||||||
origin_code VARCHAR(80),
|
|
||||||
reference_text TEXT,
|
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
||||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
||||||
CONSTRAINT uq_food_attribute_value UNIQUE (food_id, attribute_id),
|
|
||||||
CONSTRAINT chk_food_attr_one_value CHECK (
|
|
||||||
(
|
|
||||||
(value_num IS NOT NULL)::int
|
|
||||||
+ (value_bool IS NOT NULL)::int
|
|
||||||
+ (value_text IS NOT NULL)::int
|
|
||||||
) <= 1
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_fav_food ON food_attribute_values (food_id);
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_fav_attr ON food_attribute_values (attribute_id);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS food_name_mappings (
|
|
||||||
id SERIAL PRIMARY KEY,
|
|
||||||
source_system VARCHAR(20) NOT NULL DEFAULT 'fddb',
|
|
||||||
source_name_raw VARCHAR(500) NOT NULL,
|
|
||||||
source_name_normalized VARCHAR(500) NOT NULL,
|
|
||||||
food_id UUID NOT NULL REFERENCES food_catalog(id) ON DELETE CASCADE,
|
|
||||||
profile_id UUID REFERENCES profiles(id) ON DELETE CASCADE,
|
|
||||||
source VARCHAR(20) NOT NULL DEFAULT 'manual'
|
|
||||||
CHECK (source IN ('manual', 'bulk', 'admin')),
|
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
||||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_food_map_global
|
|
||||||
ON food_name_mappings (source_system, source_name_normalized)
|
|
||||||
WHERE profile_id IS NULL;
|
|
||||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_food_map_user
|
|
||||||
ON food_name_mappings (source_system, source_name_normalized, profile_id)
|
|
||||||
WHERE profile_id IS NOT NULL;
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_food_map_food ON food_name_mappings (food_id);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS nutrition_items (
|
|
||||||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
|
||||||
profile_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
|
||||||
date DATE NOT NULL,
|
|
||||||
logged_at TIMESTAMPTZ,
|
|
||||||
source_name_raw VARCHAR(500) NOT NULL,
|
|
||||||
source_name_normalized VARCHAR(500) NOT NULL,
|
|
||||||
source_system VARCHAR(20) NOT NULL DEFAULT 'fddb',
|
|
||||||
quantity_raw VARCHAR(80),
|
|
||||||
quantity_g NUMERIC(10,3),
|
|
||||||
fddb_kcal NUMERIC(8,2),
|
|
||||||
fddb_protein_g NUMERIC(8,2),
|
|
||||||
fddb_fat_g NUMERIC(8,2),
|
|
||||||
fddb_carbs_g NUMERIC(8,2),
|
|
||||||
food_id UUID REFERENCES food_catalog(id) ON DELETE SET NULL,
|
|
||||||
mapping_id INT REFERENCES food_name_mappings(id) ON DELETE SET NULL,
|
|
||||||
value_origin VARCHAR(20) NOT NULL DEFAULT 'fddb'
|
|
||||||
CHECK (value_origin IN ('fddb', 'bls', 'manual_catalog')),
|
|
||||||
source VARCHAR(20) NOT NULL DEFAULT 'csv'
|
|
||||||
CHECK (source IN ('csv', 'manual')),
|
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
||||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_nutrition_items_profile_date
|
|
||||||
ON nutrition_items (profile_id, date DESC);
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_nutrition_items_unmapped
|
|
||||||
ON nutrition_items (profile_id, source_name_normalized)
|
|
||||||
WHERE food_id IS NULL;
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS nutrition_daily_nutrients (
|
|
||||||
id BIGSERIAL PRIMARY KEY,
|
|
||||||
profile_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
|
||||||
date DATE NOT NULL,
|
|
||||||
attribute_id INT NOT NULL REFERENCES food_attributes(id) ON DELETE CASCADE,
|
|
||||||
value DOUBLE PRECISION NOT NULL,
|
|
||||||
contributing_item_count INT NOT NULL DEFAULT 0,
|
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
||||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
||||||
CONSTRAINT uq_nutrition_daily_nutrient UNIQUE (profile_id, date, attribute_id)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_ndn_profile_date
|
|
||||||
ON nutrition_daily_nutrients (profile_id, date DESC);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS nutrition_day_marks (
|
|
||||||
id SERIAL PRIMARY KEY,
|
|
||||||
profile_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
|
||||||
date DATE NOT NULL,
|
|
||||||
mark_type VARCHAR(20) NOT NULL CHECK (mark_type IN ('fasting', 'incomplete')),
|
|
||||||
note TEXT,
|
|
||||||
source VARCHAR(20) NOT NULL DEFAULT 'manual',
|
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
||||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
||||||
CONSTRAINT uq_nutrition_day_mark UNIQUE (profile_id, date)
|
|
||||||
);
|
|
||||||
|
|
||||||
ALTER TABLE nutrition_log ADD COLUMN IF NOT EXISTS macro_origin VARCHAR(20);
|
|
||||||
ALTER TABLE nutrition_log ADD COLUMN IF NOT EXISTS mapped_item_count INT;
|
|
||||||
ALTER TABLE nutrition_log ADD COLUMN IF NOT EXISTS unmapped_item_count INT;
|
|
||||||
ALTER TABLE nutrition_log ADD COLUMN IF NOT EXISTS has_items BOOLEAN DEFAULT false;
|
|
||||||
ALTER TABLE nutrition_log ADD COLUMN IF NOT EXISTS last_import_at TIMESTAMPTZ;
|
|
||||||
ALTER TABLE nutrition_log ADD COLUMN IF NOT EXISTS macros_confirmed_at TIMESTAMPTZ;
|
|
||||||
|
|
||||||
DELETE FROM nutrition_log a
|
|
||||||
USING nutrition_log b
|
|
||||||
WHERE a.profile_id = b.profile_id
|
|
||||||
AND a.date = b.date
|
|
||||||
AND (a.created < b.created OR (a.created = b.created AND a.id::text < b.id::text));
|
|
||||||
|
|
||||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_nutrition_log_profile_date
|
|
||||||
ON nutrition_log (profile_id, date);
|
|
||||||
|
|
||||||
ALTER TABLE profiles
|
|
||||||
ADD COLUMN IF NOT EXISTS nutrition_import_conflict_policy VARCHAR(30) DEFAULT 'prompt';
|
|
||||||
|
|
||||||
DO $$
|
|
||||||
BEGIN
|
|
||||||
IF NOT EXISTS (
|
|
||||||
SELECT 1 FROM pg_constraint WHERE conname = 'chk_nutrition_import_policy'
|
|
||||||
) THEN
|
|
||||||
ALTER TABLE profiles ADD CONSTRAINT chk_nutrition_import_policy
|
|
||||||
CHECK (nutrition_import_conflict_policy IN (
|
|
||||||
'prompt', 'overwrite_catalog', 'overwrite_fddb', 'keep_existing'
|
|
||||||
));
|
|
||||||
END IF;
|
|
||||||
END $$;
|
|
||||||
|
|
||||||
UPDATE csv_field_mappings
|
|
||||||
SET field_mappings = COALESCE(field_mappings, '{}'::jsonb)
|
|
||||||
|| '{"bezeichnung": "food_name", "menge": "quantity_raw"}'::jsonb,
|
|
||||||
updated_at = NOW()
|
|
||||||
WHERE is_system = true
|
|
||||||
AND module = 'nutrition'
|
|
||||||
AND (
|
|
||||||
mapping_name ILIKE '%FDDB%'
|
|
||||||
OR mapping_name ILIKE '%fddb%'
|
|
||||||
);
|
|
||||||
|
|
||||||
COMMENT ON TABLE food_catalog IS 'BLS 4.0 official foods + manual catalog extensions';
|
|
||||||
COMMENT ON COLUMN food_catalog.bls_code IS 'Stable official BLS identity; upsert key';
|
|
||||||
COMMENT ON TABLE food_name_mappings IS 'Learned FDDB name -> food_catalog; user overrides global';
|
|
||||||
COMMENT ON TABLE nutrition_day_marks IS 'Explicit fasting/incomplete; import must not delete';
|
|
||||||
|
|
||||||
DO $$
|
|
||||||
BEGIN
|
|
||||||
RAISE NOTICE 'Migration 062: BLS food catalog, mappings, nutrition items, day marks';
|
|
||||||
END $$;
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
-- Migration 063: FDDB-Listen/Rezepte + Verknüpfung an nutrition_items
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS food_recipes (
|
|
||||||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
|
||||||
profile_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
|
||||||
name_raw VARCHAR(500) NOT NULL,
|
|
||||||
name_normalized VARCHAR(500) NOT NULL,
|
|
||||||
portions NUMERIC(8,2) NOT NULL DEFAULT 1,
|
|
||||||
description TEXT,
|
|
||||||
source VARCHAR(20) NOT NULL DEFAULT 'fddb_list',
|
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
||||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
||||||
CONSTRAINT uq_food_recipe_profile_name UNIQUE (profile_id, name_normalized)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_food_recipes_profile ON food_recipes (profile_id);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS food_recipe_ingredients (
|
|
||||||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
|
||||||
recipe_id UUID NOT NULL REFERENCES food_recipes(id) ON DELETE CASCADE,
|
|
||||||
source_name_raw VARCHAR(500) NOT NULL,
|
|
||||||
source_name_normalized VARCHAR(500) NOT NULL,
|
|
||||||
quantity_raw VARCHAR(80),
|
|
||||||
quantity_g NUMERIC(10,3),
|
|
||||||
sort_order INT NOT NULL DEFAULT 0
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_food_recipe_ing_recipe ON food_recipe_ingredients (recipe_id);
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_food_recipe_ing_norm ON food_recipe_ingredients (source_name_normalized);
|
|
||||||
|
|
||||||
ALTER TABLE nutrition_items
|
|
||||||
ADD COLUMN IF NOT EXISTS recipe_id UUID REFERENCES food_recipes(id) ON DELETE SET NULL;
|
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_nutrition_items_recipe
|
|
||||||
ON nutrition_items (recipe_id)
|
|
||||||
WHERE recipe_id IS NOT NULL;
|
|
||||||
|
|
||||||
COMMENT ON TABLE food_recipes IS 'FDDB-Listen/Rezepte; Tagebuchzeile kann statt Einzel-BLS auf ein Rezept zeigen';
|
|
||||||
|
|
||||||
DO $$
|
|
||||||
BEGIN
|
|
||||||
RAISE NOTICE 'Migration 063: FDDB recipes + nutrition_items.recipe_id';
|
|
||||||
END $$;
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
-- Migration 064: Mengeneinheiten am Mapping + source=import erlaubt
|
|
||||||
|
|
||||||
ALTER TABLE food_name_mappings
|
|
||||||
ADD COLUMN IF NOT EXISTS grams_per_unit NUMERIC(10,3),
|
|
||||||
ADD COLUMN IF NOT EXISTS source_unit VARCHAR(20);
|
|
||||||
|
|
||||||
ALTER TABLE food_name_mappings DROP CONSTRAINT IF EXISTS food_name_mappings_source_check;
|
|
||||||
|
|
||||||
ALTER TABLE food_name_mappings
|
|
||||||
ADD CONSTRAINT food_name_mappings_source_check
|
|
||||||
CHECK (source IN ('manual', 'bulk', 'admin', 'import'));
|
|
||||||
|
|
||||||
COMMENT ON COLUMN food_name_mappings.grams_per_unit IS 'Gramm pro Quell-Einheit (Stück, EL, …); NULL = Masse/Volumen bereits in g';
|
|
||||||
|
|
||||||
DO $$
|
|
||||||
BEGIN
|
|
||||||
RAISE NOTICE 'Migration 064: mapping units + source import';
|
|
||||||
END $$;
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
-- Common supplement fatty acids if BLS did not import these exact keys.
|
|
||||||
-- ON CONFLICT keeps official BLS rows unchanged.
|
|
||||||
|
|
||||||
INSERT INTO food_attributes
|
|
||||||
(attr_key, name_de, name_en, unit, category, data_type, origin, sort_order)
|
|
||||||
VALUES
|
|
||||||
('EPA', 'Eicosapentaensäure (EPA)', 'EPA', 'g', 'fatty_acids', 'num_per_100g', 'extension', 8001),
|
|
||||||
('DHA', 'Docosahexaensäure (DHA)', 'DHA', 'g', 'fatty_acids', 'num_per_100g', 'extension', 8002),
|
|
||||||
('DPA', 'Docosapentaensäure (DPA)', 'DPA', 'g', 'fatty_acids', 'num_per_100g', 'extension', 8003),
|
|
||||||
('ALA', 'Alpha-Linolensäure (ALA)', 'ALA', 'g', 'fatty_acids', 'num_per_100g', 'extension', 8004),
|
|
||||||
('OMEGA3', 'Omega-3-Fettsäuren gesamt', 'Omega-3', 'g', 'fatty_acids', 'num_per_100g', 'extension', 8005),
|
|
||||||
('OMEGA6', 'Omega-6-Fettsäuren gesamt', 'Omega-6', 'g', 'fatty_acids', 'num_per_100g', 'extension', 8006)
|
|
||||||
ON CONFLICT (attr_key) DO NOTHING;
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
-- Menge + Einheit an Rezeptzutaten; quantity_g bleibt die Standardeinheit (Gramm).
|
|
||||||
|
|
||||||
ALTER TABLE food_recipe_ingredients
|
|
||||||
ADD COLUMN IF NOT EXISTS quantity_amount NUMERIC(10,3),
|
|
||||||
ADD COLUMN IF NOT EXISTS source_unit VARCHAR(20);
|
|
||||||
|
|
||||||
COMMENT ON COLUMN food_recipe_ingredients.quantity_amount IS 'Menge in source_unit (z. B. 2 TL)';
|
|
||||||
COMMENT ON COLUMN food_recipe_ingredients.source_unit IS 'Kanonische Einheit: g, ml, el, tl, prise, stück, …';
|
|
||||||
COMMENT ON COLUMN food_recipe_ingredients.quantity_g IS 'Umrechnung auf Gramm, wenn Faktor bekannt';
|
|
||||||
|
|
||||||
UPDATE food_recipe_ingredients
|
|
||||||
SET quantity_amount = quantity_g, source_unit = 'g'
|
|
||||||
WHERE quantity_g IS NOT NULL AND quantity_amount IS NULL AND source_unit IS NULL;
|
|
||||||
|
|
||||||
DO $$
|
|
||||||
BEGIN
|
|
||||||
RAISE NOTICE 'Migration 066: recipe ingredient amount + unit';
|
|
||||||
END $$;
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
-- Per-user Tandoor connection (URL + API token). Not a system-wide connector.
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS profile_tandoor_settings (
|
|
||||||
profile_id UUID PRIMARY KEY REFERENCES profiles(id) ON DELETE CASCADE,
|
|
||||||
base_url VARCHAR(300) NOT NULL,
|
|
||||||
api_token TEXT NOT NULL,
|
|
||||||
last_ok_at TIMESTAMPTZ,
|
|
||||||
last_error TEXT,
|
|
||||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
|
||||||
);
|
|
||||||
|
|
||||||
COMMENT ON TABLE profile_tandoor_settings IS 'Persönliche Tandoor-API (Familie); Token nie systemweit';
|
|
||||||
COMMENT ON COLUMN profile_tandoor_settings.api_token IS 'Tandoor Access Token; nur Backend, nie im JSON-Export';
|
|
||||||
|
|
||||||
DO $$
|
|
||||||
BEGIN
|
|
||||||
RAISE NOTICE 'Migration 067: profile_tandoor_settings';
|
|
||||||
END $$;
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
-- Migration 068: schnellere Unmapped-Liste und Listen-Nutzung
|
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_nutrition_items_unmapped_open
|
|
||||||
ON nutrition_items (profile_id, source_name_normalized)
|
|
||||||
WHERE food_id IS NULL AND recipe_id IS NULL;
|
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_nutrition_items_profile_recipe_date
|
|
||||||
ON nutrition_items (profile_id, recipe_id, date)
|
|
||||||
WHERE recipe_id IS NOT NULL;
|
|
||||||
|
|
||||||
COMMENT ON INDEX idx_nutrition_items_unmapped_open IS
|
|
||||||
'Tagebuchzeilen ohne Lebensmittel und ohne Liste';
|
|
||||||
COMMENT ON INDEX idx_nutrition_items_profile_recipe_date IS
|
|
||||||
'Letzte Nutzung einer Liste ohne Join über den Listennamen';
|
|
||||||
|
|
||||||
DO $$
|
|
||||||
BEGIN
|
|
||||||
RAISE NOTICE 'Migration 068: nutrition unmapped/recipe indexes';
|
|
||||||
END $$;
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
-- Visibility invariant: personal foods have an owner; BLS and central admin foods do not.
|
|
||||||
|
|
||||||
UPDATE food_catalog
|
|
||||||
SET owner_profile_id = NULL
|
|
||||||
WHERE catalog_kind IN ('official_bls', 'manual_admin')
|
|
||||||
AND owner_profile_id IS NOT NULL;
|
|
||||||
|
|
||||||
UPDATE food_catalog
|
|
||||||
SET is_active = false
|
|
||||||
WHERE catalog_kind = 'manual_user'
|
|
||||||
AND owner_profile_id IS NULL;
|
|
||||||
|
|
||||||
ALTER TABLE food_catalog DROP CONSTRAINT IF EXISTS chk_food_catalog_owner;
|
|
||||||
|
|
||||||
ALTER TABLE food_catalog ADD CONSTRAINT chk_food_catalog_owner CHECK (
|
|
||||||
(catalog_kind = 'manual_user' AND owner_profile_id IS NOT NULL)
|
|
||||||
OR (catalog_kind IN ('official_bls', 'manual_admin') AND owner_profile_id IS NULL)
|
|
||||||
);
|
|
||||||
@@ -30,7 +30,6 @@ class ProfileUpdate(BaseModel):
|
|||||||
goal_bf_pct: Optional[float] = None
|
goal_bf_pct: Optional[float] = None
|
||||||
quality_filter_level: Optional[str] = None # Issue #31: Global quality filter
|
quality_filter_level: Optional[str] = None # Issue #31: Global quality filter
|
||||||
email: Optional[str] = None # Self-service; leer = entfernen; Änderung setzt Verifikation zurück
|
email: Optional[str] = None # Self-service; leer = entfernen; Änderung setzt Verifikation zurück
|
||||||
nutrition_import_conflict_policy: Optional[str] = None
|
|
||||||
|
|
||||||
|
|
||||||
# ── Tracking Models ───────────────────────────────────────────────────────────
|
# ── Tracking Models ───────────────────────────────────────────────────────────
|
||||||
|
|||||||
@@ -1,139 +0,0 @@
|
|||||||
"""
|
|
||||||
Chart-Daten für Berichts-PDF: dieselbe Logik wie /api/charts/* (Data Layer), ohne HTTP.
|
|
||||||
"""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from typing import Any, Callable
|
|
||||||
|
|
||||||
from data_layer.activity_metrics import (
|
|
||||||
build_training_type_distribution_chart_payload,
|
|
||||||
build_training_volume_chart_payload,
|
|
||||||
)
|
|
||||||
from data_layer.body_metrics import get_weight_trend_data
|
|
||||||
from data_layer.nutrition_chart_payloads import build_energy_balance_chart_payload
|
|
||||||
from data_layer.nutrition_metrics import get_nutrition_average_data
|
|
||||||
from data_layer.utils import serialize_dates
|
|
||||||
|
|
||||||
|
|
||||||
def _weight_trend_payload(profile_id: str, days: int) -> dict[str, Any]:
|
|
||||||
d = min(max(days, 7), 365)
|
|
||||||
trend_data = get_weight_trend_data(profile_id, d)
|
|
||||||
if trend_data["confidence"] == "insufficient":
|
|
||||||
return {
|
|
||||||
"chart_type": "line",
|
|
||||||
"data": {"labels": [], "datasets": []},
|
|
||||||
"metadata": {
|
|
||||||
"confidence": "insufficient",
|
|
||||||
"data_points": 0,
|
|
||||||
"message": "Nicht genug Daten für Trend-Analyse",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
series = trend_data.get("series") or []
|
|
||||||
labels = [
|
|
||||||
pt["date"].isoformat() if hasattr(pt["date"], "isoformat") else str(pt["date"]) for pt in series
|
|
||||||
]
|
|
||||||
values = [pt["weight"] for pt in series]
|
|
||||||
return {
|
|
||||||
"chart_type": "line",
|
|
||||||
"data": {
|
|
||||||
"labels": labels,
|
|
||||||
"datasets": [
|
|
||||||
{
|
|
||||||
"label": "Gewicht",
|
|
||||||
"data": values,
|
|
||||||
"borderColor": "#1D9E75",
|
|
||||||
"backgroundColor": "rgba(29, 158, 117, 0.1)",
|
|
||||||
"borderWidth": 2,
|
|
||||||
"tension": 0.4,
|
|
||||||
"fill": True,
|
|
||||||
"pointRadius": 2,
|
|
||||||
}
|
|
||||||
],
|
|
||||||
},
|
|
||||||
"metadata": serialize_dates(
|
|
||||||
{
|
|
||||||
"confidence": trend_data["confidence"],
|
|
||||||
"data_points": trend_data["data_points"],
|
|
||||||
"first_value": trend_data["first_value"],
|
|
||||||
"last_value": trend_data["last_value"],
|
|
||||||
"delta": trend_data["delta"],
|
|
||||||
"direction": trend_data["direction"],
|
|
||||||
}
|
|
||||||
),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def _macro_distribution_payload(profile_id: str, days: int) -> dict[str, Any]:
|
|
||||||
d = min(max(days, 7), 90)
|
|
||||||
macro_data = get_nutrition_average_data(profile_id, d)
|
|
||||||
if macro_data["confidence"] == "insufficient":
|
|
||||||
return {
|
|
||||||
"chart_type": "pie",
|
|
||||||
"data": {"labels": [], "datasets": []},
|
|
||||||
"metadata": {"confidence": "insufficient", "message": "Keine Ernährungsdaten vorhanden"},
|
|
||||||
}
|
|
||||||
protein_kcal = macro_data["protein_avg"] * 4
|
|
||||||
carbs_kcal = macro_data["carbs_avg"] * 4
|
|
||||||
fat_kcal = macro_data["fat_avg"] * 9
|
|
||||||
total_kcal = protein_kcal + carbs_kcal + fat_kcal
|
|
||||||
if total_kcal == 0:
|
|
||||||
return {
|
|
||||||
"chart_type": "pie",
|
|
||||||
"data": {"labels": [], "datasets": []},
|
|
||||||
"metadata": {"confidence": "insufficient", "message": "Keine Makronährstoff-Daten"},
|
|
||||||
}
|
|
||||||
protein_pct = protein_kcal / total_kcal * 100
|
|
||||||
carbs_pct = carbs_kcal / total_kcal * 100
|
|
||||||
fat_pct = fat_kcal / total_kcal * 100
|
|
||||||
return {
|
|
||||||
"chart_type": "pie",
|
|
||||||
"data": {
|
|
||||||
"labels": ["Protein", "Kohlenhydrate", "Fett"],
|
|
||||||
"datasets": [
|
|
||||||
{
|
|
||||||
"data": [round(protein_pct, 1), round(carbs_pct, 1), round(fat_pct, 1)],
|
|
||||||
"backgroundColor": ["#1D9E75", "#F59E0B", "#EF4444"],
|
|
||||||
"borderWidth": 2,
|
|
||||||
"borderColor": "#fff",
|
|
||||||
}
|
|
||||||
],
|
|
||||||
},
|
|
||||||
"metadata": {"confidence": macro_data.get("confidence", "high")},
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def _training_volume_payload(profile_id: str, window_days: int) -> dict[str, Any]:
|
|
||||||
w = max(4, min(52, window_days // 7))
|
|
||||||
return build_training_volume_chart_payload(profile_id, w)
|
|
||||||
|
|
||||||
|
|
||||||
_CHART_FETCHERS: dict[str, Callable[[str, int], dict[str, Any]]] = {
|
|
||||||
"weight_trend": _weight_trend_payload,
|
|
||||||
"energy_balance": lambda pid, d: build_energy_balance_chart_payload(pid, min(max(d, 7), 90)),
|
|
||||||
"macro_distribution": _macro_distribution_payload,
|
|
||||||
"training_volume": _training_volume_payload,
|
|
||||||
"training_type_distribution": lambda pid, d: build_training_type_distribution_chart_payload(
|
|
||||||
pid, min(max(d, 7), 90)
|
|
||||||
),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def fetch_chart_payload(chart_id: str, profile_id: str, window_days: int) -> dict[str, Any]:
|
|
||||||
fn = _CHART_FETCHERS.get(chart_id)
|
|
||||||
if not fn:
|
|
||||||
raise ValueError(f"Unbekanntes chart_id: {chart_id}")
|
|
||||||
return fn(profile_id, window_days)
|
|
||||||
|
|
||||||
|
|
||||||
CHART_CATALOG_FOR_API: list[dict[str, Any]] = [
|
|
||||||
{"id": "weight_trend", "title": "Gewichtstrend", "default_window_days": 90, "window_max": 365},
|
|
||||||
{"id": "energy_balance", "title": "Energiebilanz", "default_window_days": 28, "window_max": 90},
|
|
||||||
{"id": "macro_distribution", "title": "Makroverteilung (Ø)", "default_window_days": 28, "window_max": 90},
|
|
||||||
{"id": "training_volume", "title": "Trainingsvolumen (Wochen)", "default_window_days": 84, "window_max": 365},
|
|
||||||
{
|
|
||||||
"id": "training_type_distribution",
|
|
||||||
"title": "Trainingsart-Verteilung",
|
|
||||||
"default_window_days": 28,
|
|
||||||
"window_max": 90,
|
|
||||||
},
|
|
||||||
]
|
|
||||||
@@ -1,177 +0,0 @@
|
|||||||
"""Chart.js-ähnliche Payloads → PNG (Matplotlib). Von PDF- und Bundle-Rendering gemeinsam genutzt."""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import io
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
import matplotlib
|
|
||||||
|
|
||||||
matplotlib.use("Agg")
|
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
|
|
||||||
|
|
||||||
def _safe_float_series(raw: list, n: int) -> list[float]:
|
|
||||||
out: list[float] = []
|
|
||||||
for i in range(n):
|
|
||||||
if i >= len(raw):
|
|
||||||
out.append(0.0)
|
|
||||||
continue
|
|
||||||
v = raw[i]
|
|
||||||
try:
|
|
||||||
out.append(float(v) if v is not None else 0.0)
|
|
||||||
except (TypeError, ValueError):
|
|
||||||
out.append(0.0)
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
def _color_to_rgb(hex_or_rgba: str) -> tuple[float, float, float]:
|
|
||||||
s = (hex_or_rgba or "#333333").strip()
|
|
||||||
if s.startswith("#") and len(s) >= 9:
|
|
||||||
s = s[:7]
|
|
||||||
if s.startswith("#") and len(s) >= 7:
|
|
||||||
try:
|
|
||||||
r = int(s[1:3], 16) / 255.0
|
|
||||||
g = int(s[3:5], 16) / 255.0
|
|
||||||
b = int(s[5:7], 16) / 255.0
|
|
||||||
return (r, g, b)
|
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
return (0.12, 0.62, 0.46)
|
|
||||||
|
|
||||||
|
|
||||||
def chart_payload_to_png(payload: dict[str, Any], fig_width_in: float = 6.2, fig_height_in: float = 3.4) -> bytes:
|
|
||||||
chart_type = payload.get("chart_type") or "line"
|
|
||||||
data = payload.get("data") or {}
|
|
||||||
labels = data.get("labels") or []
|
|
||||||
datasets = data.get("datasets") or []
|
|
||||||
|
|
||||||
fig, ax = plt.subplots(figsize=(fig_width_in, fig_height_in), dpi=120)
|
|
||||||
ax.set_facecolor("#fafaf9")
|
|
||||||
fig.patch.set_facecolor("#ffffff")
|
|
||||||
|
|
||||||
if chart_type == "pie" and datasets:
|
|
||||||
ds0 = datasets[0]
|
|
||||||
values = ds0.get("data") or []
|
|
||||||
colors = ds0.get("backgroundColor") or ["#1D9E75", "#378ADD", "#D85A30"]
|
|
||||||
if labels and values and len(labels) == len(values):
|
|
||||||
ax.pie(values, labels=labels, autopct="%1.0f%%", colors=colors[: len(values)], startangle=90)
|
|
||||||
ax.axis("equal")
|
|
||||||
else:
|
|
||||||
ax.text(0.5, 0.5, "Keine Daten", ha="center", va="center", transform=ax.transAxes)
|
|
||||||
|
|
||||||
elif chart_type == "dual_axis_line" and datasets and labels:
|
|
||||||
ax2 = ax.twinx()
|
|
||||||
x = list(range(len(labels)))
|
|
||||||
step = max(1, len(labels) // 8)
|
|
||||||
h1, l1 = [], []
|
|
||||||
h2, l2 = [], []
|
|
||||||
for i, ds in enumerate(datasets):
|
|
||||||
y_raw = ds.get("data") or []
|
|
||||||
if not y_raw:
|
|
||||||
continue
|
|
||||||
y = _safe_float_series(y_raw, len(labels))
|
|
||||||
lab = ds.get("label") or f"Serie {i + 1}"
|
|
||||||
col = _color_to_rgb(str(ds.get("borderColor") or ds.get("backgroundColor") or "#333333"))
|
|
||||||
y_axis = str(ds.get("yAxisID") or "").lower()
|
|
||||||
use_right = y_axis in ("right", "weight", "y1")
|
|
||||||
use_left = not use_right
|
|
||||||
target = ax if use_left else ax2
|
|
||||||
linestyle = "--" if ds.get("borderDash") else "-"
|
|
||||||
(ln,) = target.plot(
|
|
||||||
x[: len(y)],
|
|
||||||
y,
|
|
||||||
label=lab,
|
|
||||||
color=col,
|
|
||||||
linewidth=1.8,
|
|
||||||
linestyle=linestyle,
|
|
||||||
marker="o",
|
|
||||||
markersize=2 if linestyle == "-" else 0,
|
|
||||||
)
|
|
||||||
if use_left:
|
|
||||||
h1.append(ln)
|
|
||||||
l1.append(lab)
|
|
||||||
else:
|
|
||||||
h2.append(ln)
|
|
||||||
l2.append(lab)
|
|
||||||
ax.set_xticks(x[::step])
|
|
||||||
ax.set_xticklabels([labels[j] for j in range(0, len(labels), step)], rotation=25, fontsize=7)
|
|
||||||
ax.grid(True, alpha=0.25)
|
|
||||||
ax.set_xmargin(0.02)
|
|
||||||
leg = h1 + h2
|
|
||||||
leg_l = l1 + l2
|
|
||||||
if leg:
|
|
||||||
ax.legend(leg, leg_l, loc="upper right", fontsize=7)
|
|
||||||
|
|
||||||
elif chart_type == "bar" and datasets and labels:
|
|
||||||
n = len(labels)
|
|
||||||
x = list(range(n))
|
|
||||||
stack_keys_nonnull = [ds.get("stack") for ds in datasets if (ds.get("data") or []) != []]
|
|
||||||
use_stacked = (
|
|
||||||
len(datasets) >= 2
|
|
||||||
and len(stack_keys_nonnull) == len(datasets)
|
|
||||||
and all(sk for sk in stack_keys_nonnull)
|
|
||||||
and len(set(stack_keys_nonnull)) == 1
|
|
||||||
)
|
|
||||||
if use_stacked:
|
|
||||||
bottom = [0.0] * n
|
|
||||||
for ds in datasets:
|
|
||||||
y_raw = ds.get("data") or []
|
|
||||||
yv = _safe_float_series(y_raw, n)
|
|
||||||
lab = ds.get("label") or "Serie"
|
|
||||||
col = _color_to_rgb(str(ds.get("backgroundColor") or ds.get("borderColor") or "#1D9E75"))
|
|
||||||
ax.bar(x, yv, bottom=bottom, label=lab, color=col, alpha=0.9, width=0.72)
|
|
||||||
bottom = [bottom[i] + yv[i] for i in range(n)]
|
|
||||||
ax.set_xticks(x)
|
|
||||||
ax.set_xticklabels(labels, rotation=30, fontsize=7)
|
|
||||||
else:
|
|
||||||
width = 0.8 / max(len(datasets), 1)
|
|
||||||
for i, ds in enumerate(datasets):
|
|
||||||
y_raw = ds.get("data") or []
|
|
||||||
yv = _safe_float_series(y_raw, n)
|
|
||||||
offset = (i - len(datasets) / 2 + 0.5) * width
|
|
||||||
xpos = [xj + offset for xj in x]
|
|
||||||
lab = ds.get("label") or f"Serie {i + 1}"
|
|
||||||
col = _color_to_rgb(str(ds.get("backgroundColor") or ds.get("borderColor") or "#1D9E75"))
|
|
||||||
ax.bar(xpos, yv, width=width * 0.9, label=lab, color=col, alpha=0.88)
|
|
||||||
ax.set_xticks(x)
|
|
||||||
ax.set_xticklabels(labels, rotation=30, fontsize=7)
|
|
||||||
ax.legend(loc="upper right", fontsize=7)
|
|
||||||
ax.grid(True, axis="y", alpha=0.25)
|
|
||||||
ax.set_xmargin(0.02)
|
|
||||||
|
|
||||||
elif chart_type in ("line", "scatter") and datasets:
|
|
||||||
x = range(len(labels)) if labels else []
|
|
||||||
for i, ds in enumerate(datasets):
|
|
||||||
y = ds.get("data") or []
|
|
||||||
if not y:
|
|
||||||
continue
|
|
||||||
lab = ds.get("label") or f"Serie {i + 1}"
|
|
||||||
col = _color_to_rgb(str(ds.get("borderColor") or ds.get("backgroundColor") or "#1D9E75"))
|
|
||||||
ls = "--" if ds.get("borderDash") else "-"
|
|
||||||
ax.plot(
|
|
||||||
list(x)[: len(y)],
|
|
||||||
y,
|
|
||||||
label=lab,
|
|
||||||
color=col,
|
|
||||||
linewidth=1.6,
|
|
||||||
linestyle=ls,
|
|
||||||
marker="o",
|
|
||||||
markersize=2,
|
|
||||||
)
|
|
||||||
if labels:
|
|
||||||
step = max(1, len(labels) // 8)
|
|
||||||
ax.set_xticks(list(x)[::step])
|
|
||||||
ax.set_xticklabels([labels[j] for j in range(0, len(labels), step)], rotation=25, fontsize=7)
|
|
||||||
ax.legend(loc="upper right", fontsize=7)
|
|
||||||
ax.grid(True, alpha=0.25)
|
|
||||||
ax.set_xmargin(0.02)
|
|
||||||
|
|
||||||
else:
|
|
||||||
ax.text(0.5, 0.5, "Diagrammtyp nicht unterstützt oder leer", ha="center", va="center", transform=ax.transAxes)
|
|
||||||
|
|
||||||
fig.tight_layout()
|
|
||||||
buf = io.BytesIO()
|
|
||||||
fig.savefig(buf, format="png", bbox_inches="tight", facecolor=fig.get_facecolor())
|
|
||||||
plt.close(fig)
|
|
||||||
buf.seek(0)
|
|
||||||
return buf.read()
|
|
||||||
@@ -1,130 +0,0 @@
|
|||||||
"""
|
|
||||||
PDF-Bericht aus ReportProfilePayload: ReportLab für Text/Layout, Matplotlib für Chart-Payloads.
|
|
||||||
"""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import io
|
|
||||||
import logging
|
|
||||||
from typing import Any
|
|
||||||
from xml.sax.saxutils import escape
|
|
||||||
|
|
||||||
from reportlab.lib.pagesizes import A4
|
|
||||||
from reportlab.lib.styles import getSampleStyleSheet
|
|
||||||
from reportlab.lib.units import mm
|
|
||||||
from reportlab.platypus import Image as RLImage
|
|
||||||
from reportlab.platypus import Paragraph, SimpleDocTemplate, Spacer
|
|
||||||
|
|
||||||
from db import get_cursor, get_db
|
|
||||||
from report_chart_fetch import fetch_chart_payload
|
|
||||||
from report_chart_plotting import chart_payload_to_png
|
|
||||||
from report_profile_schema import (
|
|
||||||
AiInsightBlock,
|
|
||||||
ChartBlock,
|
|
||||||
ReportProfilePayload,
|
|
||||||
SectionBlock,
|
|
||||||
VizBundleBlock,
|
|
||||||
)
|
|
||||||
from report_viz_bundle_pdf import append_viz_bundle_to_story
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
_CONTENT_TRUNCATE = 12000
|
|
||||||
|
|
||||||
|
|
||||||
def _insight_text(profile_id: str, insight_id: str | None) -> tuple[str, str]:
|
|
||||||
"""Returns (heading, body_text)."""
|
|
||||||
if not insight_id:
|
|
||||||
return (
|
|
||||||
"KI-Auswertung",
|
|
||||||
"(Noch keine Auswahl — in einer späteren Version kannst du hier eine gespeicherte KI-Analyse "
|
|
||||||
"verknüpfen.)",
|
|
||||||
)
|
|
||||||
try:
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute(
|
|
||||||
"SELECT scope, content, created FROM ai_insights WHERE id = %s AND profile_id = %s",
|
|
||||||
(insight_id, profile_id),
|
|
||||||
)
|
|
||||||
row = cur.fetchone()
|
|
||||||
if not row:
|
|
||||||
return ("KI-Auswertung", "Eintrag nicht gefunden oder keine Berechtigung.")
|
|
||||||
scope = row.get("scope") or "Analyse"
|
|
||||||
content = row.get("content") or ""
|
|
||||||
if len(content) > _CONTENT_TRUNCATE:
|
|
||||||
content = content[:_CONTENT_TRUNCATE] + "\n\n[… gekürzt …]"
|
|
||||||
created = row.get("created")
|
|
||||||
sub = f"{scope}" + (f" · {created}" if created else "")
|
|
||||||
return (sub, content)
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning("report pdf insight load failed: %s", e)
|
|
||||||
return ("KI-Auswertung", "Fehler beim Laden des Eintrags.")
|
|
||||||
|
|
||||||
|
|
||||||
def build_structured_report_pdf(
|
|
||||||
*,
|
|
||||||
profile_id: str,
|
|
||||||
profile_name: str,
|
|
||||||
payload: ReportProfilePayload,
|
|
||||||
) -> bytes:
|
|
||||||
"""Vollständiges PDF als Bytes (A4)."""
|
|
||||||
buf = io.BytesIO()
|
|
||||||
doc = SimpleDocTemplate(
|
|
||||||
buf,
|
|
||||||
pagesize=A4,
|
|
||||||
leftMargin=14 * mm,
|
|
||||||
rightMargin=14 * mm,
|
|
||||||
topMargin=16 * mm,
|
|
||||||
bottomMargin=16 * mm,
|
|
||||||
)
|
|
||||||
styles = getSampleStyleSheet()
|
|
||||||
story: list[Any] = []
|
|
||||||
|
|
||||||
title = (payload.document_title or "").strip() or f"{profile_name} – Bericht"
|
|
||||||
story.append(Paragraph(escape(title), styles["Title"]))
|
|
||||||
story.append(Spacer(1, 6 * mm))
|
|
||||||
|
|
||||||
for block in payload.blocks:
|
|
||||||
if isinstance(block, SectionBlock):
|
|
||||||
story.append(Spacer(1, 4 * mm))
|
|
||||||
story.append(Paragraph(escape(block.title), styles["Heading2"]))
|
|
||||||
story.append(Spacer(1, 2 * mm))
|
|
||||||
elif isinstance(block, VizBundleBlock):
|
|
||||||
append_viz_bundle_to_story(story, styles, profile_id, block.bundle_id, block.config)
|
|
||||||
elif isinstance(block, ChartBlock):
|
|
||||||
try:
|
|
||||||
chart = fetch_chart_payload(block.chart_id, profile_id, block.window_days)
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning("chart fetch %s: %s", block.chart_id, e)
|
|
||||||
story.append(Paragraph(f"Diagramm {block.chart_id}: Fehler bei Daten.", styles["Normal"]))
|
|
||||||
continue
|
|
||||||
meta = chart.get("metadata") or {}
|
|
||||||
if meta.get("confidence") == "insufficient":
|
|
||||||
msg = meta.get("message") or "Nicht genug Daten"
|
|
||||||
story.append(Paragraph(f"<i>{block.chart_id}</i>: {msg}", styles["Normal"]))
|
|
||||||
story.append(Spacer(1, 3 * mm))
|
|
||||||
continue
|
|
||||||
try:
|
|
||||||
png = chart_payload_to_png(chart)
|
|
||||||
img_buf = io.BytesIO(png)
|
|
||||||
iw = 170 * mm
|
|
||||||
ih = 85 * mm
|
|
||||||
story.append(RLImage(img_buf, width=iw, height=ih))
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning("chart render %s: %s", block.chart_id, e)
|
|
||||||
story.append(Paragraph(f"Diagramm {block.chart_id}: Darstellung fehlgeschlagen.", styles["Normal"]))
|
|
||||||
story.append(Spacer(1, 4 * mm))
|
|
||||||
elif isinstance(block, AiInsightBlock):
|
|
||||||
heading, body = _insight_text(profile_id, block.insight_id)
|
|
||||||
if block.title.strip():
|
|
||||||
story.append(Paragraph(escape(block.title), styles["Heading3"]))
|
|
||||||
else:
|
|
||||||
story.append(Paragraph(escape(heading), styles["Heading3"]))
|
|
||||||
for para in body.split("\n\n"):
|
|
||||||
p = (para or "").strip()
|
|
||||||
if p:
|
|
||||||
story.append(Paragraph(escape(p), styles["BodyText"]))
|
|
||||||
story.append(Spacer(1, 4 * mm))
|
|
||||||
|
|
||||||
doc.build(story)
|
|
||||||
return buf.getvalue()
|
|
||||||
@@ -1,126 +0,0 @@
|
|||||||
"""
|
|
||||||
Konfigurierbarer PDF-Bericht v1: Payload-Schema (unabhängig vom Dashboard-Layout).
|
|
||||||
|
|
||||||
Block-Typen:
|
|
||||||
- section: Überschrift
|
|
||||||
- viz_bundle: Layer-2b-Ver bundles (KPIs, Text, Charts) — gleiche Config wie Dashboard
|
|
||||||
- chart: diagramm via report_chart_fetch (chart_id + window_days)
|
|
||||||
- ai_insight: optional insight_id (UUID), sonst Platzhalter für spätere Auswahl
|
|
||||||
"""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from typing import Any, Literal, Union
|
|
||||||
|
|
||||||
from pydantic import BaseModel, Field, model_validator
|
|
||||||
|
|
||||||
from dashboard_widget_config import validate_widget_entry_config
|
|
||||||
|
|
||||||
ALLOWED_CHART_IDS: frozenset[str] = frozenset(
|
|
||||||
{
|
|
||||||
"weight_trend",
|
|
||||||
"energy_balance",
|
|
||||||
"macro_distribution",
|
|
||||||
"training_volume",
|
|
||||||
"training_type_distribution",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
_MAX_BLOCKS = 32
|
|
||||||
|
|
||||||
ALLOWED_VIZ_BUNDLE_IDS: frozenset[str] = frozenset(
|
|
||||||
{
|
|
||||||
"body_history_viz",
|
|
||||||
"nutrition_history_viz",
|
|
||||||
"fitness_history_viz",
|
|
||||||
"recovery_history_viz",
|
|
||||||
"history_overview_viz",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class SectionBlock(BaseModel):
|
|
||||||
type: Literal["section"] = "section"
|
|
||||||
title: str = Field(min_length=1, max_length=200)
|
|
||||||
|
|
||||||
|
|
||||||
class ChartBlock(BaseModel):
|
|
||||||
type: Literal["chart"] = "chart"
|
|
||||||
chart_id: str = Field(min_length=1, max_length=64)
|
|
||||||
window_days: int = Field(default=28, ge=7, le=365)
|
|
||||||
|
|
||||||
@model_validator(mode="after")
|
|
||||||
def _chart_known(self) -> ChartBlock:
|
|
||||||
if self.chart_id not in ALLOWED_CHART_IDS:
|
|
||||||
raise ValueError(f"Unbekanntes chart_id: {self.chart_id!r} (erlaubt: {sorted(ALLOWED_CHART_IDS)})")
|
|
||||||
return self
|
|
||||||
|
|
||||||
|
|
||||||
class AiInsightBlock(BaseModel):
|
|
||||||
type: Literal["ai_insight"] = "ai_insight"
|
|
||||||
title: str = Field(default="", max_length=200)
|
|
||||||
insight_id: str | None = Field(default=None, max_length=48)
|
|
||||||
|
|
||||||
|
|
||||||
class VizBundleBlock(BaseModel):
|
|
||||||
"""Gleiche Layer-2b-Bundles wie im Dashboard; config wie validate_widget_entry_config."""
|
|
||||||
|
|
||||||
type: Literal["viz_bundle"] = "viz_bundle"
|
|
||||||
bundle_id: str = Field(min_length=1, max_length=64)
|
|
||||||
config: dict[str, Any] = Field(default_factory=dict)
|
|
||||||
|
|
||||||
@model_validator(mode="after")
|
|
||||||
def _bundle_config(self) -> VizBundleBlock:
|
|
||||||
if self.bundle_id not in ALLOWED_VIZ_BUNDLE_IDS:
|
|
||||||
raise ValueError(
|
|
||||||
f"Unbekanntes bundle_id: {self.bundle_id!r} (erlaubt: {sorted(ALLOWED_VIZ_BUNDLE_IDS)})"
|
|
||||||
)
|
|
||||||
self.config = validate_widget_entry_config(self.bundle_id, self.config)
|
|
||||||
return self
|
|
||||||
|
|
||||||
|
|
||||||
class ReportProfilePayload(BaseModel):
|
|
||||||
version: Literal[1] = 1
|
|
||||||
document_title: str = Field(default="", max_length=120)
|
|
||||||
blocks: list[Union[SectionBlock, ChartBlock, AiInsightBlock, VizBundleBlock]]
|
|
||||||
|
|
||||||
@model_validator(mode="after")
|
|
||||||
def _blocks_limit(self) -> ReportProfilePayload:
|
|
||||||
if len(self.blocks) > _MAX_BLOCKS:
|
|
||||||
raise ValueError(f"Maximal {_MAX_BLOCKS} Blöcke erlaubt")
|
|
||||||
if not self.blocks:
|
|
||||||
raise ValueError("Mindestens ein Block erforderlich")
|
|
||||||
return self
|
|
||||||
|
|
||||||
def to_stored_dict(self) -> dict:
|
|
||||||
return {
|
|
||||||
"version": self.version,
|
|
||||||
"document_title": self.document_title,
|
|
||||||
"blocks": [b.model_dump(mode="json") for b in self.blocks],
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def default_report_profile_dict() -> dict:
|
|
||||||
"""Standard-Bericht beim ersten Zugriff (ohne DB-Zeile)."""
|
|
||||||
p = ReportProfilePayload(
|
|
||||||
document_title="",
|
|
||||||
blocks=[
|
|
||||||
SectionBlock(title="Verlauf — Körper"),
|
|
||||||
VizBundleBlock(bundle_id="body_history_viz", config={"chart_days": 90}),
|
|
||||||
SectionBlock(title="Verlauf — Ernährung"),
|
|
||||||
VizBundleBlock(bundle_id="nutrition_history_viz", config={"chart_days": 90}),
|
|
||||||
SectionBlock(title="Verlauf — Fitness"),
|
|
||||||
VizBundleBlock(bundle_id="fitness_history_viz", config={"chart_days": 90}),
|
|
||||||
SectionBlock(title="Verlauf — Erholung"),
|
|
||||||
VizBundleBlock(bundle_id="recovery_history_viz", config={"chart_days": 90}),
|
|
||||||
SectionBlock(title="Gesamtübersicht"),
|
|
||||||
VizBundleBlock(bundle_id="history_overview_viz", config={"chart_days": 90}),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
return p.to_stored_dict()
|
|
||||||
|
|
||||||
|
|
||||||
def parse_report_profile(raw: dict | None) -> ReportProfilePayload:
|
|
||||||
if raw is None or raw == {}:
|
|
||||||
return ReportProfilePayload.model_validate(default_report_profile_dict())
|
|
||||||
return ReportProfilePayload.model_validate(raw)
|
|
||||||
|
|
||||||
@@ -1,496 +0,0 @@
|
|||||||
"""
|
|
||||||
Layer-2b Verlauf-Bundles → PDF-Abschnitte (KPIs + eingebettete Chart-Payloads).
|
|
||||||
|
|
||||||
Gleiche Datenquellen und Config-Validierung wie Dashboard-Widgets (dashboard_widget_config).
|
|
||||||
"""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import io
|
|
||||||
import logging
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from reportlab.lib.units import mm
|
|
||||||
from reportlab.platypus import Image as RLImage
|
|
||||||
from reportlab.platypus import Paragraph, Spacer
|
|
||||||
from xml.sax.saxutils import escape
|
|
||||||
|
|
||||||
from dashboard_widget_config import validate_widget_entry_config
|
|
||||||
from data_layer.body_viz import get_body_history_viz_bundle
|
|
||||||
from data_layer.fitness_viz import get_fitness_dashboard_viz_bundle
|
|
||||||
from data_layer.history_overview_viz import get_history_overview_viz_bundle
|
|
||||||
from data_layer.nutrition_viz import get_nutrition_history_viz_bundle
|
|
||||||
from data_layer.recovery_viz import get_recovery_dashboard_viz_bundle
|
|
||||||
from data_layer.utils import safe_float
|
|
||||||
from report_chart_plotting import chart_payload_to_png
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
BUNDLE_HEADINGS: dict[str, str] = {
|
|
||||||
"body_history_viz": "Körper — Kennwerte & Verlauf",
|
|
||||||
"nutrition_history_viz": "Ernährung — Kennwerte & Charts",
|
|
||||||
"fitness_history_viz": "Fitness / Training",
|
|
||||||
"recovery_history_viz": "Erholung & Vitalwerte",
|
|
||||||
"history_overview_viz": "Gesamtübersicht & Korrelationen",
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def _add_chart_to_story(story: list, styles: dict, payload: dict[str, Any], caption: str | None = None) -> None:
|
|
||||||
meta = payload.get("metadata") or {}
|
|
||||||
if meta.get("confidence") == "insufficient":
|
|
||||||
msg = escape(meta.get("message") or "Keine Daten")
|
|
||||||
story.append(Paragraph(f"<i>{msg}</i>", styles["Normal"]))
|
|
||||||
story.append(Spacer(1, 2 * mm))
|
|
||||||
return
|
|
||||||
if caption:
|
|
||||||
story.append(Paragraph(f"<b>{escape(caption)}</b>", styles["Normal"]))
|
|
||||||
try:
|
|
||||||
png = chart_payload_to_png(payload)
|
|
||||||
story.append(RLImage(io.BytesIO(png), width=170 * mm, height=85 * mm))
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning("bundle chart png: %s", e)
|
|
||||||
story.append(Paragraph("Diagramm konnte nicht gerendert werden.", styles["Normal"]))
|
|
||||||
story.append(Spacer(1, 4 * mm))
|
|
||||||
|
|
||||||
|
|
||||||
def _append_interpretation_tiles(story: list, styles: dict, tiles: list[dict[str, Any]]) -> None:
|
|
||||||
if not tiles:
|
|
||||||
return
|
|
||||||
story.append(Paragraph("<b>Einschätzungen</b>", styles["Heading4"]))
|
|
||||||
for t in tiles:
|
|
||||||
cat = escape(str(t.get("category") or t.get("title") or "—"))
|
|
||||||
title = t.get("title")
|
|
||||||
detail = t.get("detail")
|
|
||||||
val = t.get("value")
|
|
||||||
parts = [f"<b>{cat}</b>"]
|
|
||||||
if title and str(title) != str(cat):
|
|
||||||
parts.append(escape(str(title)))
|
|
||||||
if val is not None and val != "":
|
|
||||||
parts.append(f"({escape(str(val))})")
|
|
||||||
story.append(Paragraph(" — ".join(parts), styles["Normal"]))
|
|
||||||
if detail:
|
|
||||||
story.append(Paragraph(escape(str(detail)[:500]), styles["BodyText"]))
|
|
||||||
story.append(Spacer(1, 3 * mm))
|
|
||||||
|
|
||||||
|
|
||||||
def _append_kpi_tiles_fitness_nutreco(story: list, styles: dict, tiles: list[dict[str, Any]], compact: bool) -> None:
|
|
||||||
if not tiles:
|
|
||||||
return
|
|
||||||
use = tiles[:4] if compact else tiles
|
|
||||||
story.append(Paragraph("<b>KPI-Kacheln</b>", styles["Heading4"]))
|
|
||||||
for t in use:
|
|
||||||
cat = escape(str(t.get("category") or t.get("title") or "—"))
|
|
||||||
val = escape(str(t.get("value") or "—"))
|
|
||||||
sub = t.get("sublabel") or t.get("body")
|
|
||||||
line = f"• <b>{cat}</b>: {val}"
|
|
||||||
if sub:
|
|
||||||
line += f" — {escape(str(sub)[:180])}"
|
|
||||||
story.append(Paragraph(line, styles["Normal"]))
|
|
||||||
story.append(Spacer(1, 3 * mm))
|
|
||||||
|
|
||||||
|
|
||||||
def _append_insights_lines(story: list, styles: dict, insights: list[dict[str, Any]], label: str) -> None:
|
|
||||||
if not insights:
|
|
||||||
return
|
|
||||||
story.append(Paragraph(f"<b>{escape(label)}</b>", styles["Heading4"]))
|
|
||||||
for item in insights:
|
|
||||||
title = item.get("title") or item.get("heading")
|
|
||||||
body = item.get("body") or item.get("text")
|
|
||||||
if title:
|
|
||||||
story.append(Paragraph(escape(str(title)), styles["Normal"]))
|
|
||||||
if body:
|
|
||||||
story.append(Paragraph(escape(str(body)[:600]), styles["BodyText"]))
|
|
||||||
story.append(Spacer(1, 2 * mm))
|
|
||||||
|
|
||||||
|
|
||||||
def _weight_series_payload(bundle_weight: dict[str, Any]) -> dict[str, Any] | None:
|
|
||||||
series = bundle_weight.get("series") or []
|
|
||||||
if len(series) < 2:
|
|
||||||
return None
|
|
||||||
labels = [str(p.get("date") or "") for p in series]
|
|
||||||
datasets: list[dict[str, Any]] = [
|
|
||||||
{
|
|
||||||
"label": "Gewicht (kg)",
|
|
||||||
"data": [safe_float(p.get("weight")) for p in series],
|
|
||||||
"borderColor": "#1D9E75",
|
|
||||||
}
|
|
||||||
]
|
|
||||||
if any(p.get("avg7") is not None for p in series):
|
|
||||||
datasets.append(
|
|
||||||
{
|
|
||||||
"label": "Ø 7T",
|
|
||||||
"data": [safe_float(p.get("avg7")) for p in series],
|
|
||||||
"borderColor": "#378ADD",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
if any(p.get("avg14") is not None for p in series):
|
|
||||||
datasets.append(
|
|
||||||
{
|
|
||||||
"label": "Ø 14T",
|
|
||||||
"data": [safe_float(p.get("avg14")) for p in series],
|
|
||||||
"borderColor": "#085041",
|
|
||||||
"borderDash": [6, 3],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
return {"chart_type": "line", "data": {"labels": labels, "datasets": datasets}, "metadata": {"confidence": "high"}}
|
|
||||||
|
|
||||||
|
|
||||||
def _line_payload_from_points(
|
|
||||||
points: list[dict[str, Any]],
|
|
||||||
x_key: str,
|
|
||||||
y_key: str,
|
|
||||||
label: str,
|
|
||||||
) -> dict[str, Any] | None:
|
|
||||||
if len(points) < 2:
|
|
||||||
return None
|
|
||||||
labels = [str(p.get(x_key) or "") for p in points]
|
|
||||||
ys = [safe_float(p.get(y_key)) for p in points]
|
|
||||||
return {
|
|
||||||
"chart_type": "line",
|
|
||||||
"data": {
|
|
||||||
"labels": labels,
|
|
||||||
"datasets": [{"label": label, "data": ys, "borderColor": "#1D9E75"}],
|
|
||||||
},
|
|
||||||
"metadata": {"confidence": "high"},
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def _donut_avg_to_pie_payload(donut: list[dict[str, Any]] | None) -> dict[str, Any] | None:
|
|
||||||
if not donut:
|
|
||||||
return None
|
|
||||||
labels = [str(x.get("name") or "") for x in donut]
|
|
||||||
values = [safe_float(x.get("value")) for x in donut]
|
|
||||||
colors = [str(x.get("color") or "#666666") for x in donut]
|
|
||||||
if not labels or not values or len(labels) != len(values):
|
|
||||||
return None
|
|
||||||
return {
|
|
||||||
"chart_type": "pie",
|
|
||||||
"data": {
|
|
||||||
"labels": labels,
|
|
||||||
"datasets": [{"data": values, "backgroundColor": colors}],
|
|
||||||
},
|
|
||||||
"metadata": {"confidence": "high"},
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def _daily_macros_stacked_bar_payload(daily_macros: list[dict[str, Any]]) -> dict[str, Any] | None:
|
|
||||||
if len(daily_macros) < 2:
|
|
||||||
return None
|
|
||||||
labels = [str(d.get("date") or "") for d in daily_macros]
|
|
||||||
return {
|
|
||||||
"chart_type": "bar",
|
|
||||||
"data": {
|
|
||||||
"labels": labels,
|
|
||||||
"datasets": [
|
|
||||||
{
|
|
||||||
"label": "Protein (g)",
|
|
||||||
"data": [safe_float(d.get("Protein")) for d in daily_macros],
|
|
||||||
"backgroundColor": "#4a8f72",
|
|
||||||
"stack": "daily",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"label": "Fett (g)",
|
|
||||||
"data": [safe_float(d.get("Fett")) for d in daily_macros],
|
|
||||||
"backgroundColor": "#6e8eb8",
|
|
||||||
"stack": "daily",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"label": "KH (g)",
|
|
||||||
"data": [safe_float(d.get("KH")) for d in daily_macros],
|
|
||||||
"backgroundColor": "#c17d45",
|
|
||||||
"stack": "daily",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
"metadata": {"confidence": "high"},
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def _kcal_vs_weight_dual_payload(kw: dict[str, Any]) -> dict[str, Any] | None:
|
|
||||||
pts = kw.get("points") or []
|
|
||||||
if len(pts) < 2:
|
|
||||||
return None
|
|
||||||
labels = [str(p.get("date") or "") for p in pts]
|
|
||||||
tdee = kw.get("tdee_reference_kcal")
|
|
||||||
datasets: list[dict[str, Any]] = [
|
|
||||||
{
|
|
||||||
"label": "Ø Kalorien (7T)",
|
|
||||||
"data": [safe_float(p.get("kcal_avg")) for p in pts],
|
|
||||||
"borderColor": "#EA580C",
|
|
||||||
"yAxisID": "kcal",
|
|
||||||
},
|
|
||||||
]
|
|
||||||
if tdee is not None and float(safe_float(tdee) or 0) > 0:
|
|
||||||
td = float(tdee)
|
|
||||||
datasets.append(
|
|
||||||
{
|
|
||||||
"label": "TDEE (geschätzt)",
|
|
||||||
"data": [td] * len(labels),
|
|
||||||
"borderColor": "#888888",
|
|
||||||
"yAxisID": "kcal",
|
|
||||||
"borderDash": [5, 5],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
datasets.append(
|
|
||||||
{
|
|
||||||
"label": "Gewicht (kg)",
|
|
||||||
"data": [safe_float(p.get("weight")) for p in pts],
|
|
||||||
"borderColor": "#2563EB",
|
|
||||||
"yAxisID": "weight",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
return {
|
|
||||||
"chart_type": "dual_axis_line",
|
|
||||||
"data": {"labels": labels, "datasets": datasets},
|
|
||||||
"metadata": {"confidence": "high"},
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def _append_body_bundle(story: list, styles: dict, profile_id: str, cfg: dict[str, Any]) -> None:
|
|
||||||
days = int(cfg.get("chart_days") or 30)
|
|
||||||
bundle = get_body_history_viz_bundle(profile_id, days)
|
|
||||||
story.append(Paragraph(escape(BUNDLE_HEADINGS["body_history_viz"]), styles["Heading2"]))
|
|
||||||
if bundle.get("confidence") == "insufficient":
|
|
||||||
story.append(Paragraph(escape(bundle.get("message") or "Keine Körperdaten"), styles["Normal"]))
|
|
||||||
story.append(Spacer(1, 4 * mm))
|
|
||||||
return
|
|
||||||
summ = bundle.get("summary") or {}
|
|
||||||
if summ:
|
|
||||||
w = summ.get("weight_kg")
|
|
||||||
bf = summ.get("body_fat_pct")
|
|
||||||
parts = []
|
|
||||||
if w is not None:
|
|
||||||
parts.append(f"Gewicht: {w} kg")
|
|
||||||
if bf is not None:
|
|
||||||
parts.append(f"KF%: {bf}")
|
|
||||||
if parts:
|
|
||||||
story.append(Paragraph(escape(" · ".join(parts)), styles["Normal"]))
|
|
||||||
story.append(Spacer(1, 2 * mm))
|
|
||||||
if cfg.get("show_kpis", True):
|
|
||||||
_append_interpretation_tiles(story, styles, bundle.get("interpretation_tiles") or [])
|
|
||||||
w = bundle.get("weight") or {}
|
|
||||||
if cfg.get("show_weight_chart", True):
|
|
||||||
pl = _weight_series_payload(w)
|
|
||||||
if pl:
|
|
||||||
_add_chart_to_story(story, styles, pl, "Gewicht")
|
|
||||||
cal = bundle.get("caliper") or {}
|
|
||||||
if cfg.get("show_body_fat_chart", False):
|
|
||||||
ser = cal.get("series") or []
|
|
||||||
pts = [{"date": p.get("date"), "y": p.get("body_fat_pct")} for p in ser if p.get("body_fat_pct") is not None]
|
|
||||||
pl = _line_payload_from_points(pts, "date", "y", "KF %")
|
|
||||||
if pl:
|
|
||||||
_add_chart_to_story(story, styles, pl, "Körperfett (Caliper)")
|
|
||||||
circ = bundle.get("circumference") or {}
|
|
||||||
if cfg.get("show_proportion_chart", False):
|
|
||||||
prop = circ.get("proportion_series") or []
|
|
||||||
pts = [{"date": p.get("date"), "y": p.get("v_taper_cm")} for p in prop if p.get("v_taper_cm") is not None]
|
|
||||||
pl = _line_payload_from_points(pts, "date", "y", "V-Taper (cm)")
|
|
||||||
if pl:
|
|
||||||
_add_chart_to_story(story, styles, pl, "Proportion (Brust–Taille)")
|
|
||||||
if cfg.get("show_circumference_index_chart", False):
|
|
||||||
idx = circ.get("index_series") or []
|
|
||||||
if len(idx) >= 2:
|
|
||||||
labels = [str(p.get("date") or "") for p in idx]
|
|
||||||
ds: list[dict[str, Any]] = []
|
|
||||||
for key, lab, col in (
|
|
||||||
("waist_idx", "Taille-Index", "#D85A30"),
|
|
||||||
("chest_idx", "Brust-Index", "#1D9E75"),
|
|
||||||
("belly_idx", "Bauch-Index", "#378ADD"),
|
|
||||||
):
|
|
||||||
ys = [safe_float(p.get(key)) for p in idx]
|
|
||||||
if any(v is not None for v in ys):
|
|
||||||
ds.append({"label": lab, "data": ys, "borderColor": col})
|
|
||||||
if ds:
|
|
||||||
pl = {"chart_type": "line", "data": {"labels": labels, "datasets": ds}, "metadata": {"confidence": "high"}}
|
|
||||||
_add_chart_to_story(story, styles, pl, "Umfang-Indizes")
|
|
||||||
story.append(Spacer(1, 2 * mm))
|
|
||||||
|
|
||||||
|
|
||||||
def _append_nutrition_bundle(story: list, styles: dict, profile_id: str, cfg: dict[str, Any]) -> None:
|
|
||||||
days = int(cfg.get("chart_days") or 30)
|
|
||||||
bundle = get_nutrition_history_viz_bundle(profile_id, days)
|
|
||||||
story.append(Paragraph(escape(BUNDLE_HEADINGS["nutrition_history_viz"]), styles["Heading2"]))
|
|
||||||
if not bundle.get("has_nutrition_entries"):
|
|
||||||
story.append(Paragraph(escape(bundle.get("message") or "Keine Ernährungsdaten"), styles["Normal"]))
|
|
||||||
story.append(Spacer(1, 4 * mm))
|
|
||||||
return
|
|
||||||
compact = cfg.get("kpi_detail") == "compact"
|
|
||||||
if cfg.get("show_kpis", True):
|
|
||||||
_append_kpi_tiles_fitness_nutreco(story, styles, bundle.get("kpi_tiles") or [], compact)
|
|
||||||
if cfg.get("show_heuristics", False):
|
|
||||||
h = bundle.get("nutrition_correlation_heuristics") or []
|
|
||||||
for item in h:
|
|
||||||
t = item.get("text") or item.get("title")
|
|
||||||
if t:
|
|
||||||
story.append(Paragraph(f"• {escape(str(t))}", styles["Normal"]))
|
|
||||||
story.append(Spacer(1, 2 * mm))
|
|
||||||
charts = bundle.get("chart_payloads") or {}
|
|
||||||
if cfg.get("show_calorie_balance_chart", False) or cfg.get("show_energy_protein_charts", False):
|
|
||||||
pl = charts.get("energy_balance")
|
|
||||||
if pl:
|
|
||||||
_add_chart_to_story(story, styles, pl, "Energiebilanz")
|
|
||||||
if cfg.get("show_energy_protein_charts", False) or cfg.get("show_protein_lean_chart", False):
|
|
||||||
pl = charts.get("protein_adequacy")
|
|
||||||
if pl:
|
|
||||||
_add_chart_to_story(story, styles, pl, "Protein-Adäquanz")
|
|
||||||
pl2 = charts.get("nutrition_adherence")
|
|
||||||
if pl2:
|
|
||||||
_add_chart_to_story(story, styles, pl2, "Ernährungs-Adherence")
|
|
||||||
if cfg.get("show_macro_daily_bars", False):
|
|
||||||
dm = bundle.get("daily_macros") or []
|
|
||||||
pl_dm = _daily_macros_stacked_bar_payload(dm)
|
|
||||||
if pl_dm:
|
|
||||||
_add_chart_to_story(story, styles, pl_dm, "Makroverteilung täglich (g) · Fokus Protein")
|
|
||||||
|
|
||||||
if cfg.get("show_macro_distribution_pair", False):
|
|
||||||
pie_pl = _donut_avg_to_pie_payload(bundle.get("donut_avg_pct"))
|
|
||||||
if pie_pl:
|
|
||||||
_add_chart_to_story(story, styles, pie_pl, "Ø Makro-Quote (% der Makro-kcal)")
|
|
||||||
wm = bundle.get("weekly_macro_chart")
|
|
||||||
if isinstance(wm, dict) and wm.get("chart_type"):
|
|
||||||
meta_wm = wm.get("metadata") or {}
|
|
||||||
if meta_wm.get("confidence") != "insufficient":
|
|
||||||
_add_chart_to_story(story, styles, wm, "Wöch. Makro-Verteilung (Anteil %)")
|
|
||||||
|
|
||||||
kw = bundle.get("kcal_vs_weight") or {}
|
|
||||||
if cfg.get("show_kcal_vs_weight", False) and kw.get("points"):
|
|
||||||
pl_kw = _kcal_vs_weight_dual_payload(kw)
|
|
||||||
if pl_kw:
|
|
||||||
_add_chart_to_story(story, styles, pl_kw, "Kalorien (Ø 7 Tage) vs. Gewicht")
|
|
||||||
story.append(Spacer(1, 2 * mm))
|
|
||||||
|
|
||||||
|
|
||||||
def _append_fitness_bundle(story: list, styles: dict, profile_id: str, cfg: dict[str, Any]) -> None:
|
|
||||||
days = int(cfg.get("chart_days") or 30)
|
|
||||||
bundle = get_fitness_dashboard_viz_bundle(profile_id, days)
|
|
||||||
story.append(Paragraph(escape(BUNDLE_HEADINGS["fitness_history_viz"]), styles["Heading2"]))
|
|
||||||
if not bundle.get("has_activity_entries"):
|
|
||||||
story.append(Paragraph(escape(bundle.get("message") or "Keine Aktivitätsdaten"), styles["Normal"]))
|
|
||||||
story.append(Spacer(1, 4 * mm))
|
|
||||||
return
|
|
||||||
compact = cfg.get("kpi_detail") == "compact"
|
|
||||||
if cfg.get("show_kpis", True):
|
|
||||||
_append_kpi_tiles_fitness_nutreco(story, styles, bundle.get("kpi_tiles") or [], compact)
|
|
||||||
if cfg.get("show_progress_insights", False):
|
|
||||||
_append_insights_lines(story, styles, bundle.get("progress_insights") or [], "Einschätzungen")
|
|
||||||
charts = bundle.get("charts") or {}
|
|
||||||
if cfg.get("show_chart_training_volume", True) and charts.get("training_volume"):
|
|
||||||
_add_chart_to_story(story, styles, charts["training_volume"], "Trainingsvolumen")
|
|
||||||
if cfg.get("show_chart_training_type_distribution", True) and charts.get("training_type_distribution"):
|
|
||||||
_add_chart_to_story(story, styles, charts["training_type_distribution"], "Trainingsarten")
|
|
||||||
if cfg.get("show_chart_quality_sessions", False) and charts.get("quality_sessions"):
|
|
||||||
_add_chart_to_story(story, styles, charts["quality_sessions"], "Qualitätssessions")
|
|
||||||
if cfg.get("show_chart_load_monitoring", False) and charts.get("load_monitoring"):
|
|
||||||
_add_chart_to_story(story, styles, charts["load_monitoring"], "Last / ACWR")
|
|
||||||
story.append(Spacer(1, 2 * mm))
|
|
||||||
|
|
||||||
|
|
||||||
def _append_recovery_bundle(story: list, styles: dict, profile_id: str, cfg: dict[str, Any]) -> None:
|
|
||||||
days = int(cfg.get("chart_days") or 30)
|
|
||||||
bundle = get_recovery_dashboard_viz_bundle(profile_id, days)
|
|
||||||
story.append(Paragraph(escape(BUNDLE_HEADINGS["recovery_history_viz"]), styles["Heading2"]))
|
|
||||||
if not bundle.get("has_recovery_data"):
|
|
||||||
story.append(Paragraph(escape(bundle.get("message") or "Keine Erholungsdaten"), styles["Normal"]))
|
|
||||||
story.append(Spacer(1, 4 * mm))
|
|
||||||
return
|
|
||||||
compact = cfg.get("kpi_detail") == "compact"
|
|
||||||
if cfg.get("show_kpis", True):
|
|
||||||
_append_kpi_tiles_fitness_nutreco(story, styles, bundle.get("kpi_tiles") or [], compact)
|
|
||||||
if cfg.get("show_progress_insights", False):
|
|
||||||
_append_insights_lines(story, styles, bundle.get("progress_insights") or [], "Einschätzungen")
|
|
||||||
charts = bundle.get("charts") or {}
|
|
||||||
if cfg.get("show_chart_recovery_score", True) and charts.get("recovery_score"):
|
|
||||||
_add_chart_to_story(story, styles, charts["recovery_score"], "Recovery-Score")
|
|
||||||
if cfg.get("show_chart_hrv_rhr", True) and charts.get("hrv_rhr"):
|
|
||||||
_add_chart_to_story(story, styles, charts["hrv_rhr"], "HRV / RHR")
|
|
||||||
if cfg.get("show_chart_sleep_quality", True) and charts.get("sleep_duration_quality"):
|
|
||||||
_add_chart_to_story(story, styles, charts["sleep_duration_quality"], "Schlaf Dauer & Qualität")
|
|
||||||
if cfg.get("show_chart_sleep_debt", False) and charts.get("sleep_debt"):
|
|
||||||
_add_chart_to_story(story, styles, charts["sleep_debt"], "Schlafschuld")
|
|
||||||
if cfg.get("show_vitals_extra_trends", False):
|
|
||||||
if charts.get("vital_signs_matrix"):
|
|
||||||
_add_chart_to_story(story, styles, charts["vital_signs_matrix"], "Vital-Matrix")
|
|
||||||
if charts.get("vitals_history"):
|
|
||||||
_add_chart_to_story(story, styles, charts["vitals_history"], "Vital-Trends")
|
|
||||||
story.append(Spacer(1, 2 * mm))
|
|
||||||
|
|
||||||
|
|
||||||
def _append_history_overview_bundle(story: list, styles: dict, profile_id: str, cfg: dict[str, Any]) -> None:
|
|
||||||
days = int(cfg.get("chart_days") or 30)
|
|
||||||
bundle = get_history_overview_viz_bundle(profile_id, days)
|
|
||||||
story.append(Paragraph(escape(BUNDLE_HEADINGS["history_overview_viz"]), styles["Heading2"]))
|
|
||||||
sect_keys = {
|
|
||||||
"body": cfg.get("show_section_body", True),
|
|
||||||
"nutrition": cfg.get("show_section_nutrition", True),
|
|
||||||
"fitness": cfg.get("show_section_fitness", True),
|
|
||||||
"recovery": cfg.get("show_section_recovery", True),
|
|
||||||
}
|
|
||||||
for sec in bundle.get("sections") or []:
|
|
||||||
sid = sec.get("id")
|
|
||||||
if not sect_keys.get(str(sid), True):
|
|
||||||
continue
|
|
||||||
title = escape(str(sec.get("title") or sid))
|
|
||||||
line = escape(str(sec.get("summary_line") or ""))
|
|
||||||
story.append(Paragraph(f"<b>{title}</b>: {line}", styles["Normal"]))
|
|
||||||
for it in sec.get("interpretation_short") or []:
|
|
||||||
t = it.get("title") if isinstance(it, dict) else None
|
|
||||||
if t:
|
|
||||||
story.append(Paragraph(f"• {escape(str(t))}", styles["BodyText"]))
|
|
||||||
for k in sec.get("kpi_short") or []:
|
|
||||||
if isinstance(k, dict):
|
|
||||||
cat = k.get("category") or k.get("title")
|
|
||||||
val = k.get("value")
|
|
||||||
if cat:
|
|
||||||
story.append(Paragraph(f"• {escape(str(cat))}: {escape(str(val or ''))}", styles["BodyText"]))
|
|
||||||
story.append(Spacer(1, 2 * mm))
|
|
||||||
if cfg.get("show_correlation_c1_c3", True) or cfg.get("show_drivers_c4", True):
|
|
||||||
lag = bundle.get("lag_correlations") or {}
|
|
||||||
we = lag.get("weight_energy") or {}
|
|
||||||
if we.get("available") and (we.get("interpretation") or we.get("label")):
|
|
||||||
lab = escape(str(we.get("label") or "C1"))
|
|
||||||
interp = escape(str(we.get("interpretation") or "").strip())
|
|
||||||
if interp:
|
|
||||||
story.append(Paragraph(f"{lab}: {interp}", styles["Normal"]))
|
|
||||||
charts = bundle.get("chart_payloads") or {}
|
|
||||||
if cfg.get("show_correlation_c1_c3", True):
|
|
||||||
for key, cap in (
|
|
||||||
("c1_weight_energy", "Korrelation Gewicht / Energie"),
|
|
||||||
("c2_protein_lbm", "Protein / Magermasse"),
|
|
||||||
("c3_load_vitals", "Last / Vitalwerte"),
|
|
||||||
):
|
|
||||||
pl = charts.get(key)
|
|
||||||
if pl:
|
|
||||||
_add_chart_to_story(story, styles, pl, cap)
|
|
||||||
if cfg.get("show_drivers_c4", True):
|
|
||||||
pl = charts.get("c4_recovery_performance")
|
|
||||||
if pl:
|
|
||||||
_add_chart_to_story(story, styles, pl, "Top-Treiber")
|
|
||||||
drv = (bundle.get("lag_correlations") or {}).get("recovery_performance") or {}
|
|
||||||
for d in (drv.get("drivers") or [])[:12]:
|
|
||||||
if isinstance(d, dict):
|
|
||||||
lab = d.get("label") or d.get("factor")
|
|
||||||
val = d.get("impact") or d.get("score")
|
|
||||||
if lab:
|
|
||||||
story.append(Paragraph(f"• {escape(str(lab))}: {escape(str(val or ''))}", styles["Normal"]))
|
|
||||||
story.append(Spacer(1, 2 * mm))
|
|
||||||
|
|
||||||
|
|
||||||
def append_viz_bundle_to_story(
|
|
||||||
story: list,
|
|
||||||
styles: dict,
|
|
||||||
profile_id: str,
|
|
||||||
bundle_id: str,
|
|
||||||
raw_config: dict[str, Any],
|
|
||||||
) -> None:
|
|
||||||
cfg = validate_widget_entry_config(bundle_id, raw_config)
|
|
||||||
if bundle_id == "body_history_viz":
|
|
||||||
_append_body_bundle(story, styles, profile_id, cfg)
|
|
||||||
elif bundle_id == "nutrition_history_viz":
|
|
||||||
_append_nutrition_bundle(story, styles, profile_id, cfg)
|
|
||||||
elif bundle_id == "fitness_history_viz":
|
|
||||||
_append_fitness_bundle(story, styles, profile_id, cfg)
|
|
||||||
elif bundle_id == "recovery_history_viz":
|
|
||||||
_append_recovery_bundle(story, styles, profile_id, cfg)
|
|
||||||
elif bundle_id == "history_overview_viz":
|
|
||||||
_append_history_overview_bundle(story, styles, profile_id, cfg)
|
|
||||||
else:
|
|
||||||
story.append(Paragraph(escape(f"Unbekanntes Bundle: {bundle_id}"), styles["Normal"]))
|
|
||||||
@@ -10,6 +10,3 @@ slowapi==0.1.9
|
|||||||
psycopg2-binary==2.9.9
|
psycopg2-binary==2.9.9
|
||||||
python-dateutil==2.9.0
|
python-dateutil==2.9.0
|
||||||
tzdata>=2024.1 # ZoneInfo (Europe/Berlin) auch unter Windows
|
tzdata>=2024.1 # ZoneInfo (Europe/Berlin) auch unter Windows
|
||||||
matplotlib==3.8.4
|
|
||||||
reportlab==4.2.0
|
|
||||||
openpyxl==3.1.5
|
|
||||||
@@ -617,24 +617,6 @@ async def import_activity_csv(file: UploadFile=File(...), x_profile_id: Optional
|
|||||||
Persistenz läuft über activity_persistence_orchestrator — gleiche Schicht wie Universal-CSV.
|
Persistenz läuft über activity_persistence_orchestrator — gleiche Schicht wie Universal-CSV.
|
||||||
"""
|
"""
|
||||||
pid = get_pid(x_profile_id)
|
pid = get_pid(x_profile_id)
|
||||||
|
|
||||||
# Feature-Enforcement (wie manueller Create + nutrition/import-csv): activity_entries
|
|
||||||
access = check_feature_access(pid, "activity_entries")
|
|
||||||
log_feature_usage(pid, "activity_entries", access, "import_csv")
|
|
||||||
if not access["allowed"]:
|
|
||||||
logger.warning(
|
|
||||||
f"[FEATURE-LIMIT] User {pid} blocked: "
|
|
||||||
f"activity_entries {access['reason']} (used: {access['used']}, limit: {access['limit']})"
|
|
||||||
)
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=403,
|
|
||||||
detail=(
|
|
||||||
f"Limit erreicht: Du hast das Kontingent für Aktivitätseinträge überschritten "
|
|
||||||
f"({access['used']}/{access['limit']}). "
|
|
||||||
f"Bitte kontaktiere den Admin oder warte bis zum nächsten Reset."
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
raw = await file.read()
|
raw = await file.read()
|
||||||
try: text = raw.decode('utf-8')
|
try: text = raw.decode('utf-8')
|
||||||
except: text = raw.decode('latin-1')
|
except: text = raw.decode('latin-1')
|
||||||
@@ -747,8 +729,4 @@ async def import_activity_csv(file: UploadFile=File(...), x_profile_id: Optional
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"Import row failed: {e}")
|
logger.warning(f"Import row failed: {e}")
|
||||||
skipped+=1
|
skipped+=1
|
||||||
|
|
||||||
for _ in range(inserted):
|
|
||||||
increment_feature_usage(pid, "activity_entries")
|
|
||||||
|
|
||||||
return {"inserted":inserted,"skipped":skipped,"message":f"{inserted} Trainings importiert"}
|
return {"inserted":inserted,"skipped":skipped,"message":f"{inserted} Trainings importiert"}
|
||||||
@@ -14,12 +14,7 @@ from fastapi import APIRouter, HTTPException, Depends
|
|||||||
from db import get_db, get_cursor, r2d
|
from db import get_db, get_cursor, r2d
|
||||||
from auth import require_admin, hash_pin
|
from auth import require_admin, hash_pin
|
||||||
from models import AdminProfileUpdate
|
from models import AdminProfileUpdate
|
||||||
from dashboard_layout_schema import (
|
from dashboard_layout_schema import ALLOWED_WIDGET_IDS, DashboardLayoutPayload, product_default_layout_dict
|
||||||
ALLOWED_WIDGET_IDS,
|
|
||||||
DashboardLayoutPayload,
|
|
||||||
merge_missing_catalog_widgets,
|
|
||||||
product_default_layout_dict,
|
|
||||||
)
|
|
||||||
from dashboard_widget_entitlements import widgets_catalog_admin_payload
|
from dashboard_widget_entitlements import widgets_catalog_admin_payload
|
||||||
from widget_catalog import WIDGET_CATALOG
|
from widget_catalog import WIDGET_CATALOG
|
||||||
from widget_feature_requirements_db import (
|
from widget_feature_requirements_db import (
|
||||||
@@ -189,7 +184,7 @@ def admin_get_dashboard_product_default(session: dict = Depends(require_admin)):
|
|||||||
"""Aktueller Produkt-Dashboard-Standard (DB oder Code)."""
|
"""Aktueller Produkt-Dashboard-Standard (DB oder Code)."""
|
||||||
_ = session
|
_ = session
|
||||||
with get_db() as conn:
|
with get_db() as conn:
|
||||||
layout = merge_missing_catalog_widgets(get_product_default_base_dict(conn))
|
layout = get_product_default_base_dict(conn)
|
||||||
from_database = get_stored_product_default_validated(conn) is not None
|
from_database = get_stored_product_default_validated(conn) is not None
|
||||||
code_ref = product_default_layout_dict()
|
code_ref = product_default_layout_dict()
|
||||||
return {
|
return {
|
||||||
@@ -222,7 +217,7 @@ def admin_delete_dashboard_product_default(session: dict = Depends(require_admin
|
|||||||
_ = session
|
_ = session
|
||||||
with get_db() as conn:
|
with get_db() as conn:
|
||||||
delete_product_default_override(conn)
|
delete_product_default_override(conn)
|
||||||
layout = merge_missing_catalog_widgets(get_product_default_base_dict(conn))
|
layout = get_product_default_base_dict(conn)
|
||||||
return {"ok": True, "layout": layout, "from_database": False}
|
return {"ok": True, "layout": layout, "from_database": False}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,399 +0,0 @@
|
|||||||
"""Admin BLS catalog import and attribute/food maintenance."""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile
|
|
||||||
from fastapi.concurrency import run_in_threadpool
|
|
||||||
from pydantic import BaseModel
|
|
||||||
|
|
||||||
from auth import require_admin
|
|
||||||
from bls.import_service import upsert_attributes, upsert_foods
|
|
||||||
from bls.jobs import create_and_check, get_job, start_apply
|
|
||||||
from bls.parser import parse_components_xlsx, parse_foods_xlsx
|
|
||||||
from db import get_cursor, get_db, r2d
|
|
||||||
|
|
||||||
MAX_IMPORT_BYTES = 50 * 1024 * 1024
|
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/admin/bls", tags=["admin", "bls"])
|
|
||||||
|
|
||||||
|
|
||||||
class AttributeCreate(BaseModel):
|
|
||||||
attr_key: str
|
|
||||||
name_de: str
|
|
||||||
name_en: Optional[str] = None
|
|
||||||
unit: Optional[str] = None
|
|
||||||
category: Optional[str] = None
|
|
||||||
data_type: str = "num_per_100g"
|
|
||||||
enum_values: Optional[list] = None
|
|
||||||
|
|
||||||
|
|
||||||
class AttributeUpdate(BaseModel):
|
|
||||||
name_de: Optional[str] = None
|
|
||||||
name_en: Optional[str] = None
|
|
||||||
unit: Optional[str] = None
|
|
||||||
category: Optional[str] = None
|
|
||||||
sort_order: Optional[int] = None
|
|
||||||
|
|
||||||
|
|
||||||
class ManualFoodCreate(BaseModel):
|
|
||||||
name_de: str
|
|
||||||
name_en: Optional[str] = None
|
|
||||||
macros_per_100g: Optional[dict] = None
|
|
||||||
attributes: Optional[dict] = None
|
|
||||||
serving_g: Optional[float] = None
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/status")
|
|
||||||
def bls_status(session: dict = Depends(require_admin)):
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute("SELECT COUNT(*) AS n FROM food_catalog WHERE catalog_kind = 'official_bls'")
|
|
||||||
foods = cur.fetchone()["n"]
|
|
||||||
cur.execute("SELECT COUNT(*) AS n FROM food_attributes WHERE origin = 'official_bls'")
|
|
||||||
attrs = cur.fetchone()["n"]
|
|
||||||
cur.execute("SELECT COUNT(*) AS n FROM food_catalog WHERE catalog_kind <> 'official_bls'")
|
|
||||||
manual = cur.fetchone()["n"]
|
|
||||||
cur.execute("SELECT MAX(updated_at) AS last_updated FROM food_catalog WHERE catalog_kind = 'official_bls'")
|
|
||||||
last = cur.fetchone()["last_updated"]
|
|
||||||
return {
|
|
||||||
"official_foods": foods,
|
|
||||||
"official_attributes": attrs,
|
|
||||||
"manual_foods": manual,
|
|
||||||
"last_updated": last,
|
|
||||||
"source": "Max Rubner-Institut, BLS 4.0 (frei verfügbar)",
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/import/components")
|
|
||||||
async def import_components(
|
|
||||||
file: UploadFile = File(...),
|
|
||||||
dry_run: bool = True,
|
|
||||||
session: dict = Depends(require_admin),
|
|
||||||
):
|
|
||||||
raw = await file.read()
|
|
||||||
if not raw:
|
|
||||||
raise HTTPException(400, "Leere Datei")
|
|
||||||
try:
|
|
||||||
attrs = await run_in_threadpool(parse_components_xlsx, raw)
|
|
||||||
except Exception as e:
|
|
||||||
raise HTTPException(400, f"Components-Datei unlesbar: {e}") from e
|
|
||||||
if dry_run:
|
|
||||||
return {"dry_run": True, "attributes": len(attrs), "sample": attrs[:8]}
|
|
||||||
|
|
||||||
def apply():
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
return upsert_attributes(cur, attrs)
|
|
||||||
|
|
||||||
stats = await run_in_threadpool(apply)
|
|
||||||
return {"dry_run": False, **stats}
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/import/foods")
|
|
||||||
async def import_foods(
|
|
||||||
file: UploadFile = File(...),
|
|
||||||
dry_run: bool = True,
|
|
||||||
session: dict = Depends(require_admin),
|
|
||||||
):
|
|
||||||
raw = await file.read()
|
|
||||||
if not raw:
|
|
||||||
raise HTTPException(400, "Leere Datei")
|
|
||||||
try:
|
|
||||||
parsed = await run_in_threadpool(parse_foods_xlsx, raw)
|
|
||||||
except Exception as e:
|
|
||||||
raise HTTPException(400, f"Datendatei unlesbar: {e}") from e
|
|
||||||
foods = parsed["foods"]
|
|
||||||
if dry_run:
|
|
||||||
return {
|
|
||||||
"dry_run": True,
|
|
||||||
"foods": len(foods),
|
|
||||||
"attribute_columns": len(parsed["attribute_headers"]),
|
|
||||||
"sample": [
|
|
||||||
{"bls_code": f["bls_code"], "name_de": f["name_de"]}
|
|
||||||
for f in foods[:8]
|
|
||||||
],
|
|
||||||
}
|
|
||||||
|
|
||||||
def apply():
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
return upsert_foods(cur, foods)
|
|
||||||
|
|
||||||
stats = await run_in_threadpool(apply)
|
|
||||||
return {"dry_run": False, **stats}
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/import/jobs")
|
|
||||||
async def start_import_job(
|
|
||||||
kind: str,
|
|
||||||
file: UploadFile = File(...),
|
|
||||||
session: dict = Depends(require_admin),
|
|
||||||
):
|
|
||||||
if kind not in ("components", "foods"):
|
|
||||||
raise HTTPException(400, "kind muss components oder foods sein")
|
|
||||||
raw = await file.read()
|
|
||||||
if not raw:
|
|
||||||
raise HTTPException(400, "Leere Datei")
|
|
||||||
if len(raw) > MAX_IMPORT_BYTES:
|
|
||||||
raise HTTPException(400, "Datei größer als 50 MB")
|
|
||||||
job_id = create_and_check(kind, raw)
|
|
||||||
return {"id": job_id, "status": "checking"}
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/import/jobs/{job_id}")
|
|
||||||
def import_job_status(job_id: str, session: dict = Depends(require_admin)):
|
|
||||||
job = get_job(job_id)
|
|
||||||
if not job:
|
|
||||||
raise HTTPException(404, "Import-Job nicht gefunden")
|
|
||||||
return job
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/import/jobs/{job_id}/apply")
|
|
||||||
def apply_import_job(job_id: str, session: dict = Depends(require_admin)):
|
|
||||||
try:
|
|
||||||
start_apply(job_id)
|
|
||||||
except KeyError:
|
|
||||||
raise HTTPException(404, "Import-Job nicht gefunden") from None
|
|
||||||
except ValueError as e:
|
|
||||||
raise HTTPException(409, str(e)) from e
|
|
||||||
return get_job(job_id)
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/foods")
|
|
||||||
def admin_list_foods(
|
|
||||||
q: Optional[str] = None,
|
|
||||||
kind: Optional[str] = None,
|
|
||||||
limit: int = 50,
|
|
||||||
session: dict = Depends(require_admin),
|
|
||||||
):
|
|
||||||
limit = min(max(limit, 1), 200)
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
conds = ["is_active = true"]
|
|
||||||
params: list = []
|
|
||||||
if kind == "manual":
|
|
||||||
conds.append("catalog_kind IN ('manual_user', 'manual_admin')")
|
|
||||||
elif kind:
|
|
||||||
conds.append("catalog_kind = %s")
|
|
||||||
params.append(kind)
|
|
||||||
if q:
|
|
||||||
conds.append("(name_de ILIKE %s OR COALESCE(name_en,'') ILIKE %s OR COALESCE(bls_code,'') ILIKE %s)")
|
|
||||||
like = f"%{q}%"
|
|
||||||
params.extend([like, like, like])
|
|
||||||
params.append(limit)
|
|
||||||
cur.execute(
|
|
||||||
f"""
|
|
||||||
SELECT id, bls_code, name_de, name_en, catalog_kind, food_group, bls_version, owner_profile_id
|
|
||||||
FROM food_catalog
|
|
||||||
WHERE {' AND '.join(conds)}
|
|
||||||
ORDER BY name_de
|
|
||||||
LIMIT %s
|
|
||||||
""",
|
|
||||||
params,
|
|
||||||
)
|
|
||||||
return [r2d(r) for r in cur.fetchall()]
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/foods/{food_id}")
|
|
||||||
def admin_food_detail(food_id: str, editor: bool = False, session: dict = Depends(require_admin)):
|
|
||||||
from data_layer.nutrition_items import resolve_food_attributes
|
|
||||||
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute("SELECT * FROM food_catalog WHERE id = %s", (food_id,))
|
|
||||||
row = cur.fetchone()
|
|
||||||
if not row:
|
|
||||||
raise HTTPException(404, "Lebensmittel nicht gefunden")
|
|
||||||
food = dict(row)
|
|
||||||
if editor:
|
|
||||||
from data_layer.food_attributes import editor_payload_for_food
|
|
||||||
from data_layer.food_visibility import attach_visibility
|
|
||||||
|
|
||||||
editable = food.get("catalog_kind") in ("manual_admin", "manual_user")
|
|
||||||
return attach_visibility(editor_payload_for_food(cur, food, editable=editable))
|
|
||||||
attrs = resolve_food_attributes(cur, food_id)
|
|
||||||
return {**r2d(row), "attributes": attrs}
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/foods/manual")
|
|
||||||
def admin_create_manual_food(body: ManualFoodCreate, session: dict = Depends(require_admin)):
|
|
||||||
from data_layer.food_attributes import macros_and_attributes_to_values, write_numeric_attributes
|
|
||||||
from data_layer.food_mapping import normalize_food_name
|
|
||||||
from data_layer.food_visibility import attach_visibility
|
|
||||||
|
|
||||||
name = (body.name_de or "").strip()
|
|
||||||
if not name:
|
|
||||||
raise HTTPException(400, "Name fehlt")
|
|
||||||
serving = body.serving_g if body.serving_g and body.serving_g > 0 else None
|
|
||||||
values = macros_and_attributes_to_values(body.macros_per_100g, body.attributes, serving)
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO food_catalog (name_de, name_en, catalog_kind, source, external_key)
|
|
||||||
VALUES (%s, %s, 'manual_admin', 'manual', %s)
|
|
||||||
RETURNING *
|
|
||||||
""",
|
|
||||||
(name, body.name_en, f"man-admin-{normalize_food_name(name)[:40]}"),
|
|
||||||
)
|
|
||||||
food = r2d(cur.fetchone())
|
|
||||||
write_numeric_attributes(cur, food["id"], values)
|
|
||||||
from data_layer.food_suggest import invalidate_suggest_index
|
|
||||||
invalidate_suggest_index()
|
|
||||||
return attach_visibility(food)
|
|
||||||
|
|
||||||
|
|
||||||
@router.put("/foods/manual/{food_id}")
|
|
||||||
def admin_update_manual_food(food_id: str, body: ManualFoodCreate, session: dict = Depends(require_admin)):
|
|
||||||
from collections import defaultdict
|
|
||||||
|
|
||||||
from data_layer.food_attributes import (
|
|
||||||
editor_payload_for_food,
|
|
||||||
macros_and_attributes_to_values,
|
|
||||||
replace_numeric_attributes,
|
|
||||||
)
|
|
||||||
from data_layer.food_visibility import attach_visibility
|
|
||||||
from data_layer.nutrition_items import profiles_dates_using_food, schedule_daily_nutrient_rebuild
|
|
||||||
|
|
||||||
name = (body.name_de or "").strip()
|
|
||||||
if not name:
|
|
||||||
raise HTTPException(400, "Name fehlt")
|
|
||||||
serving = body.serving_g if body.serving_g and body.serving_g > 0 else None
|
|
||||||
values = macros_and_attributes_to_values(body.macros_per_100g, body.attributes, serving)
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id, name_de, name_en, catalog_kind, owner_profile_id
|
|
||||||
FROM food_catalog
|
|
||||||
WHERE id = %s AND catalog_kind IN ('manual_admin', 'manual_user') AND is_active = true
|
|
||||||
""",
|
|
||||||
(food_id,),
|
|
||||||
)
|
|
||||||
food = cur.fetchone()
|
|
||||||
if not food:
|
|
||||||
raise HTTPException(404, "Manuelles Lebensmittel nicht gefunden")
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
UPDATE food_catalog
|
|
||||||
SET name_de = %s, name_en = %s, updated_at = NOW()
|
|
||||||
WHERE id = %s
|
|
||||||
RETURNING id, name_de, name_en, catalog_kind, owner_profile_id
|
|
||||||
""",
|
|
||||||
(name, body.name_en, food_id),
|
|
||||||
)
|
|
||||||
updated = dict(cur.fetchone())
|
|
||||||
replace_numeric_attributes(cur, food_id, values)
|
|
||||||
pairs = profiles_dates_using_food(cur, food_id)
|
|
||||||
payload = attach_visibility(editor_payload_for_food(cur, updated, editable=True))
|
|
||||||
from data_layer.food_suggest import invalidate_suggest_index
|
|
||||||
invalidate_suggest_index()
|
|
||||||
by_profile = defaultdict(list)
|
|
||||||
for pid, day in pairs:
|
|
||||||
by_profile[pid].append(day)
|
|
||||||
for pid, dates in by_profile.items():
|
|
||||||
schedule_daily_nutrient_rebuild(pid, dates, f"Zentrales Lebensmittel {name}")
|
|
||||||
payload["rebuild_pending"] = len(pairs)
|
|
||||||
return payload
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/attributes")
|
|
||||||
def admin_list_attributes(session: dict = Depends(require_admin)):
|
|
||||||
from bls.attribute_labels import annotate_attribute, sort_attributes
|
|
||||||
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id, attr_key, name_de, name_en, unit, category, data_type, origin, sort_order
|
|
||||||
FROM food_attributes
|
|
||||||
WHERE is_active = true
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
return sort_attributes([annotate_attribute(r2d(r)) for r in cur.fetchall()])
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/attributes/fill-labels")
|
|
||||||
def admin_fill_attribute_labels(session: dict = Depends(require_admin)):
|
|
||||||
from bls.attribute_labels import fill_placeholder_labels
|
|
||||||
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
return fill_placeholder_labels(cur)
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/attributes")
|
|
||||||
def admin_create_attribute(body: AttributeCreate, session: dict = Depends(require_admin)):
|
|
||||||
key = body.attr_key.strip().upper().replace(" ", "_")
|
|
||||||
if not key:
|
|
||||||
raise HTTPException(400, "attr_key fehlt")
|
|
||||||
if body.data_type not in ("num_per_100g", "boolean", "text", "enum"):
|
|
||||||
raise HTTPException(400, "Ungültiger data_type")
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
try:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO food_attributes
|
|
||||||
(attr_key, name_de, name_en, unit, category, data_type, enum_values, origin, sort_order)
|
|
||||||
VALUES (%s, %s, %s, %s, %s, %s, %s, 'extension', 9000)
|
|
||||||
RETURNING *
|
|
||||||
""",
|
|
||||||
(
|
|
||||||
key, body.name_de, body.name_en, body.unit, body.category,
|
|
||||||
body.data_type, None if not body.enum_values else body.enum_values,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
except Exception as e:
|
|
||||||
raise HTTPException(409, f"Attribut existiert bereits oder ist ungültig: {e}") from e
|
|
||||||
return r2d(cur.fetchone())
|
|
||||||
|
|
||||||
|
|
||||||
@router.put("/attributes/{attr_id}")
|
|
||||||
def admin_update_attribute(attr_id: int, body: AttributeUpdate, session: dict = Depends(require_admin)):
|
|
||||||
fields = []
|
|
||||||
params: list = []
|
|
||||||
if body.name_de is not None:
|
|
||||||
name = body.name_de.strip()
|
|
||||||
if not name:
|
|
||||||
raise HTTPException(400, "Name fehlt")
|
|
||||||
fields.append("name_de = %s")
|
|
||||||
params.append(name)
|
|
||||||
if body.name_en is not None:
|
|
||||||
fields.append("name_en = %s")
|
|
||||||
params.append(body.name_en.strip() or None)
|
|
||||||
if body.unit is not None:
|
|
||||||
fields.append("unit = %s")
|
|
||||||
params.append(body.unit.strip() or None)
|
|
||||||
if body.category is not None:
|
|
||||||
fields.append("category = %s")
|
|
||||||
params.append(body.category.strip() or None)
|
|
||||||
if body.sort_order is not None:
|
|
||||||
fields.append("sort_order = %s")
|
|
||||||
params.append(body.sort_order)
|
|
||||||
if not fields:
|
|
||||||
raise HTTPException(400, "Keine Änderung")
|
|
||||||
fields.append("updated_at = NOW()")
|
|
||||||
params.append(attr_id)
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute(
|
|
||||||
f"""
|
|
||||||
UPDATE food_attributes
|
|
||||||
SET {', '.join(fields)}
|
|
||||||
WHERE id = %s AND is_active = true
|
|
||||||
RETURNING id, attr_key, name_de, name_en, unit, category, data_type, origin, sort_order
|
|
||||||
""",
|
|
||||||
params,
|
|
||||||
)
|
|
||||||
row = cur.fetchone()
|
|
||||||
if not row:
|
|
||||||
raise HTTPException(404, "Stoff nicht gefunden")
|
|
||||||
from bls.attribute_labels import annotate_attribute
|
|
||||||
return annotate_attribute(r2d(row))
|
|
||||||
|
|
||||||
|
|
||||||
def _write_manual_macros(cur, food_id: str, macros: dict | None) -> None:
|
|
||||||
from data_layer.food_attributes import macros_and_attributes_to_values, write_numeric_attributes
|
|
||||||
write_numeric_attributes(cur, food_id, macros_and_attributes_to_values(macros, None))
|
|
||||||
@@ -1,166 +0,0 @@
|
|||||||
"""Admin CRUD for food_name_mappings."""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, HTTPException
|
|
||||||
from pydantic import BaseModel
|
|
||||||
|
|
||||||
from auth import require_admin
|
|
||||||
from data_layer.food_mapping import apply_mapping_to_items, upsert_food_mapping
|
|
||||||
from data_layer.nutrition_items import dates_for_normalized_name, rebuild_daily_nutrients
|
|
||||||
from db import get_cursor, get_db, r2d
|
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/admin/food-mappings", tags=["admin", "food-mappings"])
|
|
||||||
|
|
||||||
|
|
||||||
class FoodMappingCreate(BaseModel):
|
|
||||||
source_name: str
|
|
||||||
food_id: str
|
|
||||||
profile_id: Optional[str] = None
|
|
||||||
source_system: str = "fddb"
|
|
||||||
|
|
||||||
|
|
||||||
class FoodMappingUpdate(BaseModel):
|
|
||||||
food_id: str
|
|
||||||
grams_per_unit: Optional[float] = None
|
|
||||||
source_unit: Optional[str] = None
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("")
|
|
||||||
def list_food_mappings(
|
|
||||||
profile_id: Optional[str] = None,
|
|
||||||
global_only: bool = False,
|
|
||||||
session: dict = Depends(require_admin),
|
|
||||||
):
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
q = """
|
|
||||||
SELECT m.id, m.source_name_raw, m.source_name_normalized, m.food_id,
|
|
||||||
m.profile_id, m.source, m.created_at, m.updated_at,
|
|
||||||
f.name_de AS food_name_de, f.bls_code, f.catalog_kind
|
|
||||||
FROM food_name_mappings m
|
|
||||||
JOIN food_catalog f ON f.id = m.food_id
|
|
||||||
"""
|
|
||||||
conds, params = [], []
|
|
||||||
if global_only:
|
|
||||||
conds.append("m.profile_id IS NULL")
|
|
||||||
elif profile_id:
|
|
||||||
conds.append("m.profile_id = %s")
|
|
||||||
params.append(profile_id)
|
|
||||||
if conds:
|
|
||||||
q += " WHERE " + " AND ".join(conds)
|
|
||||||
q += " ORDER BY m.source_name_normalized"
|
|
||||||
cur.execute(q, params)
|
|
||||||
from data_layer.food_visibility import attach_visibility
|
|
||||||
return [attach_visibility(r2d(r)) for r in cur.fetchall()]
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/stats/coverage")
|
|
||||||
def mapping_coverage(session: dict = Depends(require_admin)):
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT
|
|
||||||
COUNT(*) AS total_items,
|
|
||||||
COUNT(food_id) AS mapped_items,
|
|
||||||
COUNT(*) - COUNT(food_id) AS unmapped_items,
|
|
||||||
COUNT(DISTINCT source_name_normalized) AS unique_names,
|
|
||||||
COUNT(DISTINCT CASE WHEN food_id IS NULL THEN source_name_normalized END) AS unmapped_names
|
|
||||||
FROM nutrition_items
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
return r2d(cur.fetchone())
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("")
|
|
||||||
def create_food_mapping(body: FoodMappingCreate, session: dict = Depends(require_admin)):
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
from data_layer.food_visibility import require_mapping_target
|
|
||||||
try:
|
|
||||||
require_mapping_target(cur, body.food_id, body.profile_id or None)
|
|
||||||
except LookupError as e:
|
|
||||||
raise HTTPException(404, str(e)) from e
|
|
||||||
except PermissionError as e:
|
|
||||||
raise HTTPException(400, str(e)) from e
|
|
||||||
mid = upsert_food_mapping(
|
|
||||||
cur,
|
|
||||||
source_name_raw=body.source_name,
|
|
||||||
food_id=body.food_id,
|
|
||||||
profile_id=body.profile_id or None,
|
|
||||||
source="admin",
|
|
||||||
source_system=body.source_system,
|
|
||||||
)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT m.*, f.name_de AS food_name_de, f.bls_code
|
|
||||||
FROM food_name_mappings m
|
|
||||||
JOIN food_catalog f ON f.id = m.food_id
|
|
||||||
WHERE m.id = %s
|
|
||||||
""",
|
|
||||||
(mid,),
|
|
||||||
)
|
|
||||||
return r2d(cur.fetchone())
|
|
||||||
|
|
||||||
|
|
||||||
@router.put("/{mapping_id}")
|
|
||||||
def update_food_mapping(mapping_id: int, body: FoodMappingUpdate, session: dict = Depends(require_admin)):
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id, source_name_raw, source_name_normalized, profile_id
|
|
||||||
FROM food_name_mappings WHERE id = %s
|
|
||||||
""",
|
|
||||||
(mapping_id,),
|
|
||||||
)
|
|
||||||
row = cur.fetchone()
|
|
||||||
if not row:
|
|
||||||
raise HTTPException(404, "Mapping nicht gefunden")
|
|
||||||
cur.execute("SELECT id, catalog_kind, owner_profile_id, is_active FROM food_catalog WHERE id = %s AND is_active = true", (body.food_id,))
|
|
||||||
food = cur.fetchone()
|
|
||||||
if not food:
|
|
||||||
raise HTTPException(404, "Lebensmittel nicht gefunden")
|
|
||||||
from data_layer.food_visibility import require_mapping_target
|
|
||||||
try:
|
|
||||||
require_mapping_target(cur, body.food_id, row.get("profile_id"))
|
|
||||||
except LookupError as e:
|
|
||||||
raise HTTPException(404, str(e)) from e
|
|
||||||
except PermissionError as e:
|
|
||||||
raise HTTPException(400, str(e)) from e
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
UPDATE food_name_mappings
|
|
||||||
SET food_id = %s, grams_per_unit = COALESCE(%s, grams_per_unit),
|
|
||||||
source_unit = COALESCE(%s, source_unit), source = 'admin', updated_at = NOW()
|
|
||||||
WHERE id = %s
|
|
||||||
""",
|
|
||||||
(body.food_id, body.grams_per_unit, body.source_unit, mapping_id),
|
|
||||||
)
|
|
||||||
if row.get("profile_id"):
|
|
||||||
apply_mapping_to_items(cur, str(row["profile_id"]), row["source_name_normalized"], body.food_id, mapping_id)
|
|
||||||
dates = dates_for_normalized_name(cur, str(row["profile_id"]), row["source_name_normalized"])
|
|
||||||
for d in dates:
|
|
||||||
rebuild_daily_nutrients(cur, str(row["profile_id"]), d)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT m.*, f.name_de AS food_name_de, f.bls_code
|
|
||||||
FROM food_name_mappings m
|
|
||||||
JOIN food_catalog f ON f.id = m.food_id
|
|
||||||
WHERE m.id = %s
|
|
||||||
""",
|
|
||||||
(mapping_id,),
|
|
||||||
)
|
|
||||||
return r2d(cur.fetchone())
|
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/{mapping_id}")
|
|
||||||
def delete_food_mapping(mapping_id: int, session: dict = Depends(require_admin)):
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute("DELETE FROM food_name_mappings WHERE id = %s RETURNING id", (mapping_id,))
|
|
||||||
if not cur.fetchone():
|
|
||||||
raise HTTPException(404, "Mapping nicht gefunden")
|
|
||||||
return {"ok": True}
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
"""
|
"""
|
||||||
Geschützter App-Bereich: Dashboard-Layout und Widget-Katalog.
|
Geschützter App-Bereich: Dashboard-Lab Layout (kein Produktiv-Dashboard).
|
||||||
|
|
||||||
/api/app/dashboard-layout — nur mit Session + aktivem Profil (X-Profile-Id).
|
/api/app/dashboard-layout — nur mit Session + aktivem Profil (X-Profile-Id).
|
||||||
"""
|
"""
|
||||||
@@ -13,14 +13,13 @@ from dashboard_layout_schema import (
|
|||||||
DashboardLayoutPayload,
|
DashboardLayoutPayload,
|
||||||
coalesce_effective_layout,
|
coalesce_effective_layout,
|
||||||
lab_default_layout_dict,
|
lab_default_layout_dict,
|
||||||
merge_missing_catalog_widgets,
|
|
||||||
)
|
)
|
||||||
from dashboard_widget_entitlements import apply_entitlements_to_layout_dict, widgets_catalog_payload
|
from dashboard_widget_entitlements import apply_entitlements_to_layout_dict, widgets_catalog_payload
|
||||||
from db import get_cursor, get_db
|
from db import get_cursor, get_db
|
||||||
from routers.profiles import get_pid
|
from routers.profiles import get_pid
|
||||||
from system_dashboard_product_default import get_product_default_base_dict
|
from system_dashboard_product_default import get_product_default_base_dict
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/app", tags=["app-dashboard"])
|
router = APIRouter(prefix="/api/app", tags=["app-dashboard-lab"])
|
||||||
|
|
||||||
|
|
||||||
@router.get("/widgets/catalog")
|
@router.get("/widgets/catalog")
|
||||||
@@ -52,11 +51,9 @@ def get_dashboard_layout(
|
|||||||
raw = row["dashboard_layout"] if row else None
|
raw = row["dashboard_layout"] if row else None
|
||||||
custom, effective = coalesce_effective_layout(raw)
|
custom, effective = coalesce_effective_layout(raw)
|
||||||
with get_db() as conn:
|
with get_db() as conn:
|
||||||
base_product = merge_missing_catalog_widgets(get_product_default_base_dict(conn))
|
base_product = get_product_default_base_dict(conn)
|
||||||
if not custom:
|
if not custom:
|
||||||
effective = base_product
|
effective = base_product
|
||||||
else:
|
|
||||||
effective = merge_missing_catalog_widgets(effective)
|
|
||||||
effective = apply_entitlements_to_layout_dict(effective, pid, conn)
|
effective = apply_entitlements_to_layout_dict(effective, pid, conn)
|
||||||
product_adj = apply_entitlements_to_layout_dict(base_product, pid, conn)
|
product_adj = apply_entitlements_to_layout_dict(base_product, pid, conn)
|
||||||
lab_adj = apply_entitlements_to_layout_dict(lab_default_layout_dict(), pid, conn)
|
lab_adj = apply_entitlements_to_layout_dict(lab_default_layout_dict(), pid, conn)
|
||||||
|
|||||||
@@ -1,393 +0,0 @@
|
|||||||
"""Authenticated catalog search and user-owned foods / mappings."""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import logging
|
|
||||||
import threading
|
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, Header, HTTPException
|
|
||||||
from pydantic import BaseModel
|
|
||||||
|
|
||||||
from auth import require_auth
|
|
||||||
from data_layer.food_mapping import (
|
|
||||||
apply_mapping_to_items,
|
|
||||||
apply_quantities_to_items,
|
|
||||||
clear_mapping_from_items,
|
|
||||||
list_quantity_units,
|
|
||||||
normalize_food_name,
|
|
||||||
upsert_food_mapping,
|
|
||||||
)
|
|
||||||
from data_layer.food_suggest import (
|
|
||||||
invalidate_suggest_index,
|
|
||||||
suggest_batch,
|
|
||||||
suggest_catalog_foods_ranked,
|
|
||||||
)
|
|
||||||
from data_layer.nutrition_items import dates_for_normalized_name, rebuild_daily_nutrients
|
|
||||||
from db import get_cursor, get_db, r2d
|
|
||||||
from routers.profiles import get_pid
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/bls", tags=["bls"])
|
|
||||||
|
|
||||||
|
|
||||||
class UserFoodCreate(BaseModel):
|
|
||||||
name_de: str
|
|
||||||
name_en: Optional[str] = None
|
|
||||||
macros_per_100g: Optional[dict] = None
|
|
||||||
attributes: Optional[dict] = None
|
|
||||||
serving_g: Optional[float] = None
|
|
||||||
|
|
||||||
|
|
||||||
class MappingUpsert(BaseModel):
|
|
||||||
source_name: str
|
|
||||||
food_id: str
|
|
||||||
source_system: str = "fddb"
|
|
||||||
grams_per_unit: Optional[float] = None
|
|
||||||
source_unit: Optional[str] = None
|
|
||||||
|
|
||||||
|
|
||||||
class SuggestBatchBody(BaseModel):
|
|
||||||
names: list[str]
|
|
||||||
limit: int = 3
|
|
||||||
|
|
||||||
|
|
||||||
class UserAttributeCreate(BaseModel):
|
|
||||||
name_de: str
|
|
||||||
unit: str = "g"
|
|
||||||
attr_key: Optional[str] = None
|
|
||||||
|
|
||||||
|
|
||||||
def _pid(session: dict, x_profile_id: Optional[str] = None) -> str:
|
|
||||||
return x_profile_id or session["profile_id"]
|
|
||||||
|
|
||||||
|
|
||||||
def _rebuild_days(cur, profile_id: str, dates: list[str]) -> None:
|
|
||||||
for d in dates:
|
|
||||||
rebuild_daily_nutrients(cur, profile_id, d)
|
|
||||||
|
|
||||||
|
|
||||||
def _rebuild_days_bg(profile_id: str, dates: list[str], context: str) -> None:
|
|
||||||
if not dates:
|
|
||||||
return
|
|
||||||
try:
|
|
||||||
with get_db() as conn:
|
|
||||||
_rebuild_days(get_cursor(conn), profile_id, dates)
|
|
||||||
except Exception:
|
|
||||||
logger.exception("Nährwert-Rebuild nach %s fehlgeschlagen", context)
|
|
||||||
|
|
||||||
|
|
||||||
def _schedule_rebuild(profile_id: str, dates: list[str], context: str) -> None:
|
|
||||||
threading.Thread(
|
|
||||||
target=_rebuild_days_bg,
|
|
||||||
args=(profile_id, list(dates), context),
|
|
||||||
daemon=True,
|
|
||||||
name="nutrition-rebuild",
|
|
||||||
).start()
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/units")
|
|
||||||
def list_food_units(session: dict = Depends(require_auth)):
|
|
||||||
return list_quantity_units()
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/foods")
|
|
||||||
def search_foods(
|
|
||||||
q: str = "",
|
|
||||||
limit: int = 20,
|
|
||||||
scope: str = "visible",
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
pid = session["profile_id"]
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
hits = suggest_catalog_foods_ranked(cur, q, pid, limit=min(max(limit, 1), 50))
|
|
||||||
if scope == "global":
|
|
||||||
hits = [h for h in hits if h.get("visibility") != "personal"]
|
|
||||||
return hits
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/attributes")
|
|
||||||
def list_food_attributes(
|
|
||||||
q: str = "",
|
|
||||||
keys: str = "",
|
|
||||||
limit: int = 40,
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
from data_layer.food_attributes import list_numeric_attributes
|
|
||||||
key_list = [p.strip() for p in (keys or "").split(",") if p.strip()]
|
|
||||||
with get_db() as conn:
|
|
||||||
return list_numeric_attributes(get_cursor(conn), q, limit=limit, keys=key_list or None)
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/attributes")
|
|
||||||
def create_food_attribute(body: UserAttributeCreate, session: dict = Depends(require_auth)):
|
|
||||||
from data_layer.food_attributes import create_extension_attribute
|
|
||||||
try:
|
|
||||||
with get_db() as conn:
|
|
||||||
return create_extension_attribute(
|
|
||||||
get_cursor(conn), body.name_de, body.unit, body.attr_key
|
|
||||||
)
|
|
||||||
except ValueError as e:
|
|
||||||
raise HTTPException(400, str(e)) from e
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/foods/suggest-batch")
|
|
||||||
def suggest_foods_batch(
|
|
||||||
body: SuggestBatchBody,
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
names = [n for n in (body.names or []) if isinstance(n, str)][:80]
|
|
||||||
limit = min(max(body.limit or 3, 1), 5)
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
return suggest_batch(cur, session["profile_id"], names, limit=limit)
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/foods/manual")
|
|
||||||
def create_user_food(
|
|
||||||
body: UserFoodCreate,
|
|
||||||
x_profile_id: Optional[str] = Header(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
from data_layer.food_attributes import macros_and_attributes_to_values, write_numeric_attributes
|
|
||||||
import uuid
|
|
||||||
|
|
||||||
pid = _pid(session, x_profile_id)
|
|
||||||
name = (body.name_de or "").strip()
|
|
||||||
if not name:
|
|
||||||
raise HTTPException(400, "Name fehlt")
|
|
||||||
serving = body.serving_g if body.serving_g and body.serving_g > 0 else None
|
|
||||||
values = macros_and_attributes_to_values(body.macros_per_100g, body.attributes, serving)
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO food_catalog
|
|
||||||
(name_de, name_en, catalog_kind, owner_profile_id, source, external_key)
|
|
||||||
VALUES (%s, %s, 'manual_user', %s, 'manual', %s)
|
|
||||||
RETURNING *
|
|
||||||
""",
|
|
||||||
(name, body.name_en, pid, f"man-user-{normalize_food_name(name)[:32]}-{uuid.uuid4().hex[:8]}"),
|
|
||||||
)
|
|
||||||
food = r2d(cur.fetchone())
|
|
||||||
write_numeric_attributes(cur, food["id"], values)
|
|
||||||
invalidate_suggest_index(pid)
|
|
||||||
return food
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/foods/mine")
|
|
||||||
def list_my_manual_foods(session: dict = Depends(require_auth)):
|
|
||||||
pid = session["profile_id"]
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id, name_de, catalog_kind, updated_at
|
|
||||||
FROM food_catalog
|
|
||||||
WHERE owner_profile_id = %s AND catalog_kind = 'manual_user' AND is_active = true
|
|
||||||
ORDER BY name_de
|
|
||||||
""",
|
|
||||||
(pid,),
|
|
||||||
)
|
|
||||||
rows = [r2d(r) for r in cur.fetchall()]
|
|
||||||
from data_layer.food_visibility import attach_visibility
|
|
||||||
for row in rows:
|
|
||||||
row["id"] = str(row["id"])
|
|
||||||
attach_visibility(row)
|
|
||||||
return rows
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/foods/shared")
|
|
||||||
def list_shared_manual_foods(session: dict = Depends(require_auth)):
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id, name_de, catalog_kind, updated_at
|
|
||||||
FROM food_catalog
|
|
||||||
WHERE catalog_kind = 'manual_admin' AND owner_profile_id IS NULL AND is_active = true
|
|
||||||
ORDER BY name_de
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
from data_layer.food_visibility import attach_visibility
|
|
||||||
rows = [r2d(r) for r in cur.fetchall()]
|
|
||||||
for row in rows:
|
|
||||||
row["id"] = str(row["id"])
|
|
||||||
attach_visibility(row)
|
|
||||||
return rows
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/foods/{food_id}")
|
|
||||||
def get_food_detail(food_id: str, session: dict = Depends(require_auth)):
|
|
||||||
from data_layer.food_attributes import editor_payload_for_food
|
|
||||||
|
|
||||||
pid = session["profile_id"]
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id, name_de, name_en, catalog_kind, owner_profile_id
|
|
||||||
FROM food_catalog
|
|
||||||
WHERE id = %s AND is_active = true
|
|
||||||
AND (owner_profile_id IS NULL OR owner_profile_id = %s)
|
|
||||||
""",
|
|
||||||
(food_id, pid),
|
|
||||||
)
|
|
||||||
food = cur.fetchone()
|
|
||||||
if not food:
|
|
||||||
raise HTTPException(404, "Lebensmittel nicht gefunden")
|
|
||||||
food = dict(food)
|
|
||||||
from data_layer.food_visibility import attach_visibility, can_edit_food
|
|
||||||
editable = can_edit_food(food, pid, is_admin=session.get("role") == "admin")
|
|
||||||
return attach_visibility(editor_payload_for_food(cur, food, editable=editable))
|
|
||||||
|
|
||||||
|
|
||||||
@router.put("/foods/manual/{food_id}")
|
|
||||||
def update_user_food(food_id: str, body: UserFoodCreate, session: dict = Depends(require_auth)):
|
|
||||||
from data_layer.food_attributes import (
|
|
||||||
editor_payload_for_food,
|
|
||||||
macros_and_attributes_to_values,
|
|
||||||
replace_numeric_attributes,
|
|
||||||
)
|
|
||||||
from data_layer.nutrition_items import dates_using_food
|
|
||||||
|
|
||||||
pid = session["profile_id"]
|
|
||||||
name = (body.name_de or "").strip()
|
|
||||||
if not name:
|
|
||||||
raise HTTPException(400, "Name fehlt")
|
|
||||||
serving = body.serving_g if body.serving_g and body.serving_g > 0 else None
|
|
||||||
values = macros_and_attributes_to_values(body.macros_per_100g, body.attributes, serving)
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id, name_de, name_en, catalog_kind, owner_profile_id
|
|
||||||
FROM food_catalog
|
|
||||||
WHERE id = %s AND catalog_kind = 'manual_user' AND owner_profile_id = %s AND is_active = true
|
|
||||||
""",
|
|
||||||
(food_id, pid),
|
|
||||||
)
|
|
||||||
food = cur.fetchone()
|
|
||||||
if not food:
|
|
||||||
raise HTTPException(404, "Eigenes Lebensmittel nicht gefunden")
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
UPDATE food_catalog
|
|
||||||
SET name_de = %s, name_en = %s, updated_at = NOW()
|
|
||||||
WHERE id = %s
|
|
||||||
RETURNING id, name_de, name_en, catalog_kind, owner_profile_id
|
|
||||||
""",
|
|
||||||
(name, body.name_en, food_id),
|
|
||||||
)
|
|
||||||
updated = dict(cur.fetchone())
|
|
||||||
replace_numeric_attributes(cur, food_id, values)
|
|
||||||
dates = dates_using_food(cur, pid, food_id)
|
|
||||||
payload = editor_payload_for_food(cur, updated, editable=True)
|
|
||||||
from data_layer.food_visibility import attach_visibility
|
|
||||||
payload = attach_visibility(payload)
|
|
||||||
invalidate_suggest_index(pid)
|
|
||||||
_schedule_rebuild(pid, dates, f"Lebensmittel {name}")
|
|
||||||
payload["rebuild_pending"] = len(dates)
|
|
||||||
return payload
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/mappings")
|
|
||||||
def list_my_mappings(
|
|
||||||
x_profile_id: Optional[str] = Header(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
pid = _pid(session, x_profile_id)
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT m.id, m.source_name_raw, m.source_name_normalized, m.food_id, m.source,
|
|
||||||
m.grams_per_unit, m.source_unit,
|
|
||||||
f.name_de AS food_name_de, f.bls_code, f.catalog_kind
|
|
||||||
FROM food_name_mappings m
|
|
||||||
JOIN food_catalog f ON f.id = m.food_id
|
|
||||||
WHERE m.profile_id = %s
|
|
||||||
ORDER BY m.source_name_normalized
|
|
||||||
""",
|
|
||||||
(pid,),
|
|
||||||
)
|
|
||||||
from data_layer.food_visibility import attach_visibility
|
|
||||||
return [attach_visibility(r2d(r)) for r in cur.fetchall()]
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/mappings")
|
|
||||||
def upsert_my_mapping(
|
|
||||||
body: MappingUpsert,
|
|
||||||
x_profile_id: Optional[str] = Header(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
pid = _pid(session, x_profile_id)
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
from data_layer.food_visibility import require_visible_food, visibility_of
|
|
||||||
try:
|
|
||||||
food = require_visible_food(cur, body.food_id, pid)
|
|
||||||
except LookupError as e:
|
|
||||||
raise HTTPException(404, str(e)) from e
|
|
||||||
try:
|
|
||||||
mid = upsert_food_mapping(
|
|
||||||
cur,
|
|
||||||
source_name_raw=body.source_name,
|
|
||||||
food_id=body.food_id,
|
|
||||||
profile_id=pid,
|
|
||||||
source="bulk",
|
|
||||||
source_system=body.source_system,
|
|
||||||
grams_per_unit=body.grams_per_unit,
|
|
||||||
source_unit=body.source_unit,
|
|
||||||
)
|
|
||||||
except PermissionError as e:
|
|
||||||
raise HTTPException(400, str(e)) from e
|
|
||||||
except LookupError as e:
|
|
||||||
raise HTTPException(404, str(e)) from e
|
|
||||||
norm = normalize_food_name(body.source_name)
|
|
||||||
n = apply_mapping_to_items(cur, pid, norm, body.food_id, mid)
|
|
||||||
apply_quantities_to_items(cur, pid, norm, body.grams_per_unit)
|
|
||||||
dates = dates_for_normalized_name(cur, pid, norm)
|
|
||||||
_schedule_rebuild(pid, dates, f"Mapping {norm}")
|
|
||||||
return {
|
|
||||||
"mapping_id": mid,
|
|
||||||
"items_updated": n,
|
|
||||||
"source_name_normalized": norm,
|
|
||||||
"food_id": body.food_id,
|
|
||||||
"food_name_de": food["name_de"],
|
|
||||||
"bls_code": food.get("bls_code"),
|
|
||||||
"catalog_kind": food.get("catalog_kind"),
|
|
||||||
"visibility": visibility_of(food),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/mappings/{mapping_id}")
|
|
||||||
def delete_my_mapping(
|
|
||||||
mapping_id: int,
|
|
||||||
x_profile_id: Optional[str] = Header(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
pid = _pid(session, x_profile_id)
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT source_name_normalized FROM food_name_mappings
|
|
||||||
WHERE id = %s AND profile_id = %s
|
|
||||||
""",
|
|
||||||
(mapping_id, pid),
|
|
||||||
)
|
|
||||||
row = cur.fetchone()
|
|
||||||
if not row:
|
|
||||||
raise HTTPException(404, "Mapping nicht gefunden")
|
|
||||||
norm = row["source_name_normalized"]
|
|
||||||
dates = dates_for_normalized_name(cur, pid, norm)
|
|
||||||
clear_mapping_from_items(cur, pid, norm)
|
|
||||||
cur.execute("DELETE FROM food_name_mappings WHERE id = %s AND profile_id = %s", (mapping_id, pid))
|
|
||||||
_schedule_rebuild(pid, dates, "Mapping-Löschen")
|
|
||||||
return {"ok": True}
|
|
||||||
|
|
||||||
|
|
||||||
# keep get_pid imported for consistency with other routers
|
|
||||||
_ = get_pid
|
|
||||||
+619
-31
@@ -33,7 +33,7 @@ from data_layer.body_metrics import (
|
|||||||
)
|
)
|
||||||
from data_layer.body_viz import get_body_history_viz_bundle
|
from data_layer.body_viz import get_body_history_viz_bundle
|
||||||
from data_layer.nutrition_viz import get_nutrition_history_viz_bundle
|
from data_layer.nutrition_viz import get_nutrition_history_viz_bundle
|
||||||
from data_layer.fitness_viz import get_fitness_dashboard_viz_bundle, get_activity_last_updated_iso
|
from data_layer.fitness_viz import get_fitness_dashboard_viz_bundle
|
||||||
from data_layer.recovery_viz import get_recovery_dashboard_viz_bundle
|
from data_layer.recovery_viz import get_recovery_dashboard_viz_bundle
|
||||||
from data_layer.history_overview_viz import get_history_overview_viz_bundle
|
from data_layer.history_overview_viz import get_history_overview_viz_bundle
|
||||||
from data_layer.recovery_chart_payloads import (
|
from data_layer.recovery_chart_payloads import (
|
||||||
@@ -46,7 +46,9 @@ from data_layer.recovery_chart_payloads import (
|
|||||||
from data_layer.nutrition_metrics import (
|
from data_layer.nutrition_metrics import (
|
||||||
get_nutrition_average_data,
|
get_nutrition_average_data,
|
||||||
get_protein_targets_data,
|
get_protein_targets_data,
|
||||||
|
get_protein_adequacy_data,
|
||||||
get_macro_consistency_data,
|
get_macro_consistency_data,
|
||||||
|
get_energy_balance_data,
|
||||||
get_weekly_macro_distribution_chart_data,
|
get_weekly_macro_distribution_chart_data,
|
||||||
get_energy_availability_warning_payload,
|
get_energy_availability_warning_payload,
|
||||||
)
|
)
|
||||||
@@ -69,18 +71,12 @@ from data_layer.recovery_metrics import (
|
|||||||
calculate_rhr_vs_baseline_pct,
|
calculate_rhr_vs_baseline_pct,
|
||||||
calculate_sleep_debt_hours
|
calculate_sleep_debt_hours
|
||||||
)
|
)
|
||||||
from data_layer.correlation_chart_payloads import (
|
from data_layer.correlations import (
|
||||||
build_lbm_protein_correlation_chart_payload,
|
calculate_lag_correlation,
|
||||||
build_load_vitals_correlation_chart_payload,
|
calculate_correlation_sleep_recovery,
|
||||||
build_recovery_performance_chart_payload,
|
calculate_top_drivers
|
||||||
build_weight_energy_correlation_chart_payload,
|
|
||||||
)
|
)
|
||||||
from data_layer.utils import serialize_dates, safe_float, calculate_confidence
|
from data_layer.utils import serialize_dates, safe_float, calculate_confidence
|
||||||
from data_layer.nutrition_chart_payloads import (
|
|
||||||
build_energy_balance_chart_payload,
|
|
||||||
build_protein_adequacy_chart_payload,
|
|
||||||
build_nutrition_adherence_score_payload,
|
|
||||||
)
|
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/charts", tags=["charts"])
|
router = APIRouter(prefix="/api/charts", tags=["charts"])
|
||||||
|
|
||||||
@@ -323,17 +319,6 @@ def get_fitness_dashboard_viz(
|
|||||||
return serialize_dates(bundle)
|
return serialize_dates(bundle)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/activity-last-updated")
|
|
||||||
def get_activity_last_updated(session: dict = Depends(require_auth)) -> Dict:
|
|
||||||
"""
|
|
||||||
Minimal-Metadatum: letztes Trainingsdatum — gleiche Quelle wie ``last_updated`` im Fitness-Viz-Bundle.
|
|
||||||
|
|
||||||
Vermeidet Massen-Ladevorgänge (z. B. listActivity) nur für Datumsanzeige im Verlauf.
|
|
||||||
"""
|
|
||||||
pid = session["profile_id"]
|
|
||||||
return {"last_activity_date": get_activity_last_updated_iso(pid)}
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/recovery-dashboard-viz")
|
@router.get("/recovery-dashboard-viz")
|
||||||
def get_recovery_dashboard_viz(
|
def get_recovery_dashboard_viz(
|
||||||
days: int = Query(
|
days: int = Query(
|
||||||
@@ -363,8 +348,7 @@ def get_history_overview_viz(
|
|||||||
session: dict = Depends(require_auth),
|
session: dict = Depends(require_auth),
|
||||||
) -> Dict:
|
) -> Dict:
|
||||||
"""
|
"""
|
||||||
Layer 2b: Gesamtansicht «Verlauf» — KPI-Kurzformen aus den vier History-Bundles,
|
Layer 2b: Gesamtansicht «Verlauf» — KPI-Kurzformen aus den vier History-Bundles + Lag-Korrelationen C1–C4 (Metadaten).
|
||||||
Lag-Korrelationen C1–C4 (Metadaten) und Chart.js-Payloads C1–C4 (chart_payloads, wie /charts/*).
|
|
||||||
"""
|
"""
|
||||||
profile_id = session["profile_id"]
|
profile_id = session["profile_id"]
|
||||||
bundle = get_history_overview_viz_bundle(profile_id, days)
|
bundle = get_history_overview_viz_bundle(profile_id, days)
|
||||||
@@ -467,7 +451,136 @@ def get_energy_balance_chart(
|
|||||||
Chart.js line chart with multiple datasets
|
Chart.js line chart with multiple datasets
|
||||||
"""
|
"""
|
||||||
profile_id = session['profile_id']
|
profile_id = session['profile_id']
|
||||||
return build_energy_balance_chart_payload(profile_id, days)
|
|
||||||
|
balance_meta = get_energy_balance_data(profile_id, days)
|
||||||
|
|
||||||
|
from db import get_db, get_cursor
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
||||||
|
|
||||||
|
cur.execute(
|
||||||
|
"""SELECT date, SUM(kcal)::float AS kcal
|
||||||
|
FROM nutrition_log
|
||||||
|
WHERE profile_id=%s AND date >= %s AND kcal IS NOT NULL
|
||||||
|
GROUP BY date
|
||||||
|
ORDER BY date""",
|
||||||
|
(profile_id, cutoff),
|
||||||
|
)
|
||||||
|
rows = cur.fetchall()
|
||||||
|
|
||||||
|
if not rows or len(rows) < 3:
|
||||||
|
return {
|
||||||
|
"chart_type": "line",
|
||||||
|
"data": {
|
||||||
|
"labels": [],
|
||||||
|
"datasets": []
|
||||||
|
},
|
||||||
|
"metadata": {
|
||||||
|
"confidence": "insufficient",
|
||||||
|
"data_points": len(rows) if rows else 0,
|
||||||
|
"message": "Nicht genug Ernährungsdaten (min. 3 Tage)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
estimated_tdee = balance_meta.get("estimated_tdee") or 0
|
||||||
|
if estimated_tdee <= 0:
|
||||||
|
return {
|
||||||
|
"chart_type": "line",
|
||||||
|
"data": {
|
||||||
|
"labels": [],
|
||||||
|
"datasets": []
|
||||||
|
},
|
||||||
|
"metadata": {
|
||||||
|
"confidence": "insufficient",
|
||||||
|
"data_points": len(rows),
|
||||||
|
"message": "Kein Gewicht für TDEE-Schätzung (weight_log erforderlich)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
labels = []
|
||||||
|
daily_values = []
|
||||||
|
avg_7d = []
|
||||||
|
avg_14d = []
|
||||||
|
|
||||||
|
for i, row in enumerate(rows):
|
||||||
|
labels.append(row['date'].isoformat())
|
||||||
|
daily_values.append(safe_float(row['kcal']))
|
||||||
|
|
||||||
|
start_7d = max(0, i - 6)
|
||||||
|
window_7d = [safe_float(rows[j]['kcal']) for j in range(start_7d, i + 1)]
|
||||||
|
avg_7d.append(round(sum(window_7d) / len(window_7d), 1) if window_7d else None)
|
||||||
|
|
||||||
|
start_14d = max(0, i - 13)
|
||||||
|
window_14d = [safe_float(rows[j]['kcal']) for j in range(start_14d, i + 1)]
|
||||||
|
avg_14d.append(round(sum(window_14d) / len(window_14d), 1) if window_14d else None)
|
||||||
|
|
||||||
|
avg_intake = float(balance_meta.get("avg_intake") or (sum(daily_values) / len(daily_values) if daily_values else 0))
|
||||||
|
energy_balance = float(balance_meta.get("energy_balance") or (avg_intake - estimated_tdee))
|
||||||
|
balance_status = balance_meta.get("status") or (
|
||||||
|
"deficit" if energy_balance < -200 else "surplus" if energy_balance > 200 else "maintenance"
|
||||||
|
)
|
||||||
|
|
||||||
|
datasets = [
|
||||||
|
{
|
||||||
|
"label": "Kalorien (täglich)",
|
||||||
|
"data": daily_values,
|
||||||
|
"borderColor": "#1D9E7599",
|
||||||
|
"backgroundColor": "rgba(29, 158, 117, 0.1)",
|
||||||
|
"borderWidth": 1.5,
|
||||||
|
"tension": 0.2,
|
||||||
|
"fill": False,
|
||||||
|
"pointRadius": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "Ø 7 Tage",
|
||||||
|
"data": avg_7d,
|
||||||
|
"borderColor": "#1D9E75",
|
||||||
|
"borderWidth": 2.5,
|
||||||
|
"tension": 0.3,
|
||||||
|
"fill": False,
|
||||||
|
"pointRadius": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "Ø 14 Tage",
|
||||||
|
"data": avg_14d,
|
||||||
|
"borderColor": "#085041",
|
||||||
|
"borderWidth": 2,
|
||||||
|
"tension": 0.3,
|
||||||
|
"fill": False,
|
||||||
|
"pointRadius": 0,
|
||||||
|
"borderDash": [6, 3]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "TDEE (geschätzt)",
|
||||||
|
"data": [estimated_tdee] * len(labels),
|
||||||
|
"borderColor": "#888",
|
||||||
|
"borderWidth": 1,
|
||||||
|
"borderDash": [5, 5],
|
||||||
|
"fill": False,
|
||||||
|
"pointRadius": 0
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
confidence = balance_meta.get("confidence") or "low"
|
||||||
|
|
||||||
|
return {
|
||||||
|
"chart_type": "line",
|
||||||
|
"data": {
|
||||||
|
"labels": labels,
|
||||||
|
"datasets": datasets
|
||||||
|
},
|
||||||
|
"metadata": serialize_dates({
|
||||||
|
"confidence": confidence,
|
||||||
|
"data_points": len(rows),
|
||||||
|
"avg_kcal": round(avg_intake, 1),
|
||||||
|
"estimated_tdee": estimated_tdee,
|
||||||
|
"energy_balance": round(energy_balance, 1),
|
||||||
|
"balance_status": balance_status,
|
||||||
|
"first_date": rows[0]['date'],
|
||||||
|
"last_date": rows[-1]['date']
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@router.get("/macro-distribution")
|
@router.get("/macro-distribution")
|
||||||
@@ -582,7 +695,136 @@ def get_protein_adequacy_chart(
|
|||||||
Chart.js line chart with protein intake + averages + target bands
|
Chart.js line chart with protein intake + averages + target bands
|
||||||
"""
|
"""
|
||||||
profile_id = session['profile_id']
|
profile_id = session['profile_id']
|
||||||
return build_protein_adequacy_chart_payload(profile_id, days)
|
|
||||||
|
# Get protein targets
|
||||||
|
targets = get_protein_targets_data(profile_id)
|
||||||
|
|
||||||
|
from db import get_db, get_cursor
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
||||||
|
|
||||||
|
cur.execute(
|
||||||
|
"""SELECT date, SUM(protein_g)::float AS protein_g
|
||||||
|
FROM nutrition_log
|
||||||
|
WHERE profile_id=%s AND date >= %s AND protein_g IS NOT NULL
|
||||||
|
GROUP BY date
|
||||||
|
ORDER BY date""",
|
||||||
|
(profile_id, cutoff),
|
||||||
|
)
|
||||||
|
rows = cur.fetchall()
|
||||||
|
|
||||||
|
if not rows or len(rows) < 3:
|
||||||
|
return {
|
||||||
|
"chart_type": "line",
|
||||||
|
"data": {
|
||||||
|
"labels": [],
|
||||||
|
"datasets": []
|
||||||
|
},
|
||||||
|
"metadata": {
|
||||||
|
"confidence": "insufficient",
|
||||||
|
"data_points": len(rows) if rows else 0,
|
||||||
|
"message": "Nicht genug Protein-Daten (min. 3 Tage)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Prepare data
|
||||||
|
labels = []
|
||||||
|
daily_values = []
|
||||||
|
avg_7d = []
|
||||||
|
avg_28d = []
|
||||||
|
|
||||||
|
for i, row in enumerate(rows):
|
||||||
|
labels.append(row['date'].isoformat())
|
||||||
|
daily_values.append(safe_float(row['protein_g']))
|
||||||
|
|
||||||
|
# 7d rolling average
|
||||||
|
start_7d = max(0, i - 6)
|
||||||
|
window_7d = [safe_float(rows[j]['protein_g']) for j in range(start_7d, i + 1)]
|
||||||
|
avg_7d.append(round(sum(window_7d) / len(window_7d), 1) if window_7d else None)
|
||||||
|
|
||||||
|
# 28d rolling average
|
||||||
|
start_28d = max(0, i - 27)
|
||||||
|
window_28d = [safe_float(rows[j]['protein_g']) for j in range(start_28d, i + 1)]
|
||||||
|
avg_28d.append(round(sum(window_28d) / len(window_28d), 1) if window_28d else None)
|
||||||
|
|
||||||
|
# Add target range bands
|
||||||
|
target_low = targets['protein_target_low']
|
||||||
|
target_high = targets['protein_target_high']
|
||||||
|
|
||||||
|
datasets = [
|
||||||
|
{
|
||||||
|
"label": "Protein (täglich)",
|
||||||
|
"data": daily_values,
|
||||||
|
"borderColor": "#1D9E7599",
|
||||||
|
"backgroundColor": "rgba(29, 158, 117, 0.1)",
|
||||||
|
"borderWidth": 1.5,
|
||||||
|
"tension": 0.2,
|
||||||
|
"fill": False,
|
||||||
|
"pointRadius": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "Ø 7 Tage",
|
||||||
|
"data": avg_7d,
|
||||||
|
"borderColor": "#1D9E75",
|
||||||
|
"borderWidth": 2.5,
|
||||||
|
"tension": 0.3,
|
||||||
|
"fill": False,
|
||||||
|
"pointRadius": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "Ø 28 Tage",
|
||||||
|
"data": avg_28d,
|
||||||
|
"borderColor": "#085041",
|
||||||
|
"borderWidth": 2,
|
||||||
|
"tension": 0.3,
|
||||||
|
"fill": False,
|
||||||
|
"pointRadius": 0,
|
||||||
|
"borderDash": [6, 3]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "Ziel Min",
|
||||||
|
"data": [target_low] * len(labels),
|
||||||
|
"borderColor": "#888",
|
||||||
|
"borderWidth": 1,
|
||||||
|
"borderDash": [5, 5],
|
||||||
|
"fill": False,
|
||||||
|
"pointRadius": 0
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
datasets.append({
|
||||||
|
"label": "Ziel Max",
|
||||||
|
"data": [target_high] * len(labels),
|
||||||
|
"borderColor": "#888",
|
||||||
|
"borderWidth": 1,
|
||||||
|
"borderDash": [5, 5],
|
||||||
|
"fill": False,
|
||||||
|
"pointRadius": 0
|
||||||
|
})
|
||||||
|
|
||||||
|
from data_layer.utils import calculate_confidence
|
||||||
|
confidence = calculate_confidence(len(rows), days, "general")
|
||||||
|
|
||||||
|
days_in_target = sum(1 for v in daily_values if target_low <= v <= target_high)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"chart_type": "line",
|
||||||
|
"data": {
|
||||||
|
"labels": labels,
|
||||||
|
"datasets": datasets
|
||||||
|
},
|
||||||
|
"metadata": serialize_dates({
|
||||||
|
"confidence": confidence,
|
||||||
|
"data_points": len(rows),
|
||||||
|
"target_low": round(target_low, 1),
|
||||||
|
"target_high": round(target_high, 1),
|
||||||
|
"days_in_target": days_in_target,
|
||||||
|
"target_compliance_pct": round(days_in_target / len(daily_values) * 100, 1) if daily_values else 0,
|
||||||
|
"first_date": rows[0]['date'],
|
||||||
|
"last_date": rows[-1]['date']
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@router.get("/nutrition-consistency")
|
@router.get("/nutrition-consistency")
|
||||||
@@ -710,7 +952,138 @@ def get_nutrition_adherence_score(
|
|||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
profile_id = session['profile_id']
|
profile_id = session['profile_id']
|
||||||
return build_nutrition_adherence_score_payload(profile_id, days)
|
|
||||||
|
from db import get_db, get_cursor
|
||||||
|
from data_layer.nutrition_metrics import (
|
||||||
|
get_protein_adequacy_data,
|
||||||
|
calculate_macro_consistency_score
|
||||||
|
)
|
||||||
|
|
||||||
|
# Get user's goal mode (weight_loss, strength, endurance, etc.)
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cur.execute("SELECT goal_mode FROM profiles WHERE id = %s", (profile_id,))
|
||||||
|
profile_row = cur.fetchone()
|
||||||
|
goal_mode = profile_row['goal_mode'] if profile_row and profile_row['goal_mode'] else 'health'
|
||||||
|
|
||||||
|
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
||||||
|
|
||||||
|
# Get nutrition data
|
||||||
|
cur.execute(
|
||||||
|
"""WITH daily AS (
|
||||||
|
SELECT date,
|
||||||
|
COALESCE(SUM(kcal), 0)::float AS dk,
|
||||||
|
COALESCE(SUM(protein_g), 0)::float AS dp,
|
||||||
|
COALESCE(SUM(carbs_g), 0)::float AS dc,
|
||||||
|
COALESCE(SUM(fat_g), 0)::float AS df FROM nutrition_log
|
||||||
|
WHERE profile_id=%s AND date >= %s AND kcal IS NOT NULL
|
||||||
|
GROUP BY date
|
||||||
|
)
|
||||||
|
SELECT COUNT(*)::int AS cnt,
|
||||||
|
AVG(dk) AS avg_kcal,
|
||||||
|
STDDEV(dk) AS std_kcal,
|
||||||
|
AVG(dp) AS avg_protein,
|
||||||
|
AVG(dc) AS avg_carbs,
|
||||||
|
AVG(df) AS avg_fat
|
||||||
|
FROM daily""",
|
||||||
|
(profile_id, cutoff),
|
||||||
|
)
|
||||||
|
stats = cur.fetchone()
|
||||||
|
|
||||||
|
if not stats or stats['cnt'] < 7:
|
||||||
|
return {
|
||||||
|
"score": 0,
|
||||||
|
"components": {},
|
||||||
|
"metadata": {
|
||||||
|
"confidence": "insufficient",
|
||||||
|
"message": "Nicht genug Daten (min. 7 Tage)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get protein adequacy
|
||||||
|
protein_data = get_protein_adequacy_data(profile_id, days)
|
||||||
|
|
||||||
|
# Calculate components based on goal mode
|
||||||
|
components = {}
|
||||||
|
|
||||||
|
# 1. Calorie adherence (placeholder, needs goal-specific logic)
|
||||||
|
calorie_adherence = 70.0 # TODO: Calculate based on TDEE target
|
||||||
|
|
||||||
|
# 2. Protein adherence
|
||||||
|
protein_adequacy_pct = protein_data.get('adequacy_score', 0)
|
||||||
|
protein_adherence = min(100, protein_adequacy_pct)
|
||||||
|
|
||||||
|
# 3. Intake consistency (low volatility = good)
|
||||||
|
kcal_cv = (safe_float(stats['std_kcal']) / safe_float(stats['avg_kcal']) * 100) if safe_float(stats['avg_kcal']) > 0 else 100
|
||||||
|
intake_consistency = max(0, 100 - kcal_cv) # Invert: low CV = high score
|
||||||
|
|
||||||
|
# 4. Food quality (placeholder for fiber/sugar analysis)
|
||||||
|
food_quality = 60.0 # TODO: Calculate from fiber/sugar data
|
||||||
|
|
||||||
|
# Goal-specific weighting (from concept E4)
|
||||||
|
if goal_mode == 'weight_loss':
|
||||||
|
weights = {
|
||||||
|
'calorie': 0.35,
|
||||||
|
'protein': 0.25,
|
||||||
|
'consistency': 0.20,
|
||||||
|
'quality': 0.20
|
||||||
|
}
|
||||||
|
elif goal_mode == 'strength':
|
||||||
|
weights = {
|
||||||
|
'calorie': 0.25,
|
||||||
|
'protein': 0.35,
|
||||||
|
'consistency': 0.20,
|
||||||
|
'quality': 0.20
|
||||||
|
}
|
||||||
|
elif goal_mode == 'endurance':
|
||||||
|
weights = {
|
||||||
|
'calorie': 0.30,
|
||||||
|
'protein': 0.20,
|
||||||
|
'consistency': 0.20,
|
||||||
|
'quality': 0.30
|
||||||
|
}
|
||||||
|
else: # health, recomposition
|
||||||
|
weights = {
|
||||||
|
'calorie': 0.25,
|
||||||
|
'protein': 0.25,
|
||||||
|
'consistency': 0.25,
|
||||||
|
'quality': 0.25
|
||||||
|
}
|
||||||
|
|
||||||
|
# Calculate weighted score
|
||||||
|
final_score = (
|
||||||
|
calorie_adherence * weights['calorie'] +
|
||||||
|
protein_adherence * weights['protein'] +
|
||||||
|
intake_consistency * weights['consistency'] +
|
||||||
|
food_quality * weights['quality']
|
||||||
|
)
|
||||||
|
|
||||||
|
components = {
|
||||||
|
'calorie_adherence': round(calorie_adherence, 1),
|
||||||
|
'protein_adherence': round(protein_adherence, 1),
|
||||||
|
'intake_consistency': round(intake_consistency, 1),
|
||||||
|
'food_quality': round(food_quality, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
# Generate recommendation
|
||||||
|
weak_areas = [k for k, v in components.items() if v < 60]
|
||||||
|
if weak_areas:
|
||||||
|
recommendation = f"Verbesserungspotenzial: {', '.join(weak_areas)}"
|
||||||
|
else:
|
||||||
|
recommendation = "Gute Adhärenz, weiter so!"
|
||||||
|
|
||||||
|
return {
|
||||||
|
"score": round(final_score, 1),
|
||||||
|
"components": components,
|
||||||
|
"goal_mode": goal_mode,
|
||||||
|
"weights": weights,
|
||||||
|
"recommendation": recommendation,
|
||||||
|
"metadata": {
|
||||||
|
"confidence": calculate_confidence(stats['cnt'], days, "general"),
|
||||||
|
"data_points": stats['cnt'],
|
||||||
|
"days_analyzed": days
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@router.get("/energy-availability-warning")
|
@router.get("/energy-availability-warning")
|
||||||
@@ -1113,7 +1486,51 @@ def get_weight_energy_correlation_chart(
|
|||||||
Chart.js scatter chart with correlation data
|
Chart.js scatter chart with correlation data
|
||||||
"""
|
"""
|
||||||
profile_id = session['profile_id']
|
profile_id = session['profile_id']
|
||||||
return build_weight_energy_correlation_chart_payload(profile_id, max_lag)
|
|
||||||
|
corr_data = calculate_lag_correlation(profile_id, "energy_balance", "weight", max_lag)
|
||||||
|
|
||||||
|
if not corr_data or corr_data.get('correlation') is None:
|
||||||
|
return {
|
||||||
|
"chart_type": "scatter",
|
||||||
|
"data": {
|
||||||
|
"labels": [],
|
||||||
|
"datasets": []
|
||||||
|
},
|
||||||
|
"metadata": {
|
||||||
|
"confidence": "insufficient",
|
||||||
|
"data_points": 0,
|
||||||
|
"message": "Nicht genug Daten für Korrelationsanalyse"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Create lag vs correlation data for chart
|
||||||
|
# For simplicity, show best lag point as single data point
|
||||||
|
best_lag = corr_data.get('best_lag_days', 0)
|
||||||
|
correlation = corr_data.get('correlation', 0)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"chart_type": "scatter",
|
||||||
|
"data": {
|
||||||
|
"labels": [f"Lag {best_lag} Tage"],
|
||||||
|
"datasets": [
|
||||||
|
{
|
||||||
|
"label": "Korrelation",
|
||||||
|
"data": [{"x": best_lag, "y": correlation}],
|
||||||
|
"backgroundColor": "#1D9E75",
|
||||||
|
"borderColor": "#085041",
|
||||||
|
"borderWidth": 2,
|
||||||
|
"pointRadius": 8
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {
|
||||||
|
"confidence": corr_data.get('confidence', 'low'),
|
||||||
|
"correlation": round(correlation, 3),
|
||||||
|
"best_lag_days": best_lag,
|
||||||
|
"interpretation": corr_data.get('interpretation', ''),
|
||||||
|
"data_points": corr_data.get('data_points', 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@router.get("/lbm-protein-correlation")
|
@router.get("/lbm-protein-correlation")
|
||||||
@@ -1134,7 +1551,49 @@ def get_lbm_protein_correlation_chart(
|
|||||||
Chart.js scatter chart with correlation data
|
Chart.js scatter chart with correlation data
|
||||||
"""
|
"""
|
||||||
profile_id = session['profile_id']
|
profile_id = session['profile_id']
|
||||||
return build_lbm_protein_correlation_chart_payload(profile_id, max_lag)
|
|
||||||
|
corr_data = calculate_lag_correlation(profile_id, "protein", "lbm", max_lag)
|
||||||
|
|
||||||
|
if not corr_data or corr_data.get('correlation') is None:
|
||||||
|
return {
|
||||||
|
"chart_type": "scatter",
|
||||||
|
"data": {
|
||||||
|
"labels": [],
|
||||||
|
"datasets": []
|
||||||
|
},
|
||||||
|
"metadata": {
|
||||||
|
"confidence": "insufficient",
|
||||||
|
"data_points": 0,
|
||||||
|
"message": "Nicht genug Daten für LBM-Protein Korrelation"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
best_lag = corr_data.get('best_lag_days', 0)
|
||||||
|
correlation = corr_data.get('correlation', 0)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"chart_type": "scatter",
|
||||||
|
"data": {
|
||||||
|
"labels": [f"Lag {best_lag} Tage"],
|
||||||
|
"datasets": [
|
||||||
|
{
|
||||||
|
"label": "Korrelation",
|
||||||
|
"data": [{"x": best_lag, "y": correlation}],
|
||||||
|
"backgroundColor": "#3B82F6",
|
||||||
|
"borderColor": "#1E40AF",
|
||||||
|
"borderWidth": 2,
|
||||||
|
"pointRadius": 8
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {
|
||||||
|
"confidence": corr_data.get('confidence', 'low'),
|
||||||
|
"correlation": round(correlation, 3),
|
||||||
|
"best_lag_days": best_lag,
|
||||||
|
"interpretation": corr_data.get('interpretation', ''),
|
||||||
|
"data_points": corr_data.get('data_points', 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@router.get("/load-vitals-correlation")
|
@router.get("/load-vitals-correlation")
|
||||||
@@ -1155,7 +1614,62 @@ def get_load_vitals_correlation_chart(
|
|||||||
Chart.js scatter chart with correlation data
|
Chart.js scatter chart with correlation data
|
||||||
"""
|
"""
|
||||||
profile_id = session['profile_id']
|
profile_id = session['profile_id']
|
||||||
return build_load_vitals_correlation_chart_payload(profile_id, max_lag)
|
|
||||||
|
# Try HRV first
|
||||||
|
corr_hrv = calculate_lag_correlation(profile_id, "load", "hrv", max_lag)
|
||||||
|
corr_rhr = calculate_lag_correlation(profile_id, "load", "rhr", max_lag)
|
||||||
|
|
||||||
|
# Use whichever has stronger correlation
|
||||||
|
if corr_hrv and corr_rhr:
|
||||||
|
corr_data = corr_hrv if abs(corr_hrv.get('correlation', 0)) > abs(corr_rhr.get('correlation', 0)) else corr_rhr
|
||||||
|
metric_name = "HRV" if corr_data == corr_hrv else "RHR"
|
||||||
|
elif corr_hrv:
|
||||||
|
corr_data = corr_hrv
|
||||||
|
metric_name = "HRV"
|
||||||
|
elif corr_rhr:
|
||||||
|
corr_data = corr_rhr
|
||||||
|
metric_name = "RHR"
|
||||||
|
else:
|
||||||
|
return {
|
||||||
|
"chart_type": "scatter",
|
||||||
|
"data": {
|
||||||
|
"labels": [],
|
||||||
|
"datasets": []
|
||||||
|
},
|
||||||
|
"metadata": {
|
||||||
|
"confidence": "insufficient",
|
||||||
|
"data_points": 0,
|
||||||
|
"message": "Nicht genug Daten für Load-Vitals Korrelation"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
best_lag = corr_data.get('best_lag_days', 0)
|
||||||
|
correlation = corr_data.get('correlation', 0)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"chart_type": "scatter",
|
||||||
|
"data": {
|
||||||
|
"labels": [f"Load → {metric_name} (Lag {best_lag}d)"],
|
||||||
|
"datasets": [
|
||||||
|
{
|
||||||
|
"label": "Korrelation",
|
||||||
|
"data": [{"x": best_lag, "y": correlation}],
|
||||||
|
"backgroundColor": "#F59E0B",
|
||||||
|
"borderColor": "#D97706",
|
||||||
|
"borderWidth": 2,
|
||||||
|
"pointRadius": 8
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {
|
||||||
|
"confidence": corr_data.get('confidence', 'low'),
|
||||||
|
"correlation": round(correlation, 3),
|
||||||
|
"best_lag_days": best_lag,
|
||||||
|
"metric": metric_name,
|
||||||
|
"interpretation": corr_data.get('interpretation', ''),
|
||||||
|
"data_points": corr_data.get('data_points', 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@router.get("/recovery-performance")
|
@router.get("/recovery-performance")
|
||||||
@@ -1174,7 +1688,81 @@ def get_recovery_performance_chart(
|
|||||||
Chart.js bar chart with top drivers
|
Chart.js bar chart with top drivers
|
||||||
"""
|
"""
|
||||||
profile_id = session['profile_id']
|
profile_id = session['profile_id']
|
||||||
return build_recovery_performance_chart_payload(profile_id)
|
|
||||||
|
# Get top drivers (hindering/helpful factors)
|
||||||
|
drivers = calculate_top_drivers(profile_id)
|
||||||
|
|
||||||
|
if not drivers or len(drivers) == 0:
|
||||||
|
return {
|
||||||
|
"chart_type": "bar",
|
||||||
|
"data": {
|
||||||
|
"labels": [],
|
||||||
|
"datasets": []
|
||||||
|
},
|
||||||
|
"metadata": {
|
||||||
|
"confidence": "insufficient",
|
||||||
|
"data_points": 0,
|
||||||
|
"message": "Nicht genug Daten für Driver-Analyse"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Separate hindering and helpful
|
||||||
|
hindering = [d for d in drivers if d.get('impact', '') == 'hindering']
|
||||||
|
helpful = [d for d in drivers if d.get('impact', '') == 'helpful']
|
||||||
|
|
||||||
|
# Take top 3 of each
|
||||||
|
top_hindering = hindering[:3]
|
||||||
|
top_helpful = helpful[:3]
|
||||||
|
|
||||||
|
labels = []
|
||||||
|
values = []
|
||||||
|
colors = []
|
||||||
|
|
||||||
|
for d in top_hindering:
|
||||||
|
labels.append(f"❌ {d.get('factor', '')}")
|
||||||
|
values.append(-abs(d.get('score', 0))) # Negative for hindering
|
||||||
|
colors.append("#EF4444")
|
||||||
|
|
||||||
|
for d in top_helpful:
|
||||||
|
labels.append(f"✅ {d.get('factor', '')}")
|
||||||
|
values.append(abs(d.get('score', 0))) # Positive for helpful
|
||||||
|
colors.append("#1D9E75")
|
||||||
|
|
||||||
|
if not labels:
|
||||||
|
return {
|
||||||
|
"chart_type": "bar",
|
||||||
|
"data": {
|
||||||
|
"labels": [],
|
||||||
|
"datasets": []
|
||||||
|
},
|
||||||
|
"metadata": {
|
||||||
|
"confidence": "low",
|
||||||
|
"data_points": 0,
|
||||||
|
"message": "Keine signifikanten Treiber gefunden"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
"chart_type": "bar",
|
||||||
|
"data": {
|
||||||
|
"labels": labels,
|
||||||
|
"datasets": [
|
||||||
|
{
|
||||||
|
"label": "Impact Score",
|
||||||
|
"data": values,
|
||||||
|
"backgroundColor": colors,
|
||||||
|
"borderColor": "#085041",
|
||||||
|
"borderWidth": 1
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {
|
||||||
|
"confidence": "medium",
|
||||||
|
"hindering_count": len(top_hindering),
|
||||||
|
"helpful_count": len(top_helpful),
|
||||||
|
"total_factors": len(drivers)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
# ── Health Endpoint ──────────────────────────────────────────────────────────
|
# ── Health Endpoint ──────────────────────────────────────────────────────────
|
||||||
|
|||||||
@@ -35,7 +35,6 @@ from csv_parser.type_converter import build_row_after_mapping, diagnose_row_mapp
|
|||||||
from csv_parser.field_units import source_unit_choices_for_field
|
from csv_parser.field_units import source_unit_choices_for_field
|
||||||
from csv_parser.import_errors import enrich_row_error
|
from csv_parser.import_errors import enrich_row_error
|
||||||
from csv_parser.module_registry import get_module_definition, list_modules, validate_field_mappings
|
from csv_parser.module_registry import get_module_definition, list_modules, validate_field_mappings
|
||||||
from csv_parser.template_validator import validate_csv_template
|
|
||||||
from data_layer.activity_persistence_orchestrator import merge_activity_csv_module_fields
|
from data_layer.activity_persistence_orchestrator import merge_activity_csv_module_fields
|
||||||
from csv_parser.sleep_apple_import import detect_apple_sleep_csv_format
|
from csv_parser.sleep_apple_import import detect_apple_sleep_csv_format
|
||||||
|
|
||||||
@@ -51,38 +50,6 @@ def _load_import_limits() -> dict[str, int]:
|
|||||||
return get_csv_import_limits(r2d(row) if row else None)
|
return get_csv_import_limits(r2d(row) if row else None)
|
||||||
|
|
||||||
|
|
||||||
def _mapping_column_signature(m: dict) -> list[str] | None:
|
|
||||||
sig = m.get("column_signature")
|
|
||||||
if not sig:
|
|
||||||
return None
|
|
||||||
return list(sig)
|
|
||||||
|
|
||||||
|
|
||||||
def _validate_mapping_config(cur, m: dict) -> dict:
|
|
||||||
"""Strukturelle Vorlagen-Prüfung (wie Admin validate / Create)."""
|
|
||||||
return validate_csv_template(
|
|
||||||
m["module"],
|
|
||||||
m.get("field_mappings") or {},
|
|
||||||
m.get("type_conversions"),
|
|
||||||
m.get("import_row_processing"),
|
|
||||||
_mapping_column_signature(m),
|
|
||||||
cur=cur,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _ensure_mapping_valid(cur, m: dict) -> dict:
|
|
||||||
report = _validate_mapping_config(cur, m)
|
|
||||||
if not report.get("valid"):
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=422,
|
|
||||||
detail={
|
|
||||||
"message": "CSV-Vorlage ist strukturell ungültig.",
|
|
||||||
"validation": report,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
return report
|
|
||||||
|
|
||||||
|
|
||||||
def _mapping_to_summary(m: dict) -> dict:
|
def _mapping_to_summary(m: dict) -> dict:
|
||||||
return {
|
return {
|
||||||
"id": m["id"],
|
"id": m["id"],
|
||||||
@@ -217,16 +184,6 @@ def copy_csv_mapping(
|
|||||||
n += 1
|
n += 1
|
||||||
name = f"{base_name} {n}"
|
name = f"{base_name} {n}"
|
||||||
|
|
||||||
validation = _validate_mapping_config(cur, src)
|
|
||||||
if not validation.get("valid"):
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=422,
|
|
||||||
detail={
|
|
||||||
"message": "Quell-Vorlage ist ungültig — Kopie abgebrochen.",
|
|
||||||
"validation": validation,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"""
|
"""
|
||||||
INSERT INTO csv_field_mappings (
|
INSERT INTO csv_field_mappings (
|
||||||
@@ -256,21 +213,7 @@ def copy_csv_mapping(
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
new_id = cur.fetchone()["id"]
|
new_id = cur.fetchone()["id"]
|
||||||
return {"new_mapping_id": new_id, "mapping_name": name, "validation": validation}
|
return {"new_mapping_id": new_id, "mapping_name": name}
|
||||||
|
|
||||||
|
|
||||||
@router.get("/mappings/{mapping_id}/validate")
|
|
||||||
def validate_csv_mapping(
|
|
||||||
mapping_id: int,
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
"""Strukturprüfung einer gespeicherten Vorlage (System oder eigenes Profil-Mapping)."""
|
|
||||||
pid = str(session["profile_id"])
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
m = _fetch_mapping_row(cur, mapping_id, pid)
|
|
||||||
report = _validate_mapping_config(cur, m)
|
|
||||||
return {"mapping_id": mapping_id, "mapping_name": m.get("mapping_name"), **report}
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/analyze")
|
@router.post("/analyze")
|
||||||
@@ -595,7 +538,6 @@ async def csv_import_execute(
|
|||||||
)
|
)
|
||||||
|
|
||||||
_check_module_feature_access(pid, exec_module)
|
_check_module_feature_access(pid, exec_module)
|
||||||
_ensure_mapping_valid(cur, m)
|
|
||||||
|
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"""
|
"""
|
||||||
@@ -712,8 +654,6 @@ async def csv_import_execute(
|
|||||||
"updated": result["rows_updated"],
|
"updated": result["rows_updated"],
|
||||||
"skipped": result["rows_skipped"],
|
"skipped": result["rows_skipped"],
|
||||||
"errors": result["rows_errors"],
|
"errors": result["rows_errors"],
|
||||||
"items_written": result.get("items_written", 0),
|
|
||||||
"unmapped_names": result.get("unmapped_names", 0),
|
|
||||||
},
|
},
|
||||||
"error_details": result["error_details"],
|
"error_details": result["error_details"],
|
||||||
}
|
}
|
||||||
+32
-514
@@ -11,8 +11,6 @@ from typing import Optional
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from fastapi import APIRouter, HTTPException, UploadFile, File, Header, Depends
|
from fastapi import APIRouter, HTTPException, UploadFile, File, Header, Depends
|
||||||
from fastapi.responses import Response
|
|
||||||
from pydantic import BaseModel
|
|
||||||
|
|
||||||
from db import get_db, get_cursor, r2d
|
from db import get_db, get_cursor, r2d
|
||||||
from auth import require_auth, check_feature_access, increment_feature_usage
|
from auth import require_auth, check_feature_access, increment_feature_usage
|
||||||
@@ -24,22 +22,6 @@ router = APIRouter(prefix="/api/nutrition", tags=["nutrition"])
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class RecipeIngredientIn(BaseModel):
|
|
||||||
source_name_raw: str
|
|
||||||
quantity_raw: Optional[str] = None
|
|
||||||
quantity_g: Optional[float] = None
|
|
||||||
quantity_amount: Optional[float] = None
|
|
||||||
source_unit: Optional[str] = None
|
|
||||||
grams_per_unit: Optional[float] = None
|
|
||||||
|
|
||||||
|
|
||||||
class RecipeIn(BaseModel):
|
|
||||||
name_raw: str
|
|
||||||
portions: float = 1
|
|
||||||
description: Optional[str] = None
|
|
||||||
ingredients: list[RecipeIngredientIn] = []
|
|
||||||
|
|
||||||
|
|
||||||
# ── Helper ────────────────────────────────────────────────────────────────────
|
# ── Helper ────────────────────────────────────────────────────────────────────
|
||||||
def _pf(s):
|
def _pf(s):
|
||||||
"""Parse float from string (handles comma decimal separator)."""
|
"""Parse float from string (handles comma decimal separator)."""
|
||||||
@@ -49,13 +31,8 @@ def _pf(s):
|
|||||||
|
|
||||||
# ── Endpoints ─────────────────────────────────────────────────────────────────
|
# ── Endpoints ─────────────────────────────────────────────────────────────────
|
||||||
@router.post("/import-csv")
|
@router.post("/import-csv")
|
||||||
async def import_nutrition_csv(
|
async def import_nutrition_csv(file: UploadFile=File(...), x_profile_id: Optional[str]=Header(default=None), session: dict=Depends(require_auth)):
|
||||||
file: UploadFile = File(...),
|
"""Import FDDB nutrition CSV."""
|
||||||
overwrite_existing: bool = False,
|
|
||||||
x_profile_id: Optional[str] = Header(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
"""Import FDDB nutrition CSV (optional item persist + conflict policy)."""
|
|
||||||
pid = get_pid(x_profile_id)
|
pid = get_pid(x_profile_id)
|
||||||
|
|
||||||
# Phase 4: Check feature access and ENFORCE
|
# Phase 4: Check feature access and ENFORCE
|
||||||
@@ -79,97 +56,48 @@ async def import_nutrition_csv(
|
|||||||
except: text = raw.decode('latin-1')
|
except: text = raw.decode('latin-1')
|
||||||
if text.startswith('\ufeff'): text = text[1:]
|
if text.startswith('\ufeff'): text = text[1:]
|
||||||
if not text.strip(): raise HTTPException(400,"Leere Datei")
|
if not text.strip(): raise HTTPException(400,"Leere Datei")
|
||||||
from data_layer.nutrition_items import get_import_policy, replace_csv_items_for_dates
|
|
||||||
|
|
||||||
overwrite = bool(overwrite_existing)
|
|
||||||
reader = csv.DictReader(io.StringIO(text), delimiter=';')
|
reader = csv.DictReader(io.StringIO(text), delimiter=';')
|
||||||
item_rows = []
|
|
||||||
days: dict = {}
|
days: dict = {}
|
||||||
count = 0
|
count = 0
|
||||||
for row in reader:
|
for row in reader:
|
||||||
rd = row.get('datum_tag_monat_jahr_stunde_minute','').strip().strip('"')
|
rd = row.get('datum_tag_monat_jahr_stunde_minute','').strip().strip('"')
|
||||||
if not rd: continue
|
if not rd: continue
|
||||||
try:
|
try:
|
||||||
parts = rd.split(' ')
|
p = rd.split(' ')[0].split('.')
|
||||||
p = parts[0].split('.')
|
|
||||||
iso = f"{p[2]}-{p[1]}-{p[0]}"
|
iso = f"{p[2]}-{p[1]}-{p[0]}"
|
||||||
logged_at = None
|
except: continue
|
||||||
if len(parts) > 1:
|
days.setdefault(iso,{'kcal':0,'fat_g':0,'carbs_g':0,'protein_g':0})
|
||||||
try:
|
days[iso]['kcal'] += _pf(row.get('kj',0))/4.184
|
||||||
logged_at = datetime.strptime(rd.strip(), '%d.%m.%Y %H:%M')
|
days[iso]['fat_g'] += _pf(row.get('fett_g',0))
|
||||||
except ValueError:
|
days[iso]['carbs_g'] += _pf(row.get('kh_g',0))
|
||||||
logged_at = None
|
days[iso]['protein_g'] += _pf(row.get('protein_g',0))
|
||||||
except Exception:
|
count+=1
|
||||||
continue
|
inserted=0
|
||||||
kcal = _pf(row.get('kj', 0)) / 4.184
|
new_entries=0
|
||||||
fat = _pf(row.get('fett_g', 0))
|
|
||||||
carbs = _pf(row.get('kh_g', 0))
|
|
||||||
prot = _pf(row.get('protein_g', 0))
|
|
||||||
days.setdefault(iso, {'kcal': 0, 'fat_g': 0, 'carbs_g': 0, 'protein_g': 0})
|
|
||||||
days[iso]['kcal'] += kcal
|
|
||||||
days[iso]['fat_g'] += fat
|
|
||||||
days[iso]['carbs_g'] += carbs
|
|
||||||
days[iso]['protein_g'] += prot
|
|
||||||
name = (row.get('bezeichnung') or '').strip().strip('"')
|
|
||||||
if name:
|
|
||||||
item_rows.append({
|
|
||||||
"date": iso,
|
|
||||||
"logged_at": logged_at,
|
|
||||||
"food_name": name,
|
|
||||||
"quantity_raw": (row.get('menge') or '').strip() or None,
|
|
||||||
"kcal": kcal,
|
|
||||||
"protein_g": prot,
|
|
||||||
"fat_g": fat,
|
|
||||||
"carbs_g": carbs,
|
|
||||||
})
|
|
||||||
count += 1
|
|
||||||
inserted = 0
|
|
||||||
new_entries = 0
|
|
||||||
ingest_result = {"conflicts": [], "items_written": 0, "policy": "prompt"}
|
|
||||||
with get_db() as conn:
|
with get_db() as conn:
|
||||||
cur = get_cursor(conn)
|
cur = get_cursor(conn)
|
||||||
policy = get_import_policy(cur, pid)
|
for iso,vals in days.items():
|
||||||
override = "overwrite_catalog" if overwrite else None
|
kcal=round(vals['kcal'],1); fat=round(vals['fat_g'],1)
|
||||||
if item_rows:
|
carbs=round(vals['carbs_g'],1); prot=round(vals['protein_g'],1)
|
||||||
ingest_result = replace_csv_items_for_dates(
|
cur.execute("SELECT id FROM nutrition_log WHERE profile_id=%s AND date=%s",(pid,iso))
|
||||||
cur, pid, item_rows, policy=policy, policy_override=override,
|
|
||||||
)
|
|
||||||
new_entries = ingest_result.get("new_log_days") or 0
|
|
||||||
inserted = ingest_result.get("days_written") or 0
|
|
||||||
else:
|
|
||||||
for iso, vals in days.items():
|
|
||||||
kcal = round(vals['kcal'], 1)
|
|
||||||
fat = round(vals['fat_g'], 1)
|
|
||||||
carbs = round(vals['carbs_g'], 1)
|
|
||||||
prot = round(vals['protein_g'], 1)
|
|
||||||
cur.execute("SELECT id FROM nutrition_log WHERE profile_id=%s AND date=%s", (pid, iso))
|
|
||||||
is_new = not cur.fetchone()
|
is_new = not cur.fetchone()
|
||||||
if not is_new:
|
if not is_new:
|
||||||
if policy in ("overwrite_catalog", "overwrite_fddb") or overwrite:
|
# UPDATE existing
|
||||||
cur.execute(
|
cur.execute("UPDATE nutrition_log SET kcal=%s,protein_g=%s,fat_g=%s,carbs_g=%s WHERE profile_id=%s AND date=%s",
|
||||||
"UPDATE nutrition_log SET kcal=%s,protein_g=%s,fat_g=%s,carbs_g=%s,source='csv',macro_origin='fddb' WHERE profile_id=%s AND date=%s",
|
(kcal,prot,fat,carbs,pid,iso))
|
||||||
(kcal, prot, fat, carbs, pid, iso),
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
cur.execute(
|
# INSERT new
|
||||||
"INSERT INTO nutrition_log (id,profile_id,date,kcal,protein_g,fat_g,carbs_g,source,macro_origin,created) VALUES (%s,%s,%s,%s,%s,%s,%s,'csv','fddb',CURRENT_TIMESTAMP)",
|
cur.execute("INSERT INTO nutrition_log (id,profile_id,date,kcal,protein_g,fat_g,carbs_g,source,created) VALUES (%s,%s,%s,%s,%s,%s,%s,'csv',CURRENT_TIMESTAMP)",
|
||||||
(str(uuid.uuid4()), pid, iso, kcal, prot, fat, carbs),
|
(str(uuid.uuid4()),pid,iso,kcal,prot,fat,carbs))
|
||||||
)
|
|
||||||
new_entries += 1
|
new_entries += 1
|
||||||
inserted += 1
|
inserted+=1
|
||||||
|
|
||||||
|
# Phase 2: Increment usage counter for each new entry created
|
||||||
for _ in range(new_entries):
|
for _ in range(new_entries):
|
||||||
increment_feature_usage(pid, 'nutrition_entries')
|
increment_feature_usage(pid, 'nutrition_entries')
|
||||||
|
|
||||||
return {
|
return {"rows_parsed":count,"days_imported":inserted,"new_entries":new_entries,
|
||||||
"rows_parsed": count,
|
"date_range":{"from":min(days) if days else None,"to":max(days) if days else None}}
|
||||||
"days_imported": inserted,
|
|
||||||
"new_entries": new_entries,
|
|
||||||
"items_written": ingest_result.get("items_written", 0),
|
|
||||||
"conflicts": ingest_result.get("conflicts") or [],
|
|
||||||
"policy": ingest_result.get("policy", "prompt"),
|
|
||||||
"date_range": {"from": min(days) if days else None, "to": max(days) if days else None},
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("")
|
@router.post("")
|
||||||
@@ -194,7 +122,7 @@ def create_nutrition(date: str, kcal: float, protein_g: float, fat_g: float, car
|
|||||||
# UPDATE existing entry
|
# UPDATE existing entry
|
||||||
cur.execute("""
|
cur.execute("""
|
||||||
UPDATE nutrition_log
|
UPDATE nutrition_log
|
||||||
SET kcal=%s, protein_g=%s, fat_g=%s, carbs_g=%s, source='manual', macro_origin='manual'
|
SET kcal=%s, protein_g=%s, fat_g=%s, carbs_g=%s, source='manual'
|
||||||
WHERE id=%s AND profile_id=%s
|
WHERE id=%s AND profile_id=%s
|
||||||
""", (round(kcal,1), round(protein_g,1), round(fat_g,1), round(carbs_g,1), existing['id'], pid))
|
""", (round(kcal,1), round(protein_g,1), round(fat_g,1), round(carbs_g,1), existing['id'], pid))
|
||||||
return {"success": True, "mode": "updated", "id": existing['id']}
|
return {"success": True, "mode": "updated", "id": existing['id']}
|
||||||
@@ -217,8 +145,8 @@ def create_nutrition(date: str, kcal: float, protein_g: float, fat_g: float, car
|
|||||||
# INSERT new entry
|
# INSERT new entry
|
||||||
new_id = str(uuid.uuid4())
|
new_id = str(uuid.uuid4())
|
||||||
cur.execute("""
|
cur.execute("""
|
||||||
INSERT INTO nutrition_log (id, profile_id, date, kcal, protein_g, fat_g, carbs_g, source, macro_origin, created)
|
INSERT INTO nutrition_log (id, profile_id, date, kcal, protein_g, fat_g, carbs_g, source, created)
|
||||||
VALUES (%s, %s, %s, %s, %s, %s, %s, 'manual', 'manual', CURRENT_TIMESTAMP)
|
VALUES (%s, %s, %s, %s, %s, %s, %s, 'manual', CURRENT_TIMESTAMP)
|
||||||
""", (new_id, pid, date, round(kcal,1), round(protein_g,1), round(fat_g,1), round(carbs_g,1)))
|
""", (new_id, pid, date, round(kcal,1), round(protein_g,1), round(fat_g,1), round(carbs_g,1)))
|
||||||
|
|
||||||
# Phase 2: Increment usage counter
|
# Phase 2: Increment usage counter
|
||||||
@@ -234,34 +162,8 @@ def list_nutrition(limit: int=365, x_profile_id: Optional[str]=Header(default=No
|
|||||||
with get_db() as conn:
|
with get_db() as conn:
|
||||||
cur = get_cursor(conn)
|
cur = get_cursor(conn)
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"""
|
"SELECT * FROM nutrition_log WHERE profile_id=%s ORDER BY date DESC LIMIT %s", (pid,limit))
|
||||||
SELECT n.*, m.mark_type, m.note AS mark_note
|
return [r2d(r) for r in cur.fetchall()]
|
||||||
FROM nutrition_log n
|
|
||||||
LEFT JOIN nutrition_day_marks m
|
|
||||||
ON m.profile_id = n.profile_id AND m.date = n.date
|
|
||||||
WHERE n.profile_id=%s
|
|
||||||
ORDER BY n.date DESC
|
|
||||||
LIMIT %s
|
|
||||||
""",
|
|
||||||
(pid, limit),
|
|
||||||
)
|
|
||||||
rows = [r2d(r) for r in cur.fetchall()]
|
|
||||||
dates = [r["date"] for r in rows if r.get("date") is not None]
|
|
||||||
counts = {}
|
|
||||||
if dates:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT date, COUNT(*) AS item_count
|
|
||||||
FROM nutrition_items
|
|
||||||
WHERE profile_id = %s AND date = ANY(%s)
|
|
||||||
GROUP BY date
|
|
||||||
""",
|
|
||||||
(pid, dates),
|
|
||||||
)
|
|
||||||
counts = {str(r["date"]): int(r["item_count"] or 0) for r in cur.fetchall()}
|
|
||||||
for r in rows:
|
|
||||||
r["item_count"] = counts.get(str(r.get("date")), 0)
|
|
||||||
return rows
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/by-date/{date}")
|
@router.get("/by-date/{date}")
|
||||||
@@ -326,390 +228,6 @@ def import_history(x_profile_id: Optional[str]=Header(default=None), session: di
|
|||||||
return [r2d(r) for r in cur.fetchall()]
|
return [r2d(r) for r in cur.fetchall()]
|
||||||
|
|
||||||
|
|
||||||
@router.get("/items")
|
|
||||||
def list_nutrition_items(
|
|
||||||
date: Optional[str] = None,
|
|
||||||
limit: int = 200,
|
|
||||||
x_profile_id: Optional[str] = Header(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
pid = get_pid(x_profile_id)
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
if date:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT i.*, f.name_de AS food_name_de, f.bls_code, f.catalog_kind,
|
|
||||||
r.name_raw AS recipe_name_de
|
|
||||||
FROM nutrition_items i
|
|
||||||
LEFT JOIN food_catalog f ON f.id = i.food_id
|
|
||||||
LEFT JOIN food_recipes r ON r.id = i.recipe_id
|
|
||||||
WHERE i.profile_id=%s AND i.date=%s
|
|
||||||
ORDER BY i.logged_at NULLS LAST, i.source_name_raw
|
|
||||||
""",
|
|
||||||
(pid, date),
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT i.*, f.name_de AS food_name_de, f.bls_code, f.catalog_kind,
|
|
||||||
r.name_raw AS recipe_name_de
|
|
||||||
FROM nutrition_items i
|
|
||||||
LEFT JOIN food_catalog f ON f.id = i.food_id
|
|
||||||
LEFT JOIN food_recipes r ON r.id = i.recipe_id
|
|
||||||
WHERE i.profile_id=%s
|
|
||||||
ORDER BY i.date DESC, i.logged_at NULLS LAST
|
|
||||||
LIMIT %s
|
|
||||||
""",
|
|
||||||
(pid, min(limit, 500)),
|
|
||||||
)
|
|
||||||
from data_layer.nutrition_items import annotate_nutrition_item
|
|
||||||
return [annotate_nutrition_item(cur, pid, r2d(r)) for r in cur.fetchall()]
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/unmapped")
|
|
||||||
def list_unmapped_foods(
|
|
||||||
since_days: int = 0,
|
|
||||||
count_only: bool = False,
|
|
||||||
meta: bool = False,
|
|
||||||
x_profile_id: Optional[str] = Header(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
from data_layer.food_mapping import list_unmapped_payload
|
|
||||||
|
|
||||||
pid = x_profile_id or session["profile_id"]
|
|
||||||
with get_db() as conn:
|
|
||||||
return list_unmapped_payload(get_cursor(conn), pid, since_days, count_only, meta)
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/ingredient-names")
|
|
||||||
def list_food_ingredient_names(
|
|
||||||
x_profile_id: Optional[str] = Header(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
from data_layer.food_recipes import list_known_ingredient_names
|
|
||||||
pid = get_pid(x_profile_id)
|
|
||||||
with get_db() as conn:
|
|
||||||
return list_known_ingredient_names(get_cursor(conn), pid)
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/recipes")
|
|
||||||
def list_food_recipes(
|
|
||||||
brief: bool = False,
|
|
||||||
x_profile_id: Optional[str] = Header(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
from data_layer.food_recipes import list_recipes
|
|
||||||
pid = get_pid(x_profile_id)
|
|
||||||
with get_db() as conn:
|
|
||||||
return list_recipes(get_cursor(conn), pid, with_ingredients=not brief)
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/recipes/{recipe_id}")
|
|
||||||
def get_food_recipe(
|
|
||||||
recipe_id: str,
|
|
||||||
x_profile_id: Optional[str] = Header(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
from data_layer.food_recipes import get_recipe
|
|
||||||
pid = get_pid(x_profile_id)
|
|
||||||
with get_db() as conn:
|
|
||||||
rec = get_recipe(get_cursor(conn), pid, recipe_id)
|
|
||||||
if not rec:
|
|
||||||
raise HTTPException(404, "Rezept nicht gefunden")
|
|
||||||
return rec
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/recipes")
|
|
||||||
def create_food_recipe(
|
|
||||||
body: RecipeIn,
|
|
||||||
x_profile_id: Optional[str] = Header(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
from data_layer.food_recipes import save_recipe
|
|
||||||
from data_layer.nutrition_items import schedule_daily_nutrient_rebuild
|
|
||||||
pid = get_pid(x_profile_id)
|
|
||||||
try:
|
|
||||||
with get_db() as conn:
|
|
||||||
saved = save_recipe(get_cursor(conn), pid, body.model_dump())
|
|
||||||
schedule_daily_nutrient_rebuild(pid, saved.get("dates_linked") or [], "Liste angelegt")
|
|
||||||
return saved
|
|
||||||
except ValueError as e:
|
|
||||||
raise HTTPException(400, str(e)) from e
|
|
||||||
|
|
||||||
|
|
||||||
@router.put("/recipes/{recipe_id}")
|
|
||||||
def update_food_recipe(
|
|
||||||
recipe_id: str,
|
|
||||||
body: RecipeIn,
|
|
||||||
x_profile_id: Optional[str] = Header(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
from data_layer.food_recipes import save_recipe
|
|
||||||
from data_layer.nutrition_items import schedule_daily_nutrient_rebuild
|
|
||||||
pid = get_pid(x_profile_id)
|
|
||||||
try:
|
|
||||||
with get_db() as conn:
|
|
||||||
saved = save_recipe(get_cursor(conn), pid, body.model_dump(), recipe_id)
|
|
||||||
schedule_daily_nutrient_rebuild(pid, saved.get("dates_linked") or [], "Liste geändert")
|
|
||||||
return saved
|
|
||||||
except KeyError:
|
|
||||||
raise HTTPException(404, "Rezept nicht gefunden") from None
|
|
||||||
except ValueError as e:
|
|
||||||
raise HTTPException(400, str(e)) from e
|
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/recipes/{recipe_id}")
|
|
||||||
def delete_food_recipe(
|
|
||||||
recipe_id: str,
|
|
||||||
x_profile_id: Optional[str] = Header(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
from data_layer.food_recipes import delete_recipe
|
|
||||||
pid = get_pid(x_profile_id)
|
|
||||||
try:
|
|
||||||
with get_db() as conn:
|
|
||||||
delete_recipe(get_cursor(conn), pid, recipe_id)
|
|
||||||
except KeyError:
|
|
||||||
raise HTTPException(404, "Rezept nicht gefunden") from None
|
|
||||||
return {"ok": True}
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/recipes/import-fddb-lists")
|
|
||||||
async def import_fddb_lists(
|
|
||||||
file: UploadFile = File(...),
|
|
||||||
x_profile_id: Optional[str] = Header(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
from bls.recipe_parser import parse_fddb_lists_csv
|
|
||||||
from data_layer.food_recipes import upsert_recipes
|
|
||||||
|
|
||||||
pid = get_pid(x_profile_id)
|
|
||||||
raw = await file.read()
|
|
||||||
if not raw:
|
|
||||||
raise HTTPException(400, "Leere Datei")
|
|
||||||
try:
|
|
||||||
text = raw.decode("utf-8-sig")
|
|
||||||
except UnicodeDecodeError:
|
|
||||||
text = raw.decode("latin-1")
|
|
||||||
recipes = parse_fddb_lists_csv(text)
|
|
||||||
if not recipes:
|
|
||||||
raise HTTPException(400, "Keine Rezepte in der Datei erkannt")
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
stats = upsert_recipes(cur, pid, recipes)
|
|
||||||
dates = stats.pop("dates_linked", []) or []
|
|
||||||
from data_layer.nutrition_items import schedule_daily_nutrient_rebuild
|
|
||||||
schedule_daily_nutrient_rebuild(pid, dates, "FDDB-Listenimport")
|
|
||||||
return {"ok": True, "recipes": len(recipes), "rebuild_pending": len(dates), **stats}
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/recipes/{recipe_id}/apply")
|
|
||||||
def apply_recipe_name(
|
|
||||||
recipe_id: str,
|
|
||||||
body: dict,
|
|
||||||
x_profile_id: Optional[str] = Header(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
from data_layer.food_mapping import normalize_food_name
|
|
||||||
from data_layer.food_recipes import apply_recipe_to_items
|
|
||||||
from data_layer.nutrition_items import dates_for_normalized_name, rebuild_daily_nutrients
|
|
||||||
|
|
||||||
pid = get_pid(x_profile_id)
|
|
||||||
source_name = (body.get("source_name") or "").strip()
|
|
||||||
if not source_name:
|
|
||||||
raise HTTPException(400, "source_name fehlt")
|
|
||||||
norm = normalize_food_name(source_name)
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute("SELECT id FROM food_recipes WHERE id = %s AND profile_id = %s", (recipe_id, pid))
|
|
||||||
if not cur.fetchone():
|
|
||||||
raise HTTPException(404, "Rezept nicht gefunden")
|
|
||||||
n = apply_recipe_to_items(cur, pid, norm, recipe_id)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT DISTINCT date::text AS date
|
|
||||||
FROM nutrition_items
|
|
||||||
WHERE profile_id = %s AND recipe_id = %s
|
|
||||||
""",
|
|
||||||
(pid, recipe_id),
|
|
||||||
)
|
|
||||||
days = [r["date"] for r in cur.fetchall()]
|
|
||||||
if not days:
|
|
||||||
days = dates_for_normalized_name(cur, pid, norm)
|
|
||||||
for d in days:
|
|
||||||
rebuild_daily_nutrients(cur, pid, d)
|
|
||||||
return {"ok": True, "items_updated": n}
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/food-knowledge")
|
|
||||||
def export_food_knowledge_file(
|
|
||||||
x_profile_id: Optional[str] = Header(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
import json
|
|
||||||
from data_layer.food_knowledge import export_food_knowledge
|
|
||||||
|
|
||||||
pid = get_pid(x_profile_id)
|
|
||||||
try:
|
|
||||||
with get_db() as conn:
|
|
||||||
bundle = export_food_knowledge(get_cursor(conn), pid)
|
|
||||||
body = json.dumps(bundle, ensure_ascii=False, indent=2, default=str)
|
|
||||||
except Exception:
|
|
||||||
logger.exception("Export food-knowledge fehlgeschlagen")
|
|
||||||
raise HTTPException(500, "Export fehlgeschlagen") from None
|
|
||||||
stamp = datetime.now().strftime("%Y-%m-%d")
|
|
||||||
return Response(
|
|
||||||
content=body.encode("utf-8"),
|
|
||||||
media_type="application/json; charset=utf-8",
|
|
||||||
headers={"Content-Disposition": f'attachment; filename="mitai-food-knowledge-{stamp}.json"'},
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/food-knowledge")
|
|
||||||
async def import_food_knowledge_file(
|
|
||||||
file: UploadFile = File(...),
|
|
||||||
x_profile_id: Optional[str] = Header(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
import json
|
|
||||||
from data_layer.food_knowledge import import_food_knowledge, parse_food_knowledge_bundle
|
|
||||||
|
|
||||||
pid = get_pid(x_profile_id)
|
|
||||||
raw = await file.read()
|
|
||||||
if not raw:
|
|
||||||
raise HTTPException(400, "Leere Datei")
|
|
||||||
try:
|
|
||||||
data = json.loads(raw.decode("utf-8-sig"))
|
|
||||||
parse_food_knowledge_bundle(data)
|
|
||||||
except ValueError as e:
|
|
||||||
raise HTTPException(400, str(e)) from e
|
|
||||||
except Exception as e:
|
|
||||||
raise HTTPException(400, f"Ungültiges JSON: {e}") from e
|
|
||||||
with get_db() as conn:
|
|
||||||
return import_food_knowledge(get_cursor(conn), pid, data)
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/import-conflicts/resolve")
|
|
||||||
def resolve_import_conflicts(
|
|
||||||
body: dict,
|
|
||||||
x_profile_id: Optional[str] = Header(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
from data_layer.nutrition_items import (
|
|
||||||
apply_nutrition_day_macros,
|
|
||||||
compute_day_macro_sums,
|
|
||||||
resolve_choice_macros,
|
|
||||||
)
|
|
||||||
|
|
||||||
pid = get_pid(x_profile_id)
|
|
||||||
decisions = body.get("decisions") or []
|
|
||||||
applied = 0
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
for dec in decisions:
|
|
||||||
day = dec.get("date")
|
|
||||||
choice = dec.get("choice")
|
|
||||||
if not day or not choice:
|
|
||||||
continue
|
|
||||||
sums = compute_day_macro_sums(cur, pid, day)
|
|
||||||
if not sums.get("existing") and choice == "existing":
|
|
||||||
continue
|
|
||||||
macros, origin = resolve_choice_macros(sums, choice)
|
|
||||||
apply_nutrition_day_macros(
|
|
||||||
cur, pid, day, macros, macro_origin=origin, source="csv", confirm=True,
|
|
||||||
)
|
|
||||||
applied += 1
|
|
||||||
return {"applied": applied}
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/days/{day}/macros")
|
|
||||||
def get_day_macros(
|
|
||||||
day: str,
|
|
||||||
x_profile_id: Optional[str] = Header(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
from data_layer.nutrition_items import day_macro_preview
|
|
||||||
|
|
||||||
pid = get_pid(x_profile_id)
|
|
||||||
with get_db() as conn:
|
|
||||||
return day_macro_preview(get_cursor(conn), pid, day)
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/days/{day}/apply-catalog-macros")
|
|
||||||
def apply_catalog_macros_for_day(
|
|
||||||
day: str,
|
|
||||||
x_profile_id: Optional[str] = Header(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
from data_layer.nutrition_items import apply_day_catalog_macros
|
|
||||||
|
|
||||||
pid = get_pid(x_profile_id)
|
|
||||||
try:
|
|
||||||
with get_db() as conn:
|
|
||||||
return apply_day_catalog_macros(get_cursor(conn), pid, day)
|
|
||||||
except ValueError as e:
|
|
||||||
raise HTTPException(400, str(e)) from e
|
|
||||||
|
|
||||||
|
|
||||||
@router.put("/days/{day}/mark")
|
|
||||||
def upsert_day_mark(
|
|
||||||
day: str,
|
|
||||||
body: dict,
|
|
||||||
x_profile_id: Optional[str] = Header(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
pid = get_pid(x_profile_id)
|
|
||||||
mark_type = (body or {}).get("mark_type")
|
|
||||||
note = (body or {}).get("note")
|
|
||||||
if mark_type not in ("fasting", "incomplete"):
|
|
||||||
raise HTTPException(400, "mark_type muss fasting oder incomplete sein")
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO nutrition_day_marks (profile_id, date, mark_type, note, source, updated_at)
|
|
||||||
VALUES (%s, %s, %s, %s, 'manual', NOW())
|
|
||||||
ON CONFLICT (profile_id, date)
|
|
||||||
DO UPDATE SET mark_type = EXCLUDED.mark_type, note = EXCLUDED.note, updated_at = NOW()
|
|
||||||
RETURNING *
|
|
||||||
""",
|
|
||||||
(pid, day, mark_type, note),
|
|
||||||
)
|
|
||||||
return r2d(cur.fetchone())
|
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/days/{day}/mark")
|
|
||||||
def delete_day_mark(
|
|
||||||
day: str,
|
|
||||||
x_profile_id: Optional[str] = Header(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
pid = get_pid(x_profile_id)
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute(
|
|
||||||
"DELETE FROM nutrition_day_marks WHERE profile_id=%s AND date=%s",
|
|
||||||
(pid, day),
|
|
||||||
)
|
|
||||||
return {"ok": True}
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/marks")
|
|
||||||
def list_day_marks(
|
|
||||||
x_profile_id: Optional[str] = Header(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
pid = get_pid(x_profile_id)
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute(
|
|
||||||
"SELECT * FROM nutrition_day_marks WHERE profile_id=%s ORDER BY date DESC",
|
|
||||||
(pid,),
|
|
||||||
)
|
|
||||||
return [r2d(r) for r in cur.fetchall()]
|
|
||||||
|
|
||||||
|
|
||||||
@router.put("/{entry_id}")
|
@router.put("/{entry_id}")
|
||||||
def update_nutrition(entry_id: str, kcal: float, protein_g: float, fat_g: float, carbs_g: float,
|
def update_nutrition(entry_id: str, kcal: float, protein_g: float, fat_g: float, carbs_g: float,
|
||||||
x_profile_id: Optional[str]=Header(default=None), session: dict=Depends(require_auth)):
|
x_profile_id: Optional[str]=Header(default=None), session: dict=Depends(require_auth)):
|
||||||
@@ -724,7 +242,7 @@ def update_nutrition(entry_id: str, kcal: float, protein_g: float, fat_g: float,
|
|||||||
|
|
||||||
cur.execute("""
|
cur.execute("""
|
||||||
UPDATE nutrition_log
|
UPDATE nutrition_log
|
||||||
SET kcal=%s, protein_g=%s, fat_g=%s, carbs_g=%s, source='manual', macro_origin='manual'
|
SET kcal=%s, protein_g=%s, fat_g=%s, carbs_g=%s
|
||||||
WHERE id=%s AND profile_id=%s
|
WHERE id=%s AND profile_id=%s
|
||||||
""", (round(kcal,1), round(protein_g,1), round(fat_g,1), round(carbs_g,1), entry_id, pid))
|
""", (round(kcal,1), round(protein_g,1), round(fat_g,1), round(carbs_g,1), entry_id, pid))
|
||||||
|
|
||||||
|
|||||||
@@ -109,17 +109,9 @@ def update_profile(pid: str, p: ProfileUpdate, session=Depends(require_auth)):
|
|||||||
data["verification_expires"] = None
|
data["verification_expires"] = None
|
||||||
|
|
||||||
nullable_keys = {"goal_weight", "goal_bf_pct", "dob"}
|
nullable_keys = {"goal_weight", "goal_bf_pct", "dob"}
|
||||||
allowed_nutrition_policy = {
|
|
||||||
"prompt", "overwrite_catalog", "overwrite_fddb", "keep_existing",
|
|
||||||
}
|
|
||||||
for k, v in patch.items():
|
for k, v in patch.items():
|
||||||
if k == "email":
|
if k == "email":
|
||||||
continue
|
continue
|
||||||
if k == "nutrition_import_conflict_policy":
|
|
||||||
if v not in allowed_nutrition_policy:
|
|
||||||
raise HTTPException(400, "Ungültige Import-Policy")
|
|
||||||
data[k] = v
|
|
||||||
continue
|
|
||||||
if v is None and k in nullable_keys:
|
if v is None and k in nullable_keys:
|
||||||
data[k] = None
|
data[k] = None
|
||||||
elif v is not None:
|
elif v is not None:
|
||||||
|
|||||||
@@ -1,324 +0,0 @@
|
|||||||
"""
|
|
||||||
Strukturierter PDF-Bericht: mehrere Definitionen pro Profil, Katalog, PDF-Erzeugung.
|
|
||||||
|
|
||||||
PDF-Zähler: data_export (wie andere Exporte).
|
|
||||||
"""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import logging
|
|
||||||
from datetime import datetime
|
|
||||||
from uuid import UUID
|
|
||||||
|
|
||||||
from fastapi import APIRouter, Body, Depends, HTTPException
|
|
||||||
from fastapi.responses import Response
|
|
||||||
from pydantic import BaseModel, Field
|
|
||||||
from psycopg2.extras import Json
|
|
||||||
|
|
||||||
from auth import check_feature_access, increment_feature_usage, require_auth
|
|
||||||
from db import get_cursor, get_db
|
|
||||||
from feature_logger import log_feature_usage
|
|
||||||
from report_chart_fetch import CHART_CATALOG_FOR_API
|
|
||||||
from report_pdf_render import build_structured_report_pdf
|
|
||||||
from report_profile_schema import (
|
|
||||||
ALLOWED_VIZ_BUNDLE_IDS,
|
|
||||||
ReportProfilePayload,
|
|
||||||
default_report_profile_dict,
|
|
||||||
parse_report_profile,
|
|
||||||
)
|
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/reports", tags=["reports"])
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
_MAX_REPORT_DEFINITIONS = 20
|
|
||||||
|
|
||||||
|
|
||||||
class CreateReportDefinitionBody(BaseModel):
|
|
||||||
name: str = Field(default="Neuer Bericht", min_length=1, max_length=120)
|
|
||||||
|
|
||||||
|
|
||||||
class UpdateReportDefinitionBody(BaseModel):
|
|
||||||
name: str | None = Field(default=None, min_length=1, max_length=120)
|
|
||||||
payload: dict | None = None
|
|
||||||
|
|
||||||
|
|
||||||
class GeneratePdfRequest(BaseModel):
|
|
||||||
definition_id: UUID | None = None
|
|
||||||
|
|
||||||
|
|
||||||
def _profile_display_name(profile_id: str) -> str:
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute("SELECT name FROM profiles WHERE id = %s", (profile_id,))
|
|
||||||
row = cur.fetchone()
|
|
||||||
if not row:
|
|
||||||
return "Profil"
|
|
||||||
return (row.get("name") or "Profil").strip() or "Profil"
|
|
||||||
|
|
||||||
|
|
||||||
def _row_to_definition(row: dict) -> dict:
|
|
||||||
pl = row.get("payload")
|
|
||||||
if not isinstance(pl, dict):
|
|
||||||
pl = {}
|
|
||||||
return {
|
|
||||||
"id": str(row["id"]),
|
|
||||||
"name": (row.get("name") or "Bericht").strip() or "Bericht",
|
|
||||||
"sort_order": int(row.get("sort_order") or 0),
|
|
||||||
"updated_at": row.get("updated_at").isoformat() if row.get("updated_at") else None,
|
|
||||||
"payload": pl,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/catalog")
|
|
||||||
def get_reports_catalog(session: dict = Depends(require_auth)):
|
|
||||||
"""Metadaten für UI: verfügbare Diagramme, Bundles, Zeitraumgrenzen."""
|
|
||||||
viz_titles = {
|
|
||||||
"body_history_viz": "Körper (Verlauf-Bundle)",
|
|
||||||
"nutrition_history_viz": "Ernährung (Verlauf-Bundle)",
|
|
||||||
"fitness_history_viz": "Fitness (Verlauf-Bundle)",
|
|
||||||
"recovery_history_viz": "Erholung (Verlauf-Bundle)",
|
|
||||||
"history_overview_viz": "Gesamtübersicht (Korrelationen)",
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
"catalog_version": 3,
|
|
||||||
"chart_days": {"min": 7, "max": 90},
|
|
||||||
"charts": CHART_CATALOG_FOR_API,
|
|
||||||
"viz_bundles": [{"id": bid, "title": viz_titles.get(bid, bid)} for bid in sorted(ALLOWED_VIZ_BUNDLE_IDS)],
|
|
||||||
"block_types": [
|
|
||||||
{"id": "section", "title": "Überschrift"},
|
|
||||||
{"id": "viz_bundle", "title": "Verlauf-Bundle (KPIs & Charts)"},
|
|
||||||
{"id": "chart", "title": "Einzel-Diagramm (Legacy)"},
|
|
||||||
{"id": "ai_insight", "title": "KI-Auswertung"},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/definitions")
|
|
||||||
def list_report_definitions(session: dict = Depends(require_auth)):
|
|
||||||
pid = session["profile_id"]
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT id, name, sort_order, updated_at, payload
|
|
||||||
FROM report_definitions
|
|
||||||
WHERE profile_id = %s
|
|
||||||
ORDER BY sort_order ASC, name ASC, updated_at DESC
|
|
||||||
""",
|
|
||||||
(pid,),
|
|
||||||
)
|
|
||||||
rows = cur.fetchall()
|
|
||||||
return {"definitions": [_row_to_definition(dict(r)) for r in rows]}
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/definitions")
|
|
||||||
def create_report_definition(
|
|
||||||
body: CreateReportDefinitionBody | None = Body(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
req = body or CreateReportDefinitionBody()
|
|
||||||
pid = session["profile_id"]
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute(
|
|
||||||
"SELECT COUNT(*) AS n FROM report_definitions WHERE profile_id = %s",
|
|
||||||
(pid,),
|
|
||||||
)
|
|
||||||
n = int((cur.fetchone() or {}).get("n") or 0)
|
|
||||||
if n >= _MAX_REPORT_DEFINITIONS:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=400,
|
|
||||||
detail=f"Maximal {_MAX_REPORT_DEFINITIONS} Berichte erlaubt.",
|
|
||||||
)
|
|
||||||
|
|
||||||
payload_dict = default_report_profile_dict()
|
|
||||||
payload_dict["document_title"] = ""
|
|
||||||
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute(
|
|
||||||
"SELECT COALESCE(MAX(sort_order), -1) + 1 AS next_so FROM report_definitions WHERE profile_id = %s",
|
|
||||||
(pid,),
|
|
||||||
)
|
|
||||||
next_so = int((cur.fetchone() or {}).get("next_so") or 0)
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO report_definitions (profile_id, name, payload, sort_order, updated_at)
|
|
||||||
VALUES (%s, %s, %s, %s, CURRENT_TIMESTAMP)
|
|
||||||
RETURNING id, name, sort_order, updated_at, payload
|
|
||||||
""",
|
|
||||||
(pid, req.name.strip(), Json(payload_dict), next_so),
|
|
||||||
)
|
|
||||||
row = cur.fetchone()
|
|
||||||
conn.commit()
|
|
||||||
|
|
||||||
if not row:
|
|
||||||
raise HTTPException(status_code=500, detail="Bericht konnte nicht angelegt werden.")
|
|
||||||
return {"definition": _row_to_definition(dict(row))}
|
|
||||||
|
|
||||||
|
|
||||||
@router.put("/definitions/{definition_id}")
|
|
||||||
def update_report_definition(
|
|
||||||
definition_id: UUID,
|
|
||||||
body: UpdateReportDefinitionBody,
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
pid = session["profile_id"]
|
|
||||||
name = body.name.strip() if body.name else None
|
|
||||||
parsed_payload: ReportProfilePayload | None = None
|
|
||||||
if body.payload is not None:
|
|
||||||
try:
|
|
||||||
parsed_payload = ReportProfilePayload.model_validate(body.payload)
|
|
||||||
except Exception as e:
|
|
||||||
raise HTTPException(status_code=400, detail=str(e))
|
|
||||||
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
if parsed_payload is not None and name is not None:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
UPDATE report_definitions
|
|
||||||
SET name = %s, payload = %s, updated_at = CURRENT_TIMESTAMP
|
|
||||||
WHERE id = %s AND profile_id = %s
|
|
||||||
RETURNING id, name, sort_order, updated_at, payload
|
|
||||||
""",
|
|
||||||
(name, Json(parsed_payload.to_stored_dict()), str(definition_id), pid),
|
|
||||||
)
|
|
||||||
elif parsed_payload is not None:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
UPDATE report_definitions
|
|
||||||
SET payload = %s, updated_at = CURRENT_TIMESTAMP
|
|
||||||
WHERE id = %s AND profile_id = %s
|
|
||||||
RETURNING id, name, sort_order, updated_at, payload
|
|
||||||
""",
|
|
||||||
(Json(parsed_payload.to_stored_dict()), str(definition_id), pid),
|
|
||||||
)
|
|
||||||
elif name is not None:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
UPDATE report_definitions
|
|
||||||
SET name = %s, updated_at = CURRENT_TIMESTAMP
|
|
||||||
WHERE id = %s AND profile_id = %s
|
|
||||||
RETURNING id, name, sort_order, updated_at, payload
|
|
||||||
""",
|
|
||||||
(name, str(definition_id), pid),
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
raise HTTPException(status_code=400, detail="Nichts zu aktualisieren (name oder payload fehlt).")
|
|
||||||
|
|
||||||
row = cur.fetchone()
|
|
||||||
conn.commit()
|
|
||||||
|
|
||||||
if not row:
|
|
||||||
raise HTTPException(status_code=404, detail="Bericht nicht gefunden.")
|
|
||||||
return {"definition": _row_to_definition(dict(row))}
|
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/definitions/{definition_id}")
|
|
||||||
def delete_report_definition(definition_id: UUID, session: dict = Depends(require_auth)):
|
|
||||||
pid = session["profile_id"]
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute(
|
|
||||||
"DELETE FROM report_definitions WHERE id = %s AND profile_id = %s RETURNING id",
|
|
||||||
(str(definition_id), pid),
|
|
||||||
)
|
|
||||||
deleted = cur.fetchone()
|
|
||||||
conn.commit()
|
|
||||||
if not deleted:
|
|
||||||
raise HTTPException(status_code=404, detail="Bericht nicht gefunden.")
|
|
||||||
return {"ok": True}
|
|
||||||
|
|
||||||
|
|
||||||
def _fetch_definition_payload(profile_id: str, definition_id: UUID | None) -> tuple[dict, str]:
|
|
||||||
"""Returns (raw_payload_dict, report_label_for_filename)."""
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
if definition_id is not None:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT payload, name FROM report_definitions
|
|
||||||
WHERE id = %s AND profile_id = %s
|
|
||||||
""",
|
|
||||||
(str(definition_id), profile_id),
|
|
||||||
)
|
|
||||||
row = cur.fetchone()
|
|
||||||
if not row:
|
|
||||||
raise HTTPException(status_code=404, detail="Bericht nicht gefunden.")
|
|
||||||
pl = row.get("payload")
|
|
||||||
label = (row.get("name") or "Bericht").strip() or "Bericht"
|
|
||||||
if not isinstance(pl, dict):
|
|
||||||
raise HTTPException(status_code=400, detail="Ungültige Berichtsdaten.")
|
|
||||||
return pl, label
|
|
||||||
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
SELECT payload, name FROM report_definitions
|
|
||||||
WHERE profile_id = %s
|
|
||||||
ORDER BY sort_order ASC, name ASC, updated_at DESC
|
|
||||||
LIMIT 1
|
|
||||||
""",
|
|
||||||
(profile_id,),
|
|
||||||
)
|
|
||||||
row = cur.fetchone()
|
|
||||||
if not row:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=400,
|
|
||||||
detail="Kein Bericht angelegt — bitte unter Einstellungen › PDF-Berichte einen Bericht erstellen.",
|
|
||||||
)
|
|
||||||
pl = row.get("payload")
|
|
||||||
label = (row.get("name") or "Bericht").strip() or "Bericht"
|
|
||||||
if not isinstance(pl, dict):
|
|
||||||
raise HTTPException(status_code=400, detail="Ungültige Berichtsdaten.")
|
|
||||||
return pl, label
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/generate-pdf")
|
|
||||||
def generate_structured_report_pdf(
|
|
||||||
body: GeneratePdfRequest | None = Body(default=None),
|
|
||||||
session: dict = Depends(require_auth),
|
|
||||||
):
|
|
||||||
pid = session["profile_id"]
|
|
||||||
req = body or GeneratePdfRequest()
|
|
||||||
|
|
||||||
access = check_feature_access(pid, "data_export")
|
|
||||||
log_feature_usage(pid, "data_export", access, "report_generate_pdf")
|
|
||||||
if not access["allowed"]:
|
|
||||||
logger.warning(
|
|
||||||
"[FEATURE-LIMIT] report pdf blocked: %s used=%s limit=%s",
|
|
||||||
pid,
|
|
||||||
access.get("used"),
|
|
||||||
access.get("limit"),
|
|
||||||
)
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=403,
|
|
||||||
detail=(
|
|
||||||
"Limit erreicht: Daten-Export nicht möglich "
|
|
||||||
f"({access.get('used')}/{access.get('limit')})."
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
raw, report_label = _fetch_definition_payload(pid, req.definition_id)
|
|
||||||
try:
|
|
||||||
payload = parse_report_profile(raw)
|
|
||||||
except Exception as e:
|
|
||||||
raise HTTPException(status_code=400, detail=f"Berichtsprofil ungültig: {e}")
|
|
||||||
|
|
||||||
profile_name = _profile_display_name(pid)
|
|
||||||
try:
|
|
||||||
pdf_bytes = build_structured_report_pdf(profile_id=pid, profile_name=profile_name, payload=payload)
|
|
||||||
except Exception as e:
|
|
||||||
logger.exception("report pdf build failed")
|
|
||||||
raise HTTPException(status_code=500, detail=f"PDF-Erzeugung fehlgeschlagen: {e}")
|
|
||||||
|
|
||||||
increment_feature_usage(pid, "data_export")
|
|
||||||
|
|
||||||
doc_title = (payload.document_title or "").strip()
|
|
||||||
base_label = doc_title or report_label
|
|
||||||
safe_name = "".join(c for c in base_label if c.isalnum() or c in (" ", "-", "_")).strip() or "bericht"
|
|
||||||
fn = f"mitai-bericht-{safe_name.replace(' ', '-')}-{datetime.now().strftime('%Y-%m-%d')}.pdf"
|
|
||||||
return Response(
|
|
||||||
content=pdf_bytes,
|
|
||||||
media_type="application/pdf",
|
|
||||||
headers={"Content-Disposition": f'attachment; filename="{fn}"'},
|
|
||||||
)
|
|
||||||
@@ -1,76 +0,0 @@
|
|||||||
"""Per-user Tandoor connection. No recipe import yet."""
|
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, HTTPException
|
|
||||||
from pydantic import BaseModel, Field
|
|
||||||
from starlette.requests import Request
|
|
||||||
from slowapi import Limiter
|
|
||||||
from slowapi.util import get_remote_address
|
|
||||||
|
|
||||||
from auth import require_auth
|
|
||||||
from data_layer.tandoor_client import TandoorError, normalize_base_url, probe_connection
|
|
||||||
from data_layer.tandoor_settings import (
|
|
||||||
get_public_settings,
|
|
||||||
get_settings,
|
|
||||||
record_probe,
|
|
||||||
upsert_settings,
|
|
||||||
)
|
|
||||||
from db import get_cursor, get_db
|
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/tandoor", tags=["tandoor"])
|
|
||||||
limiter = Limiter(key_func=get_remote_address)
|
|
||||||
|
|
||||||
|
|
||||||
class TandoorSettingsIn(BaseModel):
|
|
||||||
base_url: str = ""
|
|
||||||
token: Optional[str] = None
|
|
||||||
clear_token: bool = False
|
|
||||||
|
|
||||||
|
|
||||||
class TandoorProbeIn(BaseModel):
|
|
||||||
base_url: Optional[str] = None
|
|
||||||
token: Optional[str] = Field(default=None, description="Nur zum Testen; leer = gespeichertes Token")
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/settings")
|
|
||||||
def read_settings(session: dict = Depends(require_auth)):
|
|
||||||
pid = session["profile_id"]
|
|
||||||
with get_db() as conn:
|
|
||||||
return get_public_settings(get_cursor(conn), pid)
|
|
||||||
|
|
||||||
|
|
||||||
@router.put("/settings")
|
|
||||||
def write_settings(body: TandoorSettingsIn, session: dict = Depends(require_auth)):
|
|
||||||
pid = session["profile_id"]
|
|
||||||
try:
|
|
||||||
if body.clear_token:
|
|
||||||
url = ""
|
|
||||||
else:
|
|
||||||
url = normalize_base_url(body.base_url)
|
|
||||||
with get_db() as conn:
|
|
||||||
return upsert_settings(
|
|
||||||
get_cursor(conn), pid, url, body.token, clear_token=body.clear_token
|
|
||||||
)
|
|
||||||
except (TandoorError, ValueError) as e:
|
|
||||||
raise HTTPException(400, str(e)) from e
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/test")
|
|
||||||
@limiter.limit("10/minute")
|
|
||||||
def test_connection(request: Request, body: Optional[TandoorProbeIn] = None, session: dict = Depends(require_auth)):
|
|
||||||
pid = session["profile_id"]
|
|
||||||
body = body or TandoorProbeIn()
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
stored = get_settings(cur, pid)
|
|
||||||
url = (body.base_url or "").strip() or ((stored or {}).get("base_url") or "")
|
|
||||||
token = (body.token or "").strip() or ((stored or {}).get("api_token") or "")
|
|
||||||
try:
|
|
||||||
result = probe_connection(url, token)
|
|
||||||
if stored:
|
|
||||||
record_probe(cur, pid, True)
|
|
||||||
return result
|
|
||||||
except TandoorError as e:
|
|
||||||
if stored:
|
|
||||||
record_probe(cur, pid, False, str(e))
|
|
||||||
raise HTTPException(400, str(e)) from e
|
|
||||||
@@ -1,88 +0,0 @@
|
|||||||
"""Audit: INSERT %s-Platzhalter vs. Parameter-Anzahl in backend/."""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import ast
|
|
||||||
import re
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
ROOT = Path(__file__).resolve().parents[1]
|
|
||||||
|
|
||||||
|
|
||||||
def _count_tuple_elements(node: ast.AST) -> int | None:
|
|
||||||
if isinstance(node, ast.Tuple):
|
|
||||||
return len(node.elts)
|
|
||||||
if isinstance(node, ast.List):
|
|
||||||
return len(node.elts)
|
|
||||||
if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute):
|
|
||||||
if node.func.attr in {"values", "keys"}:
|
|
||||||
return None
|
|
||||||
if isinstance(node, ast.BinOp) and isinstance(node.op, ast.Add):
|
|
||||||
left = _count_tuple_elements(node.left)
|
|
||||||
right = _count_tuple_elements(node.right)
|
|
||||||
if left is not None and right is not None:
|
|
||||||
return left + right
|
|
||||||
if isinstance(node, ast.ListComp):
|
|
||||||
return None
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def audit_file(path: Path) -> list[str]:
|
|
||||||
issues: list[str] = []
|
|
||||||
try:
|
|
||||||
tree = ast.parse(path.read_text(encoding="utf-8"), filename=str(path))
|
|
||||||
except SyntaxError:
|
|
||||||
return issues
|
|
||||||
|
|
||||||
for node in ast.walk(tree):
|
|
||||||
if not isinstance(node, ast.Call):
|
|
||||||
continue
|
|
||||||
func = node.func
|
|
||||||
if not (
|
|
||||||
isinstance(func, ast.Attribute)
|
|
||||||
and func.attr == "execute"
|
|
||||||
and isinstance(func.value, ast.Name)
|
|
||||||
and func.value.id == "cur"
|
|
||||||
):
|
|
||||||
continue
|
|
||||||
if len(node.args) < 2:
|
|
||||||
continue
|
|
||||||
sql_node, params_node = node.args[0], node.args[1]
|
|
||||||
sql = None
|
|
||||||
if isinstance(sql_node, ast.Constant) and isinstance(sql_node.value, str):
|
|
||||||
sql = sql_node.value
|
|
||||||
elif isinstance(sql_node, ast.JoinedStr):
|
|
||||||
continue
|
|
||||||
if not sql or "INSERT" not in sql.upper():
|
|
||||||
continue
|
|
||||||
if "VALUES" not in sql.upper():
|
|
||||||
continue
|
|
||||||
ph = sql.count("%s")
|
|
||||||
param_count = _count_tuple_elements(params_node)
|
|
||||||
if param_count is None:
|
|
||||||
continue
|
|
||||||
if ph != param_count:
|
|
||||||
rel = path.relative_to(ROOT)
|
|
||||||
issues.append(
|
|
||||||
f"{rel}:{node.lineno} INSERT placeholders={ph} params={param_count}"
|
|
||||||
)
|
|
||||||
return issues
|
|
||||||
|
|
||||||
|
|
||||||
def main() -> int:
|
|
||||||
issues: list[str] = []
|
|
||||||
for path in ROOT.rglob("*.py"):
|
|
||||||
if "venv" in path.parts or "__pycache__" in path.parts:
|
|
||||||
continue
|
|
||||||
issues.extend(audit_file(path))
|
|
||||||
if issues:
|
|
||||||
print("MISMATCHES:")
|
|
||||||
for issue in sorted(issues):
|
|
||||||
print(issue)
|
|
||||||
return 1
|
|
||||||
print("OK: keine INSERT-Platzhalter-Mismatches gefunden (statisch prüfbar).")
|
|
||||||
return 0
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
raise SystemExit(main())
|
|
||||||
@@ -1,110 +0,0 @@
|
|||||||
"""Feature-Enforcement für Legacy Activity CSV-Import (#37)."""
|
|
||||||
|
|
||||||
import asyncio
|
|
||||||
from unittest.mock import AsyncMock, patch
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
from fastapi import HTTPException
|
|
||||||
|
|
||||||
from routers.activity import import_activity_csv
|
|
||||||
|
|
||||||
|
|
||||||
def test_import_activity_csv_blocks_when_limit_reached():
|
|
||||||
upload = AsyncMock()
|
|
||||||
upload.read = AsyncMock(return_value=b"Workout Type,Start\n")
|
|
||||||
|
|
||||||
with patch("routers.activity.get_pid", return_value="profile-1"), patch(
|
|
||||||
"routers.activity.check_feature_access",
|
|
||||||
return_value={
|
|
||||||
"allowed": False,
|
|
||||||
"reason": "limit_exceeded",
|
|
||||||
"used": 30,
|
|
||||||
"limit": 30,
|
|
||||||
},
|
|
||||||
), patch("routers.activity.log_feature_usage"):
|
|
||||||
with pytest.raises(HTTPException) as exc:
|
|
||||||
asyncio.run(
|
|
||||||
import_activity_csv(
|
|
||||||
file=upload,
|
|
||||||
x_profile_id=None,
|
|
||||||
session={"profile_id": "profile-1"},
|
|
||||||
)
|
|
||||||
)
|
|
||||||
assert exc.value.status_code == 403
|
|
||||||
|
|
||||||
|
|
||||||
def test_import_activity_csv_increments_usage_per_insert(monkeypatch):
|
|
||||||
upload = AsyncMock()
|
|
||||||
upload.read = AsyncMock(
|
|
||||||
return_value=(
|
|
||||||
"Workout Type,Start,Duration,Aktive Energie (kJ),Ruheeinträge (kJ),"
|
|
||||||
"Durchschn. Herzfrequenz (count/min),Max. Herzfrequenz (count/min),Distanz (km),End\n"
|
|
||||||
"Running,2026-07-22 10:00:00 +0200,0:45:00,1000,500,140,160,5.0,\n"
|
|
||||||
).encode("utf-8")
|
|
||||||
)
|
|
||||||
|
|
||||||
increments = []
|
|
||||||
|
|
||||||
class FakeCursor:
|
|
||||||
def execute(self, *args, **kwargs):
|
|
||||||
return None
|
|
||||||
|
|
||||||
def fetchone(self):
|
|
||||||
return None
|
|
||||||
|
|
||||||
class FakeConn:
|
|
||||||
def __enter__(self):
|
|
||||||
return self
|
|
||||||
|
|
||||||
def __exit__(self, *args):
|
|
||||||
return False
|
|
||||||
|
|
||||||
fake_cur = FakeCursor()
|
|
||||||
|
|
||||||
monkeypatch.setattr("routers.activity.get_pid", lambda _h: "profile-1")
|
|
||||||
monkeypatch.setattr(
|
|
||||||
"routers.activity.check_feature_access",
|
|
||||||
lambda *_a, **_k: {"allowed": True, "reason": "ok", "used": 0, "limit": 100},
|
|
||||||
)
|
|
||||||
monkeypatch.setattr("routers.activity.log_feature_usage", lambda *_a, **_k: None)
|
|
||||||
monkeypatch.setattr("routers.activity.get_db", lambda: FakeConn())
|
|
||||||
monkeypatch.setattr("routers.activity.get_cursor", lambda _c: fake_cur)
|
|
||||||
monkeypatch.setattr(
|
|
||||||
"routers.activity.normalize_activity_start",
|
|
||||||
lambda _s: ("2026-07-22", "10:00:00"),
|
|
||||||
)
|
|
||||||
monkeypatch.setattr(
|
|
||||||
"routers.activity.get_training_type_for_activity",
|
|
||||||
lambda *_a, **_k: (1, "cardio", None),
|
|
||||||
)
|
|
||||||
monkeypatch.setattr(
|
|
||||||
"routers.activity.find_activity_duplicate_id",
|
|
||||||
lambda *_a, **_k: None,
|
|
||||||
)
|
|
||||||
monkeypatch.setattr(
|
|
||||||
"routers.activity.new_activity_id",
|
|
||||||
lambda: "new-id",
|
|
||||||
)
|
|
||||||
monkeypatch.setattr(
|
|
||||||
"routers.activity.insert_activity_csv_minimal",
|
|
||||||
lambda *_a, **_k: None,
|
|
||||||
)
|
|
||||||
monkeypatch.setattr(
|
|
||||||
"routers.activity.run_activity_post_write_hooks_import",
|
|
||||||
lambda *_a, **_k: None,
|
|
||||||
)
|
|
||||||
monkeypatch.setattr(
|
|
||||||
"routers.activity.increment_feature_usage",
|
|
||||||
lambda _pid, feature: increments.append(feature),
|
|
||||||
)
|
|
||||||
|
|
||||||
result = asyncio.run(
|
|
||||||
import_activity_csv(
|
|
||||||
file=upload,
|
|
||||||
x_profile_id=None,
|
|
||||||
session={"profile_id": "profile-1"},
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
assert result["inserted"] == 1
|
|
||||||
assert increments == ["activity_entries"]
|
|
||||||
Loaded 100 of 203 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user