Compare commits
95
Commits
V1.0
...
a5aad0da7e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a5aad0da7e | ||
|
|
ce5b96f373 | ||
|
|
11fac3d123 | ||
|
|
f0ad900565 | ||
|
|
36478863a2 | ||
|
|
ec667a75b6 | ||
|
|
f864f9894d | ||
|
|
73104a1a4c | ||
|
|
d66e68a5df | ||
|
|
d2b4f74cd2 | ||
|
|
1a826973a9 | ||
|
|
d13e7cda26 | ||
|
|
ec85d5f5f6 | ||
|
|
1139b00743 | ||
|
|
e9712cef23 | ||
|
|
1220ee54fb | ||
|
|
92e334dcd2 | ||
|
|
bc8e9fb7fa | ||
|
|
c3be745efa | ||
|
|
680ecd1c06 | ||
|
|
38797d687d | ||
|
|
fa3e66fb31 | ||
|
|
cc0f57758a | ||
|
|
2a6c437a08 | ||
|
|
c9d71c0179 | ||
|
|
9d5e16455c | ||
|
|
2a26e4fecf | ||
|
|
94bb4a8199 | ||
|
|
fd7a2dac6d | ||
|
|
8d0a6dd487 | ||
|
|
06f83e2ffc | ||
|
|
026c51b6b5 | ||
|
|
7d6fdab812 | ||
|
|
5cda485458 | ||
|
|
cd29c7d433 | ||
|
|
ca8cee990b | ||
|
|
58ddde6b1e | ||
|
|
08eae86ddc | ||
|
|
9d47c4ef84 | ||
|
|
e4e8c70cd2 | ||
|
|
c570e67a09 | ||
|
|
574af61349 | ||
|
|
934b915357 | ||
|
|
c6e8371d5a | ||
|
|
f718785145 | ||
|
|
9fdb02ff8b | ||
|
|
1f51c32521 | ||
|
|
766b64cd64 | ||
|
|
3296dfca28 | ||
|
|
db9952525a | ||
|
|
196b6c5cf1 | ||
|
|
cf7379b2f6 | ||
|
|
48508c164e | ||
|
|
1b01f5e6d0 | ||
|
|
df8e732709 | ||
|
|
d5325acee6 | ||
|
|
b7062d32bf | ||
|
|
5fa2ea2e6b | ||
|
|
f97d15288d | ||
|
|
736dc58d81 | ||
|
|
0a27533262 | ||
|
|
7388776b29 | ||
|
|
a515a5d563 | ||
|
|
12d4d7c63b | ||
|
|
3664f53c51 | ||
|
|
fb2e0803c0 | ||
|
|
bb01283727 | ||
|
|
bc60b9f5c9 | ||
|
|
fbeabcde97 | ||
|
|
ba474b0a57 | ||
|
|
790e6df8ef | ||
|
|
057df0afc8 | ||
|
|
ba04e0c0b6 | ||
|
|
f5ce1ec941 | ||
|
|
2deb6510f8 | ||
|
|
0eac40abf6 | ||
|
|
e915d3fb13 | ||
|
|
60f6cf3c6d | ||
|
|
e09cbc112e | ||
|
|
f6b3182a80 | ||
|
|
cb3aa48999 | ||
|
|
77f1ed14c5 | ||
|
|
08c9cccdcc | ||
|
|
4b6e1bed11 | ||
|
|
90a27846ca | ||
|
|
d7cefdd9e9 | ||
|
|
4868e44882 | ||
|
|
a9a414b956 | ||
|
|
baeddd7c13 | ||
|
|
41bf593d4c | ||
|
|
04e23d8115 | ||
|
|
052ba195cc | ||
|
|
2ea5f905c4 | ||
|
|
e9e094c6a4 | ||
|
|
61a5bb39ae |
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
# Gitea Issues – Landkarte (Auswertung)
|
# Gitea Issues – Landkarte (Auswertung)
|
||||||
|
|
||||||
**Quelle:** Gitea `Lars/mitai-jinkendo`, Stand **2026-04-09** (Abfrage `state=all`, ergänzt: #71).
|
**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.
|
||||||
@@ -88,6 +88,7 @@ Dieses Dokument ist ein **Orientierungs-Index** für Agenten und Entwickler. Ver
|
|||||||
| # | Titel |
|
| # | Titel |
|
||||||
|---|--------|
|
|---|--------|
|
||||||
| 15 | [FEAT-002] Quality-Filter für KI-Auswertungen & Charts integrieren |
|
| 15 | [FEAT-002] Quality-Filter für KI-Auswertungen & Charts integrieren |
|
||||||
|
| 76 | Trainings-Qualität: zielbezogene Logik + Listen-Filter statt globalem „Hochwertig“-Hide |
|
||||||
| 36 | BUG-009: Trainingstyp-Erstellung führt zu Internal Server Error |
|
| 36 | BUG-009: Trainingstyp-Erstellung führt zu Internal Server Error |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ _Dieser Ordner `.claude/docs/` ist per `.gitignore`-Ausnahme **versioniert** (Sp
|
|||||||
| Dashboard-Lab-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` |
|
||||||
|
| 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 |
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -113,6 +114,12 @@ _Dieser Ordner `.claude/docs/` ist per `.gitignore`-Ausnahme **versioniert** (Sp
|
|||||||
| `TRAINING_PROFILE_RESOLVER_LAYER1.md` | Training-Resolver Schicht 1 |
|
| `TRAINING_PROFILE_RESOLVER_LAYER1.md` | Training-Resolver Schicht 1 |
|
||||||
| `TRAINING_TYPE_PROFILES_TECHNICAL.md` | Trainingsprofile technisch |
|
| `TRAINING_TYPE_PROFILES_TECHNICAL.md` | Trainingsprofile technisch |
|
||||||
| `UNIVERSAL_CSV_IMPORT_AGENT_GUIDE.md` | Universal CSV: Registry, Executor, Vorlagen, Agent-Checkliste |
|
| `UNIVERSAL_CSV_IMPORT_AGENT_GUIDE.md` | Universal CSV: Registry, Executor, Vorlagen, Agent-Checkliste |
|
||||||
|
| `ACTIVITY_SESSION_METRICS_EAV_AGENT_GUIDE.md` | Session-Metriken EAV, Attributprofile, Layer-1, Prod-Migration |
|
||||||
|
| `ACTIVITY_COMPOSITE_METRICS_IMPLEMENTATION_CONCEPT.md` | Composite-Metriken in EAV (JSONB), Archetypen, CSV-Slots, Layer-1-Expand, Migration/Test-Checkliste |
|
||||||
|
| `ACTIVITY_PRODUCTION_ARCHITECTURE_AND_PHASES.md` | **Zielarchitektur** Aktivität (Spine/EAV/Composites/Import/Layer 1–2) + **Phasenplan A–F** Produktionsreife |
|
||||||
|
| `ACTIVITY_LAYER2A_PLACEHOLDER_AUDIT.md` | Issue #53: Aktivitäts-Platzhalter Layer 1 ↔ 2a (Audit Schritt 1) |
|
||||||
|
| `ACTIVITY_SCALAR_KANON_TABLE.md` | **Skalar-Kanon** Aktivität (eine Semantik → eine Quelle); Phase A |
|
||||||
|
| *(Code)* `backend/data_layer/activity_data_canon.py` | **Kanon** activity CSV-Modul vs. EAV-primär; Legacy-Lesefallback |
|
||||||
| `V9D_PHASE2_VITALS_SLEEP.md` | v9d Vitalwerte/Schlaf (Release-Bezug) |
|
| `V9D_PHASE2_VITALS_SLEEP.md` | v9d Vitalwerte/Schlaf (Release-Bezug) |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|||||||
@@ -0,0 +1,317 @@
|
|||||||
|
# Activity Session Metrics: Composite-Daten (EAV) – Umsetzungskonzept
|
||||||
|
|
||||||
|
**Stand:** 2026-04-16
|
||||||
|
**Status:** Normatives Konzept zur nahtlosen Weiterarbeit durch Code-Agenten
|
||||||
|
**Bezieht sich auf:** `ACTIVITY_PRODUCTION_ARCHITECTURE_AND_PHASES.md` (§2.3–2.4, Phasen D–E), `ACTIVITY_SESSION_METRICS_EAV_AGENT_GUIDE.md`, Issue #53 (Layer-1-Prinzip: Auswertungen nur über `data_layer`)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. Ziel und Abgrenzung
|
||||||
|
|
||||||
|
### 1.1 Ziel
|
||||||
|
|
||||||
|
- **Composite-Messgrößen** (strukturierte Werte mit mehreren benannten Slots) werden wie **normale Trainingsparameter** im Katalog geführt, **Kategorie-/Typ-Profilen** zugeordnet und pro Session in der **EAV-Tabelle** persistiert.
|
||||||
|
- **Persistenz:** ein JSON-Dokument pro Session und `training_parameter_id` (kanonisch **JSONB**), kompatibel mit der bestehenden „eine Zeile pro Parameter“-Semantik.
|
||||||
|
- **Import:** CSV liefert typischerweise **eine Spalte pro atomarem Slot**; das Mapping verweist auf **`(Parameter-Key, Slot-Key)`** (stabile Strings, nicht Spaltenreihenfolge).
|
||||||
|
- **Layer 1:** liefert für Consumer weiterhin **eine konsistente API**: Rohdokument **und** optional **aufgelöste Einzelwerte** (flach oder namenspaced), ohne dass Charts/Platzhalter direkt JSON parsen müssen.
|
||||||
|
|
||||||
|
### 1.2 Nicht-Ziele (explizit)
|
||||||
|
|
||||||
|
- Kein „freies“ JSON-Schema im Admin ohne Archetyp-Bindung (verhindert Datenmüll und nicht validierbare Dokumente).
|
||||||
|
- Keine Abschwächung bestehender **Skalar-Parameter** (`integer`, `float`, `string`, `boolean`): alle bisherigen Pfade bleiben gültig.
|
||||||
|
- Kein Ersatz für `activity_log`-**Spine** oder Session-Qualitätsblobs (`evaluation`, …).
|
||||||
|
|
||||||
|
### 1.3 Kompatibilitätsgarantie („keine Regression“)
|
||||||
|
|
||||||
|
| Bereich | Maßnahme |
|
||||||
|
|---------|----------|
|
||||||
|
| DB | Nur **additive** Migrationen; bestehende `CHECK`-Regeln für Skalare bleiben für Zeilen **ohne** Composite erhalten bzw. werden zu einer **Oder-Verknüpfung** erweitert (siehe §4). |
|
||||||
|
| `training_parameters` | Neuer `data_type`-Wert **`composite`** zusätzlich zu den vier bestehenden; bestehende CHECK-Constraint muss erweitert werden (Migration). |
|
||||||
|
| `activity_session_metrics` | Skalare Zeilen unverändert; Composite-Zeilen nutzen **`value_json`** (neu), alle `value_*` NULL. |
|
||||||
|
| Layer 1 | `resolve_activity_attribute_schema`, Merge, Replace: Composite erscheint als **ein** Schema-Eintrag; Lese-/Schreibpfade erweitern, nicht ersetzen. |
|
||||||
|
| CSV | Bestehende Map-Ziele auf Skalare/Registry unverändert; neue Zielnotation nur für Composites. |
|
||||||
|
| Admin | tcp/ttp-UI: gleiche Zuordnung wie heute; Zusatzfelder nur bei `data_type === composite`. |
|
||||||
|
|
||||||
|
### 1.4 Abgleich mit `functional_concept_composite_data.md` (fachliches Konzept)
|
||||||
|
|
||||||
|
Das **fachliche Konzeptpapier** (Composite Scalar/Layer-Trennung) und dieses **Umsetzungskonzept** sind **vereinbar**, wenn die Rollen klar getrennt bleiben:
|
||||||
|
|
||||||
|
| Thema | Fachliches Konzept (`functional_concept_composite_data.md`) | Dieses Umsetzungskonzept (technisch) |
|
||||||
|
|--------|-------------------------------------------------------------|--------------------------------------|
|
||||||
|
| **Speicher in der DB** | Einheitlicher Store; Composite = `jsonb` mit **kleinem Basisschema** (`v`, `kind`, `domain`, `items`, optional `basis`, `meta`) | `activity_session_metrics.value_json`; CHECK Skalar vs. Composite |
|
||||||
|
| **Technische Container** | Genau **vier** `kind`-Werte: `group_set`, `distribution_set`, `sequence_set`, `model_set` | Layer-1-Validierung **muss** diese Hülle durchsetzen; kein freies JSON ohne `kind`/`v`/`items` |
|
||||||
|
| **„Archetypen“** | **Fachliche** Ausprägungen werden in **Layer 2a** aus L1-Objekten abgeleitet | Benannte **Preset-/Validierungsprofile** im Code (z. B. Zonenverteilung HF) sind **kein** zweites Persistenz-Schema: sie legen fest, *welches* der vier `kind`-Muster, *welches* `domain`, *welche* Item-Keys/Typen erlaubt sind — inkl. CSV-Slot-Mapping |
|
||||||
|
| **Layer 1** | Validiert, minimal normalisiert, **keine** Scores/Bewertungen/KI-Texte | Validator + Merge + optional `expand_*` (**technische** Flachstellung für Consumer, z. B. `param.slot` → Skalar) |
|
||||||
|
| **Layer 2** | Diagramme, Kennzahlen, KI-Platzhalter-**Formulierung** | unverändert; konsumiert L1 (und ggf. L2a) |
|
||||||
|
|
||||||
|
**Konsequenz für die Registry:** Statt „8 freie JSON-Archetypen“ implementiert die Code-Registry **Validierungs-Presets**, die alle auf die **vier technischen `kind`-Formen** abbilden. Die Tabelle in §3 beschreibt weiterhin **fachlich benannte MVP-Anker** — technisch übersetzen sie sich in `(kind, domain, Item-Regeln, v)`.
|
||||||
|
|
||||||
|
**Konsequenz für Platzhalter:** Roh-JSON aus der DB **nicht** ungefiltert in Prompts; L2b nutzt L1/L2a-Aufbereitung (wie im fachlichen Konzept).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. Begriffe
|
||||||
|
|
||||||
|
| Begriff | Bedeutung |
|
||||||
|
|---------|-----------|
|
||||||
|
| **Archetyp** | Im **Repo versionierte** Strukturvorlage (erlaubte Slots, Typen, Pflichtfelder, Validator, Version). **7–8** Stück geplant; Erweiterung nur per Code-Release. |
|
||||||
|
| **Slot** | Benanntes Teilfeld innerhalb des Composite-Dokuments, z. B. `z1_sec`, `z2_sec`, `avg_cadence`. |
|
||||||
|
| **Parameter-Instanz** | Eine Zeile in `training_parameters` mit `data_type = composite` und Metadaten, **welcher** Archetyp gilt (siehe §5). |
|
||||||
|
| **Dokument** | Ein JSON-Objekt, das alle Slots abbildet; gespeichert in `activity_session_metrics.value_json`. |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. Archetypen-Katalog (Planungsstand) — fachliche Namen → technische `kind`-Presets
|
||||||
|
|
||||||
|
Die **konkrete** Slot-Liste und Validierung wird im Code als **Registry** geführt (z. B. `backend/data_layer/activity_composite_archetypes.py`). Jedes Preset **mappt** auf genau eines von **`group_set` | `distribution_set` | `sequence_set` | `model_set`** und erfüllt das **Basisschema** aus `functional_concept_composite_data.md` §7.
|
||||||
|
|
||||||
|
Inhaltlich orientiert an `ACTIVITY_PRODUCTION_ARCHITECTURE_AND_PHASES.md` §2.4.
|
||||||
|
|
||||||
|
**Beispielhafte fachliche MVP-Anker** (8 Kandidaten; im Code als Preset-Key + `kind`/`domain` abbilden):
|
||||||
|
|
||||||
|
| `archetype_key` (stabil) | Kurzbeschreibung | Typische Slots (Beispiel) |
|
||||||
|
|--------------------------|------------------|---------------------------|
|
||||||
|
| `hr_zone_distribution` | Zeit-/Anteil je HF-Zone | `z1_sec`…`z5_sec` oder `zones[]` |
|
||||||
|
| `power_zone_distribution` | Leistungszonen | analog |
|
||||||
|
| `pace_band_profile` | Pace-Bänder / Histogramm | bucket-Struktur |
|
||||||
|
| `interval_block_summary` | Intervallblöcke aggregiert | `blocks[]` mit Dauer, Ziel, Ist |
|
||||||
|
| `event_marker_sequence` | Ereignisse mit Zeitstempel | `events[]` |
|
||||||
|
| `coupling_efficiency_profile` | Kopplungs-/Effizienzmetriken | sportabhängig |
|
||||||
|
| `model_parameter_profile` | Modell-/Schwellenparameter | key-value-ähnlich, validiert |
|
||||||
|
| `readiness_recovery_snapshot` | optional: kurzes Multi-Signal-Bundle | nur wenn fachlich gewünscht |
|
||||||
|
|
||||||
|
**Regel:** Jeder Archetyp hat `version` (Integer). Validator lehnt Dokumente mit falscher/fehlender Version ab oder migriert definiert (nur wenn spezifiziert).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. Datenmodell-Erweiterungen
|
||||||
|
|
||||||
|
### 4.1 `training_parameters`
|
||||||
|
|
||||||
|
**Migration (additiv):**
|
||||||
|
|
||||||
|
1. `CHECK (data_type IN (...))` erweitern um **`composite`**.
|
||||||
|
2. Optional eigene Spalte **`composite_archetype_key` `VARCHAR(64)`** (NOT NULL wenn `data_type = composite`, sonst NULL) — **oder** ausschließlich in `validation_rules` speichern (siehe unten).
|
||||||
|
**Empfehlung:** Spalte `composite_archetype_key` + `composite_archetype_version INT` für einfache Admin-Queries und klare Semantik; `validation_rules` für archetyp-spezifische Feinheiten (z. B. erlaubte Zonenanzahl).
|
||||||
|
|
||||||
|
**Konsistenz-Constraint (DB oder App):**
|
||||||
|
|
||||||
|
- Wenn `data_type = composite`: `composite_archetype_key` gesetzt, `source_field` typischerweise **NULL** (kein `activity_log`-Skalar-Shadowing).
|
||||||
|
- `unit` am Parameter: optional für „Anzeige-Einheit“ des Gesamtwerts oder leer; Slots haben Einheiten im Archetyp oder in Slot-Metadaten.
|
||||||
|
|
||||||
|
### 4.2 `activity_session_metrics`
|
||||||
|
|
||||||
|
**Migration (additiv):**
|
||||||
|
|
||||||
|
```text
|
||||||
|
value_json JSONB NULL
|
||||||
|
```
|
||||||
|
|
||||||
|
**CHECK-Constraint ersetzen/erweitern** (Konzept):
|
||||||
|
|
||||||
|
- **Modus Skalar:** genau eine der Spalten `value_num`, `value_int`, `value_text`, `value_bool` ist NOT NULL; `value_json` IS NULL.
|
||||||
|
- **Modus Composite:** `value_json` IS NOT NULL; alle vier Skalar-Spalten IS NULL.
|
||||||
|
|
||||||
|
Damit bleibt die bestehende Semantik „eine Zeile = ein Parameter“ erhalten.
|
||||||
|
|
||||||
|
**Kommentar:** Tabelle trägt weiterhin „EAV“; Composites sind **keine** zusätzlichen Zeilen pro Slot.
|
||||||
|
|
||||||
|
### 4.3 Profil-Zuordnung (tcp / ttp)
|
||||||
|
|
||||||
|
**Keine** Tabellenänderung: `training_category_parameter` und `training_type_parameter` verweisen weiter nur auf `training_parameter_id`. Composite-Parameter verhalten sich wie Skalare in Bezug auf **Zuordnung**, **sort_order**, **required**, **ui_group**.
|
||||||
|
|
||||||
|
**`required`:** bedeutet „Dokument muss nach Validator vollständig sein“, nicht „jede CSV-Spalte muss in jeder Zeile vorkommen“.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. Metadaten pro Composite-Parameter
|
||||||
|
|
||||||
|
Minimal in der DB (Beispiel):
|
||||||
|
|
||||||
|
| Feld | Zweck |
|
||||||
|
|------|--------|
|
||||||
|
| `data_type` | `composite` |
|
||||||
|
| `composite_archetype_key` | Verweis auf Code-Registry |
|
||||||
|
| `composite_archetype_version` | Schema-Version |
|
||||||
|
| `validation_rules` | optional: Overrides (z. B. `max_zones`, sport-spezifisch) — nur was der Validator explizit auswertet |
|
||||||
|
|
||||||
|
**Admin-API:** bestehende Endpoints erweitern (Payload-Validierung): bei `composite` müssen Archetyp + Version gesetzt sein und in der **Registry** existieren.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. Layer 1 – Kontrakt (`activity_session_metrics.py` + Helfer)
|
||||||
|
|
||||||
|
### 6.1 Schema-Auflösung
|
||||||
|
|
||||||
|
`resolve_activity_attribute_schema` liefert pro Composite **einen** Eintrag wie bei Skalaren, mit:
|
||||||
|
|
||||||
|
- `data_type: "composite"`
|
||||||
|
- `composite_archetype_key`, `composite_archetype_version` (aus DB oder Join)
|
||||||
|
- ggf. `composite_slot_catalog`: **nur wenn** für Admin/UI gewünscht — alternativ separater Endpoint `GET .../composite-archetypes` (read-only) aus Registry, um Bundle-Größe klein zu halten.
|
||||||
|
|
||||||
|
### 6.2 Lesen / Merge
|
||||||
|
|
||||||
|
- `fetch_activity_session_metrics`: SELECT inkl. `value_json`.
|
||||||
|
- `merge_column_backed_and_eav_metrics`: Composites **nur** aus EAV (`value_json`), kein `activity_log`-Shadowing (außer später explizit im Kanon — Standard: nein).
|
||||||
|
- Ausgabe in `metrics`-Liste: ein Eintrag pro Parameter mit z. B.
|
||||||
|
`value: { "_composite": true, "document": { ... } }` **oder** kanonisch getrennt: `value_document` + `value` null — **festlegen beim Implementieren** und in API-Doku halten; Empfehlung: **`value` = deserialisiertes Objekt (dict)** für Composites, damit Frontend dieselbe Struktur wie Speicher hat.
|
||||||
|
|
||||||
|
### 6.3 „Einzelwerte für Layer 1 / Issue 53“
|
||||||
|
|
||||||
|
Neue **pure** Funktion (kein SQL im Router), z. B.:
|
||||||
|
|
||||||
|
```text
|
||||||
|
expand_composite_metrics_for_session(
|
||||||
|
schema: list[dict],
|
||||||
|
metrics: list[dict],
|
||||||
|
) -> dict[str, Any]
|
||||||
|
```
|
||||||
|
|
||||||
|
- Input: effektives Schema + gemergte Metriken.
|
||||||
|
- Output: flaches Dict **`slot_path → typisierter Wert`**, z. B.
|
||||||
|
`hr_zones.z1_sec → 1200`, oder namespaced Keys `training_param_key.slot_key` zur Kollisionssicherheit.
|
||||||
|
- Nutzung: `activity_metrics`, Chart-Builder, später Platzhalter-Registry (`data_layer_function`), **ohne** JSON-Parsing in Layer 2.
|
||||||
|
|
||||||
|
**Wichtig:** Skalare Parameter erscheinen im expandierten Dict mit ihrem `parameter_key` wie bisher (kein Breaking Change für Consumer, die nur Skalare erwarten).
|
||||||
|
|
||||||
|
### 6.4 Validierung / Schreiben
|
||||||
|
|
||||||
|
- **`replace_activity_session_metrics`:** Payload-Item für Composite: `value` ist **Objekt** (dict) oder JSON-String — Server normalisiert zu dict, validiert mit Archetyp-Validator, speichert als `value_json`.
|
||||||
|
- **`upsert_session_metrics_from_csv_mapped`:** siehe §7 (Zusammenbau aus Partial-Updates pro Zeile).
|
||||||
|
|
||||||
|
**Pflicht:** Keine Teil-Updates in DB, die ein halbes Dokument hinterlassen, ohne Validierung — außer explizit als „Draft“-Modus spezifiziert (nicht Teil dieses Konzepts).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. CSV / Universal Import
|
||||||
|
|
||||||
|
### 7.1 Map-Ziel-Notation
|
||||||
|
|
||||||
|
Stabiles Muster (Vorschlag, im Import-Modul zentral parsen):
|
||||||
|
|
||||||
|
```text
|
||||||
|
"<parameter_key>.<slot_key>"
|
||||||
|
```
|
||||||
|
|
||||||
|
Beispiel: `my_hr_zones.z1_sec` → nach Import-Zusammenfügung in den Parameter `my_hr_zones` unter Slot `z1_sec`.
|
||||||
|
|
||||||
|
**Alternative:** explizites Präfix `composite:` in der Vorlage — nur nötig, wenn Kollisionen mit normalen Keys befürchtet werden; sonst Punkt-Notation reicht.
|
||||||
|
|
||||||
|
### 7.2 Executor-Flow (Konzept)
|
||||||
|
|
||||||
|
1. `build_row_after_mapping` liefert flache Keys inkl. `param.slot`.
|
||||||
|
2. Nach Schreiben von `activity_log` / Skalar-EAV: **Composite-Accumulator** pro `activity_log_id` und `parameter_key`:
|
||||||
|
- Sammelt alle Slot-Werte aus der Zeile.
|
||||||
|
3. Vor Commit der Zeile (oder am Ende der Datei — **pro Zeile empfohlen**, damit SAVEPOINT pro Row funktioniert):
|
||||||
|
- Dokument aus Slots bauen → Validator → Upsert `activity_session_metrics` mit `value_json`.
|
||||||
|
|
||||||
|
**Teilbefüllung:** Validator entscheidet (Archetyp: optional vs. required Slots). CSV darf nur Teilmengen liefern, wenn Archetyp erlaubt.
|
||||||
|
|
||||||
|
### 7.3 Typkonvertierung
|
||||||
|
|
||||||
|
Pro **Slot** im Archetyp: definierter skalarer Typ (`float`, `int`, …). Converter wie bei Skalaren (Executor / zentrale Converter), **keine** Parallel-Logik in Routern.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8. Admin-UI / Mapping-UX
|
||||||
|
|
||||||
|
### 8.1 Parameter anlegen
|
||||||
|
|
||||||
|
- Auswahl **Datentyp „Composite“** → Dropdown **Archetyp** (aus Registry-API), Version readonly oder wählbar gemäß Policy.
|
||||||
|
- Rest wie Skalar: Name, Kategorie (`training_parameters.category`), Aktiv-Flag.
|
||||||
|
|
||||||
|
### 8.2 Profil zuordnen
|
||||||
|
|
||||||
|
Unverändert: Kategorie-/Typ-Matrix wie heute.
|
||||||
|
|
||||||
|
### 8.3 Universal-CSV-Vorlage
|
||||||
|
|
||||||
|
- Mapping-Ziele: neben bisherigen Keys **Slot-Ziele** `parameter_key.slot_key`.
|
||||||
|
- UI-Gruppierung: optisch **Composite-Block** (wie in `ACTIVITY_PRODUCTION_ARCHITECTURE` §2.5 angedeutet), um Verwechslung mit Spine-Spalten zu vermeiden.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 9. API-Oberflächen (Erweiterungen)
|
||||||
|
|
||||||
|
| Bereich | Änderung |
|
||||||
|
|---------|-----------|
|
||||||
|
| `GET /api/activity/{id}` | `metrics` enthält Composite-Werte als Objekt; `schema` kennzeichnet `data_type: composite`. |
|
||||||
|
| `PUT /api/activity/{id}/metrics` | Eintrag `{ parameter_key, value: { ... } }` für Composites. |
|
||||||
|
| Admin `training-parameters` | Create/Update mit Composite-Feldern. |
|
||||||
|
| Optional | `GET /api/admin/composite-archetypes` | Registry export für UI (Keys, Slot-Liste, Version). |
|
||||||
|
|
||||||
|
**Rückwärtskompatibilität:** Clients, die nur Skalare senden, unverändert.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 10. Frontend (Kurz)
|
||||||
|
|
||||||
|
- `ActivityPage` / Session-Metrik-Editor: für `data_type === composite` **strukturierte Teilfelder** aus Slot-Katalog rendern (oder JSON-Editor nur als Entwickler-Fallback — Produkt: strukturierte Felder).
|
||||||
|
- Sortierung/Gruppierung: bestehende `param_category` / `ui_group` / `sort_order` gelten unverändert.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 11. Tests (pytest)
|
||||||
|
|
||||||
|
| Test | Beschreibung |
|
||||||
|
|------|----------------|
|
||||||
|
| Archetyp-Validator | gültige / ungültige Dokumente je Version |
|
||||||
|
| DB-Constraint | Skalar vs. Composite Ausschluss |
|
||||||
|
| `expand_composite_metrics_for_session` | flache Keys, Kollisionen |
|
||||||
|
| CSV-Zusammenbau | mehrere Spalten → ein `value_json` |
|
||||||
|
| Regression | bestehende `test_activity_session_metrics.py` unverändert grün halten |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 12. Rollout-Phasen (operativ)
|
||||||
|
|
||||||
|
Stimmt mit `ACTIVITY_PRODUCTION_ARCHITECTURE_AND_PHASES.md` überein:
|
||||||
|
|
||||||
|
1. **Phase D – MVP:** ein Preset (z. B. HF-Zonen → `distribution_set`, `domain: heart_rate`), Migration `value_json` + `composite` data_type, Validator gegen Basisschema §7, Import 3–5 Spalten → `items`, GET/PUT, minimale Admin-Anbindung.
|
||||||
|
2. **Phase E:** weitere Presets / `kind`-Varianten, Mapping-UX, `expand_*` für ausgewählte Layer-1-Consumer.
|
||||||
|
3. **Phase F:** Observability, Performance, Doku, Gitea-Issues schließen.
|
||||||
|
|
||||||
|
### 12.1 Empfohlene Reihenfolge: Skalar-Pipeline vs. Composite-Speicherung
|
||||||
|
|
||||||
|
**Frage:** Zuerst Skalar-EAV vollständig bis Platzhalter/Orchestrator abschließen, oder zuerst Composite-Speicherung?
|
||||||
|
|
||||||
|
| Option | Vorteil | Risiko |
|
||||||
|
|--------|---------|--------|
|
||||||
|
| **A: Nur Skalar zuerst** (Kanon, L1-Härtung, Platzhalter aus EAV/L1) | Eine klare, end-to-end **Referenzpipeline**; weniger gleichzeitige Variablen | Composite-Datenstrome verzögern sich |
|
||||||
|
| **B: Composite-Speicher zuerst** | JSON landet früh in der DB | Platzhalter/Charts nutzen noch **alte** Pfade → **zwei Wahrheiten** (Detail-API vs. KI) bis L1 vereinheitlicht ist |
|
||||||
|
| **C (Empfehlung): Skalar L1 + Platzhalter-Orchestrierung *vor* Composite-MVP**, oder **eng parallel** mit gemeinsamem L1-Einstieg | `get_activity_session_logical_unit` / `activity_metrics` werden **kanonisch**; Platzhalter lesen **dieselbe** Schicht; Composite wird **additiv** (`value_json` + Validator + später `expand_*`) | Erfordert kurze Planungsdisziplin: Composite-MVP **ohne** sofort alle KI-Platzhalter |
|
||||||
|
|
||||||
|
**Konkrete Empfehlung**
|
||||||
|
|
||||||
|
1. **`ACTIVITY_PRODUCTION` Phase A–B** nicht überspringen: Kanon „eine Semantik / eine Quelle“ + alle relevanten Consumer über **Layer 1** (mind. Session-Detail, Listen-Anreicherung, erste Platzhalter-Pfade für **Skalare**).
|
||||||
|
2. **Dann Phase D (Composite-MVP):** Migration + Speichern/Lesen mit **Basisschema** (`kind`/`items`/…); L1 liefert dasselbe API-Objekt wie Skalare, nur `value` als strukturiertes Dokument.
|
||||||
|
3. **Platzhalter für Composite:** erst **nach** L1 liefert stabil `value_json` **und** optional `expand_composite_metrics_*` — ein Orchestrator-Endpoint bzw. Resolver-Aufruf, der **eine** L1-Funktion nutzt, vermeidet doppelte Logik für Skalar vs. Composite.
|
||||||
|
|
||||||
|
**Kurz:** Composite **persistieren** kann kurz nach stabiler **Skalar-Lese-/Merge-API** folgen; **KI/Platzhalter für Composite** sinnvoll **gemeinsam** mit der erweiterten L1-Ausgabe bauen, nicht gegen eine noch nicht vereinheitlichte Skalar-Pipeline.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 13. Checkliste für den nächsten Agenten
|
||||||
|
|
||||||
|
- [ ] Migration: `value_json`, erweiterte CHECKs, `training_parameters.data_type` + ggf. `composite_archetype_*` Spalten.
|
||||||
|
- [ ] Registry-Modul: Archetypen + Versionen + Slot-Metadaten + Validator-Einstieg.
|
||||||
|
- [ ] `activity_session_metrics.py`: Fetch/Merge/Replace/Upsert-Integration; keine Regression für Skalare.
|
||||||
|
- [ ] Optional: `expand_composite_metrics_for_session` + erste Nutzung in einem Layer-1-Consumer (Tests).
|
||||||
|
- [ ] CSV: Parser für `parameter_key.slot_key`, Row-Accumulator, Fehler melden wie bestehender Import.
|
||||||
|
- [ ] Admin-API + UI: Composite anlegen, tcp/ttp unverändert nutzbar.
|
||||||
|
- [ ] Doku: dieses Dokument mit **festgelegter** JSON-Beispielstruktur pro MVP-Archetyp ergänzen.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 14. Referenzen
|
||||||
|
|
||||||
|
- `functional_concept_composite_data.md` – **fachliches** Schichtenmodell, vier technische `kind`-Container, Basisschema JSON
|
||||||
|
- `ACTIVITY_PRODUCTION_ARCHITECTURE_AND_PHASES.md` – Zielbild, Phasen A–F
|
||||||
|
- `ACTIVITY_SESSION_METRICS_EAV_AGENT_GUIDE.md` – Ist-Layer-1, APIs
|
||||||
|
- `UNIVERSAL_CSV_IMPORT_AGENT_GUIDE.md` – Executor, Vorlagen
|
||||||
|
- Migration `054_activity_session_metrics_eav.sql` – Ist-Constraint Skalar
|
||||||
|
- Migration `013_training_parameters.sql` – Ist-`data_type`-Enum
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Version:** 1.1 · Abgleich mit fachlichem Konzept (§1.4, §3, §12.1); MVP auf `distribution_set` o. ä. konkretisieren.
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
# Aktivität: Layer-2a-Platzhalter — Audit Schritt 1 (Issue #53)
|
||||||
|
|
||||||
|
**Stand:** 2026-04-16
|
||||||
|
**Bezug:** [Issue #53 — Multi-Layer Architecture](../../../docs/issues/issue-53-phase-0c-multi-layer-architecture.md): Layer 1 = strukturierte Daten, Layer 2a = KI-Formatierung (keine parallele Domänen-Logik im Resolver).
|
||||||
|
|
||||||
|
**Ziel dieses Dokuments:** Jeder Aktivitäts-Platzhalter hat genau eine **Layer‑1‑Quelle** (`data_layer/activity_metrics.py`); `placeholder_resolver.py` formatiert oder serialisiert nur noch.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. Ergebnisübersicht
|
||||||
|
|
||||||
|
| Kategorie | Anzahl | Resolver-SQL für Aktivität? |
|
||||||
|
|-----------|--------|------------------------------|
|
||||||
|
| Gebündelt in `PLACEHOLDER_MAP` (Training/Aktivität) | 20 | **Nein** |
|
||||||
|
| Abweichungen / offene Punkte | 0 | — |
|
||||||
|
|
||||||
|
**Hinweis:** `{{rest_days_count}}` steht in der Karte unter „Schlaf & Erholung“ und nutzt `recovery_metrics.get_rest_days_data` — nicht in dieser Tabelle.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. Platzhalter → Layer 1 → Layer 2a
|
||||||
|
|
||||||
|
| Key | Layer 1 (`activity_metrics`) | Layer 2a (`placeholder_resolver`) | Bemerkung |
|
||||||
|
|-----|------------------------------|-------------------------------------|-----------|
|
||||||
|
| `activity_summary` | `get_activity_summary_data` | `get_activity_summary` | String-Zusammenfassung |
|
||||||
|
| `activity_detail` | `get_activity_detail_data` (+ `enrich_sessions_with_metrics`) | `get_activity_detail` | Dynamische `session_metrics[]` pro Zeile (Profil/EAV) |
|
||||||
|
| `trainingstyp_verteilung` | `get_training_type_distribution_data` | `get_trainingstyp_verteilung` | Ausgabe: Top-3-Text (kein JSON); Registry 2026-04 an Ist angeglichen |
|
||||||
|
| `training_minutes_week` | `calculate_training_minutes_week` | `_safe_int` | |
|
||||||
|
| `training_frequency_7d` | `calculate_training_frequency_7d` | `_safe_int` | |
|
||||||
|
| `quality_sessions_pct` | `calculate_quality_sessions_pct` | `_safe_int` | |
|
||||||
|
| `proxy_internal_load_7d` | `calculate_proxy_internal_load_7d` | `_safe_int` | |
|
||||||
|
| `monotony_score` | `calculate_monotony_score` | `_safe_float` | |
|
||||||
|
| `strain_score` | `calculate_strain_score` | `_safe_int` | |
|
||||||
|
| `rest_day_compliance` | `calculate_rest_day_compliance` | `_safe_int` | |
|
||||||
|
| `ability_balance_strength` | `calculate_ability_balance_strength` | `_safe_int` | abilities in `activity_log` |
|
||||||
|
| `ability_balance_endurance` | `calculate_ability_balance_endurance` | `_safe_int` | |
|
||||||
|
| `ability_balance_mental` | `calculate_ability_balance_mental` | `_safe_int` | |
|
||||||
|
| `ability_balance_coordination` | `calculate_ability_balance_coordination` | `_safe_int` | |
|
||||||
|
| `ability_balance_mobility` | `calculate_ability_balance_mobility` | `_safe_int` | |
|
||||||
|
| `vo2max_trend_28d` | `calculate_vo2max_trend_28d` | `_safe_float` | |
|
||||||
|
| `activity_score` | `calculate_activity_score` | `_safe_int` | |
|
||||||
|
| `training_frequency_by_type_md` | `get_training_frequency_by_type_data` | `get_training_frequency_by_type_md` | Markdown-Tabelle |
|
||||||
|
| `training_inter_session_gap_md` | `get_training_inter_session_gap_data` | `get_training_inter_session_gap_md` | Markdown-Text |
|
||||||
|
| `training_sessions_recent_json` | `get_training_sessions_recent_weeks_data` (+ `enrich_sessions_with_metrics`) | `_safe_json('training_sessions_recent_json')` | JSON inkl. `session_metrics[]` pro Session |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. Schichten-Disziplin (Checkliste)
|
||||||
|
|
||||||
|
- [x] Kein `SELECT` auf `activity_log` / `activity_session_metrics` in den **Layer‑2a**-Funktionen oben — nur Aufrufe in Layer 1 bzw. `_safe_*`-Wrapper.
|
||||||
|
- [x] `get_activity_detail` / `get_training_sessions_recent_json` liefern EAV nur über **bereits gemergte** `session_metrics` (Merge-Kanon: `activity_log` vor EAV).
|
||||||
|
- [x] Registry-Metadaten: `data_layer_module` / `data_layer_function` pro Key in `placeholder_registrations/activity_metrics.py` und `activity_session_insights.py`.
|
||||||
|
- [x] Korrektur Registry: `activity_summary.resolver_function` = `get_activity_summary` (war veraltet: `_format_activity_summary`).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. Nächste Schritte (Roadmap)
|
||||||
|
|
||||||
|
2. ~~**Registry-Texte:** `semantic_contract` / `known_limitations` für dynamische `session_metrics` (tcp/ttp) und Merge-Kanon — **erledigt** (`activity_detail`, `training_sessions_recent_json`); dazu **`trainingstyp_verteilung`**-Metadaten von veraltetem „JSON/Resolver-SQL“ auf Ist (**Layer 1 + Top-3-Text**) korrigiert.~~
|
||||||
|
3. **History / Layer 2b:** EAV-Zeitreihen nicht über Platzhalter, sondern dedizierte Layer‑1-/Chart-Pfade.
|
||||||
|
4. **Optional:** Gitea-Issue „Activity Layer 2a“ bei Änderungen an `activity_metrics` pflegen.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. Referenzen
|
||||||
|
|
||||||
|
- `backend/placeholder_resolver.py` — `PLACEHOLDER_MAP` (Training/Aktivität)
|
||||||
|
- `backend/placeholder_registrations/activity_metrics.py`
|
||||||
|
- `backend/placeholder_registrations/activity_session_insights.py`
|
||||||
|
- `ACTIVITY_PRODUCTION_ARCHITECTURE_AND_PHASES.md` §2.1a (Navigation Read vs. Berechnen)
|
||||||
@@ -0,0 +1,215 @@
|
|||||||
|
# Aktivität: Zielarchitektur & Phasenplan (Produktionsreife)
|
||||||
|
|
||||||
|
**Stand:** 2026-04-16
|
||||||
|
**Status:** Normative Zielrichtung für `activity_log`, EAV, Composites, Import, Layer 1/2.
|
||||||
|
**Ergänzt:** `ACTIVITY_SESSION_METRICS_EAV_AGENT_GUIDE.md` (Ist-Modell, APIs, Tests).
|
||||||
|
**Phase A:** abgeschlossen — Kanon-Tabelle [`ACTIVITY_SCALAR_KANON_TABLE.md`](./ACTIVITY_SCALAR_KANON_TABLE.md).
|
||||||
|
**Phase B:** in Arbeit — Consumer-Audit und Lesepfad-Härtung (siehe §4 Phase B).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. Leitprinzipien
|
||||||
|
|
||||||
|
| Prinzip | Bedeutung |
|
||||||
|
|---------|-----------|
|
||||||
|
| **Layer 1 = Single Source of Truth** | Alle Auswertungen (Charts, Scores, strukturierte Platzhalter) lesen **nur** über `data_layer` (kanonische Funktionen). Keine parallele SQL-Logik in Routern oder im Placeholder-Resolver für Aktivität. |
|
||||||
|
| **Eine semantische Größe, eine kanonische Quelle** | Kein Dauer-Sync derselben Bedeutung in `activity_log`-Spalte **und** EAV. Übergang: dokumentierte Abschaltung, nicht implizites Driften. |
|
||||||
|
| **Spine vs. Parameter** | `activity_log` trägt Identität, Zeit, Typ, Notizen, Audit + **heiße** universelle Skalare (siehe §2.2). Alles Typ-/Admin-Dynamische über EAV. |
|
||||||
|
| **Composites = Archetyp im Code, Konfiguration in der DB** | Struktur (7+2 Archetypen) und Validierung **versioniert im Repo**; Admin **wählt** Archetyp, **benennt** Slots, **bindet** Sportarten, **mappt** CSV → `(parameter_id, slot_key)`. Kein freies JSON-Schema im Admin. |
|
||||||
|
| **Import explizit** | Jede CSV-Spalte hat ein klares Ziel: Spine-Spalte, skalarer Parameter oder **Slot** eines Composite-Parameters. Typkonvertierung zentral (Executor / Converter), nicht verteilt. |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. Zielarchitektur (Gesamtbild)
|
||||||
|
|
||||||
|
### 2.1 Schichtenmodell
|
||||||
|
|
||||||
|
```
|
||||||
|
[CSV / UI / API Write]
|
||||||
|
↓
|
||||||
|
Orchestrator & Router (Auth, Transaktionen, Feature-Checks)
|
||||||
|
↓
|
||||||
|
Persistenz: activity_log (Spine + heiße Skalare) + activity_session_metrics (EAV)
|
||||||
|
↓
|
||||||
|
Layer 1: data_layer (activity_session_metrics.py, activity_metrics.py, …)
|
||||||
|
↓
|
||||||
|
Layer 2a/2b: Platzhalter-Resolver (Formatierung), Chart-Endpoints (Chart.js-Shapes)
|
||||||
|
↓
|
||||||
|
KI / UI / Export
|
||||||
|
```
|
||||||
|
|
||||||
|
- **Orchestrator:** Schreibpfad, Konsistenz nach Write (kein zweites „Lesen der Wahrheit“ neben Layer 1; optional nur Post-Write-Hooks).
|
||||||
|
- **Resolver:** für Aktivität **kein** direkter DB-Zugriff; nur Aufruf von Layer 1.
|
||||||
|
|
||||||
|
### 2.1a Navigationsregel: wo nachsehen (ohne Datei-Zwang)
|
||||||
|
|
||||||
|
Die **physische** Aufteilung ist dreigeteilt: **`activity_log`** (Spine + heiße Spalten), **EAV-Skalare** (`activity_session_metrics` + numerische/textuelle `value_*`), **EAV-Composites** (ein Parameter, Nutzlast z. B. JSON/JSONB im EAV-Datensatz). **Fachlich** soll nach außen **eine homogene Session-Sicht** entstehen — Consumer sollen nicht selbst entscheiden, aus welcher Tabelle/Welche Form ein Wert kommt.
|
||||||
|
|
||||||
|
| Thema | Wo nachsehen (Ist; Ziel: Schnittstelle stabil, Datei optional splittbar) |
|
||||||
|
|--------|--------------------------------------------------------------------------|
|
||||||
|
| **Homogene Session lesen** (Merge Spalte + EAV-Skalare + später Composite-Payload) | `data_layer/activity_session_metrics.py` — u. a. `get_activity_session_logical_unit`, `enrich_sessions_with_metrics`, `merge_column_backed_and_eav_metrics` |
|
||||||
|
| **Schreiben / Import / API-Persistenz** | `data_layer/activity_persistence_orchestrator.py` (+ Router) |
|
||||||
|
| **Berechnungen, Aggregationen, Scores** über viele Sessions oder Zeitfenster | `data_layer/activity_metrics.py` — arbeitet auf der **vereinheitlichten** Session-Datenlage (über die Read-Funktionen oben), nicht durch paralleles Mergen der drei Quellen im Caller |
|
||||||
|
|
||||||
|
**Hinweis:** Orchestrator und Read-Merge **müssen nicht** in derselben Datei stehen. Entscheidend ist, dass es **genau eine dokumentierte Read-Fassade** für „Session inkl. aller effektiven Metriken“ gibt und Layer‑1‑Berechnungen **nur** diese Fassade (oder deren Ergebnisstrukturen) nutzen. Eine spätere Umbenennung oder Auslagerung in z. B. `activity_read_gateway.py` ändert die Rolle nicht — nur der **eine Einstieg** muss in dieser Doku und im Code auffindbar bleiben.
|
||||||
|
|
||||||
|
### 2.2 `activity_log` (Spine + heiße Skalare)
|
||||||
|
|
||||||
|
**Maschinenlesbarer Kanon:** `backend/data_layer/activity_data_canon.py` (`ACTIVITY_MODULE_REGISTRY_FIELD_KEYS`, `ACTIVITY_EAV_PRIMARY_PARAMETER_KEYS`, Legacy-Lesefallback für EAV-primäre Parameter).
|
||||||
|
|
||||||
|
**Immer (fachlich minimal + listenfähig):** `id`, `profile_id`, Kalender-/Zeitfenster (`date`, `started_at`/`ended_at`, ggf. `start_time`/`end_time` bis Konsolidierung), `duration_min`, `training_type_id` (+ ggf. denormalisierte Kategorie), Legacy `activity_type`, `notes`, `source`, `created`.
|
||||||
|
|
||||||
|
**Heiße Skalare (CSV-Modul + `source_field` nach Migration 057):** u. a. `kcal_active`, `kcal_resting`, `distance_km`, `hr_avg`/`hr_max` (Parameter `avg_hr`/`max_hr`), `duration_min`, `rpe` – für Listen und Standard-Aggregate ohne EAV-Join.
|
||||||
|
|
||||||
|
**EAV-primär (erweiterte Metriken):** z. B. Kadenz, Pace, Leistung, Höhe, Umgebung — `training_parameters.source_field` = NULL; Import schreibt EAV; bei leerem EAV optional Lesefallback auf bestehende `activity_log`-Spalte (Migration 057 + Merge-Logik).
|
||||||
|
|
||||||
|
**Session-Qualität / Auswertungsblob:** z. B. `evaluation`, `quality_label`, `overall_score` – **kein** EAV-Parameter-Raster; semantisch „Ergebnis der Einheit“.
|
||||||
|
|
||||||
|
**Nicht dauerhaft doppelt:** dieselbe Semantik nicht parallel pflegen; siehe entfallener Spalte→EAV-Schreib-Sync, Lesepfad `merge_column_backed_and_eav_metrics`.
|
||||||
|
|
||||||
|
### 2.3 EAV (`activity_session_metrics`)
|
||||||
|
|
||||||
|
- **Skalare:** ein `training_parameter`, genau eine `value_*`-Spalte (wie heute).
|
||||||
|
- **Composites:** ein `training_parameter` pro Composite-Instanz, **ein** gespeichertes Dokument pro Session (serialisiert z. B. in `value_text` als JSON **oder** künftig dedizierte JSONB-Spalte – technische Entscheidung in eigener Migration, Vertrag im Archetyp).
|
||||||
|
- **Merge-/Schema-Logik:** weiterhin zentral in `activity_session_metrics.py` (effektives Schema aus Kategorie + Typ-Overrides).
|
||||||
|
|
||||||
|
### 2.4 Composite-Metamodell (Ziel)
|
||||||
|
|
||||||
|
**Archetypen (Code, begrenzte Menge):** u. a. Band-/Zonenverteilung, Sequenz-/Übergangsprofil, Intervallblock-, Ereignis-/Aktions-, Kopplungs-/Effizienz-, Modellparameter-Profil; optional Technik-/Zyklus-, Readiness-/Recovery-Profil.
|
||||||
|
|
||||||
|
**Pro Archetyp:** feste strukturelle Regeln (erlaubte Slots, Typen, Pflicht/Optional), Validator + Version.
|
||||||
|
|
||||||
|
**In der DB (Admin):** Zuordnung „Parameter X hat Archetyp A“, Slot-Labels (DE/EN), Einheiten, Aktivierung pro Sportart/Kategorie, Sortierung.
|
||||||
|
|
||||||
|
**Import:** CSV-Spalten → `(training_parameter_id, slot_key)` mit stabilen Keys (`z1_sec`, …), nie nur „Spaltenreihenfolge“.
|
||||||
|
|
||||||
|
### 2.5 Universal CSV & Admin
|
||||||
|
|
||||||
|
- Vorlagen: Mapping inkl. **Composite-Slots** und Typkonvertierung (vollständige Matrix Ziel).
|
||||||
|
- UI: Trennung **Kern activity_log** vs. **Parameter/EAV** vs. **Composite-Blöcke** (optisch/UX), um Doppel-Tabellen-Chaos zu vermeiden.
|
||||||
|
|
||||||
|
### 2.6 Layer 2 (Platzhalter & Diagramme)
|
||||||
|
|
||||||
|
- Datenbezug **nur** Layer 1.
|
||||||
|
- Registry-Einträge: `data_layer_module` / `data_layer_function` pflegen; Composite-Auswertung ggf. über Hilfsfunktionen, die JSON → normierte Struktur für Prompts/Charts liefern.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. Ist → Soll (Kurz)
|
||||||
|
|
||||||
|
| Bereich | Ist (typisch) | Soll |
|
||||||
|
|---------|----------------|------|
|
||||||
|
| Schreibpfad | Teilweise Doppelhaltung Spalte ↔ EAV, Sync-Hooks | Kanon + gezielte Abschaltung; eine Quelle pro Semantik |
|
||||||
|
| Lesepfad | Layer 1 wächst; Legacy-Spalten noch relevant | `get_activity_session_logical_unit` / `activity_metrics` als alleinige Wahrheit für Consumer |
|
||||||
|
| Composites | Noch nicht im Einklang mit EAV-Metamodell | Archetypen + Slot-Admin + ein Dokument pro Parameter/Session |
|
||||||
|
| Import | Mapping teilweise; Typkonvertierung lückenhaft | Vollständige Konvertierung + Composite-Zusammenbau |
|
||||||
|
| Resolver | Aktivität sauber über Layer 1 | Profil/Focus ggf. später ebenfalls aus Layer 1 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. Vorgehensmodell (Phasen)
|
||||||
|
|
||||||
|
Phasen sind **sequentiell** wo „Abhängigkeit“ steht; Teile können parallel (z. B. UI-Polish) laufen, wenn der Kanon steht.
|
||||||
|
|
||||||
|
### Phase A – Kanon & Abschaltplan (Grundlage) ✅
|
||||||
|
|
||||||
|
**Inhalt:** Schriftliche **Kanon-Tabelle**: pro Messgröße genau eine Quelle (`activity_log` | `eav_scalar` | `eav_composite` | `session_quality`). Liste der Keys, für die **Sync/Spiegelung** endet.
|
||||||
|
|
||||||
|
**Definition of Done:** Review im Team; Referenz in diesem Dokument oder Verweis auf Gitea-Kommentar; keine Code-Änderung zwingend.
|
||||||
|
|
||||||
|
**Erledigt (2026-04-16):** [`ACTIVITY_SCALAR_KANON_TABLE.md`](./ACTIVITY_SCALAR_KANON_TABLE.md) — eine Semantik pro Zeile, verlinkt mit `activity_data_canon.py` und Merge-Logik.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Phase B – Lesepfad härten (Layer 1) 🔄
|
||||||
|
|
||||||
|
**Inhalt:** Sicherstellen, dass **alle** relevanten Consumer (mind. `activity_metrics` für Platzhalter/Charts, Activity-Detail-API) dieselbe Merge-/Fallback-Logik nutzen; Legacy-Spalten nur noch als dokumentierter Fallback bis Enddatum.
|
||||||
|
|
||||||
|
**Definition of Done:** Kurze Audit-Liste „Router/Resolver greifen nicht an Aktivität vorbei“; Tests oder manuelle Stichprobe für Detail + ein Chart + 2 Platzhalter.
|
||||||
|
|
||||||
|
**Abhängigkeit:** Phase A für „welche Spalten noch Fallback sind“.
|
||||||
|
|
||||||
|
**Audit-Stand (2026-04-16, ergänzt Export):**
|
||||||
|
|
||||||
|
| Consumer | Nutzt Layer-1-Merge (`enrich_sessions_with_metrics` / `get_activity_session_logical_unit`) | Anmerkung |
|
||||||
|
|----------|---------------------------------------------------------------------------------------------|-----------|
|
||||||
|
| `GET /api/activity/{eid}` | ✅ `get_activity_session_logical_unit` | Referenz-Detail |
|
||||||
|
| `GET /api/activity` (Liste) | ✅ seit 2026-04-16 `enrich_sessions_with_metrics` auf jeder Listen-Antwort | vorher nur Roh-Spalten |
|
||||||
|
| `activity_metrics.get_activity_detail_data` | ✅ | Platzhalter `{{activity_detail}}` |
|
||||||
|
| `activity_metrics.get_training_sessions_recent_weeks_data` | ✅ | KI-Kontext |
|
||||||
|
| `placeholder_resolver` (Aktivität) | ✅ nur `activity_metrics` | kein paralleles SQL |
|
||||||
|
| `GET /api/export/json` (`activity`) | ✅ `enrich_sessions_with_metrics` + `serialize_dates` | `session_metrics` pro Zeile |
|
||||||
|
| `GET /api/export/csv` (Training-Zeilen) | ✅ `enrich_sessions_with_metrics` | gemergte EAV in Spalte „Details“ |
|
||||||
|
| `GET /api/export/zip` (`data/activity.csv`) | ✅ `enrich_sessions_with_metrics` | Zusatzspalte `session_metrics_json` (Import ignoriert sie) |
|
||||||
|
| `get_activity_summary_data` | n. a. | rein aggregiert (`SUM`/`COUNT`), keine Session-EAV |
|
||||||
|
| `routers/charts.py` (A1–A8) | Spalten-Aggregate | bewusst: Dauer/RPE/HF aus **`activity_log`**-Kanon; kein EAV-Join nötig für definierte Charts |
|
||||||
|
| `activity_stats` (`GET /api/activity/stats`) | nur Spalten | Kacheln: `kcal`/`duration` aus Kernspalten |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Phase C – Schreibpfad entschlacken
|
||||||
|
|
||||||
|
**Inhalt:** Orchestrierung/CSV: kein Schreiben derselben Semantik an zwei Orten; `sync_column_backed_session_metrics` (o. ä.) **stufig abschalten** oder auf Notfall-Flag; Import schreibt gemäß Kanon.
|
||||||
|
|
||||||
|
**Definition of Done:** Deploy auf Prod mit Monitoring; Stichprobe Import + manuelle Bearbeitung; keine Regression in Listenansicht.
|
||||||
|
|
||||||
|
**Abhängigkeit:** Phase A + B (sonst Lücken beim Lesen).
|
||||||
|
|
||||||
|
**Analyse (2026-04-16, nur Ist-Review):** Es gibt **keinen aktiven** Schreibpfad mehr, der `activity_log`-Spalten für `source_field`-Parameter **dauerhaft nach EAV spiegelt**.
|
||||||
|
|
||||||
|
| Prüfpunkt | Ergebnis |
|
||||||
|
|-----------|----------|
|
||||||
|
| `sync_column_backed_session_metrics` | Nur noch **Definition** in `activity_session_metrics.py`, als veraltet markiert; **keine Aufrufer** im Repo (grep). Laufzeit-Sync: **abgestellt**. |
|
||||||
|
| `run_activity_post_write_hooks` / `…_import` | Nur **Auto-Eval** (optional); Kommentar: **kein** Spalte→EAV-Sync. |
|
||||||
|
| Universal-CSV (`executor.py`) | Kernfelder → `activity_log` (`activity_csv_registry_updates_from_mapped` + `update_activity_columns` / Insert); EAV → `upsert_session_metrics_from_csv_mapped`. Registry-Keys werden **nicht** nach EAV geschrieben; bei `source_field` wird EAV **übersprungen**, wenn die Spalte **bereits befüllt** ist — vermeidet bewusst doppelte Speicherung. |
|
||||||
|
| REST `PUT /metrics` | Kommentar in Code: **kein** `sync_column_backed` nach EAV-Ersatz. |
|
||||||
|
| Migrationen 055 / 057 | **Einmaliger** Backfill/Schwenk, kein fortlaufender Sync. |
|
||||||
|
|
||||||
|
**Lesepfad (2026-04-16):** `merge_column_backed_and_eav_metrics` bevorzugt **immer** `activity_log`, wenn ein kanonischer Spaltenwert existiert: zuerst `source_field`, dann Registry-Spalte gleichen Keys, dann Legacy-Spalten für EAV-primäre Parameter, zuletzt EAV. Doppelte physische Schreiborte sind damit in der effektiven Sicht **ohne EAV-Vorrang** behoben.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Phase D – Composite MVP
|
||||||
|
|
||||||
|
**Inhalt:** Ein Archetyp end-to-end (z. B. **Band-/Zonenverteilung**): Code-Validator, DB-Binding (Parameter + Slots), Admin-UI minimal, Import **5 Spalten → ein JSON-Dokument** mit festen Keys, Layer-1-Read (Roh + optional `expand_*`).
|
||||||
|
|
||||||
|
**Definition of Done:** Eine Sportart/Kategorie befüllbar; Dokumentation des JSON-Vertrags im Repo; pytest für Validator/Zusammenbau wo möglich.
|
||||||
|
|
||||||
|
**Abhängigkeit:** Phase A (Kanon „Composites nur als Dokument, nicht doppelt in Spalten“).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Phase E – Composite-Ausbau & Typkonvertierung Import
|
||||||
|
|
||||||
|
**Inhalt:** Weitere Archetypen nach Priorität; Universal-CSV **vollständige** Typkonvertierung für alle gemappten Ziele; Dialog-/Mapping-Konzept (Kern vs. Parameter vs. Composite).
|
||||||
|
|
||||||
|
**Definition of Done:** Matrix „Zieltyp × Converter“ gepflegt; Admin-Flow reviewt.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Phase F – Produktionshärtung
|
||||||
|
|
||||||
|
**Inhalt:** Performance-Indizes bei Bedarf; Observability (Import-Fehler, Validierungs-Fails); Resolver/Profil optional komplett ohne `get_db` für domänische Daten; Doku + Gitea-Issues geschlossen/aktualisiert.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. Was zuerst?
|
||||||
|
|
||||||
|
**Erledigt:** Phase A — [`ACTIVITY_SCALAR_KANON_TABLE.md`](./ACTIVITY_SCALAR_KANON_TABLE.md).
|
||||||
|
|
||||||
|
**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).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. Referenzen
|
||||||
|
|
||||||
|
- `ACTIVITY_SCALAR_KANON_TABLE.md` – **Skalar-Kanon** (Phase A)
|
||||||
|
- `ACTIVITY_SESSION_METRICS_EAV_AGENT_GUIDE.md` – Tabellen, APIs, Tests, Backfill-Hinweise
|
||||||
|
- `ACTIVITY_COMPOSITE_METRICS_IMPLEMENTATION_CONCEPT.md` – Composite-EAV (JSONB), Archetypen, Import-Slots, Layer-1-Expand, Migrations- und Testplan
|
||||||
|
- `UNIVERSAL_CSV_IMPORT_AGENT_GUIDE.md` – Executor, Vorlagen, Typen
|
||||||
|
- `PLACEHOLDER_REGISTRY_FRAMEWORK.md` – Layer-2-Registrierung
|
||||||
|
- `functional/DATA_ARCHITECTURE.md` – fachliche Datenarchitektur (Querschnitt)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Version:** 1.5 · Merge: activity_log (Registry + Legacy-Spalten) vor EAV bei Lesen.
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
# Aktivität: Skalar-Kanon (eine Semantik → eine Quelle)
|
||||||
|
|
||||||
|
**Stand:** 2026-04-16
|
||||||
|
**Normativer Code:** `backend/data_layer/activity_data_canon.py`
|
||||||
|
**Kontext:** `ACTIVITY_PRODUCTION_ARCHITECTURE_AND_PHASES.md` (Phase A abgeschlossen)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. Spine & Identität (`activity_log`, nicht EAV)
|
||||||
|
|
||||||
|
Diese Felder sind **keine** `training_parameters`-Skalare. Sie gehören zur Session-Zeile.
|
||||||
|
|
||||||
|
| Semantik | DB / API | Kanonische Quelle | Lesefallback | Sync Spalte↔EAV |
|
||||||
|
|----------|----------|-------------------|--------------|-----------------|
|
||||||
|
| Primärschlüssel | `activity_log.id` | `activity_log` | — | — |
|
||||||
|
| Profil | `profile_id` | `activity_log` | — | — |
|
||||||
|
| Kalendertag | `date` | `activity_log` | — | — |
|
||||||
|
| Start / Ende (Zeit) | `start_time`, `end_time`, `started_at`, `ended_at` | `activity_log` | — | — |
|
||||||
|
| Trainingsart (Freitext/Legacy) | `activity_type` | `activity_log` | — | — |
|
||||||
|
| Referenz Trainingstyp | `training_type_id`, `training_category`, … | `activity_log` (+ `training_types`) | — | — |
|
||||||
|
| Notiz | `notes` | `activity_log` | — | — |
|
||||||
|
| Quelle / Import | `source`, `created`, … | `activity_log` | — | — |
|
||||||
|
| Session-Auswertung | `evaluation`, `quality_label`, `overall_score`, … | `activity_log` (Blob/Ergebnis) | — | Kein EAV-Raster |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. Kernfelder CSV-Modul `activity` (= „heiße“ Skalare)
|
||||||
|
|
||||||
|
Abgeleitet aus `csv_parser.module_registry.MODULE_DEFINITIONS["activity"].fields` — maschinenlesbar über `ACTIVITY_MODULE_REGISTRY_FIELD_KEYS` in `activity_data_canon.py`.
|
||||||
|
|
||||||
|
| Semantik | Key (Registry/API) | Kanonische Quelle | Lesefallback | Bemerkung |
|
||||||
|
|----------|-------------------|-------------------|--------------|-----------|
|
||||||
|
| Dauer | `duration_min` | **`activity_log`** | — | Aggregates, Listen |
|
||||||
|
| Aktive Energie | `kcal_active` | **`activity_log`** | — | |
|
||||||
|
| Ruhe-Energie | `kcal_resting` | **`activity_log`** | — | |
|
||||||
|
| Distanz | `distance_km` | **`activity_log`** | — | |
|
||||||
|
| Ø HF | `hr_avg` (Parameter oft `avg_hr` in EAV-Schema) | **`activity_log`** | EAV nur wenn `source_field` / Profil-Schema | `merge_column_backed_and_eav_metrics`: Spalte schlägt EAV |
|
||||||
|
| Max-HF | `hr_max` | **`activity_log`** | analog | |
|
||||||
|
| RPE | `rpe` | **`activity_log`** | analog | |
|
||||||
|
|
||||||
|
Schreibpfad: Universal-CSV und API sollen diese Keys auf **`activity_log`** mappen, sofern nicht ausdrücklich ein EAV-primärer Parameter (§3) gewählt ist.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. EAV-primäre Parameter (erweiterte Skalare)
|
||||||
|
|
||||||
|
`ACTIVITY_EAV_PRIMARY_PARAMETER_KEYS` in `activity_data_canon.py`. **`training_parameters.source_field`** = NULL (nach Kanon / Migration 057): kanonischer Speicher ist **`activity_session_metrics`**.
|
||||||
|
|
||||||
|
| Parameter-Key (`training_parameters.key`) | Legacy-Spalte `activity_log` | Schreib-Kanon (Ziel) |
|
||||||
|
|-------------------------------------------|------------------------------|------------------------|
|
||||||
|
| `min_hr` | `hr_min` | **EAV** |
|
||||||
|
| `pace_min_per_km` | `pace_min_per_km` | **EAV** |
|
||||||
|
| `cadence` | `cadence` | **EAV** |
|
||||||
|
| `avg_power` | `avg_power` | **EAV** |
|
||||||
|
| `elevation_gain` | `elevation_gain` | **EAV** |
|
||||||
|
| `temperature_celsius` | `temperature_celsius` | **EAV** |
|
||||||
|
| `humidity_percent` | `humidity_percent` | **EAV** |
|
||||||
|
| `avg_hr_percent` | `avg_hr_percent` | **EAV** |
|
||||||
|
| `kcal_per_km` | `kcal_per_km` | **EAV** |
|
||||||
|
|
||||||
|
**Lesen:** `merge_column_backed_and_eav_metrics` — wenn Legacy-Spalte **und** EAV einen Wert haben, **gewinnt die Spalte** (kanonische `activity_log`-Sicht). EAV nur, wenn die Spalte leer/nicht koerzierbar ist.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. Profil-/Typ-dynamische Skalare (EAV, nicht in Registry-Kernliste)
|
||||||
|
|
||||||
|
| Semantik | Kanonische Quelle | Lesefallback |
|
||||||
|
|----------|-------------------|--------------|
|
||||||
|
| Admin-definierte Parameter (Attributprofil Kategorie/Typ) | **`activity_session_metrics`** + `training_parameters` | — |
|
||||||
|
| Parameter mit `source_field` → Spalte | **`activity_log`** (Spalte) | EAV ergänzend; Leseregel: Spalte bevorzugt (kein veraltetes EAV) |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. Composites (Zielbild, noch nicht Kanon-Zeile pro Slot)
|
||||||
|
|
||||||
|
| Semantik | Kanonische Quelle (Ziel) |
|
||||||
|
|----------|---------------------------|
|
||||||
|
| Strukturierte Composite-Dokumente (z. B. Zonen/Bänder) | **EAV** ein Dokument pro Parameter/Session (siehe `ACTIVITY_COMPOSITE_METRICS_IMPLEMENTATION_CONCEPT.md`) |
|
||||||
|
|
||||||
|
Kein dauerhaftes Spiegeln derselben Semantik in `activity_log`-Spalten.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. Sync & Übergang
|
||||||
|
|
||||||
|
- **Kein** automatischer Dauer-Sync „Spalte → EAV“ für dieselbe Semantik; Lesepfad vereinheitlicht die Sicht (`merge_column_backed_and_eav_metrics`).
|
||||||
|
- Optionale **Backfill**-Migration/Skript (idempotent) nur nach fachlicher Freigabe — siehe EAV-Agent-Guide §6.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. Referenzen
|
||||||
|
|
||||||
|
- `ACTIVITY_PRODUCTION_ARCHITECTURE_AND_PHASES.md` — Phasen A–F
|
||||||
|
- `ACTIVITY_SESSION_METRICS_EAV_AGENT_GUIDE.md` — APIs, Tests
|
||||||
|
- `activity_data_canon.py` — `ACTIVITY_LOG_PATCHABLE_COLUMNS`, Legacy-Map
|
||||||
@@ -0,0 +1,146 @@
|
|||||||
|
# Activity Session Metrics (EAV) – Umsetzungs- & Agent-Guide
|
||||||
|
|
||||||
|
**Stand:** 2026-04-14
|
||||||
|
**Status:** Kern-Backend (Migration 054, Layer 1, Admin- & Nutzer-API) umgesetzt; Admin-UI & CSV-Mapping folgen.
|
||||||
|
**Ziel:** Sportspezifische **Attributprofile** (Kategorie + optional Trainingstyp-Override) administrierbar; Messwerte pro Session in **EAV**; **alle Auswertungen** sollen künftig über **Layer 1** (`data_layer`) laufen.
|
||||||
|
|
||||||
|
**Zielarchitektur, Phasenplan (Produktionsreife):** [`ACTIVITY_PRODUCTION_ARCHITECTURE_AND_PHASES.md`](./ACTIVITY_PRODUCTION_ARCHITECTURE_AND_PHASES.md) – Kanon `activity_log`/EAV, Composites, Import, Layer 1/2, Reihenfolge A–F.
|
||||||
|
|
||||||
|
**Composite-Parameter (EAV, JSONB, Archetypen):** detailliertes Umsetzungskonzept für Agenten: [`ACTIVITY_COMPOSITE_METRICS_IMPLEMENTATION_CONCEPT.md`](./ACTIVITY_COMPOSITE_METRICS_IMPLEMENTATION_CONCEPT.md).
|
||||||
|
|
||||||
|
**Kanon (Code):** `backend/data_layer/activity_data_canon.py` (Repo-Root) — CSV-Modul `activity` vs. EAV-primär; Migration **057**.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. Produktions-Migrationen (Pflicht)
|
||||||
|
|
||||||
|
- **Nur additive Änderungen** bis zur Stabilisierung: neue Tabellen/Spalten **nullable**, kein `DROP COLUMN` / `DELETE` von Altbestand in derselben Story.
|
||||||
|
- Neue Migrationen: **`backend/migrations/054_*.sql`** (nächste freie Nummer nach 053 einhalten).
|
||||||
|
- **Prod-Checkliste vor Deploy:**
|
||||||
|
1. Backup / Snapshot der DB.
|
||||||
|
2. Migration auf **Kopie** der Prod-DB laufen lassen; Container-Start (`db_init`) verifizieren.
|
||||||
|
3. Stichprobe: `activity_log`-Zeilen unverändert; neue Tabellen leer oder nur Seed.
|
||||||
|
- **Datenhaltung:** Bestehende Spalten in `activity_log` bleiben **Quelle für Alt-Daten**; EAV (`activity_session_metrics`) ist der **kanonische Ort für konfigurierte Session-Metriken**, sobald geschrieben. Backfill Altspalten → EAV ist **separater Schritt** (siehe §6).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. Datenmodell (Ist nach Migration 054)
|
||||||
|
|
||||||
|
| Tabelle | Zweck |
|
||||||
|
|---------|--------|
|
||||||
|
| `training_parameters` | Katalog messbarer Größen (`key`, `data_type`, `unit`, `validation_rules`, …) – bereits Migration 013; Admin-API ergänzt. |
|
||||||
|
| `training_category_parameter` | Welche Parameter für welche **`training_types.category`** (z. B. `cardio`) gelten: `sort_order`, `required`, `ui_group`. |
|
||||||
|
| `training_type_parameter` | Zusatzparameter oder **Overrides** pro **`training_types.id`**: `sort_order`, `required`, `ui_group` (NULL = von Kategorie erben). |
|
||||||
|
| `activity_session_metrics` | EAV: `(activity_log_id, training_parameter_id)` eindeutig; genau eine Wertspalte `value_num` / `value_int` / `value_text` / `value_bool`. |
|
||||||
|
| `activity_log` | **Neu:** `started_at`, `ended_at` (`TIMESTAMPTZ`, nullable) – für spätere Dedupe/Intervalle; **kein** Pflichtfeld in v1. |
|
||||||
|
|
||||||
|
**Merge-Logik effektives Schema** (Layer 1, eine Funktion):
|
||||||
|
|
||||||
|
1. Kategorie ermitteln: aus Zeile `training_category` oder aus `training_types.category` via `training_type_id`.
|
||||||
|
2. Basis = alle Zeilen `training_category_parameter` für diese Kategorie, Join auf `training_parameters` (aktiv).
|
||||||
|
3. Für jeden Eintrag in `training_type_parameter` zum gewählten Typ: gleiche `training_parameter_id` → Overrides anwenden; nur im Typ vorhanden → anhängen.
|
||||||
|
4. Sortierung: `sort_order` aufsteigend, dann `key`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. Layer 1 – Kanonische Module
|
||||||
|
|
||||||
|
| Modul | Pfad | Aufgabe |
|
||||||
|
|-------|------|---------|
|
||||||
|
| Session-Metriken & Schema | `backend/data_layer/activity_session_metrics.py` | `resolve_activity_attribute_schema`, `fetch_activity_session_metrics`, `replace_activity_session_metrics`, `get_activity_session_logical_unit`, `enrich_sessions_with_metrics`, `merge_column_backed_and_eav_metrics`. |
|
||||||
|
|
||||||
|
**Spalten vs. EAV (Lesepfad):** `merge_column_backed_and_eav_metrics` / `get_activity_session_logical_unit` / `enrich_sessions_with_metrics` werten Parameter mit `source_field` **primär aus `activity_log`** aus; EAV ist Fallback (z. B. Legacy) oder für Parameter ohne Spalte. **Kein** automatischer Spalte→EAV-Schreib-Sync mehr in `run_activity_post_write_hooks` / Import-Hooks (vermeidet Doppelhaltung).
|
||||||
|
|
||||||
|
**Regeln für Agenten:**
|
||||||
|
|
||||||
|
- **Keine** zweite Implementierung derselben Merge- oder Validierungslogik in Routern.
|
||||||
|
- Platzhalter / Charts, die Session-Details brauchen: **nur** diese Layer-1-Helfer erweitern oder aufrufen (z. B. `activity_metrics.get_training_sessions_recent_weeks_data` nutzt `enrich_sessions_with_metrics`).
|
||||||
|
- Router: `get_db`, `get_cursor`, Auth; Business-Validierung delegieren an `activity_session_metrics`.
|
||||||
|
|
||||||
|
**KI-Kontext:** In `training_sessions_recent_json` enthält jedes Element von `session_metrics` neben `key`/`value` die Felder `name_de`, `name_en`, `description_de`, `description_en` (aus dem effektiven Schema). Für nicht selbsterklärende Keys soll im Katalog `training_parameters.description_*` gepflegt werden (Admin). Ergänzend liefert der Platzhalter `{{training_parameters_glossary_md}}` die gesamte aktive Parameter-Legende als Markdown-Tabelle (`get_training_parameters_ki_glossary_data` → `get_training_parameters_glossary_md`).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. API (Ist / geplant)
|
||||||
|
|
||||||
|
### Admin (`require_admin`)
|
||||||
|
|
||||||
|
| Methode | Pfad | Beschreibung |
|
||||||
|
|---------|------|--------------|
|
||||||
|
| GET/POST | `/api/admin/training-parameters` | Katalog lesen / Parameter anlegen |
|
||||||
|
| PUT/DELETE | `/api/admin/training-parameters/{id}` | Aktualisieren / Soft-deaktivieren (`is_active`) |
|
||||||
|
| GET | `/api/admin/training-category-parameters?category=` | Zuordnungen Kategorie |
|
||||||
|
| POST | `/api/admin/training-category-parameters` | Zuordnung anlegen |
|
||||||
|
| DELETE | `/api/admin/training-category-parameters/{id}` | Zuordnung entfernen |
|
||||||
|
| GET | `/api/admin/training-type-parameters?training_type_id=` | Zuordnungen Typ |
|
||||||
|
| POST | `/api/admin/training-type-parameters` | Zuordnung anlegen |
|
||||||
|
| DELETE | `/api/admin/training-type-parameters/{id}` | Zuordnung entfernen |
|
||||||
|
|
||||||
|
Router: `backend/routers/admin_training_parameters.py`, `backend/routers/admin_activity_attribute_profiles.py`.
|
||||||
|
|
||||||
|
### Nutzer (`require_auth`)
|
||||||
|
|
||||||
|
| Methode | Pfad | Beschreibung |
|
||||||
|
|---------|------|--------------|
|
||||||
|
| GET | `/api/activity/{eid}` | Session-Kopf + `schema` + `metrics` (Layer 1) |
|
||||||
|
| PUT | `/api/activity/{eid}/metrics` | **Voller Ersatz** der EAV-Metriken für diese Session (Liste `{parameter_key, value}`) |
|
||||||
|
|
||||||
|
`ActivityEntry` unverändert für bestehende Create/Update-Routen; optionale Erweiterung um `started_at`/`ended_at` in späterem Schritt.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. Agent-Checkliste (nächste Iterationen)
|
||||||
|
|
||||||
|
**Layer 2a (Platzhalter Aktivität):** Abgleich Registry ↔ Resolver ↔ Layer 1 — [`ACTIVITY_LAYER2A_PLACEHOLDER_AUDIT.md`](./ACTIVITY_LAYER2A_PLACEHOLDER_AUDIT.md) (Issue #53). **Schritt 2:** `semantic_contract` / `known_limitations` für dynamische `session_metrics` und Korrektur `trainingstyp_verteilung` in der Registry.
|
||||||
|
|
||||||
|
Siehe **Phasen A–F** in [`ACTIVITY_PRODUCTION_ARCHITECTURE_AND_PHASES.md`](./ACTIVITY_PRODUCTION_ARCHITECTURE_AND_PHASES.md). Kurz:
|
||||||
|
|
||||||
|
- [x] **Phase A:** Kanon-Tabelle (eine Quelle pro Semantik) — [`ACTIVITY_SCALAR_KANON_TABLE.md`](./ACTIVITY_SCALAR_KANON_TABLE.md).
|
||||||
|
- [ ] **Phase B:** Lesepfad Layer 1 härten (Consumer-Audit fortlaufend — siehe `ACTIVITY_PRODUCTION_ARCHITECTURE_AND_PHASES.md` §4 Phase B).
|
||||||
|
- [ ] **Phase C:** Schreibpfad: Doppelhaltung / Sync stufenweise abschalten.
|
||||||
|
- [ ] **Phase D:** Composite-MVP (ein Archetyp E2E).
|
||||||
|
- [ ] **Phase E:** Archetypen ausbauen + CSV-Typkonvertierung vollständig + Mapping-UX.
|
||||||
|
- [ ] **Phase F:** Härtung Prod (Indizes, Observability, Doku).
|
||||||
|
|
||||||
|
Legacy-Punkte:
|
||||||
|
|
||||||
|
- [x] Admin-UI: `frontend/src/pages/AdminActivityAttributeProfilesPage.jsx`, Route `/admin/activity-attribute-profiles`, Admin-Nav-Gruppe „Trainingstypen“.
|
||||||
|
- [x] `/activity` Frontend: Bearbeiten lädt `GET /api/activity/{id}`, dynamische Felder + `PUT /api/activity/{id}/metrics`.
|
||||||
|
- [ ] Universal CSV: Mapping inkl. EAV/Composite-Ziele + Executor (fortlaufend).
|
||||||
|
- [ ] Optional: Backfill / Abschluss `source_field`-Pfad nach Kanon (Phase A/C).
|
||||||
|
- [ ] Dedupe Polar/Apple: nach stabilen `started_at`/`ended_at` + Policy (eigenes Issue).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. Backfill (nicht in Migration 054)
|
||||||
|
|
||||||
|
Separates Skript oder Migration **055+**, wenn fachlich freigegeben:
|
||||||
|
|
||||||
|
- Pro aktivem `training_parameter` mit gesetztem `source_field`: Wert aus `activity_log` lesen, in EAV schreiben, wenn noch keine Zeile existiert.
|
||||||
|
- Idempotent (`ON CONFLICT DO NOTHING` oder Upsert-Regel dokumentieren).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. Automatische Tests (pytest, ohne DB)
|
||||||
|
|
||||||
|
Aus **`backend/`**:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python -m pytest tests/test_activity_session_metrics.py -v
|
||||||
|
```
|
||||||
|
|
||||||
|
Abdeckung: reine Merge-Logik (`merge_parameter_schema_rows`), Validierung (`_validate_single_value`), `resolve_activity_attribute_schema` mit Mock-Cursor, `enrich_sessions_with_metrics` mit Mock-Cursor.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8. Referenzen
|
||||||
|
|
||||||
|
- Migration 013: `training_parameters`
|
||||||
|
- Migration 004/014: `training_types`, `activity_log`-Erweiterungen
|
||||||
|
- Pattern Admin-Katalog: `routers/admin_reference_value_types.py`
|
||||||
|
- Platzhalter Session-JSON: `data_layer/activity_metrics.py` → `get_training_sessions_recent_weeks_data`
|
||||||
|
- KI-Legende: `get_training_parameters_ki_glossary_data`, Platzhalter `{{training_parameters_glossary_md}}`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Version:** 1.1 · Bei Schema- oder API-Änderungen dieses Dokument und ggf. `CLAUDE.md` Kurzverweis aktualisieren.
|
||||||
@@ -92,16 +92,10 @@ registry = get_registry()
|
|||||||
|
|
||||||
**Package:** `backend/placeholder_registrations/`
|
**Package:** `backend/placeholder_registrations/`
|
||||||
|
|
||||||
**Struktur:**
|
**Struktur:** Vollständige Cluster-Module (u. a. Ernährung, Körper, Aktivität, Schlaf,
|
||||||
```
|
Vitalwerte, Profil/Zeitraum, Phase-0b-Ziele, Korrelationen); siehe `__init__.py` für die
|
||||||
placeholder_registrations/
|
Import-Liste. **Anzahl:** 114 Platzhalter, identisch zu `PLACEHOLDER_MAP` in
|
||||||
├── __init__.py # Auto-Import aller Registrations
|
`placeholder_resolver.py`.
|
||||||
├── nutrition_part_a.py # Nutrition Basis-Metriken (4 Placeholder)
|
|
||||||
├── nutrition_part_b.py # Protein-Ziele (5 Placeholder) - TODO
|
|
||||||
├── body_metrics.py # Körper-Metriken - TODO
|
|
||||||
├── activity_metrics.py # Aktivitäts-Metriken - TODO
|
|
||||||
└── ... # Weitere Cluster
|
|
||||||
```
|
|
||||||
|
|
||||||
**Auto-Registration:**
|
**Auto-Registration:**
|
||||||
- Import des Package triggert automatische Registrierung aller Placeholder
|
- Import des Package triggert automatische Registrierung aller Placeholder
|
||||||
|
|||||||
File diff suppressed because it is too large.
Load diff
@@ -5,7 +5,9 @@ Folgende Ergebnisse des Tests:
|
|||||||
- In der automatischen Zusammenfassung in der Endnode kommt als Überschrift, z.B. Node 10, anstatt den Node-Name auszugeben.
|
- In der automatischen Zusammenfassung in der Endnode kommt als Überschrift, z.B. Node 10, anstatt den Node-Name auszugeben.
|
||||||
- Alle Änderungen an Nodes scheinen automatisch in den Gesamtflow übernommen zu werden. Diese werden dann nach dem Speichern aktiv. Da muss man sehr vorsichtig sein, bei kurzen Änderungen und dem Ausprobieren.
|
- Alle Änderungen an Nodes scheinen automatisch in den Gesamtflow übernommen zu werden. Diese werden dann nach dem Speichern aktiv. Da muss man sehr vorsichtig sein, bei kurzen Änderungen und dem Ausprobieren.
|
||||||
- Der Testlauf "Execute" sollte auf dem aktuellen Workflowstand ausgeführt werden, auch wenn dieser vom gespeicherten Abweicht. Ich würde natürlich vor dem Speichern den Workflow testen können. Prüfe und bewerte diesen Punkt, setze ihn aber noch nicht um.
|
- Der Testlauf "Execute" sollte auf dem aktuellen Workflowstand ausgeführt werden, auch wenn dieser vom gespeicherten Abweicht. Ich würde natürlich vor dem Speichern den Workflow testen können. Prüfe und bewerte diesen Punkt, setze ihn aber noch nicht um.
|
||||||
- Die Workflows werden aktuell nicht in Analyse und den verfügbaren KI-Asuwertungen angezeigt. ggf. weil wir sie aktuell noch keinem Bereich zuordnen können. Diesen könnten wir ggf. über die Start-Node im Workflow konfigurieren.
|
|
||||||
- Das löschen von Knoten und Kanten funktioniert aktuell nur über Backspace nicht über entfernen
|
- Das löschen von Knoten und Kanten funktioniert aktuell nur über Backspace nicht über entfernen
|
||||||
- Wir sollten auch dafür sorgen, dass jeweils nur eine Start-Node, End-Node in einem Workflow existiert, Prüfe ob mehrere End-Nodes sinnvoll sind, da wir ja auch Logik-Pfade abbilden und ggf. auch eine route beschreiten, die ein anderes Ende hat. (Prüfe, ob das heute schon möglich wäre!)
|
- Wir sollten auch dafür sorgen, dass jeweils nur eine Start-Node, End-Node in einem Workflow existiert, Prüfe ob mehrere End-Nodes sinnvoll sind, da wir ja auch Logik-Pfade abbilden und ggf. auch eine route beschreiten, die ein anderes Ende hat. (Prüfe, ob das heute schon möglich wäre!)
|
||||||
- Als zukünftige Ausbaustufe sollten wir überlegen, ob wir auch Trigger implementieren, z.B. um Kurzstatements zu generieren, wenn neue Daten hereinkommen und wir diese Bewertungen aktualisieren wollen
|
- Als zukünftige Ausbaustufe sollten wir überlegen, ob wir auch Trigger implementieren, z.B. um Kurzstatements zu generieren, wenn neue Daten hereinkommen und wir diese Bewertungen aktualisieren wollen
|
||||||
|
- Exportieren aller KI-Prompts/Templates/Workflows im Admin --> KI-Prompts führt zu einem "internal Server Error", Importieren konnte daraufhin nicht getestet werden
|
||||||
|
- Das duplizieren von Workflows funktioniert nicht
|
||||||
|
-
|
||||||
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
## Gesamt-Übersicht
|
## Gesamt-Übersicht
|
||||||
|
|
||||||
**Aktuelle Platzhalter:** 116
|
**Aktuelle Platzhalter:** 114 (PLACEHOLDER_MAP / Registry)
|
||||||
**Nach Phase 0c Migration:**
|
**Nach Phase 0c Migration:**
|
||||||
- ✅ **Bleiben einfach (kein Data Layer):** 8 Platzhalter
|
- ✅ **Bleiben einfach (kein Data Layer):** 8 Platzhalter
|
||||||
- 🔄 **Gehen zu Data Layer:** 108 Platzhalter
|
- 🔄 **Gehen zu Data Layer:** 108 Platzhalter
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
> | **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-Lab-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`** |
|
||||||
> | **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`** |
|
||||||
|
|
||||||
## Claude Code Verantwortlichkeiten
|
## Claude Code Verantwortlichkeiten
|
||||||
|
|
||||||
@@ -105,6 +106,37 @@ frontend/src/
|
|||||||
- **Regeln:** Verweise in `.claude/rules/ARCHITECTURE.md` (§3.2 `source`), `.claude/rules/CODING_RULES.md` (§6)
|
- **Regeln:** Verweise in `.claude/rules/ARCHITECTURE.md` (§3.2 `source`), `.claude/rules/CODING_RULES.md` (§6)
|
||||||
- **Follow-ups:** **Gitea #71** – Dry-Run inkl. `import_row_processing`, Nutzer-Mapping-Validierung, Fehler-Hints in der Import-UI ([Issue](http://192.168.2.144:3000/Lars/mitai-jinkendo/issues/71))
|
- **Follow-ups:** **Gitea #71** – Dry-Run inkl. `import_row_processing`, Nutzer-Mapping-Validierung, Fehler-Hints in der Import-UI ([Issue](http://192.168.2.144:3000/Lars/mitai-jinkendo/issues/71))
|
||||||
|
|
||||||
|
### Updates (11.04.2026 - Placeholder Phase A)
|
||||||
|
|
||||||
|
- **`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).
|
||||||
|
|
||||||
|
### 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
|
||||||
|
- **`nutrition_score`:** Registry in `backend/placeholder_registrations/nutrition_score.py`, Import in `placeholder_registrations/__init__.py`; Legacy-Duplikat unter „Scores“ im Platzhalter-Katalog entfernt.
|
||||||
|
|
||||||
|
### Updates (14.04.2026 - Activity Session Metrics EAV, Kern-Backend)
|
||||||
|
|
||||||
|
- **Agent-Guide:** `.claude/docs/technical/ACTIVITY_SESSION_METRICS_EAV_AGENT_GUIDE.md` (Prod: nur additive Migration **054**; Layer1 `data_layer/activity_session_metrics.py`).
|
||||||
|
- **DB:** `training_category_parameter`, `training_type_parameter`, `activity_session_metrics`; `activity_log.started_at` / `ended_at` (nullable).
|
||||||
|
- **API:** Admin `/api/admin/training-parameters`, `/api/admin/training-category-parameters`, `/api/admin/training-type-parameters`; Nutzer `GET /api/activity/{id}`, `PUT /api/activity/{id}/metrics`; Platzhalter-Pfad `training_sessions_recent_json` liefert pro Session `session_metrics` inkl. `name_*` / `description_*`; **`{{training_parameters_glossary_md}}`** = Markdown-Legende aller aktiven Parameter (KI).
|
||||||
|
- **Frontend:** Admin `/admin/activity-attribute-profiles`; Aktivität → Verlauf → Bearbeiten: Profil-Kennwerte; `api.js` ergänzt.
|
||||||
|
|
||||||
|
### Updates (16.04.2026 - Aktivität Phase A abgeschlossen, Phase B gestartet)
|
||||||
|
|
||||||
|
- **Phase A:** Skalar-Kanon schriftlich fixiert — `.claude/docs/technical/ACTIVITY_SCALAR_KANON_TABLE.md`; `ACTIVITY_PRODUCTION_ARCHITECTURE_AND_PHASES.md` v1.1; Agent-Guide Checkliste Phase A erledigt.
|
||||||
|
- **Phase B:** `GET /api/activity` (Liste) reichert jede Zeile mit `session_metrics` über `enrich_sessions_with_metrics` an (gleiche Merge-Logik wie Detail); Consumer-Audit-Tabelle in Produktions-Architektur-Dok §4 Phase B.
|
||||||
|
- **Phase B (Export):** `routers/exportdata.py` — JSON-Export `activity` mit `session_metrics`; CSV-Gesamtexport Training-Details mit EAV-Zusammenfassung; ZIP `data/activity.csv` mit Zusatzspalte `session_metrics_json` (Standard-Import unverändert).
|
||||||
|
- **Issue #53 / Layer 2a:** `ACTIVITY_LAYER2A_PLACEHOLDER_AUDIT.md` — alle 20 Aktivitäts-Platzhalter gegen Layer 1 geprüft; Registry-Fix `activity_summary.resolver_function` → `get_activity_summary`.
|
||||||
|
- **Layer 2a Schritt 2:** Registry-Texte `activity_detail`, `training_sessions_recent_json` (dynamische session_metrics, Merge-Kanon); `trainingstyp_verteilung` Metadaten an Phase-0c-Code angeglichen.
|
||||||
|
|
||||||
|
### Updates (11.04.2026 - Ernährung: TDEE, Bilanz, Kalorien-Score)
|
||||||
|
|
||||||
|
- **`data_layer/nutrition_metrics.py`:** TDEE für Bilanz: primär **Mifflin–St Jeor BMR × PAL 1,55**, wenn Profil (Größe, Geschlecht, DOB) und Gewicht vorhanden; sonst Fallback **kg × 32,5** (`estimate_tdee_kcal_from_latest_weight`). `get_energy_balance_data` / `calculate_energy_balance_7d` nutzen **tägliche kcal-Summen**. **`_score_calorie_adherence`** (Komponente von `calculate_nutrition_score`) wertet die 7-Tage-Bilanz nach **`profiles.goal_mode`** aus (weight_loss vs. strength/recomposition vs. maintenance/health/endurance).
|
||||||
|
- **`routers/charts.py`:** `/charts/energy-balance` und Protein-Timeline nutzen dieselbe TDEE-/Tageslogik; ohne `weight_log` liefert Energiebilanz-Chart eine klare Fehlermeldung. Adherence-Endpoint: Kcal-CV über **Tages-Summen**.
|
||||||
|
- **Doku:** Normative Platzhalter-Zahl **114** (`docs/PLACEHOLDER_*.md`); `placeholder_metadata_complete.py` als **Legacy** gekennzeichnet — maßgeblich `placeholder_registrations/` + `PLACEHOLDER_REGISTRY_FRAMEWORK.md`.
|
||||||
|
|
||||||
### GUI / Informationsarchitektur (Abnahme dieser Iteration, 2026-04-05)
|
### GUI / Informationsarchitektur (Abnahme dieser Iteration, 2026-04-05)
|
||||||
|
|
||||||
Admin-Bereich (`AdminShell`, Hub-Routen), Hauptnavigation inkl. **Ziele** (`config/appNav.js`), Einstellungen nur aktives Profil + E-Mail, KI-Analyse Ergebnis in rechter Spalte, **PWA** Bottom-Nav inkl. iOS Safe Area. Zentrale Agent-Doku: **`docs/issues/GUI_IA_ADMIN_NAV_2026-04-05.md`**. Responsive-Epic **Gitea #30:** Phasenplan `docs/issues/PHASE_PLAN_RESPONSIVE_UI.md` — **P7 Kern erledigt**, **P8** (Regression/Abnahme) ausstehend; Issue bewusst **nicht** geschlossen.
|
Admin-Bereich (`AdminShell`, Hub-Routen), Hauptnavigation inkl. **Ziele** (`config/appNav.js`), Einstellungen nur aktives Profil + E-Mail, KI-Analyse Ergebnis in rechter Spalte, **PWA** Bottom-Nav inkl. iOS Safe Area. Zentrale Agent-Doku: **`docs/issues/GUI_IA_ADMIN_NAV_2026-04-05.md`**. Responsive-Epic **Gitea #30:** Phasenplan `docs/issues/PHASE_PLAN_RESPONSIVE_UI.md` — **P7 Kern erledigt**, **P8** (Regression/Abnahme) ausstehend; Issue bewusst **nicht** geschlossen.
|
||||||
|
|||||||
+17
-3
@@ -13,6 +13,8 @@ import bcrypt
|
|||||||
|
|
||||||
from db import get_db, get_cursor
|
from db import get_db, get_cursor
|
||||||
|
|
||||||
|
print("[AUTH.PY] Module loaded - require_auth_flexible will be defined")
|
||||||
|
|
||||||
|
|
||||||
def hash_pin(pin: str) -> str:
|
def hash_pin(pin: str) -> str:
|
||||||
"""Hash password with bcrypt. Falls back gracefully from legacy SHA256."""
|
"""Hash password with bcrypt. Falls back gracefully from legacy SHA256."""
|
||||||
@@ -76,21 +78,33 @@ def require_auth(x_auth_token: Optional[str] = Header(default=None)):
|
|||||||
return session
|
return session
|
||||||
|
|
||||||
|
|
||||||
def require_auth_flexible(x_auth_token: Optional[str] = Header(default=None), token: Optional[str] = Query(default=None)):
|
def require_auth_flexible(x_auth_token: Optional[str] = Header(default=None), ssetoken: Optional[str] = Query(default=None)):
|
||||||
|
print("[AUTH_FLEX] FUNCTION CALLED!") # FIRST LINE - should always print
|
||||||
"""
|
"""
|
||||||
FastAPI dependency - auth via header OR query parameter.
|
FastAPI dependency - auth via header OR query parameter.
|
||||||
|
|
||||||
Used for endpoints accessed by <img> tags that can't send headers.
|
Used for endpoints accessed by <img> tags and SSE connections that can't send headers.
|
||||||
|
Query parameter is 'ssetoken' to avoid conflicts with endpoint 'token' parameters.
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
@app.get("/api/photos/{id}")
|
@app.get("/api/photos/{id}")
|
||||||
def get_photo(id: str, session: dict = Depends(require_auth_flexible)):
|
def get_photo(id: str, session: dict = Depends(require_auth_flexible)):
|
||||||
...
|
...
|
||||||
|
|
||||||
|
Call with: ?ssetoken=XXX or Header: X-Auth-Token: XXX
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
HTTPException 401 if not authenticated
|
HTTPException 401 if not authenticated
|
||||||
"""
|
"""
|
||||||
session = get_session(x_auth_token or token)
|
import logging
|
||||||
|
logger = logging.getLogger("uvicorn")
|
||||||
|
print(f"[AUTH_FLEX_DEBUG] header={repr(x_auth_token)}, query={repr(ssetoken)}")
|
||||||
|
logger.warning(f"[AUTH_FLEX_DEBUG] header={repr(x_auth_token)}, query={repr(ssetoken)}")
|
||||||
|
|
||||||
|
session = get_session(x_auth_token or ssetoken)
|
||||||
|
print(f"[AUTH_FLEX_DEBUG] session={repr(session)}")
|
||||||
|
logger.warning(f"[AUTH_FLEX_DEBUG] session={repr(session)}")
|
||||||
|
|
||||||
if not session:
|
if not session:
|
||||||
raise HTTPException(401, "Nicht eingeloggt")
|
raise HTTPException(401, "Nicht eingeloggt")
|
||||||
return session
|
return session
|
||||||
|
|||||||
@@ -509,17 +509,24 @@ def calculate_sleep_quality_7d(profile_id: str) -> Optional[int]:
|
|||||||
|
|
||||||
quality_scores = []
|
quality_scores = []
|
||||||
for s in sleep_data:
|
for s in sleep_data:
|
||||||
if s['deep_minutes'] and s['rem_minutes']:
|
dur = s["duration_minutes"]
|
||||||
quality_pct = ((s['deep_minutes'] + s['rem_minutes']) / s['duration_minutes']) * 100
|
if not dur or dur <= 0:
|
||||||
# 40-60% deep+REM is good
|
continue
|
||||||
if quality_pct >= 45:
|
d = s["deep_minutes"]
|
||||||
quality_scores.append(100)
|
r = s["rem_minutes"]
|
||||||
elif quality_pct >= 35:
|
if d is None and r is None:
|
||||||
quality_scores.append(75)
|
continue
|
||||||
elif quality_pct >= 25:
|
di, ri = (d or 0), (r or 0)
|
||||||
quality_scores.append(50)
|
quality_pct = ((di + ri) / dur) * 100
|
||||||
else:
|
# 40-60% deep+REM is good
|
||||||
quality_scores.append(30)
|
if quality_pct >= 45:
|
||||||
|
quality_scores.append(100)
|
||||||
|
elif quality_pct >= 35:
|
||||||
|
quality_scores.append(75)
|
||||||
|
elif quality_pct >= 25:
|
||||||
|
quality_scores.append(50)
|
||||||
|
else:
|
||||||
|
quality_scores.append(30)
|
||||||
|
|
||||||
if not quality_scores:
|
if not quality_scores:
|
||||||
return None
|
return None
|
||||||
|
|||||||
+22
-10
@@ -57,6 +57,18 @@ def _split_first_lines(text: str, max_lines: int = 5) -> List[str]:
|
|||||||
return lines
|
return lines
|
||||||
|
|
||||||
|
|
||||||
|
def canonical_csv_header_label(name: str | None) -> str:
|
||||||
|
"""
|
||||||
|
Einheitlicher Spalten-Key für Analyse (Vorlage/Dialog), Import und Signatur.
|
||||||
|
BOM und NBSP (häufig in Excel/Apple-Exporten) werden vereinheitlicht, damit
|
||||||
|
field_mappings exakt zu DictReader-Zeilen passt.
|
||||||
|
"""
|
||||||
|
if name is None:
|
||||||
|
return ""
|
||||||
|
s = str(name).replace("\ufeff", "").replace("\u00a0", " ").strip()
|
||||||
|
return s
|
||||||
|
|
||||||
|
|
||||||
def parse_csv_sample(
|
def parse_csv_sample(
|
||||||
text: str,
|
text: str,
|
||||||
delimiter: str | None = None,
|
delimiter: str | None = None,
|
||||||
@@ -85,7 +97,7 @@ def parse_csv_sample(
|
|||||||
return [], [], delim
|
return [], [], delim
|
||||||
|
|
||||||
if has_header:
|
if has_header:
|
||||||
headers = [h.strip() for h in rows_raw[0]]
|
headers = [canonical_csv_header_label(h) for h in rows_raw[0]]
|
||||||
data = rows_raw[1 : 1 + max_data_rows]
|
data = rows_raw[1 : 1 + max_data_rows]
|
||||||
else:
|
else:
|
||||||
n = len(rows_raw[0])
|
n = len(rows_raw[0])
|
||||||
@@ -103,7 +115,7 @@ def parse_csv_sample(
|
|||||||
|
|
||||||
|
|
||||||
def normalize_header_for_signature(name: str) -> str:
|
def normalize_header_for_signature(name: str) -> str:
|
||||||
s = name.strip().lower()
|
s = canonical_csv_header_label(name).lower()
|
||||||
s = re.sub(r"\s+", "_", s)
|
s = re.sub(r"\s+", "_", s)
|
||||||
s = re.sub(r"[^a-z0-9_äöüß().%-]+", "_", s)
|
s = re.sub(r"[^a-z0-9_äöüß().%-]+", "_", s)
|
||||||
return s.strip("_")
|
return s.strip("_")
|
||||||
@@ -111,7 +123,9 @@ def normalize_header_for_signature(name: str) -> str:
|
|||||||
|
|
||||||
def column_signature(headers: List[str]) -> List[str]:
|
def column_signature(headers: List[str]) -> List[str]:
|
||||||
"""Sortierte normalisierte Spaltennamen für Signatur-Vergleich."""
|
"""Sortierte normalisierte Spaltennamen für Signatur-Vergleich."""
|
||||||
return sorted({normalize_header_for_signature(h) for h in headers if h is not None and str(h).strip()})
|
return sorted(
|
||||||
|
{normalize_header_for_signature(h) for h in headers if h is not None and canonical_csv_header_label(str(h))}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def headers_signature_match_score(sig_csv: List[str], sig_template: List[str]) -> float:
|
def headers_signature_match_score(sig_csv: List[str], sig_template: List[str]) -> float:
|
||||||
@@ -178,12 +192,6 @@ def get_csv_import_limits(conn_row: dict | None) -> dict[str, int]:
|
|||||||
return defaults
|
return defaults
|
||||||
|
|
||||||
|
|
||||||
def _strip_header_key(k: str | None) -> str:
|
|
||||||
if k is None:
|
|
||||||
return ""
|
|
||||||
return str(k).strip().removeprefix("\ufeff")
|
|
||||||
|
|
||||||
|
|
||||||
def iter_csv_dict_rows(
|
def iter_csv_dict_rows(
|
||||||
text: str,
|
text: str,
|
||||||
delimiter: str,
|
delimiter: str,
|
||||||
@@ -205,4 +213,8 @@ def iter_csv_dict_rows(
|
|||||||
continue
|
continue
|
||||||
if not any(v and str(v).strip() for v in row.values()):
|
if not any(v and str(v).strip() for v in row.values()):
|
||||||
continue
|
continue
|
||||||
yield {_strip_header_key(k): (v or "").strip() for k, v in row.items() if _strip_header_key(k)}
|
yield {
|
||||||
|
canonical_csv_header_label(k): (v or "").strip()
|
||||||
|
for k, v in row.items()
|
||||||
|
if canonical_csv_header_label(k)
|
||||||
|
}
|
||||||
+77
-107
@@ -23,14 +23,6 @@ from csv_parser.type_converter import build_row_after_mapping
|
|||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
try:
|
|
||||||
from evaluation_helper import evaluate_and_save_activity as _evaluate_and_save_activity
|
|
||||||
|
|
||||||
_EVALUATION_AVAILABLE = True
|
|
||||||
except Exception: # pragma: no cover
|
|
||||||
_evaluate_and_save_activity = None
|
|
||||||
_EVALUATION_AVAILABLE = False
|
|
||||||
|
|
||||||
|
|
||||||
def _resolve_training_type_for_activity(cur, activity_type: str, profile_id: str):
|
def _resolve_training_type_for_activity(cur, activity_type: str, profile_id: str):
|
||||||
"""Lazy import — gleicher DB-Cursor wie der Import (kein verschachteltes get_db / Pool-Deadlock)."""
|
"""Lazy import — gleicher DB-Cursor wie der Import (kein verschachteltes get_db / Pool-Deadlock)."""
|
||||||
@@ -814,6 +806,17 @@ def _import_activity(
|
|||||||
error_details: list,
|
error_details: list,
|
||||||
affected_ids: dict,
|
affected_ids: dict,
|
||||||
) -> dict[str, int]:
|
) -> dict[str, int]:
|
||||||
|
from data_layer.activity_time_normalize import normalize_activity_start
|
||||||
|
from data_layer.activity_persistence_orchestrator import (
|
||||||
|
activity_csv_registry_updates_from_mapped,
|
||||||
|
find_activity_duplicate_id,
|
||||||
|
insert_activity_csv_minimal,
|
||||||
|
new_activity_id,
|
||||||
|
run_activity_post_write_hooks_import,
|
||||||
|
update_activity_columns,
|
||||||
|
)
|
||||||
|
from data_layer.activity_session_metrics import upsert_session_metrics_from_csv_mapped
|
||||||
|
|
||||||
rows_total = 0
|
rows_total = 0
|
||||||
inserted = 0
|
inserted = 0
|
||||||
updated = 0
|
updated = 0
|
||||||
@@ -885,6 +888,7 @@ def _import_activity(
|
|||||||
|
|
||||||
wtype = str(activity_type).strip()
|
wtype = str(activity_type).strip()
|
||||||
iso = date_d.isoformat()
|
iso = date_d.isoformat()
|
||||||
|
_, workout_start_t = normalize_activity_start(start_key)
|
||||||
|
|
||||||
# Pro Zeile: bei SQL-Fehler sonst „current transaction is aborted“ bis Xact-Ende.
|
# Pro Zeile: bei SQL-Fehler sonst „current transaction is aborted“ bis Xact-Ende.
|
||||||
cur.execute("SAVEPOINT csv_activity_row")
|
cur.execute("SAVEPOINT csv_activity_row")
|
||||||
@@ -892,113 +896,79 @@ def _import_activity(
|
|||||||
training_type_id, training_category, training_subcategory = _resolve_training_type_for_activity(
|
training_type_id, training_category, training_subcategory = _resolve_training_type_for_activity(
|
||||||
cur, wtype, profile_id
|
cur, wtype, profile_id
|
||||||
)
|
)
|
||||||
cur.execute(
|
registry_updates = activity_csv_registry_updates_from_mapped(mapped)
|
||||||
"""
|
existing_id = find_activity_duplicate_id(cur, profile_id, iso, workout_start_t)
|
||||||
SELECT id FROM activity_log
|
|
||||||
WHERE profile_id = %s AND date = %s AND start_time = %s
|
|
||||||
""",
|
|
||||||
(profile_id, iso, start_key),
|
|
||||||
)
|
|
||||||
existing = cur.fetchone()
|
|
||||||
|
|
||||||
if existing:
|
if existing_id:
|
||||||
eid = existing["id"]
|
upd = {
|
||||||
cur.execute(
|
"start_time": workout_start_t,
|
||||||
"""
|
"end_time": end_str or None,
|
||||||
UPDATE activity_log
|
"activity_type": wtype,
|
||||||
SET end_time = %s,
|
"duration_min": duration_min,
|
||||||
activity_type = %s,
|
"kcal_active": kcal_a,
|
||||||
duration_min = %s,
|
"kcal_resting": kcal_r,
|
||||||
kcal_active = %s,
|
"hr_avg": hr_a,
|
||||||
kcal_resting = %s,
|
"hr_max": hr_m,
|
||||||
hr_avg = %s,
|
"distance_km": dist,
|
||||||
hr_max = %s,
|
"training_type_id": training_type_id,
|
||||||
distance_km = %s,
|
"training_category": training_category,
|
||||||
training_type_id = %s,
|
"training_subcategory": training_subcategory,
|
||||||
training_category = %s,
|
"source": "csv",
|
||||||
training_subcategory = %s,
|
}
|
||||||
source = 'csv'
|
upd.update(registry_updates)
|
||||||
WHERE id = %s
|
update_activity_columns(cur, profile_id, existing_id, upd)
|
||||||
RETURNING id
|
|
||||||
""",
|
|
||||||
(
|
|
||||||
end_str or None,
|
|
||||||
wtype,
|
|
||||||
duration_min,
|
|
||||||
kcal_a,
|
|
||||||
kcal_r,
|
|
||||||
hr_a,
|
|
||||||
hr_m,
|
|
||||||
dist,
|
|
||||||
training_type_id,
|
|
||||||
training_category,
|
|
||||||
training_subcategory,
|
|
||||||
eid,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
row = cur.fetchone()
|
|
||||||
updated += 1
|
updated += 1
|
||||||
if row and row.get("id"):
|
affected_ids["activity_log"].append(str(existing_id))
|
||||||
affected_ids["activity_log"].append(str(row["id"]))
|
aid = existing_id
|
||||||
aid = eid
|
|
||||||
else:
|
else:
|
||||||
eid = str(uuid.uuid4())
|
eid = new_activity_id()
|
||||||
cur.execute(
|
insert_activity_csv_minimal(
|
||||||
"""
|
cur,
|
||||||
INSERT INTO activity_log (
|
profile_id,
|
||||||
id, profile_id, date, start_time, end_time, activity_type, duration_min,
|
eid,
|
||||||
kcal_active, kcal_resting, hr_avg, hr_max, distance_km,
|
date_iso=iso,
|
||||||
source, training_type_id, training_category, training_subcategory, created
|
start_time=workout_start_t,
|
||||||
)
|
end_time=end_str or None,
|
||||||
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,'csv',%s,%s,%s,CURRENT_TIMESTAMP)
|
activity_type=wtype,
|
||||||
RETURNING id
|
duration_min=duration_min,
|
||||||
""",
|
kcal_active=kcal_a,
|
||||||
(
|
kcal_resting=kcal_r,
|
||||||
eid,
|
hr_avg=hr_a,
|
||||||
profile_id,
|
hr_max=hr_m,
|
||||||
iso,
|
distance_km=dist,
|
||||||
start_key,
|
training_type_id=training_type_id,
|
||||||
end_str or None,
|
training_category=training_category,
|
||||||
wtype,
|
training_subcategory=training_subcategory,
|
||||||
duration_min,
|
source="csv",
|
||||||
kcal_a,
|
|
||||||
kcal_r,
|
|
||||||
hr_a,
|
|
||||||
hr_m,
|
|
||||||
dist,
|
|
||||||
training_type_id,
|
|
||||||
training_category,
|
|
||||||
training_subcategory,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
row = cur.fetchone()
|
|
||||||
inserted += 1
|
inserted += 1
|
||||||
new_entries += 1
|
new_entries += 1
|
||||||
if row and row.get("id"):
|
affected_ids["activity_log"].append(str(eid))
|
||||||
affected_ids["activity_log"].append(str(row["id"]))
|
|
||||||
aid = eid
|
aid = eid
|
||||||
|
if registry_updates:
|
||||||
|
update_activity_columns(cur, profile_id, aid, registry_updates)
|
||||||
|
|
||||||
if _EVALUATION_AVAILABLE and training_type_id and _evaluate_and_save_activity:
|
run_activity_post_write_hooks_import(
|
||||||
try:
|
cur,
|
||||||
activity_dict = {
|
profile_id,
|
||||||
"id": aid,
|
str(aid),
|
||||||
"profile_id": profile_id,
|
workout_date=iso,
|
||||||
"date": iso,
|
training_type_id=training_type_id,
|
||||||
"training_type_id": training_type_id,
|
duration_min=duration_min,
|
||||||
"duration_min": duration_min,
|
hr_avg=hr_a,
|
||||||
"hr_avg": hr_a,
|
hr_max=hr_m,
|
||||||
"hr_max": hr_m,
|
distance_km=dist,
|
||||||
"distance_km": dist,
|
kcal_active=kcal_a,
|
||||||
"kcal_active": kcal_a,
|
kcal_resting=kcal_r,
|
||||||
"kcal_resting": kcal_r,
|
)
|
||||||
"rpe": None,
|
upsert_session_metrics_from_csv_mapped(
|
||||||
"pace_min_per_km": None,
|
cur,
|
||||||
"cadence": None,
|
profile_id,
|
||||||
"elevation_gain": None,
|
str(aid),
|
||||||
}
|
mapped,
|
||||||
_evaluate_and_save_activity(cur, aid, activity_dict, training_type_id, profile_id)
|
training_category,
|
||||||
except Exception as eval_err:
|
training_type_id,
|
||||||
logger.warning("[csv activity] Auto-Eval fehlgeschlagen: %s", eval_err)
|
)
|
||||||
cur.execute("RELEASE SAVEPOINT csv_activity_row")
|
cur.execute("RELEASE SAVEPOINT csv_activity_row")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -37,12 +37,16 @@ def validate_import_row_processing(
|
|||||||
module: str,
|
module: str,
|
||||||
spec: Mapping[str, Any],
|
spec: Mapping[str, Any],
|
||||||
field_mappings: Mapping[str, Any],
|
field_mappings: Mapping[str, Any],
|
||||||
|
cur=None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Wirft ValueError bei ungültiger Konfiguration."""
|
"""Wirft ValueError bei ungültiger Konfiguration."""
|
||||||
mod = get_module_definition(module)
|
mod = get_module_definition(module)
|
||||||
if not mod:
|
if not mod:
|
||||||
raise ValueError(f"Unbekanntes Modul: {module}")
|
raise ValueError(f"Unbekanntes Modul: {module}")
|
||||||
allowed = set(mod.get("fields") or [])
|
allowed = set(mod.get("fields") or [])
|
||||||
|
if module == "activity" and cur is not None:
|
||||||
|
cur.execute("SELECT key FROM training_parameters WHERE is_active = true")
|
||||||
|
allowed.update(str(r["key"]) for r in cur.fetchall())
|
||||||
fm_targets = {str(v) for v in field_mappings.values() if v and v not in ("-", "_skip")}
|
fm_targets = {str(v) for v in field_mappings.values() if v and v not in ("-", "_skip")}
|
||||||
|
|
||||||
group_by = spec.get("group_by") or []
|
group_by = spec.get("group_by") or []
|
||||||
|
|||||||
@@ -127,13 +127,19 @@ def _match_seed_to_db_field(header: str, seed_fm: Mapping[str, str]) -> str | No
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def _alias_suggest(norm: str, module: str, used: set[str]) -> str | None:
|
def _alias_suggest(
|
||||||
|
norm: str,
|
||||||
|
module: str,
|
||||||
|
used: set[str],
|
||||||
|
*,
|
||||||
|
field_order: list[str] | None = None,
|
||||||
|
) -> str | None:
|
||||||
aliases = _MODULE_HEADER_ALIASES.get(module, {})
|
aliases = _MODULE_HEADER_ALIASES.get(module, {})
|
||||||
mod = get_module_definition(module)
|
mod = get_module_definition(module)
|
||||||
if not mod:
|
if not mod:
|
||||||
return None
|
return None
|
||||||
field_order = list(mod["fields"].keys())
|
order = field_order if field_order is not None else list(mod["fields"].keys())
|
||||||
for db_field in field_order:
|
for db_field in order:
|
||||||
if db_field in used:
|
if db_field in used:
|
||||||
continue
|
continue
|
||||||
tokens = aliases.get(db_field, frozenset())
|
tokens = aliases.get(db_field, frozenset())
|
||||||
@@ -152,6 +158,8 @@ def suggest_field_mappings(
|
|||||||
headers: list[str],
|
headers: list[str],
|
||||||
module: str,
|
module: str,
|
||||||
seed_fm: Mapping[str, str] | None = None,
|
seed_fm: Mapping[str, str] | None = None,
|
||||||
|
*,
|
||||||
|
effective_fields: Mapping[str, Any] | None = None,
|
||||||
) -> dict[str, str]:
|
) -> dict[str, str]:
|
||||||
"""
|
"""
|
||||||
Mappt jede CSV-Spalte (Roh-Header als Key) auf DB-Feld oder '-'.
|
Mappt jede CSV-Spalte (Roh-Header als Key) auf DB-Feld oder '-'.
|
||||||
@@ -164,13 +172,16 @@ def suggest_field_mappings(
|
|||||||
if not mod:
|
if not mod:
|
||||||
return {h: "-" for h in headers}
|
return {h: "-" for h in headers}
|
||||||
|
|
||||||
|
fields_map = dict(effective_fields) if effective_fields is not None else dict(mod["fields"])
|
||||||
|
field_order = list(fields_map.keys())
|
||||||
|
|
||||||
fm: dict[str, str] = {h: "-" for h in headers}
|
fm: dict[str, str] = {h: "-" for h in headers}
|
||||||
used: set[str] = set()
|
used: set[str] = set()
|
||||||
|
|
||||||
if seed_fm:
|
if seed_fm:
|
||||||
for h in headers:
|
for h in headers:
|
||||||
db = _match_seed_to_db_field(h, seed_fm)
|
db = _match_seed_to_db_field(h, seed_fm)
|
||||||
if db and db not in used:
|
if db and db not in used and db in fields_map:
|
||||||
fm[h] = db
|
fm[h] = db
|
||||||
used.add(db)
|
used.add(db)
|
||||||
|
|
||||||
@@ -178,7 +189,7 @@ def suggest_field_mappings(
|
|||||||
if fm[h] != "-":
|
if fm[h] != "-":
|
||||||
continue
|
continue
|
||||||
norm = _norm_key(h)
|
norm = _norm_key(h)
|
||||||
db = _alias_suggest(norm, module, used)
|
db = _alias_suggest(norm, module, used, field_order=field_order)
|
||||||
if db:
|
if db:
|
||||||
fm[h] = db
|
fm[h] = db
|
||||||
used.add(db)
|
used.add(db)
|
||||||
@@ -190,6 +201,8 @@ def build_type_conversions_for_mapping(
|
|||||||
module: str,
|
module: str,
|
||||||
field_mappings: Mapping[str, str],
|
field_mappings: Mapping[str, str],
|
||||||
seed_tc: Mapping[str, Any] | None = None,
|
seed_tc: Mapping[str, Any] | None = None,
|
||||||
|
*,
|
||||||
|
effective_fields: Mapping[str, Any] | None = None,
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""type_conversions nur für zugewiesene Zielfelder; Seed überschreibt Defaults."""
|
"""type_conversions nur für zugewiesene Zielfelder; Seed überschreibt Defaults."""
|
||||||
if module == "sleep":
|
if module == "sleep":
|
||||||
@@ -198,6 +211,7 @@ def build_type_conversions_for_mapping(
|
|||||||
defaults = _DEFAULT_TYPE_CONVERSIONS.get(module, {})
|
defaults = _DEFAULT_TYPE_CONVERSIONS.get(module, {})
|
||||||
out: dict[str, Any] = {}
|
out: dict[str, Any] = {}
|
||||||
targets = {v for v in field_mappings.values() if v and v not in ("-", "_skip")}
|
targets = {v for v in field_mappings.values() if v and v not in ("-", "_skip")}
|
||||||
|
field_meta = dict(effective_fields) if effective_fields is not None else None
|
||||||
|
|
||||||
if seed_tc:
|
if seed_tc:
|
||||||
for k, v in seed_tc.items():
|
for k, v in seed_tc.items():
|
||||||
@@ -208,6 +222,20 @@ def build_type_conversions_for_mapping(
|
|||||||
if t not in out and t in defaults:
|
if t not in out and t in defaults:
|
||||||
out[t] = deepcopy(defaults[t])
|
out[t] = deepcopy(defaults[t])
|
||||||
|
|
||||||
|
for t in sorted(targets):
|
||||||
|
if t in out:
|
||||||
|
continue
|
||||||
|
finfo = (field_meta or {}).get(t) if field_meta else None
|
||||||
|
if not finfo:
|
||||||
|
continue
|
||||||
|
typ = finfo.get("type")
|
||||||
|
if typ == "int":
|
||||||
|
out[t] = {"type": "int", "flexible": True}
|
||||||
|
elif typ == "float":
|
||||||
|
out[t] = {"type": "float", "decimal_separator": "auto", "flexible": True}
|
||||||
|
else:
|
||||||
|
out[t] = {"type": "string"}
|
||||||
|
|
||||||
_apply_energy_kj_hint_from_headers(module, field_mappings, out)
|
_apply_energy_kj_hint_from_headers(module, field_mappings, out)
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
|||||||
@@ -34,19 +34,39 @@ MODULE_DEFINITIONS: Dict[str, Dict[str, Any]] = {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
# Kanon: nur Kern/spine + „heiße“ Metriken → activity_log. Erweiterte Parameter → training_parameters / EAV
|
||||||
|
# (siehe backend/data_layer/activity_data_canon.py).
|
||||||
"activity": {
|
"activity": {
|
||||||
"table": "activity_log",
|
"table": "activity_log",
|
||||||
"fields": {
|
"fields": {
|
||||||
"date": {"type": "date", "required": False},
|
"date": {"type": "date", "required": False, "label_de": "Datum"},
|
||||||
"start_time": {"type": "datetime", "required": False},
|
"start_time": {
|
||||||
"end_time": {"type": "datetime", "required": False},
|
"type": "datetime",
|
||||||
"activity_type": {"type": "string", "required": True},
|
"required": False,
|
||||||
"duration_min": {"type": "float", "required": False, "min": 0},
|
"label_de": "Start (Datum/Uhrzeit)",
|
||||||
"kcal_active": {"type": "float", "required": False, "unit": "kcal"},
|
},
|
||||||
"kcal_resting": {"type": "float", "required": False, "unit": "kcal"},
|
"end_time": {"type": "datetime", "required": False, "label_de": "Ende (Datum/Uhrzeit)"},
|
||||||
"distance_km": {"type": "float", "required": False, "unit": "km"},
|
"activity_type": {"type": "string", "required": True, "label_de": "Trainingsart / Workout-Typ"},
|
||||||
"hr_avg": {"type": "float", "required": False, "min": 30, "max": 220},
|
"duration_min": {"type": "float", "required": False, "min": 0, "label_de": "Dauer (Minuten)"},
|
||||||
"hr_max": {"type": "float", "required": False, "min": 30, "max": 220},
|
"kcal_active": {"type": "float", "required": False, "unit": "kcal", "label_de": "Kalorien aktiv"},
|
||||||
|
"kcal_resting": {"type": "float", "required": False, "unit": "kcal", "label_de": "Kalorien Ruhe"},
|
||||||
|
"distance_km": {"type": "float", "required": False, "unit": "km", "label_de": "Distanz (km)"},
|
||||||
|
"hr_avg": {
|
||||||
|
"type": "float",
|
||||||
|
"required": False,
|
||||||
|
"min": 30,
|
||||||
|
"max": 220,
|
||||||
|
"label_de": "Herzfrequenz Ø (bpm)",
|
||||||
|
},
|
||||||
|
"hr_max": {
|
||||||
|
"type": "float",
|
||||||
|
"required": False,
|
||||||
|
"min": 30,
|
||||||
|
"max": 220,
|
||||||
|
"label_de": "Herzfrequenz max (bpm)",
|
||||||
|
},
|
||||||
|
"rpe": {"type": "int", "required": False, "label_de": "RPE (1–10)"},
|
||||||
|
"notes": {"type": "string", "required": False, "label_de": "Notiz"},
|
||||||
},
|
},
|
||||||
"derive_date_from_datetime_field": "start_time",
|
"derive_date_from_datetime_field": "start_time",
|
||||||
"duplicate_key": ["profile_id", "date", "start_time"],
|
"duplicate_key": ["profile_id", "date", "start_time"],
|
||||||
@@ -125,13 +145,16 @@ def list_modules() -> list[str]:
|
|||||||
return sorted(MODULE_DEFINITIONS.keys())
|
return sorted(MODULE_DEFINITIONS.keys())
|
||||||
|
|
||||||
|
|
||||||
def validate_field_mappings(module: str, field_mappings: dict) -> None:
|
def validate_field_mappings(module: str, field_mappings: dict, cur=None) -> None:
|
||||||
"""Wirft ValueError bei unbekanntem Modul oder unbekanntem DB-Feld."""
|
"""Wirft ValueError bei unbekanntem Modul oder unbekanntem DB-Feld."""
|
||||||
mod = get_module_definition(module)
|
mod = get_module_definition(module)
|
||||||
if not mod:
|
if not mod:
|
||||||
raise ValueError(f"Unbekanntes Modul: {module}")
|
raise ValueError(f"Unbekanntes Modul: {module}")
|
||||||
fields = cast(dict, mod["fields"])
|
fields = cast(dict, mod["fields"])
|
||||||
allowed = set(fields.keys())
|
allowed = set(fields.keys())
|
||||||
|
if module == "activity" and cur is not None:
|
||||||
|
cur.execute("SELECT key FROM training_parameters WHERE is_active = true")
|
||||||
|
allowed.update(str(r["key"]) for r in cur.fetchall())
|
||||||
if not allowed:
|
if not allowed:
|
||||||
for _csv_col, db_field in field_mappings.items():
|
for _csv_col, db_field in field_mappings.items():
|
||||||
if db_field not in ("", None, "-", "_skip"):
|
if db_field not in ("", None, "-", "_skip"):
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ from csv_parser.module_registry import (
|
|||||||
validate_field_mappings,
|
validate_field_mappings,
|
||||||
validate_required_field_targets,
|
validate_required_field_targets,
|
||||||
)
|
)
|
||||||
|
from data_layer.activity_persistence_orchestrator import merge_activity_csv_module_fields
|
||||||
|
|
||||||
ALLOWED_SPEC_TYPES = frozenset(
|
ALLOWED_SPEC_TYPES = frozenset(
|
||||||
{"string", "float", "number", "int", "date", "time", "datetime", "duration"}
|
{"string", "float", "number", "int", "date", "time", "datetime", "duration"}
|
||||||
@@ -50,6 +51,8 @@ def validate_csv_template(
|
|||||||
type_conversions: Mapping[str, Any] | None = None,
|
type_conversions: Mapping[str, Any] | None = None,
|
||||||
import_row_processing: Mapping[str, Any] | None = None,
|
import_row_processing: Mapping[str, Any] | None = None,
|
||||||
column_signature: list[str] | None = None,
|
column_signature: list[str] | None = None,
|
||||||
|
*,
|
||||||
|
cur=None,
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
Prüft eine Vorlage ohne Datei-Upload.
|
Prüft eine Vorlage ohne Datei-Upload.
|
||||||
@@ -74,8 +77,12 @@ def validate_csv_template(
|
|||||||
)
|
)
|
||||||
return {"valid": False, "errors": errors, "warnings": warnings}
|
return {"valid": False, "errors": errors, "warnings": warnings}
|
||||||
|
|
||||||
|
field_defs = dict(mod.get("fields") or {})
|
||||||
|
if module == "activity" and cur is not None:
|
||||||
|
field_defs = merge_activity_csv_module_fields(cur, field_defs)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
validate_field_mappings(module, fm)
|
validate_field_mappings(module, fm, cur=cur)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
errors.append(
|
errors.append(
|
||||||
_issue(
|
_issue(
|
||||||
@@ -100,7 +107,7 @@ def validate_csv_template(
|
|||||||
|
|
||||||
if import_row_processing:
|
if import_row_processing:
|
||||||
try:
|
try:
|
||||||
validate_import_row_processing_spec(module, import_row_processing, fm)
|
validate_import_row_processing_spec(module, import_row_processing, fm, cur=cur)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
errors.append(
|
errors.append(
|
||||||
_issue(
|
_issue(
|
||||||
@@ -111,7 +118,6 @@ def validate_csv_template(
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
field_defs = mod.get("fields") or {}
|
|
||||||
for db_field, spec in tc.items():
|
for db_field, spec in tc.items():
|
||||||
if db_field not in field_defs:
|
if db_field not in field_defs:
|
||||||
errors.append(
|
errors.append(
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ from typing import Any, Mapping, Sequence
|
|||||||
|
|
||||||
from dateutil import parser as dateutil_parser
|
from dateutil import parser as dateutil_parser
|
||||||
|
|
||||||
from csv_parser.core import normalize_header_for_signature
|
from csv_parser.core import canonical_csv_header_label, normalize_header_for_signature
|
||||||
from csv_parser.field_units import factor_source_to_canonical
|
from csv_parser.field_units import factor_source_to_canonical
|
||||||
|
|
||||||
# Alias → strptime (JSON in Kleinbuchstaben)
|
# Alias → strptime (JSON in Kleinbuchstaben)
|
||||||
@@ -66,7 +66,13 @@ def _parse_float_auto(s: str) -> float:
|
|||||||
"""
|
"""
|
||||||
Heuristik ohne festes Locale: Punkt/Komma als Tausender vs. Dezimal,
|
Heuristik ohne festes Locale: Punkt/Komma als Tausender vs. Dezimal,
|
||||||
basierend auf der letzten erkannten Trennstelle und Gruppierung.
|
basierend auf der letzten erkannten Trennstelle und Gruppierung.
|
||||||
|
|
||||||
|
Apple Health u. a. liefern berechnete Mittelwerte mit vielen Nachkommastellen
|
||||||
|
(z. B. «96.874937…») und Energie als «596.668904…» — dabei ist der Punkt
|
||||||
|
immer Dezimaltrenner. Früher wurden lange Nachkommateile fälschlich so
|
||||||
|
behandelt, dass der Punkt entfernt wurde (Tausender-Heuristik).
|
||||||
"""
|
"""
|
||||||
|
raw = s
|
||||||
s = _normalize_num_token(s)
|
s = _normalize_num_token(s)
|
||||||
if not s or s in ("-", "—", "–"):
|
if not s or s in ("-", "—", "–"):
|
||||||
raise ValueError("leer")
|
raise ValueError("leer")
|
||||||
@@ -90,18 +96,35 @@ def _parse_float_auto(s: str) -> float:
|
|||||||
s = s.replace(",", "")
|
s = s.replace(",", "")
|
||||||
elif last_comma >= 0:
|
elif last_comma >= 0:
|
||||||
parts = s.split(",")
|
parts = s.split(",")
|
||||||
if len(parts) == 2 and len(parts[1]) <= 2:
|
if len(parts) == 2:
|
||||||
s = parts[0].replace(".", "") + "." + parts[1]
|
left, right = parts[0], parts[1]
|
||||||
elif len(parts) == 2 and len(parts[1]) == 3 and len(parts[0]) <= 3:
|
if not right:
|
||||||
s = parts[0] + parts[1]
|
raise ValueError("leer")
|
||||||
|
left_digits = left.replace(".", "")
|
||||||
|
# Langer Nachkommateil → Dezimalkomma; «1.234,56»-Fälle oben mit Punkt+Komma
|
||||||
|
if len(right) > 3 or len(right) <= 2:
|
||||||
|
s = left_digits + "." + right.replace(".", "")
|
||||||
|
elif len(right) == 3 and len(left_digits) <= 3:
|
||||||
|
s = left_digits + right
|
||||||
|
else:
|
||||||
|
s = left_digits + "." + right.replace(".", "")
|
||||||
else:
|
else:
|
||||||
s = s.replace(",", "")
|
s = s.replace(",", "")
|
||||||
elif last_dot >= 0:
|
elif last_dot >= 0:
|
||||||
parts = s.split(".")
|
parts = s.split(".")
|
||||||
if len(parts) == 2 and len(parts[1]) <= 2:
|
if len(parts) == 2:
|
||||||
s = parts[0].replace(",", "") + "." + parts[1]
|
left, right = parts[0], parts[1]
|
||||||
elif len(parts) == 2 and len(parts[1]) == 3 and len(parts[0]) <= 3:
|
if not right:
|
||||||
s = parts[0] + parts[1]
|
raise ValueError("leer")
|
||||||
|
left_digits = left.replace(",", "")
|
||||||
|
# Genau ein Punkt: viele Nachkommastellen → Apple/US-Dezimalpunkt (nicht „.“ streichen)
|
||||||
|
if len(right) > 3 or len(right) <= 2:
|
||||||
|
s = left_digits + "." + right
|
||||||
|
elif len(right) == 3:
|
||||||
|
if len(left_digits) == 1 and left_digits != "0" and left_digits.isdigit():
|
||||||
|
s = left_digits + right
|
||||||
|
else:
|
||||||
|
s = left_digits + "." + right
|
||||||
elif len(parts) > 2:
|
elif len(parts) > 2:
|
||||||
if len(parts[-1]) <= 2:
|
if len(parts[-1]) <= 2:
|
||||||
s = "".join(parts[:-1]) + "." + parts[-1]
|
s = "".join(parts[:-1]) + "." + parts[-1]
|
||||||
@@ -345,6 +368,18 @@ def _parse_int(raw: str, spec: Mapping[str, Any]) -> int:
|
|||||||
raise ValueError("leer")
|
raise ValueError("leer")
|
||||||
v = int(digits)
|
v = int(digits)
|
||||||
return -v if neg else v
|
return -v if neg else v
|
||||||
|
# Ohne flexible: «108.0» / «96,8» trotzdem als Zahl mit Nachkommastellen
|
||||||
|
s2 = _normalize_num_token(s)
|
||||||
|
if "," in s2 or "." in s2:
|
||||||
|
dec = spec.get("decimal_separator", ".")
|
||||||
|
try:
|
||||||
|
if dec in (None, "auto"):
|
||||||
|
fv = _parse_float_auto(s2)
|
||||||
|
else:
|
||||||
|
fv = _parse_float(raw, str(dec))
|
||||||
|
return int(round(fv))
|
||||||
|
except (ValueError, InvalidOperation):
|
||||||
|
pass
|
||||||
s = re.sub(r"[^\d-]", "", s)
|
s = re.sub(r"[^\d-]", "", s)
|
||||||
if not s:
|
if not s:
|
||||||
raise ValueError("leer")
|
raise ValueError("leer")
|
||||||
@@ -442,7 +477,12 @@ def _lookup_db_field(csv_col: str, field_mappings: Mapping[str, str]) -> str | N
|
|||||||
CSV-Spaltennamen können Roh-Header sein; Vorlagen-Schlüssel oft normalisiert
|
CSV-Spaltennamen können Roh-Header sein; Vorlagen-Schlüssel oft normalisiert
|
||||||
(wie column_signature). Exakter Treffer, dann Schlüssel nach Normalisierung,
|
(wie column_signature). Exakter Treffer, dann Schlüssel nach Normalisierung,
|
||||||
dann Abgleich aller Vorlagen-Keys über deren Normalform.
|
dann Abgleich aller Vorlagen-Keys über deren Normalform.
|
||||||
|
|
||||||
|
Zusätzlich: Präfix-Treffer für lange manuelle Keys (z. B. Apple
|
||||||
|
„Aufgestiegene Höhe (m)“ → ``aufgestiegene_höhe_(m)`` vs. Mapping
|
||||||
|
„aufgestiegene Höhe“ → ``aufgestiegene_höhe``) — gewinnt der längste passende Key.
|
||||||
"""
|
"""
|
||||||
|
csv_col = canonical_csv_header_label(csv_col)
|
||||||
v = field_mappings.get(csv_col)
|
v = field_mappings.get(csv_col)
|
||||||
if v:
|
if v:
|
||||||
return v if v not in ("-", "_skip") else None
|
return v if v not in ("-", "_skip") else None
|
||||||
@@ -453,6 +493,27 @@ def _lookup_db_field(csv_col: str, field_mappings: Mapping[str, str]) -> str | N
|
|||||||
for k, fv in field_mappings.items():
|
for k, fv in field_mappings.items():
|
||||||
if normalize_header_for_signature(str(k)) == norm:
|
if normalize_header_for_signature(str(k)) == norm:
|
||||||
return fv if fv not in ("-", "_skip") else None
|
return fv if fv not in ("-", "_skip") else None
|
||||||
|
|
||||||
|
# Präfix-Match (min. Länge gegen false positives wie „datum“ → „datum_xyz“)
|
||||||
|
best_fv: str | None = None
|
||||||
|
best_nk_len = 0
|
||||||
|
min_prefix = 10
|
||||||
|
for k, fv in field_mappings.items():
|
||||||
|
if not fv or fv in ("-", "_skip"):
|
||||||
|
continue
|
||||||
|
nk = normalize_header_for_signature(str(k))
|
||||||
|
if len(nk) < min_prefix or len(nk) >= len(norm):
|
||||||
|
continue
|
||||||
|
if not norm.startswith(nk):
|
||||||
|
continue
|
||||||
|
boundary = norm[len(nk) : len(nk) + 1]
|
||||||
|
if boundary not in ("", "_", "("):
|
||||||
|
continue
|
||||||
|
if len(nk) > best_nk_len:
|
||||||
|
best_nk_len = len(nk)
|
||||||
|
best_fv = fv
|
||||||
|
if best_fv:
|
||||||
|
return best_fv
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -51,6 +51,9 @@ __all__ = [
|
|||||||
|
|
||||||
# Body Metrics (Basic)
|
# Body Metrics (Basic)
|
||||||
'get_latest_weight_data',
|
'get_latest_weight_data',
|
||||||
|
'get_bmi_data',
|
||||||
|
'get_profile_goal_weight_data',
|
||||||
|
'get_profile_goal_bf_pct_data',
|
||||||
'get_weight_trend_data',
|
'get_weight_trend_data',
|
||||||
'get_body_composition_data',
|
'get_body_composition_data',
|
||||||
'get_circumference_summary_data',
|
'get_circumference_summary_data',
|
||||||
@@ -99,6 +102,9 @@ __all__ = [
|
|||||||
'get_activity_summary_data',
|
'get_activity_summary_data',
|
||||||
'get_activity_detail_data',
|
'get_activity_detail_data',
|
||||||
'get_training_type_distribution_data',
|
'get_training_type_distribution_data',
|
||||||
|
'get_training_frequency_by_type_data',
|
||||||
|
'get_training_inter_session_gap_data',
|
||||||
|
'get_training_sessions_recent_weeks_data',
|
||||||
|
|
||||||
# Activity Metrics (Calculated)
|
# Activity Metrics (Calculated)
|
||||||
'calculate_training_minutes_week',
|
'calculate_training_minutes_week',
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
"""
|
||||||
|
Kanonische Aufteilung activity_log vs. EAV für Aktivitätssessions.
|
||||||
|
|
||||||
|
- **Kern / Mapping-Ziele für activity_log:** ausschließlich die Keys aus
|
||||||
|
``csv_parser.module_registry.MODULE_DEFINITIONS["activity"].fields`` (keine zweite hartcodierte Liste).
|
||||||
|
- **Alle anderen Attribute:** ``training_parameters`` + Attributprofil (Kategorie/Typ) → EAV;
|
||||||
|
Lesefallback für bekannte Legacy-Spalten siehe unten.
|
||||||
|
|
||||||
|
Normative Doku: .claude/docs/technical/ACTIVITY_PRODUCTION_ARCHITECTURE_AND_PHASES.md,
|
||||||
|
ACTIVITY_SCALAR_KANON_TABLE.md
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Dict, Final
|
||||||
|
|
||||||
|
from csv_parser.module_registry import get_module_definition
|
||||||
|
|
||||||
|
|
||||||
|
def get_activity_module_registry_field_keys() -> frozenset[str]:
|
||||||
|
"""Keys des Universal-CSV-Moduls ``activity`` (= feste activity_log-Kernfelder / Mapping-Ziele)."""
|
||||||
|
mod = get_module_definition("activity")
|
||||||
|
if not mod:
|
||||||
|
return frozenset()
|
||||||
|
return frozenset((mod.get("fields") or {}).keys())
|
||||||
|
|
||||||
|
|
||||||
|
# Gleiche Menge wie ``MODULE_DEFINITIONS["activity"].fields`` — zur Laufzeit aus der Registry abgeleitet.
|
||||||
|
ACTIVITY_MODULE_REGISTRY_FIELD_KEYS: Final[frozenset[str]] = get_activity_module_registry_field_keys()
|
||||||
|
|
||||||
|
# Teil-UPDATEs (Import): alle Kernfelder außer ``date`` (Identität / Duplikat-Key).
|
||||||
|
ACTIVITY_LOG_PATCHABLE_COLUMNS: Final[frozenset[str]] = ACTIVITY_MODULE_REGISTRY_FIELD_KEYS - {"date"}
|
||||||
|
|
||||||
|
# Parameter-Keys (training_parameters.key), die primär in EAV geführt werden; source_field nach Migration 057 NULL.
|
||||||
|
# Lesen (Merge): activity_log-Legacy-Spalte schlägt EAV, wenn beide befüllt; sonst EAV.
|
||||||
|
ACTIVITY_EAV_PRIMARY_PARAMETER_KEYS: Final[frozenset[str]] = frozenset(
|
||||||
|
{
|
||||||
|
"min_hr",
|
||||||
|
"pace_min_per_km",
|
||||||
|
"cadence",
|
||||||
|
"avg_power",
|
||||||
|
"elevation_gain",
|
||||||
|
"temperature_celsius",
|
||||||
|
"humidity_percent",
|
||||||
|
"avg_hr_percent",
|
||||||
|
"kcal_per_km",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
# Spaltenname activity_log für Legacy-Merge (Vorrang vor EAV bei gesetztem Spaltenwert).
|
||||||
|
ACTIVITY_LOG_LEGACY_COLUMN_FOR_EAV_PRIMARY_PARAM: Final[Dict[str, str]] = {
|
||||||
|
"min_hr": "hr_min",
|
||||||
|
"pace_min_per_km": "pace_min_per_km",
|
||||||
|
"cadence": "cadence",
|
||||||
|
"avg_power": "avg_power",
|
||||||
|
"elevation_gain": "elevation_gain",
|
||||||
|
"temperature_celsius": "temperature_celsius",
|
||||||
|
"humidity_percent": "humidity_percent",
|
||||||
|
"avg_hr_percent": "avg_hr_percent",
|
||||||
|
"kcal_per_km": "kcal_per_km",
|
||||||
|
}
|
||||||
|
|
||||||
@@ -7,6 +7,10 @@ Functions:
|
|||||||
- get_activity_summary_data(): Count, total duration, calories, averages
|
- get_activity_summary_data(): Count, total duration, calories, averages
|
||||||
- get_activity_detail_data(): Detailed activity log entries
|
- get_activity_detail_data(): Detailed activity log entries
|
||||||
- get_training_type_distribution_data(): Training category percentages
|
- get_training_type_distribution_data(): Training category percentages
|
||||||
|
- get_training_frequency_by_type_data(): Häufigkeit & Intensität pro activity_type
|
||||||
|
- get_training_inter_session_gap_data(): Pausen zwischen Einheiten (Stunden)
|
||||||
|
- get_training_sessions_recent_weeks_data(): Wochen-JSON für KI-Kontext
|
||||||
|
- get_training_parameters_ki_glossary_data(): Parameter-Katalog (Feld, Namen, Beschreibungen) für KI
|
||||||
|
|
||||||
All functions return structured data (dict) without formatting.
|
All functions return structured data (dict) without formatting.
|
||||||
Use placeholder_resolver.py for formatted strings for AI.
|
Use placeholder_resolver.py for formatted strings for AI.
|
||||||
@@ -15,11 +19,12 @@ Phase 0c: Multi-Layer Architecture
|
|||||||
Version: 1.0
|
Version: 1.0
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from typing import Dict, List, Optional
|
from typing import Dict, List, Optional, Any
|
||||||
from datetime import datetime, timedelta, date
|
from datetime import datetime, timedelta, date, time
|
||||||
import statistics
|
import statistics
|
||||||
from db import get_db, get_cursor, r2d
|
from db import get_db, get_cursor, r2d
|
||||||
from data_layer.utils import calculate_confidence, safe_float, safe_int
|
from data_layer.activity_session_metrics import enrich_sessions_with_metrics
|
||||||
|
from data_layer.utils import calculate_confidence, safe_float, safe_int, serialize_dates
|
||||||
|
|
||||||
|
|
||||||
def get_activity_summary_data(
|
def get_activity_summary_data(
|
||||||
@@ -120,7 +125,8 @@ def get_activity_detail_data(
|
|||||||
"duration_min": int,
|
"duration_min": int,
|
||||||
"kcal_active": int,
|
"kcal_active": int,
|
||||||
"hr_avg": int | None,
|
"hr_avg": int | None,
|
||||||
"training_category": str | None
|
"training_category": str | None,
|
||||||
|
"session_metrics": list | None, # EAV (enrich_sessions_with_metrics)
|
||||||
},
|
},
|
||||||
...
|
...
|
||||||
],
|
],
|
||||||
@@ -139,6 +145,7 @@ def get_activity_detail_data(
|
|||||||
|
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"""SELECT
|
"""SELECT
|
||||||
|
id,
|
||||||
date,
|
date,
|
||||||
activity_type,
|
activity_type,
|
||||||
duration_min,
|
duration_min,
|
||||||
@@ -149,7 +156,7 @@ def get_activity_detail_data(
|
|||||||
WHERE profile_id=%s AND date >= %s
|
WHERE profile_id=%s AND date >= %s
|
||||||
ORDER BY date DESC
|
ORDER BY date DESC
|
||||||
LIMIT %s""",
|
LIMIT %s""",
|
||||||
(profile_id, cutoff, limit)
|
(profile_id, cutoff, limit),
|
||||||
)
|
)
|
||||||
rows = cur.fetchall()
|
rows = cur.fetchall()
|
||||||
|
|
||||||
@@ -158,19 +165,24 @@ def get_activity_detail_data(
|
|||||||
"activities": [],
|
"activities": [],
|
||||||
"total_count": 0,
|
"total_count": 0,
|
||||||
"confidence": "insufficient",
|
"confidence": "insufficient",
|
||||||
"days_analyzed": days
|
"days_analyzed": days,
|
||||||
}
|
}
|
||||||
|
|
||||||
activities = []
|
activities = []
|
||||||
for row in rows:
|
for row in rows:
|
||||||
activities.append({
|
activities.append(
|
||||||
"date": row['date'],
|
{
|
||||||
"activity_type": row['activity_type'],
|
"id": str(row["id"]),
|
||||||
"duration_min": safe_int(row['duration_min']),
|
"date": row["date"],
|
||||||
"kcal_active": safe_int(row['kcal_active']),
|
"activity_type": row["activity_type"],
|
||||||
"hr_avg": safe_int(row['hr_avg']) if row.get('hr_avg') else None,
|
"duration_min": safe_int(row["duration_min"]),
|
||||||
"training_category": row.get('training_category')
|
"kcal_active": safe_int(row["kcal_active"]),
|
||||||
})
|
"hr_avg": safe_int(row["hr_avg"]) if row.get("hr_avg") else None,
|
||||||
|
"training_category": row.get("training_category"),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
enrich_sessions_with_metrics(cur, activities)
|
||||||
|
|
||||||
confidence = calculate_confidence(len(activities), days, "general")
|
confidence = calculate_confidence(len(activities), days, "general")
|
||||||
|
|
||||||
@@ -178,7 +190,7 @@ def get_activity_detail_data(
|
|||||||
"activities": activities,
|
"activities": activities,
|
||||||
"total_count": len(activities),
|
"total_count": len(activities),
|
||||||
"confidence": confidence,
|
"confidence": confidence,
|
||||||
"days_analyzed": days
|
"days_analyzed": days,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -601,26 +613,23 @@ def calculate_activity_score(profile_id: str, focus_weights: Optional[Dict] = No
|
|||||||
from data_layer.scores import get_user_focus_weights
|
from data_layer.scores import get_user_focus_weights
|
||||||
focus_weights = get_user_focus_weights(profile_id)
|
focus_weights = get_user_focus_weights(profile_id)
|
||||||
|
|
||||||
# Activity-related focus areas (English keys from DB)
|
# Activity-related focus areas (English keys from DB); Gewichte float (kein Decimal×float)
|
||||||
# Strength training
|
strength = float(focus_weights.get('strength', 0) or 0)
|
||||||
strength = focus_weights.get('strength', 0)
|
strength_endurance = float(focus_weights.get('strength_endurance', 0) or 0)
|
||||||
strength_endurance = focus_weights.get('strength_endurance', 0)
|
power = float(focus_weights.get('power', 0) or 0)
|
||||||
power = focus_weights.get('power', 0)
|
|
||||||
total_strength = strength + strength_endurance + power
|
total_strength = strength + strength_endurance + power
|
||||||
|
|
||||||
# Endurance training
|
aerobic = float(focus_weights.get('aerobic_endurance', 0) or 0)
|
||||||
aerobic = focus_weights.get('aerobic_endurance', 0)
|
anaerobic = float(focus_weights.get('anaerobic_endurance', 0) or 0)
|
||||||
anaerobic = focus_weights.get('anaerobic_endurance', 0)
|
cardiovascular = float(focus_weights.get('cardiovascular_health', 0) or 0)
|
||||||
cardiovascular = focus_weights.get('cardiovascular_health', 0)
|
|
||||||
total_cardio = aerobic + anaerobic + cardiovascular
|
total_cardio = aerobic + anaerobic + cardiovascular
|
||||||
|
|
||||||
# Mobility/Coordination
|
flexibility = float(focus_weights.get('flexibility', 0) or 0)
|
||||||
flexibility = focus_weights.get('flexibility', 0)
|
mobility = float(focus_weights.get('mobility', 0) or 0)
|
||||||
mobility = focus_weights.get('mobility', 0)
|
balance = float(focus_weights.get('balance', 0) or 0)
|
||||||
balance = focus_weights.get('balance', 0)
|
reaction = float(focus_weights.get('reaction', 0) or 0)
|
||||||
reaction = focus_weights.get('reaction', 0)
|
rhythm = float(focus_weights.get('rhythm', 0) or 0)
|
||||||
rhythm = focus_weights.get('rhythm', 0)
|
coordination = float(focus_weights.get('coordination', 0) or 0)
|
||||||
coordination = focus_weights.get('coordination', 0)
|
|
||||||
total_ability = flexibility + mobility + balance + reaction + rhythm + coordination
|
total_ability = flexibility + mobility + balance + reaction + rhythm + coordination
|
||||||
|
|
||||||
total_activity_weight = total_strength + total_cardio + total_ability
|
total_activity_weight = total_strength + total_cardio + total_ability
|
||||||
@@ -671,9 +680,9 @@ def calculate_activity_score(profile_id: str, focus_weights: Optional[Dict] = No
|
|||||||
if not components:
|
if not components:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Weighted average
|
# Weighted average (float: DB-Aggregate können Decimal sein)
|
||||||
total_score = sum(score * weight for _, score, weight in components)
|
total_score = sum(float(score) * float(weight) for _, score, weight in components)
|
||||||
total_weight = sum(weight for _, _, weight in components)
|
total_weight = sum(float(weight) for _, _, weight in components)
|
||||||
|
|
||||||
return int(total_score / total_weight)
|
return int(total_score / total_weight)
|
||||||
|
|
||||||
@@ -725,12 +734,13 @@ def _score_cardio_presence(profile_id: str) -> Optional[int]:
|
|||||||
if not row:
|
if not row:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
cardio_days = row['cardio_days']
|
# psycopg2: SUM() → oft Decimal — vor Mix mit float konvertieren
|
||||||
cardio_minutes = row['cardio_minutes'] or 0
|
cardio_days = int(row['cardio_days'] or 0)
|
||||||
|
cardio_minutes = float(row['cardio_minutes'] or 0)
|
||||||
|
|
||||||
# Target: 3-5 days/week, 150+ minutes
|
# Target: 3-5 days/week, 150+ minutes
|
||||||
day_score = min(100, (cardio_days / 4) * 100)
|
day_score = min(100.0, (cardio_days / 4) * 100)
|
||||||
minute_score = min(100, (cardio_minutes / 150) * 100)
|
minute_score = min(100.0, (cardio_minutes / 150) * 100)
|
||||||
|
|
||||||
return int((day_score + minute_score) / 2)
|
return int((day_score + minute_score) / 2)
|
||||||
|
|
||||||
@@ -904,3 +914,298 @@ def calculate_activity_data_quality(profile_id: str) -> Dict[str, any]:
|
|||||||
"quality": int(quality_score)
|
"quality": int(quality_score)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _session_sort_ts(row: Dict) -> datetime:
|
||||||
|
"""Einheitlicher Zeitstempel für Sortierung und Pausenberechnung."""
|
||||||
|
d = row["date"]
|
||||||
|
if isinstance(d, str):
|
||||||
|
d = datetime.strptime(d[:10], "%Y-%m-%d").date()
|
||||||
|
st = row.get("start_time")
|
||||||
|
if st is None:
|
||||||
|
t = time(12, 0, 0)
|
||||||
|
else:
|
||||||
|
t = st
|
||||||
|
return datetime.combine(d, t)
|
||||||
|
|
||||||
|
|
||||||
|
def get_training_frequency_by_type_data(
|
||||||
|
profile_id: str,
|
||||||
|
days: int = 28,
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Pro activity_type (Roh-Label aus Import/Anzeige): Häufigkeit & Intensitätskennzahlen.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
{
|
||||||
|
"days_analyzed": int,
|
||||||
|
"confidence": str,
|
||||||
|
"by_type": [
|
||||||
|
{
|
||||||
|
"activity_type": str,
|
||||||
|
"session_count": int,
|
||||||
|
"sessions_per_week": float,
|
||||||
|
"avg_duration_min": float | None,
|
||||||
|
"avg_kcal_active": float | None,
|
||||||
|
"avg_hr_avg": float | None,
|
||||||
|
"avg_hr_max": float | None,
|
||||||
|
"avg_rpe": float | None,
|
||||||
|
"avg_kcal_per_min": float | None, # grobe Intensität, wenn kcal & Dauer
|
||||||
|
},
|
||||||
|
...
|
||||||
|
],
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
weeks = max(days / 7.0, 0.01)
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cutoff = (datetime.now() - timedelta(days=days)).strftime("%Y-%m-%d")
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT
|
||||||
|
activity_type,
|
||||||
|
COUNT(*)::int AS session_count,
|
||||||
|
AVG(duration_min)::float AS avg_duration_min,
|
||||||
|
AVG(kcal_active)::float AS avg_kcal_active,
|
||||||
|
AVG(hr_avg)::float AS avg_hr_avg,
|
||||||
|
AVG(hr_max)::float AS avg_hr_max,
|
||||||
|
AVG(rpe)::float AS avg_rpe,
|
||||||
|
SUM(COALESCE(duration_min, 0))::float AS sum_duration,
|
||||||
|
SUM(COALESCE(kcal_active, 0))::float AS sum_kcal
|
||||||
|
FROM activity_log
|
||||||
|
WHERE profile_id = %s AND date >= %s
|
||||||
|
GROUP BY activity_type
|
||||||
|
ORDER BY session_count DESC
|
||||||
|
""",
|
||||||
|
(profile_id, cutoff),
|
||||||
|
)
|
||||||
|
rows = [r2d(r) for r in cur.fetchall()]
|
||||||
|
|
||||||
|
if not rows:
|
||||||
|
return {
|
||||||
|
"days_analyzed": days,
|
||||||
|
"confidence": "insufficient",
|
||||||
|
"by_type": [],
|
||||||
|
}
|
||||||
|
|
||||||
|
by_type = []
|
||||||
|
for r in rows:
|
||||||
|
sc = int(r["session_count"])
|
||||||
|
sum_dur = float(r["sum_duration"] or 0)
|
||||||
|
sum_kcal = float(r["sum_kcal"] or 0)
|
||||||
|
kcal_per_min = (sum_kcal / sum_dur) if sum_dur > 0 else None
|
||||||
|
by_type.append(
|
||||||
|
{
|
||||||
|
"activity_type": r["activity_type"],
|
||||||
|
"session_count": sc,
|
||||||
|
"sessions_per_week": round(sc / weeks, 2),
|
||||||
|
"avg_duration_min": r["avg_duration_min"],
|
||||||
|
"avg_kcal_active": r["avg_kcal_active"],
|
||||||
|
"avg_hr_avg": r["avg_hr_avg"],
|
||||||
|
"avg_hr_max": r["avg_hr_max"],
|
||||||
|
"avg_rpe": r["avg_rpe"],
|
||||||
|
"avg_kcal_per_min": round(kcal_per_min, 2) if kcal_per_min is not None else None,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
total_sessions = sum(x["session_count"] for x in by_type)
|
||||||
|
confidence = calculate_confidence(total_sessions, days, "general")
|
||||||
|
return {
|
||||||
|
"days_analyzed": days,
|
||||||
|
"confidence": confidence,
|
||||||
|
"by_type": by_type,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_training_inter_session_gap_data(
|
||||||
|
profile_id: str,
|
||||||
|
days: int = 28,
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Mittlere/median Pausen zwischen aufeinanderfolgenden Trainingseinheiten (Stunden).
|
||||||
|
|
||||||
|
Sortierung: Datum + start_time (fehlend → 12:00), dann created.
|
||||||
|
"""
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cutoff = (datetime.now() - timedelta(days=days)).strftime("%Y-%m-%d")
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT date, start_time, created
|
||||||
|
FROM activity_log
|
||||||
|
WHERE profile_id = %s AND date >= %s
|
||||||
|
ORDER BY date ASC, start_time ASC NULLS LAST, created ASC
|
||||||
|
""",
|
||||||
|
(profile_id, cutoff),
|
||||||
|
)
|
||||||
|
rows = [r2d(r) for r in cur.fetchall()]
|
||||||
|
|
||||||
|
if len(rows) < 2:
|
||||||
|
return {
|
||||||
|
"days_analyzed": days,
|
||||||
|
"confidence": "insufficient",
|
||||||
|
"gap_hours_median": None,
|
||||||
|
"gap_hours_mean": None,
|
||||||
|
"gap_hours_min": None,
|
||||||
|
"gaps_count": 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
gaps = []
|
||||||
|
prev_ts = None
|
||||||
|
for r in rows:
|
||||||
|
ts = _session_sort_ts(r)
|
||||||
|
if prev_ts is not None:
|
||||||
|
gaps.append((ts - prev_ts).total_seconds() / 3600.0)
|
||||||
|
prev_ts = ts
|
||||||
|
|
||||||
|
if not gaps:
|
||||||
|
return {
|
||||||
|
"days_analyzed": days,
|
||||||
|
"confidence": "insufficient",
|
||||||
|
"gap_hours_median": None,
|
||||||
|
"gap_hours_mean": None,
|
||||||
|
"gap_hours_min": None,
|
||||||
|
"gaps_count": 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
gaps_sorted = sorted(gaps)
|
||||||
|
mid = len(gaps_sorted) // 2
|
||||||
|
median = (
|
||||||
|
gaps_sorted[mid]
|
||||||
|
if len(gaps_sorted) % 2
|
||||||
|
else (gaps_sorted[mid - 1] + gaps_sorted[mid]) / 2.0
|
||||||
|
)
|
||||||
|
confidence = calculate_confidence(len(rows), days, "general")
|
||||||
|
return {
|
||||||
|
"days_analyzed": days,
|
||||||
|
"confidence": confidence,
|
||||||
|
"gap_hours_median": round(median, 1),
|
||||||
|
"gap_hours_mean": round(statistics.mean(gaps), 1),
|
||||||
|
"gap_hours_min": round(min(gaps), 1),
|
||||||
|
"gaps_count": len(gaps),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_training_sessions_recent_weeks_data(
|
||||||
|
profile_id: str,
|
||||||
|
weeks: int = 4,
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Letzte Wochen mit Einzeltrainings für KI-Kontext (Dauer, kcal, HF, Typ).
|
||||||
|
|
||||||
|
weeks: Anzahl zurückliegender ISO-Kalenderwochen (Default 4).
|
||||||
|
"""
|
||||||
|
days = max(weeks * 7, 7)
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cutoff = (datetime.now() - timedelta(days=days)).strftime("%Y-%m-%d")
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT
|
||||||
|
a.id,
|
||||||
|
a.date,
|
||||||
|
a.start_time,
|
||||||
|
a.activity_type,
|
||||||
|
a.training_category,
|
||||||
|
a.duration_min,
|
||||||
|
a.kcal_active,
|
||||||
|
a.hr_avg,
|
||||||
|
a.hr_max,
|
||||||
|
a.rpe,
|
||||||
|
tt.name_de AS training_type_name
|
||||||
|
FROM activity_log a
|
||||||
|
LEFT JOIN training_types tt ON tt.id = a.training_type_id
|
||||||
|
WHERE a.profile_id = %s AND a.date >= %s
|
||||||
|
ORDER BY a.date ASC, a.start_time ASC NULLS LAST, a.created ASC
|
||||||
|
""",
|
||||||
|
(profile_id, cutoff),
|
||||||
|
)
|
||||||
|
rows = [r2d(r) for r in cur.fetchall()]
|
||||||
|
enrich_sessions_with_metrics(cur, rows)
|
||||||
|
|
||||||
|
if not rows:
|
||||||
|
return {
|
||||||
|
"weeks": [],
|
||||||
|
"meta": {
|
||||||
|
"weeks_requested": weeks,
|
||||||
|
"days_loaded": days,
|
||||||
|
"session_count": 0,
|
||||||
|
"confidence": "insufficient",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
by_week: Dict[str, List[Dict]] = {}
|
||||||
|
for r in rows:
|
||||||
|
d = r["date"]
|
||||||
|
if isinstance(d, str):
|
||||||
|
d = datetime.strptime(d[:10], "%Y-%m-%d").date()
|
||||||
|
iso = d.isocalendar()
|
||||||
|
wk = f"{iso.year}-W{iso.week:02d}"
|
||||||
|
if wk not in by_week:
|
||||||
|
by_week[wk] = []
|
||||||
|
dur = r.get("duration_min")
|
||||||
|
dur_f = float(dur) if dur is not None else None
|
||||||
|
kcal = r.get("kcal_active")
|
||||||
|
kcal_f = float(kcal) if kcal is not None else None
|
||||||
|
hr_a = r.get("hr_avg")
|
||||||
|
hr_m = r.get("hr_max")
|
||||||
|
by_week[wk].append(
|
||||||
|
{
|
||||||
|
"id": str(r["id"]),
|
||||||
|
"date": d,
|
||||||
|
"start_time": str(r["start_time"]) if r.get("start_time") is not None else None,
|
||||||
|
"activity_type": r.get("activity_type"),
|
||||||
|
"training_category": r.get("training_category"),
|
||||||
|
"training_type_name": r.get("training_type_name"),
|
||||||
|
"duration_min": dur_f,
|
||||||
|
"kcal_active": kcal_f,
|
||||||
|
"hr_avg": int(hr_a) if hr_a is not None else None,
|
||||||
|
"hr_max": int(hr_m) if hr_m is not None else None,
|
||||||
|
"rpe": int(r["rpe"]) if r.get("rpe") is not None else None,
|
||||||
|
"session_metrics": r.get("session_metrics", []),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
week_keys = sorted(by_week.keys())
|
||||||
|
weeks_out = [{"week_iso": wk, "sessions": by_week[wk]} for wk in week_keys]
|
||||||
|
confidence = calculate_confidence(len(rows), days, "general")
|
||||||
|
return serialize_dates(
|
||||||
|
{
|
||||||
|
"weeks": weeks_out,
|
||||||
|
"meta": {
|
||||||
|
"weeks_requested": weeks,
|
||||||
|
"days_loaded": days,
|
||||||
|
"session_count": len(rows),
|
||||||
|
"confidence": confidence,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_training_parameters_ki_glossary_data(profile_id: str) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Alle aktiven ``training_parameters`` für KI-Kontext (z. B. neben ``training_sessions_recent_json``).
|
||||||
|
|
||||||
|
Enthält technischen key, name_de/name_en, description_de/description_en, data_type, unit, category.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
profile_id: Reserviert für spätere Einschränkung (z. B. nur im Profil vorkommende Keys);
|
||||||
|
aktuell ungenutzt, Signatur bleibt für Platzhalter-Resolver.
|
||||||
|
"""
|
||||||
|
_ = profile_id
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT key, name_de, name_en, description_de, description_en,
|
||||||
|
data_type, unit, category
|
||||||
|
FROM training_parameters
|
||||||
|
WHERE is_active = true
|
||||||
|
ORDER BY category, key
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
rows = [r2d(r) for r in cur.fetchall()]
|
||||||
|
return {
|
||||||
|
"parameters": rows,
|
||||||
|
"meta": {"count": len(rows), "scope": "global_active_catalog"},
|
||||||
|
}
|
||||||
@@ -0,0 +1,406 @@
|
|||||||
|
"""
|
||||||
|
Zentrale Persistenz für activity_log + EAV-Nebenwirkungen (Eval).
|
||||||
|
|
||||||
|
Alle Schreibpfade (REST, Universal-CSV, Legacy-Upload) laufen hier zusammen.
|
||||||
|
|
||||||
|
Feld-Katalog für CSV-Mappings: get_mappable_activity_field_catalog()
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import datetime as dt
|
||||||
|
import logging
|
||||||
|
import uuid
|
||||||
|
from typing import Any, Dict, List, Mapping, Optional
|
||||||
|
|
||||||
|
from models import ActivityEntry
|
||||||
|
|
||||||
|
from csv_parser.module_registry import get_module_definition
|
||||||
|
from data_layer.activity_data_canon import get_activity_module_registry_field_keys
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
try:
|
||||||
|
from evaluation_helper import evaluate_and_save_activity as _evaluate_and_save_activity
|
||||||
|
|
||||||
|
_EVALUATION_AVAILABLE = True
|
||||||
|
except Exception: # pragma: no cover
|
||||||
|
_evaluate_and_save_activity = None
|
||||||
|
_EVALUATION_AVAILABLE = False
|
||||||
|
|
||||||
|
|
||||||
|
def find_activity_duplicate_id(
|
||||||
|
cur,
|
||||||
|
profile_id: str,
|
||||||
|
date_iso: str,
|
||||||
|
start_time: Optional[Any],
|
||||||
|
) -> Optional[str]:
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT id FROM activity_log
|
||||||
|
WHERE profile_id = %s AND date = %s::date
|
||||||
|
AND start_time IS NOT DISTINCT FROM %s::time
|
||||||
|
""",
|
||||||
|
(profile_id, date_iso, start_time),
|
||||||
|
)
|
||||||
|
row = cur.fetchone()
|
||||||
|
return str(row["id"]) if row else None
|
||||||
|
|
||||||
|
|
||||||
|
# Datum/Start/Ende/Typ setzt der CSV-Executor explizit (Normalisierung); nicht aus diesem Patch überschreiben.
|
||||||
|
_ACTIVITY_CSV_REGISTRY_EXCLUDE = frozenset({"date", "start_time", "end_time", "activity_type"})
|
||||||
|
|
||||||
|
|
||||||
|
def activity_registry_field_keys() -> frozenset[str]:
|
||||||
|
"""Gleiche Menge wie ``ACTIVITY_MODULE_REGISTRY_FIELD_KEYS`` (Registry als Single Source)."""
|
||||||
|
return get_activity_module_registry_field_keys()
|
||||||
|
|
||||||
|
|
||||||
|
def activity_csv_registry_updates_from_mapped(mapped: Mapping[str, Any]) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
activity_log-Updates nur aus Modul-Registry-Feldern (Kernspalten).
|
||||||
|
Trainingsparameter-Keys (nur in training_parameters) laufen über EAV, nicht hier.
|
||||||
|
"""
|
||||||
|
mod = get_module_definition("activity")
|
||||||
|
if not mod:
|
||||||
|
return {}
|
||||||
|
fields = mod.get("fields") or {}
|
||||||
|
out: Dict[str, Any] = {}
|
||||||
|
|
||||||
|
def _sf(v: Any) -> float | None:
|
||||||
|
try:
|
||||||
|
if v is None or (isinstance(v, str) and not str(v).strip()):
|
||||||
|
return None
|
||||||
|
return round(float(v), 1)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _si(v: Any) -> int | None:
|
||||||
|
try:
|
||||||
|
if v is None or (isinstance(v, str) and not str(v).strip()):
|
||||||
|
return None
|
||||||
|
return int(round(float(v)))
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _hr(v: Any) -> float | None:
|
||||||
|
x = _sf(v)
|
||||||
|
if x is None or x < 20 or x > 280:
|
||||||
|
return None
|
||||||
|
return x
|
||||||
|
|
||||||
|
for key, spec in fields.items():
|
||||||
|
if key in _ACTIVITY_CSV_REGISTRY_EXCLUDE:
|
||||||
|
continue
|
||||||
|
if key not in mapped:
|
||||||
|
continue
|
||||||
|
raw = mapped[key]
|
||||||
|
if raw is None or raw == "":
|
||||||
|
continue
|
||||||
|
if isinstance(raw, str) and not raw.strip():
|
||||||
|
continue
|
||||||
|
typ = spec.get("type", "string")
|
||||||
|
if typ == "float":
|
||||||
|
v = _hr(raw) if key in ("hr_avg", "hr_max") else _sf(raw)
|
||||||
|
if v is not None:
|
||||||
|
out[key] = v
|
||||||
|
elif typ == "int":
|
||||||
|
v = _si(raw)
|
||||||
|
if v is not None:
|
||||||
|
out[key] = v
|
||||||
|
elif typ == "datetime":
|
||||||
|
if isinstance(raw, dt.datetime):
|
||||||
|
out[key] = raw.strftime("%Y-%m-%d %H:%M:%S")
|
||||||
|
elif isinstance(raw, dt.date):
|
||||||
|
out[key] = f"{raw.isoformat()} 00:00:00"
|
||||||
|
elif isinstance(raw, str) and raw.strip():
|
||||||
|
out[key] = raw.strip()
|
||||||
|
elif typ == "date":
|
||||||
|
if isinstance(raw, dt.date):
|
||||||
|
out[key] = raw.isoformat()
|
||||||
|
elif isinstance(raw, dt.datetime):
|
||||||
|
out[key] = raw.date().isoformat()
|
||||||
|
elif isinstance(raw, str) and raw.strip():
|
||||||
|
out[key] = raw.strip()
|
||||||
|
else:
|
||||||
|
out[key] = str(raw).strip()
|
||||||
|
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def insert_activity_from_entry(cur, profile_id: str, eid: str, e: ActivityEntry) -> None:
|
||||||
|
"""INSERT activity_log aus ActivityEntry (manueller API-Pfad)."""
|
||||||
|
d = e.model_dump()
|
||||||
|
cur.execute(
|
||||||
|
"""INSERT INTO activity_log (id,profile_id,date,start_time,end_time,activity_type,duration_min,kcal_active,kcal_resting,
|
||||||
|
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,
|
||||||
|
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,%s,CURRENT_TIMESTAMP)""",
|
||||||
|
(
|
||||||
|
eid,
|
||||||
|
profile_id,
|
||||||
|
d["date"],
|
||||||
|
d["start_time"],
|
||||||
|
d["end_time"],
|
||||||
|
d["activity_type"],
|
||||||
|
d["duration_min"],
|
||||||
|
d["kcal_active"],
|
||||||
|
d["kcal_resting"],
|
||||||
|
d["hr_avg"],
|
||||||
|
d["hr_max"],
|
||||||
|
d.get("hr_min"),
|
||||||
|
d["distance_km"],
|
||||||
|
d.get("pace_min_per_km"),
|
||||||
|
d.get("cadence"),
|
||||||
|
d.get("avg_power"),
|
||||||
|
d.get("elevation_gain"),
|
||||||
|
d.get("temperature_celsius"),
|
||||||
|
d.get("humidity_percent"),
|
||||||
|
d.get("avg_hr_percent"),
|
||||||
|
d.get("kcal_per_km"),
|
||||||
|
d["rpe"],
|
||||||
|
d["source"],
|
||||||
|
d["notes"],
|
||||||
|
d.get("training_type_id"),
|
||||||
|
d.get("training_category"),
|
||||||
|
d.get("training_subcategory"),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def update_activity_from_entry(cur, profile_id: str, eid: str, e: ActivityEntry) -> None:
|
||||||
|
"""Volles UPDATE aus ActivityEntry (REST PUT)."""
|
||||||
|
d = e.model_dump()
|
||||||
|
cur.execute(
|
||||||
|
f"UPDATE activity_log SET {', '.join(f'{k}=%s' for k in d)} WHERE id=%s AND profile_id=%s",
|
||||||
|
list(d.values()) + [eid, profile_id],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def update_activity_columns(
|
||||||
|
cur,
|
||||||
|
profile_id: str,
|
||||||
|
eid: str,
|
||||||
|
updates: Dict[str, Any],
|
||||||
|
) -> None:
|
||||||
|
"""Teil-UPDATE nur für übergebene Spalten (Importe)."""
|
||||||
|
if not updates:
|
||||||
|
return
|
||||||
|
cols = [f"{k} = %s" for k in updates]
|
||||||
|
vals = list(updates.values()) + [eid, profile_id]
|
||||||
|
cur.execute(
|
||||||
|
f"UPDATE activity_log SET {', '.join(cols)} WHERE id = %s AND profile_id = %s",
|
||||||
|
vals,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def insert_activity_csv_minimal(
|
||||||
|
cur,
|
||||||
|
profile_id: str,
|
||||||
|
eid: str,
|
||||||
|
*,
|
||||||
|
date_iso: str,
|
||||||
|
start_time: Any,
|
||||||
|
end_time: Any,
|
||||||
|
activity_type: str,
|
||||||
|
duration_min: Any,
|
||||||
|
kcal_active: Any,
|
||||||
|
kcal_resting: Any,
|
||||||
|
hr_avg: Any,
|
||||||
|
hr_max: Any,
|
||||||
|
distance_km: Any,
|
||||||
|
training_type_id: Any,
|
||||||
|
training_category: Any,
|
||||||
|
training_subcategory: Any,
|
||||||
|
source: str,
|
||||||
|
) -> None:
|
||||||
|
"""INSERT minimale activity_log-Zeile (Universal-CSV)."""
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO activity_log (
|
||||||
|
id, profile_id, date, start_time, end_time, activity_type, duration_min,
|
||||||
|
kcal_active, kcal_resting, hr_avg, hr_max, distance_km,
|
||||||
|
source, 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,CURRENT_TIMESTAMP)
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
eid,
|
||||||
|
profile_id,
|
||||||
|
date_iso,
|
||||||
|
start_time,
|
||||||
|
end_time,
|
||||||
|
activity_type,
|
||||||
|
duration_min,
|
||||||
|
kcal_active,
|
||||||
|
kcal_resting,
|
||||||
|
hr_avg,
|
||||||
|
hr_max,
|
||||||
|
distance_km,
|
||||||
|
source,
|
||||||
|
training_type_id,
|
||||||
|
training_category,
|
||||||
|
training_subcategory,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def run_activity_post_write_hooks(cur, profile_id: str, eid: str) -> None:
|
||||||
|
"""Auto-Eval (falls aktiv). Kein Spalte→EAV-Sync: Lesepfad merge_column_backed_and_eav_metrics."""
|
||||||
|
if _EVALUATION_AVAILABLE and _evaluate_and_save_activity:
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT id, profile_id, date, training_type_id, duration_min,
|
||||||
|
hr_avg, hr_max, distance_km, kcal_active, kcal_resting,
|
||||||
|
rpe, pace_min_per_km, cadence, elevation_gain
|
||||||
|
FROM activity_log
|
||||||
|
WHERE id = %s
|
||||||
|
""",
|
||||||
|
(eid,),
|
||||||
|
)
|
||||||
|
activity_row = cur.fetchone()
|
||||||
|
if activity_row:
|
||||||
|
activity_dict = dict(activity_row)
|
||||||
|
training_type_id = activity_dict.get("training_type_id")
|
||||||
|
if training_type_id:
|
||||||
|
try:
|
||||||
|
_evaluate_and_save_activity(cur, eid, activity_dict, training_type_id, profile_id)
|
||||||
|
except Exception as eval_error:
|
||||||
|
logger.error("[AUTO-EVAL] activity %s: %s", eid, eval_error)
|
||||||
|
|
||||||
|
|
||||||
|
def run_activity_post_write_hooks_import(
|
||||||
|
cur,
|
||||||
|
profile_id: str,
|
||||||
|
eid: str,
|
||||||
|
*,
|
||||||
|
workout_date: str,
|
||||||
|
training_type_id: Optional[int],
|
||||||
|
duration_min: Any,
|
||||||
|
hr_avg: Any,
|
||||||
|
hr_max: Any,
|
||||||
|
distance_km: Any,
|
||||||
|
kcal_active: Any,
|
||||||
|
kcal_resting: Any,
|
||||||
|
) -> None:
|
||||||
|
"""Auto-Eval nach Import. Kein Spalte→EAV-Sync (siehe run_activity_post_write_hooks)."""
|
||||||
|
if _EVALUATION_AVAILABLE and training_type_id and _evaluate_and_save_activity:
|
||||||
|
try:
|
||||||
|
activity_dict = {
|
||||||
|
"id": eid,
|
||||||
|
"profile_id": profile_id,
|
||||||
|
"date": workout_date,
|
||||||
|
"training_type_id": training_type_id,
|
||||||
|
"duration_min": duration_min,
|
||||||
|
"hr_avg": hr_avg,
|
||||||
|
"hr_max": hr_max,
|
||||||
|
"distance_km": distance_km,
|
||||||
|
"kcal_active": kcal_active,
|
||||||
|
"kcal_resting": kcal_resting,
|
||||||
|
"rpe": None,
|
||||||
|
"pace_min_per_km": None,
|
||||||
|
"cadence": None,
|
||||||
|
"elevation_gain": None,
|
||||||
|
}
|
||||||
|
_evaluate_and_save_activity(cur, eid, activity_dict, training_type_id, profile_id)
|
||||||
|
except Exception as eval_err:
|
||||||
|
logger.warning("[activity import] Auto-Eval fehlgeschlagen: %s", eval_err)
|
||||||
|
|
||||||
|
|
||||||
|
def merge_activity_csv_module_fields(
|
||||||
|
cur,
|
||||||
|
static_fields: Dict[str, Any],
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
activity-Modul für CSV: statische Registry-Felder + alle aktiven training_parameters.
|
||||||
|
|
||||||
|
Gleiche Quelle wie get_mappable_activity_field_catalog.training_parameters — erscheint
|
||||||
|
in Admin-CSV-Ziel-Liste, Validierung und Import-Zeilenaggregation.
|
||||||
|
"""
|
||||||
|
out = dict(static_fields)
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT key, data_type, unit, name_de
|
||||||
|
FROM training_parameters
|
||||||
|
WHERE is_active = true
|
||||||
|
ORDER BY key
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
for row in cur.fetchall():
|
||||||
|
k = row["key"]
|
||||||
|
if k in out:
|
||||||
|
continue
|
||||||
|
dt = row["data_type"] or "float"
|
||||||
|
if dt == "integer":
|
||||||
|
mtype = "int"
|
||||||
|
elif dt == "float":
|
||||||
|
mtype = "float"
|
||||||
|
elif dt == "boolean":
|
||||||
|
mtype = "string"
|
||||||
|
else:
|
||||||
|
mtype = "string"
|
||||||
|
spec: Dict[str, Any] = {
|
||||||
|
"type": mtype,
|
||||||
|
"required": False,
|
||||||
|
"from_training_parameter": True,
|
||||||
|
}
|
||||||
|
if row.get("unit"):
|
||||||
|
spec["unit"] = row["unit"]
|
||||||
|
if row.get("name_de"):
|
||||||
|
spec["label_de"] = row["name_de"]
|
||||||
|
out[k] = spec
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def get_mappable_activity_field_catalog(cur, profile_id: str) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Felder für konfigurierbare Import-Mappings.
|
||||||
|
|
||||||
|
core_fields: module_registry „activity“ → activity_log.
|
||||||
|
training_parameters: alle aktiven Parameter (global); bei Anwendung auf eine Session
|
||||||
|
werden Keys verworfen, die nicht in resolve_activity_attribute_schema(Kategorie/Typ) liegen.
|
||||||
|
|
||||||
|
profile_id: reserviert für künftige Profil-Filter.
|
||||||
|
"""
|
||||||
|
_ = profile_id
|
||||||
|
mod = get_module_definition("activity") or {}
|
||||||
|
fields = mod.get("fields") or {}
|
||||||
|
core_fields: List[Dict[str, Any]] = []
|
||||||
|
for key, spec in fields.items():
|
||||||
|
s = spec or {}
|
||||||
|
core_fields.append(
|
||||||
|
{
|
||||||
|
"key": key,
|
||||||
|
"target": "activity_log",
|
||||||
|
"column": key,
|
||||||
|
"data_type": s.get("type", "string"),
|
||||||
|
"required": bool(s.get("required")),
|
||||||
|
"unit": s.get("unit"),
|
||||||
|
"label_de": s.get("label_de") or key,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
core_fields.sort(key=lambda x: x["key"])
|
||||||
|
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT id, key, name_de, name_en, category AS param_category,
|
||||||
|
data_type, unit, source_field
|
||||||
|
FROM training_parameters
|
||||||
|
WHERE is_active = true
|
||||||
|
ORDER BY key
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
parameters = [dict(r) for r in cur.fetchall()]
|
||||||
|
|
||||||
|
return {
|
||||||
|
"core_fields": core_fields,
|
||||||
|
"training_parameters": parameters,
|
||||||
|
"notes": (
|
||||||
|
"training_parameters listet alle aktiven Keys. Pro Session werden Werte ignoriert, "
|
||||||
|
"die für deren training_category/training_type_id nicht im Attribut-Schema vorkommen."
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def new_activity_id() -> str:
|
||||||
|
return str(uuid.uuid4())
|
||||||
@@ -0,0 +1,755 @@
|
|||||||
|
"""
|
||||||
|
Activity session metrics (EAV) and resolved attribute schema — Layer 1.
|
||||||
|
|
||||||
|
See: .claude/docs/technical/ACTIVITY_SESSION_METRICS_EAV_AGENT_GUIDE.md
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from decimal import Decimal
|
||||||
|
from typing import Any, Dict, List, Mapping, Optional, Sequence
|
||||||
|
|
||||||
|
from data_layer.activity_data_canon import (
|
||||||
|
ACTIVITY_LOG_LEGACY_COLUMN_FOR_EAV_PRIMARY_PARAM,
|
||||||
|
ACTIVITY_MODULE_REGISTRY_FIELD_KEYS,
|
||||||
|
)
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# Diese Spalten nicht aus CSV-Parameter-Zuordnung überschreiben (kommen aus Typ-Mapping / System).
|
||||||
|
ACTIVITY_LOG_PATCH_FORBIDDEN = frozenset(
|
||||||
|
{
|
||||||
|
"id",
|
||||||
|
"profile_id",
|
||||||
|
"date",
|
||||||
|
"created",
|
||||||
|
"training_type_id",
|
||||||
|
"training_category",
|
||||||
|
"training_subcategory",
|
||||||
|
"source",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class ActivitySessionMetricsError(Exception):
|
||||||
|
"""Raised by Layer 1; routers map to HTTP (404/400)."""
|
||||||
|
|
||||||
|
def __init__(self, status_code: int, detail: str):
|
||||||
|
self.status_code = status_code
|
||||||
|
self.detail = detail
|
||||||
|
super().__init__(detail)
|
||||||
|
|
||||||
|
|
||||||
|
def _effective_training_category(
|
||||||
|
cur, training_category: Optional[str], training_type_id: Optional[int]
|
||||||
|
) -> Optional[str]:
|
||||||
|
if training_category:
|
||||||
|
return training_category.strip() or None
|
||||||
|
if training_type_id is None:
|
||||||
|
return None
|
||||||
|
cur.execute("SELECT category FROM training_types WHERE id = %s", (training_type_id,))
|
||||||
|
row = cur.fetchone()
|
||||||
|
if row and row.get("category"):
|
||||||
|
return row["category"]
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def merge_parameter_schema_rows(
|
||||||
|
category_rows: Sequence[Dict[str, Any]],
|
||||||
|
type_rows: Sequence[Dict[str, Any]],
|
||||||
|
) -> List[Dict[str, Any]]:
|
||||||
|
"""
|
||||||
|
Pure merge: category assignments + type assignments → sorted schema list.
|
||||||
|
Row shapes match SELECTs in resolve_activity_attribute_schema (cat_sort / typ_* aliases).
|
||||||
|
"""
|
||||||
|
merged: Dict[int, Dict[str, Any]] = {}
|
||||||
|
|
||||||
|
for r in category_rows:
|
||||||
|
pid = r["training_parameter_id"]
|
||||||
|
merged[pid] = {
|
||||||
|
"training_parameter_id": pid,
|
||||||
|
"key": r["key"],
|
||||||
|
"name_de": r["name_de"],
|
||||||
|
"name_en": r["name_en"],
|
||||||
|
"description_de": r.get("description_de"),
|
||||||
|
"description_en": r.get("description_en"),
|
||||||
|
"param_category": r["param_category"],
|
||||||
|
"data_type": r["data_type"],
|
||||||
|
"unit": r["unit"],
|
||||||
|
"validation_rules": r["validation_rules"] or {},
|
||||||
|
"source_field": r["source_field"],
|
||||||
|
"sort_order": r["cat_sort"],
|
||||||
|
"required": bool(r["cat_required"]),
|
||||||
|
"ui_group": r["cat_ui_group"],
|
||||||
|
}
|
||||||
|
|
||||||
|
for r in type_rows:
|
||||||
|
pid = r["training_parameter_id"]
|
||||||
|
base = merged.get(pid)
|
||||||
|
if base is None:
|
||||||
|
merged[pid] = {
|
||||||
|
"training_parameter_id": pid,
|
||||||
|
"key": r["key"],
|
||||||
|
"name_de": r["name_de"],
|
||||||
|
"name_en": r["name_en"],
|
||||||
|
"description_de": r.get("description_de"),
|
||||||
|
"description_en": r.get("description_en"),
|
||||||
|
"param_category": r["param_category"],
|
||||||
|
"data_type": r["data_type"],
|
||||||
|
"unit": r["unit"],
|
||||||
|
"validation_rules": r["validation_rules"] or {},
|
||||||
|
"source_field": r["source_field"],
|
||||||
|
"sort_order": r["typ_sort"] if r["typ_sort"] is not None else 0,
|
||||||
|
"required": bool(r["typ_required"]) if r["typ_required"] is not None else False,
|
||||||
|
"ui_group": r["typ_ui_group"],
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
if r["typ_sort"] is not None:
|
||||||
|
base["sort_order"] = r["typ_sort"]
|
||||||
|
if r["typ_required"] is not None:
|
||||||
|
base["required"] = bool(r["typ_required"])
|
||||||
|
if r["typ_ui_group"] is not None:
|
||||||
|
base["ui_group"] = r["typ_ui_group"]
|
||||||
|
|
||||||
|
out = list(merged.values())
|
||||||
|
out.sort(key=lambda x: (x["sort_order"], x["key"]))
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_activity_attribute_schema(
|
||||||
|
cur,
|
||||||
|
training_category: Optional[str],
|
||||||
|
training_type_id: Optional[int],
|
||||||
|
) -> List[Dict[str, Any]]:
|
||||||
|
"""
|
||||||
|
Merged parameter definitions for UI / validation (category base + type overrides/additions).
|
||||||
|
Sorted by sort_order, then key.
|
||||||
|
"""
|
||||||
|
cat = _effective_training_category(cur, training_category, training_type_id)
|
||||||
|
category_rows: List[Dict[str, Any]] = []
|
||||||
|
type_rows: List[Dict[str, Any]] = []
|
||||||
|
|
||||||
|
if cat:
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT
|
||||||
|
tcp.training_parameter_id,
|
||||||
|
tcp.sort_order AS cat_sort,
|
||||||
|
tcp.required AS cat_required,
|
||||||
|
tcp.ui_group AS cat_ui_group,
|
||||||
|
tp.key, tp.name_de, tp.name_en,
|
||||||
|
tp.description_de, tp.description_en,
|
||||||
|
tp.category AS param_category,
|
||||||
|
tp.data_type, tp.unit, tp.validation_rules, tp.source_field
|
||||||
|
FROM training_category_parameter tcp
|
||||||
|
JOIN training_parameters tp ON tp.id = tcp.training_parameter_id
|
||||||
|
WHERE tcp.training_category = %s AND tp.is_active = true
|
||||||
|
""",
|
||||||
|
(cat,),
|
||||||
|
)
|
||||||
|
category_rows = list(cur.fetchall())
|
||||||
|
|
||||||
|
if training_type_id is not None:
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT
|
||||||
|
ttp.training_parameter_id,
|
||||||
|
ttp.sort_order AS typ_sort,
|
||||||
|
ttp.required AS typ_required,
|
||||||
|
ttp.ui_group AS typ_ui_group,
|
||||||
|
tp.key, tp.name_de, tp.name_en,
|
||||||
|
tp.description_de, tp.description_en,
|
||||||
|
tp.category AS param_category,
|
||||||
|
tp.data_type, tp.unit, tp.validation_rules, tp.source_field
|
||||||
|
FROM training_type_parameter ttp
|
||||||
|
JOIN training_parameters tp ON tp.id = ttp.training_parameter_id
|
||||||
|
WHERE ttp.training_type_id = %s AND tp.is_active = true
|
||||||
|
""",
|
||||||
|
(training_type_id,),
|
||||||
|
)
|
||||||
|
type_rows = list(cur.fetchall())
|
||||||
|
|
||||||
|
return merge_parameter_schema_rows(category_rows, type_rows)
|
||||||
|
|
||||||
|
|
||||||
|
def _metric_human_labels(schema_row: Mapping[str, Any]) -> Dict[str, Any]:
|
||||||
|
"""Bezeichnung + Kurzbeschreibung aus training_parameters (KI / Export)."""
|
||||||
|
return {
|
||||||
|
"name_de": schema_row.get("name_de"),
|
||||||
|
"name_en": schema_row.get("name_en"),
|
||||||
|
"description_de": schema_row.get("description_de"),
|
||||||
|
"description_en": schema_row.get("description_en"),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _validation_rules_dict(raw: Any) -> Dict[str, Any]:
|
||||||
|
if isinstance(raw, dict):
|
||||||
|
return raw
|
||||||
|
return {}
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_single_value(data_type: str, value: Any, rules: Dict[str, Any]) -> None:
|
||||||
|
if data_type == "integer":
|
||||||
|
if not isinstance(value, int) or isinstance(value, bool):
|
||||||
|
raise ActivitySessionMetricsError(400, f"Erwartet integer, erhalten: {type(value).__name__}")
|
||||||
|
if "min" in rules and value < rules["min"]:
|
||||||
|
raise ActivitySessionMetricsError(400, f"Wert unter min ({rules['min']})")
|
||||||
|
if "max" in rules and value > rules["max"]:
|
||||||
|
raise ActivitySessionMetricsError(400, f"Wert über max ({rules['max']})")
|
||||||
|
elif data_type == "float":
|
||||||
|
if isinstance(value, bool) or not isinstance(value, (int, float, Decimal)):
|
||||||
|
raise ActivitySessionMetricsError(400, f"Erwartet Zahl, erhalten: {type(value).__name__}")
|
||||||
|
v = float(value)
|
||||||
|
if "min" in rules and v < float(rules["min"]):
|
||||||
|
raise ActivitySessionMetricsError(400, f"Wert unter min ({rules['min']})")
|
||||||
|
if "max" in rules and v > float(rules["max"]):
|
||||||
|
raise ActivitySessionMetricsError(400, f"Wert über max ({rules['max']})")
|
||||||
|
elif data_type == "string":
|
||||||
|
if not isinstance(value, str):
|
||||||
|
raise ActivitySessionMetricsError(400, f"Erwartet string, erhalten: {type(value).__name__}")
|
||||||
|
if rules.get("not_empty") and not value.strip():
|
||||||
|
raise ActivitySessionMetricsError(400, "Leerer String nicht erlaubt")
|
||||||
|
if "max_length" in rules and len(value) > int(rules["max_length"]):
|
||||||
|
raise ActivitySessionMetricsError(400, f"String zu lang (max {rules['max_length']})")
|
||||||
|
allowed = rules.get("allowed_values")
|
||||||
|
if allowed and value not in allowed:
|
||||||
|
raise ActivitySessionMetricsError(400, "Wert nicht in erlaubter Menge")
|
||||||
|
elif data_type == "boolean":
|
||||||
|
if not isinstance(value, bool):
|
||||||
|
raise ActivitySessionMetricsError(400, f"Erwartet boolean, erhalten: {type(value).__name__}")
|
||||||
|
else:
|
||||||
|
raise ActivitySessionMetricsError(400, f"Unbekannter data_type: {data_type}")
|
||||||
|
|
||||||
|
|
||||||
|
def _row_value_tuple(data_type: str, value: Any) -> tuple:
|
||||||
|
if data_type == "integer":
|
||||||
|
return (None, int(value), None, None)
|
||||||
|
if data_type == "float":
|
||||||
|
return (float(value), None, None, None)
|
||||||
|
if data_type == "string":
|
||||||
|
return (None, None, str(value), None)
|
||||||
|
if data_type == "boolean":
|
||||||
|
return (None, None, None, bool(value))
|
||||||
|
raise ValueError(data_type)
|
||||||
|
|
||||||
|
|
||||||
|
def _coerce_raw_value_for_parameter(data_type: str, raw: Any) -> Any:
|
||||||
|
"""Wert aus activity_log-Spalte in den Typ bringen, den training_parameters.data_type erwartet."""
|
||||||
|
if data_type == "integer":
|
||||||
|
if isinstance(raw, bool):
|
||||||
|
raise TypeError("boolean nicht als integer erlaubt")
|
||||||
|
if isinstance(raw, str):
|
||||||
|
s = raw.strip().replace(",", ".")
|
||||||
|
return int(round(float(s)))
|
||||||
|
return int(round(float(raw)))
|
||||||
|
if data_type == "float":
|
||||||
|
if isinstance(raw, str):
|
||||||
|
s = raw.strip().replace(",", ".")
|
||||||
|
return float(s)
|
||||||
|
return float(raw)
|
||||||
|
if data_type == "string":
|
||||||
|
return str(raw) if raw is not None else ""
|
||||||
|
if data_type == "boolean":
|
||||||
|
if isinstance(raw, bool):
|
||||||
|
return raw
|
||||||
|
s = str(raw).strip().lower()
|
||||||
|
if s in ("true", "1", "t", "yes"):
|
||||||
|
return True
|
||||||
|
if s in ("false", "0", "f", "no", ""):
|
||||||
|
return False
|
||||||
|
raise TypeError(f"boolean-Koercion nicht möglich: {raw!r}")
|
||||||
|
raise ValueError(data_type)
|
||||||
|
|
||||||
|
|
||||||
|
def upsert_session_metrics_from_csv_mapped(
|
||||||
|
cur,
|
||||||
|
profile_id: str,
|
||||||
|
activity_log_id: str,
|
||||||
|
mapped: Mapping[str, Any],
|
||||||
|
training_category: Optional[str],
|
||||||
|
training_type_id: Optional[int],
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
EAV für Trainingsparameter aus CSV.
|
||||||
|
|
||||||
|
Es werden nur Parameter geschrieben, die in ``resolve_activity_attribute_schema`` (Kategorie +
|
||||||
|
Trainingstyp) vorkommen. CSV-Spalten-Mappings sind import-spezifisch und definieren **nicht** das
|
||||||
|
UI-/Auswertungs-Schema — fehlende tcp/ttp-Zuordnung bedeutet: kein EAV für diesen Key (Werte ggf.
|
||||||
|
nur in ``activity_log``-Kernfeldern).
|
||||||
|
|
||||||
|
Kernfelder schreibt der Executor nach ``activity_log``; hier keine EAV-Zeilen für Registry-Keys.
|
||||||
|
|
||||||
|
Hat ein Parameter ``source_field`` (Semantik aus ``activity_log``), wird EAV nur dann **nicht**
|
||||||
|
geschrieben, wenn diese Spalte nach dem Import bereits befüllt ist — sonst gäbe es doppelte
|
||||||
|
Speicherung und der Merge würde ohnehin die Spalte bevorzugen. Ist die Spalte leer (z. B. Feld
|
||||||
|
nur noch über EAV / Custom-Mapping, ohne Registry-Patch), schreibt der Import den Wert aus
|
||||||
|
``mapped`` nach EAV — analog zum Lesepfad (Spalte zuerst, sonst EAV).
|
||||||
|
"""
|
||||||
|
cur.execute("SELECT * FROM activity_log WHERE id = %s", (activity_log_id,))
|
||||||
|
row = cur.fetchone()
|
||||||
|
if not row or str(row["profile_id"]) != str(profile_id):
|
||||||
|
return
|
||||||
|
header = dict(row)
|
||||||
|
schema = resolve_activity_attribute_schema(cur, training_category, training_type_id)
|
||||||
|
for spec in schema:
|
||||||
|
pkey = spec["key"]
|
||||||
|
if pkey not in mapped:
|
||||||
|
continue
|
||||||
|
raw = mapped[pkey]
|
||||||
|
if raw is None or raw == "":
|
||||||
|
continue
|
||||||
|
if pkey in ACTIVITY_MODULE_REGISTRY_FIELD_KEYS:
|
||||||
|
continue
|
||||||
|
sf_raw = spec.get("source_field")
|
||||||
|
if sf_raw is not None and str(sf_raw).strip():
|
||||||
|
col = str(sf_raw).strip()
|
||||||
|
if col in header and header[col] is not None:
|
||||||
|
continue
|
||||||
|
tid = spec["training_parameter_id"]
|
||||||
|
dt = spec["data_type"]
|
||||||
|
rules = _validation_rules_dict(spec["validation_rules"])
|
||||||
|
try:
|
||||||
|
coerced = _coerce_raw_value_for_parameter(dt, raw)
|
||||||
|
_validate_single_value(dt, coerced, rules)
|
||||||
|
except (ActivitySessionMetricsError, TypeError, ValueError) as ex:
|
||||||
|
logger.warning("CSV EAV skipped %s: %s", pkey, ex)
|
||||||
|
continue
|
||||||
|
vn, vi, vt, vb = _row_value_tuple(dt, coerced)
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
) VALUES (%s, %s, %s, %s, %s, %s, NOW())
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id)
|
||||||
|
DO UPDATE SET
|
||||||
|
value_num = EXCLUDED.value_num,
|
||||||
|
value_int = EXCLUDED.value_int,
|
||||||
|
value_text = EXCLUDED.value_text,
|
||||||
|
value_bool = EXCLUDED.value_bool,
|
||||||
|
updated_at = NOW()
|
||||||
|
""",
|
||||||
|
(activity_log_id, tid, vn, vi, vt, vb),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def merge_column_backed_and_eav_metrics(
|
||||||
|
header: Mapping[str, Any],
|
||||||
|
schema: Sequence[Dict[str, Any]],
|
||||||
|
eav_metrics: Sequence[Dict[str, Any]],
|
||||||
|
) -> List[Dict[str, Any]]:
|
||||||
|
"""
|
||||||
|
Effektive Metrikliste **nur** für Parameter aus ``schema`` (Kategorie + Trainingstyp / tcp+ttp).
|
||||||
|
|
||||||
|
Kanon beim Lesen: **activity_log** schlägt EAV, sobald ein passender Spaltenwert existiert und
|
||||||
|
koerzierbar ist — in dieser Reihenfolge:
|
||||||
|
|
||||||
|
1. ``source_field`` → Spalte
|
||||||
|
2. Parameter-Key = Registry-Kernfeld (``ACTIVITY_MODULE_REGISTRY_FIELD_KEYS``) → gleichnamige Spalte
|
||||||
|
3. EAV-primäre Keys → Legacy-Spalte laut ``ACTIVITY_LOG_LEGACY_COLUMN_FOR_EAV_PRIMARY_PARAM``
|
||||||
|
4. sonst EAV
|
||||||
|
|
||||||
|
EAV-Zeilen zu Parametern, die nicht im Schema sind, werden nicht ausgegeben.
|
||||||
|
"""
|
||||||
|
eav_by_key = {m["key"]: m for m in eav_metrics}
|
||||||
|
merged: List[Dict[str, Any]] = []
|
||||||
|
keys_handled: set[str] = set()
|
||||||
|
|
||||||
|
for s in schema:
|
||||||
|
k = s["key"]
|
||||||
|
tid = s["training_parameter_id"]
|
||||||
|
dt = s["data_type"]
|
||||||
|
unit = s.get("unit")
|
||||||
|
sf = s.get("source_field")
|
||||||
|
|
||||||
|
used_column = False
|
||||||
|
if sf and isinstance(sf, str) and str(sf).strip():
|
||||||
|
col = str(sf).strip()
|
||||||
|
if col in header and header[col] is not None:
|
||||||
|
try:
|
||||||
|
val = _coerce_raw_value_for_parameter(dt, header[col])
|
||||||
|
merged.append(
|
||||||
|
{
|
||||||
|
"training_parameter_id": tid,
|
||||||
|
"key": k,
|
||||||
|
"data_type": dt,
|
||||||
|
"unit": unit,
|
||||||
|
"value": val,
|
||||||
|
**_metric_human_labels(s),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
used_column = True
|
||||||
|
keys_handled.add(k)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
if used_column:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if k in ACTIVITY_MODULE_REGISTRY_FIELD_KEYS and k in header and header[k] is not None:
|
||||||
|
try:
|
||||||
|
val = _coerce_raw_value_for_parameter(dt, header[k])
|
||||||
|
merged.append(
|
||||||
|
{
|
||||||
|
"training_parameter_id": tid,
|
||||||
|
"key": k,
|
||||||
|
"data_type": dt,
|
||||||
|
"unit": unit,
|
||||||
|
"value": val,
|
||||||
|
**_metric_human_labels(s),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
keys_handled.add(k)
|
||||||
|
continue
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
legacy_col = ACTIVITY_LOG_LEGACY_COLUMN_FOR_EAV_PRIMARY_PARAM.get(k)
|
||||||
|
if legacy_col and legacy_col in header and header[legacy_col] is not None:
|
||||||
|
try:
|
||||||
|
val = _coerce_raw_value_for_parameter(dt, header[legacy_col])
|
||||||
|
merged.append(
|
||||||
|
{
|
||||||
|
"training_parameter_id": tid,
|
||||||
|
"key": k,
|
||||||
|
"data_type": dt,
|
||||||
|
"unit": unit,
|
||||||
|
"value": val,
|
||||||
|
**_metric_human_labels(s),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
keys_handled.add(k)
|
||||||
|
continue
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
if k in eav_by_key:
|
||||||
|
row = dict(eav_by_key[k])
|
||||||
|
row.update(_metric_human_labels(s))
|
||||||
|
merged.append(row)
|
||||||
|
keys_handled.add(k)
|
||||||
|
|
||||||
|
merged.sort(key=lambda x: x["key"])
|
||||||
|
return merged
|
||||||
|
|
||||||
|
|
||||||
|
def sync_column_backed_session_metrics(cur, profile_id: str, activity_log_id: str) -> None:
|
||||||
|
"""
|
||||||
|
[Veraltet / nicht mehr in Schreibpfaden aufgerufen]
|
||||||
|
|
||||||
|
Früher: EAV spiegelte activity_log-Spalten für Parameter mit source_field.
|
||||||
|
Kanon: Spaltenwerte werden bei merge_column_backed_and_eav_metrics beim Lesen berücksichtigt; keine
|
||||||
|
doppelte Speicherung. Funktion bleibt für optionale Admin-/Reparatur-Skripte.
|
||||||
|
"""
|
||||||
|
cur.execute("SELECT * FROM activity_log WHERE id = %s", (activity_log_id,))
|
||||||
|
row = cur.fetchone()
|
||||||
|
if not row or str(row["profile_id"]) != str(profile_id):
|
||||||
|
return
|
||||||
|
header = dict(row)
|
||||||
|
schema = resolve_activity_attribute_schema(
|
||||||
|
cur, header.get("training_category"), header.get("training_type_id")
|
||||||
|
)
|
||||||
|
for spec in schema:
|
||||||
|
sf = spec.get("source_field")
|
||||||
|
if sf is None or (isinstance(sf, str) and not str(sf).strip()):
|
||||||
|
continue
|
||||||
|
col = str(sf).strip()
|
||||||
|
if col not in header:
|
||||||
|
continue
|
||||||
|
raw = header[col]
|
||||||
|
tid = spec["training_parameter_id"]
|
||||||
|
dt = spec["data_type"]
|
||||||
|
rules = _validation_rules_dict(spec["validation_rules"])
|
||||||
|
|
||||||
|
if raw is None:
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
DELETE FROM activity_session_metrics
|
||||||
|
WHERE activity_log_id = %s AND training_parameter_id = %s
|
||||||
|
""",
|
||||||
|
(activity_log_id, tid),
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
|
||||||
|
try:
|
||||||
|
coerced = _coerce_raw_value_for_parameter(dt, raw)
|
||||||
|
_validate_single_value(dt, coerced, rules)
|
||||||
|
except (ActivitySessionMetricsError, TypeError, ValueError) as ex:
|
||||||
|
logger.warning(
|
||||||
|
"sync_column_backed_session_metrics: überspringe %s (Spalte %s): %s",
|
||||||
|
spec.get("key"),
|
||||||
|
col,
|
||||||
|
ex,
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
|
||||||
|
vn, vi, vt, vb = _row_value_tuple(dt, coerced)
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
) VALUES (%s, %s, %s, %s, %s, %s, NOW())
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id)
|
||||||
|
DO UPDATE SET
|
||||||
|
value_num = EXCLUDED.value_num,
|
||||||
|
value_int = EXCLUDED.value_int,
|
||||||
|
value_text = EXCLUDED.value_text,
|
||||||
|
value_bool = EXCLUDED.value_bool,
|
||||||
|
updated_at = NOW()
|
||||||
|
""",
|
||||||
|
(activity_log_id, tid, vn, vi, vt, vb),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def fetch_activity_session_metrics(cur, activity_log_id: str) -> List[Dict[str, Any]]:
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT
|
||||||
|
m.id,
|
||||||
|
m.activity_log_id,
|
||||||
|
m.training_parameter_id,
|
||||||
|
m.value_num,
|
||||||
|
m.value_int,
|
||||||
|
m.value_text,
|
||||||
|
m.value_bool,
|
||||||
|
tp.key,
|
||||||
|
tp.data_type,
|
||||||
|
tp.unit
|
||||||
|
FROM activity_session_metrics m
|
||||||
|
JOIN training_parameters tp ON tp.id = m.training_parameter_id
|
||||||
|
WHERE m.activity_log_id = %s
|
||||||
|
ORDER BY tp.key
|
||||||
|
""",
|
||||||
|
(activity_log_id,),
|
||||||
|
)
|
||||||
|
rows = cur.fetchall()
|
||||||
|
out: List[Dict[str, Any]] = []
|
||||||
|
for r in rows:
|
||||||
|
dt = r["data_type"]
|
||||||
|
if dt == "integer":
|
||||||
|
val = int(r["value_int"]) if r["value_int"] is not None else None
|
||||||
|
elif dt == "float":
|
||||||
|
val = float(r["value_num"]) if r["value_num"] is not None else None
|
||||||
|
elif dt == "string":
|
||||||
|
val = r["value_text"]
|
||||||
|
else:
|
||||||
|
val = r["value_bool"]
|
||||||
|
out.append(
|
||||||
|
{
|
||||||
|
"training_parameter_id": r["training_parameter_id"],
|
||||||
|
"key": r["key"],
|
||||||
|
"data_type": dt,
|
||||||
|
"unit": r["unit"],
|
||||||
|
"value": val,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def replace_activity_session_metrics(
|
||||||
|
cur,
|
||||||
|
profile_id: str,
|
||||||
|
activity_log_id: str,
|
||||||
|
metrics: Sequence[Dict[str, Any]],
|
||||||
|
) -> List[Dict[str, Any]]:
|
||||||
|
"""
|
||||||
|
Full replace of EAV rows for this session. metrics: [{ "parameter_key": str, "value": ... }, ...]
|
||||||
|
"""
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT id, profile_id, training_category, training_type_id
|
||||||
|
FROM activity_log WHERE id = %s
|
||||||
|
""",
|
||||||
|
(activity_log_id,),
|
||||||
|
)
|
||||||
|
row = cur.fetchone()
|
||||||
|
if not row or str(row["profile_id"]) != str(profile_id):
|
||||||
|
raise ActivitySessionMetricsError(404, "Aktivität nicht gefunden")
|
||||||
|
|
||||||
|
schema = resolve_activity_attribute_schema(
|
||||||
|
cur, row.get("training_category"), row.get("training_type_id")
|
||||||
|
)
|
||||||
|
by_key = {s["key"]: s for s in schema}
|
||||||
|
payload_by_key: Dict[str, Dict[str, Any]] = {}
|
||||||
|
for item in metrics:
|
||||||
|
raw_k = item.get("parameter_key")
|
||||||
|
if raw_k is None or not str(raw_k).strip():
|
||||||
|
raise ActivitySessionMetricsError(400, "parameter_key fehlt")
|
||||||
|
k = str(raw_k).strip()
|
||||||
|
if k not in by_key:
|
||||||
|
raise ActivitySessionMetricsError(400, f"Unbekannter oder nicht zugewiesener Parameter: {k}")
|
||||||
|
payload_by_key[k] = item
|
||||||
|
|
||||||
|
for s in schema:
|
||||||
|
if not s["required"]:
|
||||||
|
continue
|
||||||
|
itk = s["key"]
|
||||||
|
hit = payload_by_key.get(itk)
|
||||||
|
if hit is None or hit.get("value") is None:
|
||||||
|
raise ActivitySessionMetricsError(400, f"Pflichtfeld fehlt: {itk}")
|
||||||
|
|
||||||
|
cur.execute(
|
||||||
|
"DELETE FROM activity_session_metrics WHERE activity_log_id = %s",
|
||||||
|
(activity_log_id,),
|
||||||
|
)
|
||||||
|
|
||||||
|
for item in metrics:
|
||||||
|
k = str(item["parameter_key"]).strip()
|
||||||
|
spec = by_key[k]
|
||||||
|
val = item.get("value")
|
||||||
|
if val is None:
|
||||||
|
if spec["required"]:
|
||||||
|
raise ActivitySessionMetricsError(400, f"Pflichtfeld fehlt: {k}")
|
||||||
|
continue
|
||||||
|
rules = _validation_rules_dict(spec["validation_rules"])
|
||||||
|
_validate_single_value(spec["data_type"], val, rules)
|
||||||
|
vn, vi, vt, vb = _row_value_tuple(spec["data_type"], val)
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
) VALUES (%s, %s, %s, %s, %s, %s, NOW())
|
||||||
|
""",
|
||||||
|
(activity_log_id, spec["training_parameter_id"], vn, vi, vt, vb),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Kein sync_column_backed nach PUT /metrics: der Request ist maßgeblich für EAV. Ein Spalten-Sync würde
|
||||||
|
# Werte aus nicht mitgeschriebenen activity_log-Spalten wieder verwerfen.
|
||||||
|
|
||||||
|
return fetch_activity_session_metrics(cur, activity_log_id)
|
||||||
|
|
||||||
|
|
||||||
|
def get_activity_session_logical_unit(
|
||||||
|
cur,
|
||||||
|
profile_id: str,
|
||||||
|
activity_log_id: str,
|
||||||
|
*,
|
||||||
|
use_form_training_context: bool = False,
|
||||||
|
form_training_category: Optional[str] = None,
|
||||||
|
form_training_type_id: Optional[int] = None,
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
cur.execute("SELECT * FROM activity_log WHERE id = %s", (activity_log_id,))
|
||||||
|
row = cur.fetchone()
|
||||||
|
if not row or str(row["profile_id"]) != str(profile_id):
|
||||||
|
raise ActivitySessionMetricsError(404, "Aktivität nicht gefunden")
|
||||||
|
|
||||||
|
header = dict(row)
|
||||||
|
if use_form_training_context:
|
||||||
|
cat = form_training_category
|
||||||
|
if isinstance(cat, str):
|
||||||
|
cat = cat.strip() or None
|
||||||
|
tid = form_training_type_id
|
||||||
|
else:
|
||||||
|
cat = header.get("training_category")
|
||||||
|
tid = header.get("training_type_id")
|
||||||
|
if tid is not None:
|
||||||
|
try:
|
||||||
|
tid = int(tid)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
tid = None
|
||||||
|
schema = resolve_activity_attribute_schema(cur, cat, tid)
|
||||||
|
metrics = fetch_activity_session_metrics(cur, activity_log_id)
|
||||||
|
merged_metrics = merge_column_backed_and_eav_metrics(header, schema, metrics)
|
||||||
|
return {
|
||||||
|
"header": header,
|
||||||
|
"schema": schema,
|
||||||
|
"metrics": merged_metrics,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def enrich_sessions_with_metrics(cur, sessions: List[Dict[str, Any]]) -> None:
|
||||||
|
"""
|
||||||
|
Mutates each session dict: adds key 'session_metrics' (list).
|
||||||
|
|
||||||
|
Kombiniert EAV mit activity_log-Spalten für Parameter mit source_field (kanonisch: Spalte),
|
||||||
|
analog zu get_activity_session_logical_unit – ohne doppelte EAV-Speicherung beim Import.
|
||||||
|
"""
|
||||||
|
if not sessions:
|
||||||
|
return
|
||||||
|
ids = [str(s["id"]) for s in sessions if s.get("id")]
|
||||||
|
if not ids:
|
||||||
|
return
|
||||||
|
ph = ",".join(["%s"] * len(ids))
|
||||||
|
|
||||||
|
cur.execute(
|
||||||
|
f"SELECT * FROM activity_log WHERE id IN ({ph})",
|
||||||
|
ids,
|
||||||
|
)
|
||||||
|
headers_by_id: Dict[str, Dict[str, Any]] = {}
|
||||||
|
for r in cur.fetchall():
|
||||||
|
h = dict(r)
|
||||||
|
headers_by_id[str(h["id"])] = h
|
||||||
|
|
||||||
|
cur.execute(
|
||||||
|
f"""
|
||||||
|
SELECT
|
||||||
|
m.activity_log_id,
|
||||||
|
m.training_parameter_id,
|
||||||
|
tp.key,
|
||||||
|
tp.data_type,
|
||||||
|
tp.unit,
|
||||||
|
m.value_num,
|
||||||
|
m.value_int,
|
||||||
|
m.value_text,
|
||||||
|
m.value_bool
|
||||||
|
FROM activity_session_metrics m
|
||||||
|
JOIN training_parameters tp ON tp.id = m.training_parameter_id
|
||||||
|
WHERE m.activity_log_id IN ({ph})
|
||||||
|
ORDER BY m.activity_log_id, tp.key
|
||||||
|
""",
|
||||||
|
ids,
|
||||||
|
)
|
||||||
|
by_act: Dict[str, List[Dict[str, Any]]] = {}
|
||||||
|
for r in cur.fetchall():
|
||||||
|
aid = str(r["activity_log_id"])
|
||||||
|
dt = r["data_type"]
|
||||||
|
if dt == "integer":
|
||||||
|
val = int(r["value_int"]) if r["value_int"] is not None else None
|
||||||
|
elif dt == "float":
|
||||||
|
val = float(r["value_num"]) if r["value_num"] is not None else None
|
||||||
|
elif dt == "string":
|
||||||
|
val = r["value_text"]
|
||||||
|
else:
|
||||||
|
val = r["value_bool"]
|
||||||
|
by_act.setdefault(aid, []).append(
|
||||||
|
{
|
||||||
|
"training_parameter_id": r["training_parameter_id"],
|
||||||
|
"key": r["key"],
|
||||||
|
"data_type": dt,
|
||||||
|
"unit": r["unit"],
|
||||||
|
"value": val,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
schema_cache: Dict[tuple[Any, Any], List[Dict[str, Any]]] = {}
|
||||||
|
|
||||||
|
def _schema(cat: Any, tid: Any) -> List[Dict[str, Any]]:
|
||||||
|
cache_key = (cat, tid)
|
||||||
|
if cache_key not in schema_cache:
|
||||||
|
schema_cache[cache_key] = resolve_activity_attribute_schema(cur, cat, tid)
|
||||||
|
return schema_cache[cache_key]
|
||||||
|
|
||||||
|
for s in sessions:
|
||||||
|
aid = str(s.get("id"))
|
||||||
|
header = headers_by_id.get(aid)
|
||||||
|
if not header:
|
||||||
|
s["session_metrics"] = []
|
||||||
|
continue
|
||||||
|
schema = _schema(header.get("training_category"), header.get("training_type_id"))
|
||||||
|
eav_list = by_act.get(aid, [])
|
||||||
|
merged = merge_column_backed_and_eav_metrics(header, schema, eav_list)
|
||||||
|
s["session_metrics"] = [
|
||||||
|
{
|
||||||
|
"key": m["key"],
|
||||||
|
"data_type": m["data_type"],
|
||||||
|
"unit": m["unit"],
|
||||||
|
"value": m["value"],
|
||||||
|
"name_de": m.get("name_de"),
|
||||||
|
"name_en": m.get("name_en"),
|
||||||
|
"description_de": m.get("description_de"),
|
||||||
|
"description_en": m.get("description_en"),
|
||||||
|
}
|
||||||
|
for m in merged
|
||||||
|
]
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
"""
|
||||||
|
Einheitliche Startzeit-Normalisierung für Aktivität (CSV, Legacy-Import, Dedupe).
|
||||||
|
|
||||||
|
Anbieter-agnostisch: beliebige ISO-/Export-Strings über dateutil.
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from datetime import time as dt_time
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from dateutil import parser as du_parser
|
||||||
|
|
||||||
|
|
||||||
|
def normalize_activity_start(start_raw: str) -> tuple[str, Optional[dt_time]]:
|
||||||
|
"""
|
||||||
|
Roh-String „Start“ aus Exporten → (YYYY-MM-DD, TIME ohne μs) für DB Dedupe/INSERT.
|
||||||
|
|
||||||
|
Leerer Input → ("", None). Fallback bei Parse-Fehler: erstes Datum aus ersten 10 Zeichen.
|
||||||
|
"""
|
||||||
|
s = (start_raw or "").strip()
|
||||||
|
if not s:
|
||||||
|
return "", None
|
||||||
|
try:
|
||||||
|
parsed = du_parser.parse(s, dayfirst=False)
|
||||||
|
t = parsed.time().replace(microsecond=0)
|
||||||
|
return parsed.date().isoformat(), t
|
||||||
|
except (ValueError, TypeError, OverflowError):
|
||||||
|
if len(s) >= 10:
|
||||||
|
return s[:10], None
|
||||||
|
return "", None
|
||||||
@@ -5,6 +5,9 @@ Provides structured data for body composition and measurements.
|
|||||||
|
|
||||||
Functions:
|
Functions:
|
||||||
- get_latest_weight_data(): Most recent weight entry
|
- get_latest_weight_data(): Most recent weight entry
|
||||||
|
- get_bmi_data(): BMI from latest weight + profile height
|
||||||
|
- get_profile_goal_weight_data(): Zielgewicht (Profilfeld)
|
||||||
|
- get_profile_goal_bf_pct_data(): Ziel-KFA % (Profilfeld)
|
||||||
- get_weight_trend_data(): Weight trend with slope and direction
|
- get_weight_trend_data(): Weight trend with slope and direction
|
||||||
- get_body_composition_data(): Body fat percentage and lean mass
|
- get_body_composition_data(): Body fat percentage and lean mass
|
||||||
- get_circumference_summary_data(): Latest circumference measurements
|
- get_circumference_summary_data(): Latest circumference measurements
|
||||||
@@ -68,6 +71,105 @@ def get_latest_weight_data(
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_bmi_data(profile_id: str) -> Dict:
|
||||||
|
"""
|
||||||
|
BMI from latest weight_log entry and profiles.height (cm).
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
{
|
||||||
|
"bmi": float | None,
|
||||||
|
"weight_kg": float | None,
|
||||||
|
"height_cm": float | None,
|
||||||
|
"confidence": "high" | "insufficient",
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT pr.height,
|
||||||
|
(SELECT wl.weight FROM weight_log wl
|
||||||
|
WHERE wl.profile_id = pr.id
|
||||||
|
ORDER BY wl.date DESC
|
||||||
|
LIMIT 1) AS weight
|
||||||
|
FROM profiles pr
|
||||||
|
WHERE pr.id = %s
|
||||||
|
""",
|
||||||
|
(profile_id,),
|
||||||
|
)
|
||||||
|
row = cur.fetchone()
|
||||||
|
if not row:
|
||||||
|
return {
|
||||||
|
"bmi": None,
|
||||||
|
"weight_kg": None,
|
||||||
|
"height_cm": None,
|
||||||
|
"confidence": "insufficient",
|
||||||
|
}
|
||||||
|
|
||||||
|
height_cm = row["height"]
|
||||||
|
weight = row["weight"]
|
||||||
|
if height_cm is None or weight is None:
|
||||||
|
return {
|
||||||
|
"bmi": None,
|
||||||
|
"weight_kg": safe_float(weight) if weight is not None else None,
|
||||||
|
"height_cm": safe_float(height_cm) if height_cm is not None else None,
|
||||||
|
"confidence": "insufficient",
|
||||||
|
}
|
||||||
|
|
||||||
|
h = safe_float(height_cm)
|
||||||
|
w = safe_float(weight)
|
||||||
|
if h <= 0:
|
||||||
|
return {
|
||||||
|
"bmi": None,
|
||||||
|
"weight_kg": w,
|
||||||
|
"height_cm": h,
|
||||||
|
"confidence": "insufficient",
|
||||||
|
}
|
||||||
|
|
||||||
|
height_m = h / 100.0
|
||||||
|
bmi = w / (height_m ** 2)
|
||||||
|
return {
|
||||||
|
"bmi": bmi,
|
||||||
|
"weight_kg": w,
|
||||||
|
"height_cm": h,
|
||||||
|
"confidence": "high",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_profile_goal_weight_data(profile_id: str) -> Dict:
|
||||||
|
"""Strategisches Zielgewicht aus profiles.goal_weight (kg), nicht goals-Tabelle."""
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cur.execute(
|
||||||
|
"SELECT goal_weight FROM profiles WHERE id=%s",
|
||||||
|
(profile_id,),
|
||||||
|
)
|
||||||
|
row = cur.fetchone()
|
||||||
|
if not row or row.get("goal_weight") is None:
|
||||||
|
return {"goal_weight_kg": None, "confidence": "insufficient"}
|
||||||
|
return {
|
||||||
|
"goal_weight_kg": safe_float(row["goal_weight"]),
|
||||||
|
"confidence": "high",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_profile_goal_bf_pct_data(profile_id: str) -> Dict:
|
||||||
|
"""Strategisches Ziel-KFA aus profiles.goal_bf_pct (%), nicht goals-Tabelle."""
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cur.execute(
|
||||||
|
"SELECT goal_bf_pct FROM profiles WHERE id=%s",
|
||||||
|
(profile_id,),
|
||||||
|
)
|
||||||
|
row = cur.fetchone()
|
||||||
|
if not row or row.get("goal_bf_pct") is None:
|
||||||
|
return {"goal_bf_pct": None, "confidence": "insufficient"}
|
||||||
|
return {
|
||||||
|
"goal_bf_pct": safe_float(row["goal_bf_pct"]),
|
||||||
|
"confidence": "high",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def get_weight_trend_data(
|
def get_weight_trend_data(
|
||||||
profile_id: str,
|
profile_id: str,
|
||||||
days: int = 28
|
days: int = 28
|
||||||
@@ -89,7 +191,8 @@ def get_weight_trend_data(
|
|||||||
"confidence": str,
|
"confidence": str,
|
||||||
"days_analyzed": int,
|
"days_analyzed": int,
|
||||||
"first_date": date,
|
"first_date": date,
|
||||||
"last_date": date
|
"last_date": date,
|
||||||
|
"series": [{"date": date, "weight": float}, ...], # für Charts ohne zweites Query
|
||||||
}
|
}
|
||||||
|
|
||||||
Confidence Rules:
|
Confidence Rules:
|
||||||
@@ -127,7 +230,8 @@ def get_weight_trend_data(
|
|||||||
"delta": 0.0,
|
"delta": 0.0,
|
||||||
"direction": "unknown",
|
"direction": "unknown",
|
||||||
"first_date": None,
|
"first_date": None,
|
||||||
"last_date": None
|
"last_date": None,
|
||||||
|
"series": [],
|
||||||
}
|
}
|
||||||
|
|
||||||
# Extract values
|
# Extract values
|
||||||
@@ -152,7 +256,11 @@ def get_weight_trend_data(
|
|||||||
"confidence": confidence,
|
"confidence": confidence,
|
||||||
"days_analyzed": days,
|
"days_analyzed": days,
|
||||||
"first_date": rows[0]['date'],
|
"first_date": rows[0]['date'],
|
||||||
"last_date": rows[-1]['date']
|
"last_date": rows[-1]['date'],
|
||||||
|
"series": [
|
||||||
|
{"date": r["date"], "weight": safe_float(r["weight"])}
|
||||||
|
for r in rows
|
||||||
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -337,12 +445,16 @@ def calculate_weight_7d_median(profile_id: str) -> Optional[float]:
|
|||||||
ORDER BY date DESC
|
ORDER BY date DESC
|
||||||
""", (profile_id,))
|
""", (profile_id,))
|
||||||
|
|
||||||
weights = [row['weight'] for row in cur.fetchall()]
|
weights = [
|
||||||
|
safe_float(row['weight'])
|
||||||
|
for row in cur.fetchall()
|
||||||
|
if row['weight'] is not None
|
||||||
|
]
|
||||||
|
|
||||||
if len(weights) < 4: # Need at least 4 measurements
|
if len(weights) < 4: # Need at least 4 measurements
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return round(statistics.median(weights), 1)
|
return round(float(statistics.median(weights)), 1)
|
||||||
|
|
||||||
|
|
||||||
def calculate_weight_28d_slope(profile_id: str) -> Optional[float]:
|
def calculate_weight_28d_slope(profile_id: str) -> Optional[float]:
|
||||||
@@ -370,7 +482,11 @@ def _calculate_weight_slope(profile_id: str, days: int) -> Optional[float]:
|
|||||||
ORDER BY date
|
ORDER BY date
|
||||||
""", (profile_id, days))
|
""", (profile_id, days))
|
||||||
|
|
||||||
data = [(row['date'], row['weight']) for row in cur.fetchall()]
|
data = [
|
||||||
|
(row['date'], safe_float(row['weight']))
|
||||||
|
for row in cur.fetchall()
|
||||||
|
if row['weight'] is not None
|
||||||
|
]
|
||||||
|
|
||||||
# Need minimum data points based on period
|
# Need minimum data points based on period
|
||||||
min_points = max(18, int(days * 0.6)) # 60% coverage
|
min_points = max(18, int(days * 0.6)) # 60% coverage
|
||||||
@@ -380,21 +496,21 @@ def _calculate_weight_slope(profile_id: str, days: int) -> Optional[float]:
|
|||||||
# Convert dates to days since start
|
# Convert dates to days since start
|
||||||
start_date = data[0][0]
|
start_date = data[0][0]
|
||||||
x_values = [(date - start_date).days for date, _ in data]
|
x_values = [(date - start_date).days for date, _ in data]
|
||||||
y_values = [weight for _, weight in data]
|
y_values = [w for _, w in data]
|
||||||
|
|
||||||
# Linear regression
|
# Linear regression (alles float: PostgreSQL numeric → Decimal in Python)
|
||||||
n = len(data)
|
n = len(data)
|
||||||
x_mean = sum(x_values) / n
|
x_mean = float(sum(x_values)) / n
|
||||||
y_mean = sum(y_values) / n
|
y_mean = float(sum(y_values)) / n
|
||||||
|
|
||||||
numerator = sum((x - x_mean) * (y - y_mean) for x, y in zip(x_values, y_values))
|
numerator = sum(float(x - x_mean) * float(y - y_mean) for x, y in zip(x_values, y_values))
|
||||||
denominator = sum((x - x_mean) ** 2 for x in x_values)
|
denominator = float(sum((x - x_mean) ** 2 for x in x_values))
|
||||||
|
|
||||||
if denominator == 0:
|
if denominator == 0:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
slope = numerator / denominator
|
slope = numerator / denominator
|
||||||
return round(slope, 4) # kg/day
|
return round(float(slope), 4) # kg/day
|
||||||
|
|
||||||
|
|
||||||
def calculate_goal_projection_date(profile_id: str, goal_id: str) -> Optional[str]:
|
def calculate_goal_projection_date(profile_id: str, goal_id: str) -> Optional[str]:
|
||||||
@@ -486,19 +602,24 @@ def _calculate_body_composition_change(profile_id: str, metric: str, days: int)
|
|||||||
recent = data[0]
|
recent = data[0]
|
||||||
oldest = data[-1]
|
oldest = data[-1]
|
||||||
|
|
||||||
# Calculate FM and LBM
|
# Calculate FM and LBM (DB numeric → Decimal; für Regression/Scores nur float)
|
||||||
recent_fm = recent['weight'] * (recent['bf_pct'] / 100)
|
rw = float(safe_float(recent['weight']) or 0)
|
||||||
recent_lbm = recent['weight'] - recent_fm
|
ob = float(safe_float(recent['bf_pct']) or 0)
|
||||||
|
ow = float(safe_float(oldest['weight']) or 0)
|
||||||
|
obf = float(safe_float(oldest['bf_pct']) or 0)
|
||||||
|
|
||||||
oldest_fm = oldest['weight'] * (oldest['bf_pct'] / 100)
|
recent_fm = rw * (ob / 100)
|
||||||
oldest_lbm = oldest['weight'] - oldest_fm
|
recent_lbm = rw - recent_fm
|
||||||
|
|
||||||
|
oldest_fm = ow * (obf / 100)
|
||||||
|
oldest_lbm = ow - oldest_fm
|
||||||
|
|
||||||
if metric == 'fm':
|
if metric == 'fm':
|
||||||
change = recent_fm - oldest_fm
|
change = recent_fm - oldest_fm
|
||||||
else:
|
else:
|
||||||
change = recent_lbm - oldest_lbm
|
change = recent_lbm - oldest_lbm
|
||||||
|
|
||||||
return round(change, 2)
|
return round(float(change), 2)
|
||||||
|
|
||||||
|
|
||||||
# ── Circumference Calculations ──────────────────────────────────────────────
|
# ── Circumference Calculations ──────────────────────────────────────────────
|
||||||
@@ -623,9 +744,9 @@ def calculate_body_progress_score(profile_id: str, focus_weights: Optional[Dict]
|
|||||||
from data_layer.scores import get_user_focus_weights
|
from data_layer.scores import get_user_focus_weights
|
||||||
focus_weights = get_user_focus_weights(profile_id)
|
focus_weights = get_user_focus_weights(profile_id)
|
||||||
|
|
||||||
weight_loss = focus_weights.get('weight_loss', 0)
|
weight_loss = float(focus_weights.get('weight_loss', 0) or 0)
|
||||||
muscle_gain = focus_weights.get('muscle_gain', 0)
|
muscle_gain = float(focus_weights.get('muscle_gain', 0) or 0)
|
||||||
body_recomp = focus_weights.get('body_recomposition', 0)
|
body_recomp = float(focus_weights.get('body_recomposition', 0) or 0)
|
||||||
|
|
||||||
total_body_weight = weight_loss + muscle_gain + body_recomp
|
total_body_weight = weight_loss + muscle_gain + body_recomp
|
||||||
|
|
||||||
@@ -652,8 +773,8 @@ def calculate_body_progress_score(profile_id: str, focus_weights: Optional[Dict]
|
|||||||
if not components:
|
if not components:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
total_score = sum(score * weight for _, score, weight in components)
|
total_score = sum(float(score) * float(weight) for _, score, weight in components)
|
||||||
total_weight = sum(weight for _, _, weight in components)
|
total_weight = sum(float(weight) for _, _, weight in components)
|
||||||
|
|
||||||
return int(total_score / total_weight)
|
return int(total_score / total_weight)
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,88 @@ from datetime import datetime, timedelta, date
|
|||||||
from db import get_db, get_cursor, r2d
|
from db import get_db, get_cursor, r2d
|
||||||
from data_layer.utils import calculate_confidence, safe_float, safe_int
|
from data_layer.utils import calculate_confidence, safe_float, safe_int
|
||||||
|
|
||||||
|
# Fallback TDEE (kcal/day) when demographics for Mifflin–St Jeor are incomplete.
|
||||||
|
TDEE_KCAL_PER_KG_BODYWEIGHT = 32.5
|
||||||
|
# PAL applied to MSJ BMR when height, sex, dob and weight are available (moderate activity).
|
||||||
|
TDEE_PAL_MODERATE = 1.55
|
||||||
|
|
||||||
|
|
||||||
|
def _age_years_from_dob(dob) -> Optional[int]:
|
||||||
|
if dob is None:
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
if isinstance(dob, str):
|
||||||
|
birth = datetime.strptime(dob[:10], "%Y-%m-%d").date()
|
||||||
|
else:
|
||||||
|
birth = dob
|
||||||
|
today = date.today()
|
||||||
|
return today.year - birth.year - ((today.month, today.day) < (birth.month, birth.day))
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _mifflin_st_jeor_bmr_kcal(
|
||||||
|
weight_kg: float, height_cm: float, age_years: int, sex_is_male: bool
|
||||||
|
) -> float:
|
||||||
|
if sex_is_male:
|
||||||
|
return 10.0 * weight_kg + 6.25 * height_cm - 5.0 * age_years + 5.0
|
||||||
|
return 10.0 * weight_kg + 6.25 * height_cm - 5.0 * age_years - 161.0
|
||||||
|
|
||||||
|
|
||||||
|
def estimate_tdee_kcal_from_latest_weight(profile_id: str) -> Optional[float]:
|
||||||
|
"""
|
||||||
|
Estimated TDEE (kcal/day).
|
||||||
|
|
||||||
|
Primary: Mifflin–St Jeor BMR × TDEE_PAL_MODERATE when latest weight plus
|
||||||
|
profiles.height, profiles.sex, profiles.dob are usable.
|
||||||
|
|
||||||
|
Fallback: latest weight (kg) × TDEE_KCAL_PER_KG_BODYWEIGHT (legacy heuristic).
|
||||||
|
|
||||||
|
Returns None if no weight on record.
|
||||||
|
"""
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cur.execute(
|
||||||
|
"""SELECT weight FROM weight_log
|
||||||
|
WHERE profile_id=%s ORDER BY date DESC LIMIT 1""",
|
||||||
|
(profile_id,),
|
||||||
|
)
|
||||||
|
wrow = cur.fetchone()
|
||||||
|
if not wrow or wrow["weight"] is None:
|
||||||
|
return None
|
||||||
|
weight_kg = float(wrow["weight"])
|
||||||
|
|
||||||
|
cur.execute(
|
||||||
|
"SELECT height, sex, dob FROM profiles WHERE id=%s",
|
||||||
|
(profile_id,),
|
||||||
|
)
|
||||||
|
prow = cur.fetchone()
|
||||||
|
|
||||||
|
if prow and prow.get("height") and prow.get("sex") is not None and prow.get("dob"):
|
||||||
|
height_cm = float(prow["height"])
|
||||||
|
age = _age_years_from_dob(prow["dob"])
|
||||||
|
if age is not None and 10 < age < 120 and height_cm > 50:
|
||||||
|
sex_raw = str(prow["sex"]).strip().lower()
|
||||||
|
sex_is_male = sex_raw in ("m", "male", "männlich", "mann")
|
||||||
|
bmr = _mifflin_st_jeor_bmr_kcal(weight_kg, height_cm, age, sex_is_male)
|
||||||
|
if bmr > 400:
|
||||||
|
return bmr * TDEE_PAL_MODERATE
|
||||||
|
|
||||||
|
return weight_kg * TDEE_KCAL_PER_KG_BODYWEIGHT
|
||||||
|
|
||||||
|
|
||||||
|
def _get_profile_goal_mode(profile_id: str) -> str:
|
||||||
|
"""Strategic goal_mode from profiles (Phase 0a); defaults to health."""
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cur.execute("SELECT goal_mode FROM profiles WHERE id=%s", (profile_id,))
|
||||||
|
row = cur.fetchone()
|
||||||
|
if row and row.get("goal_mode"):
|
||||||
|
g = str(row["goal_mode"]).strip().lower()
|
||||||
|
if g:
|
||||||
|
return g
|
||||||
|
return "health"
|
||||||
|
|
||||||
|
|
||||||
def get_nutrition_average_data(
|
def get_nutrition_average_data(
|
||||||
profile_id: str,
|
profile_id: str,
|
||||||
@@ -56,20 +138,29 @@ def get_nutrition_average_data(
|
|||||||
cur = get_cursor(conn)
|
cur = get_cursor(conn)
|
||||||
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
||||||
|
|
||||||
|
# Mean over calendar days (per-day sums), not over raw log rows.
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"""SELECT
|
"""SELECT
|
||||||
AVG(kcal) as kcal_avg,
|
AVG(daily_kcal) AS kcal_avg,
|
||||||
AVG(protein_g) as protein_avg,
|
AVG(daily_protein) AS protein_avg,
|
||||||
AVG(carbs_g) as carbs_avg,
|
AVG(daily_carbs) AS carbs_avg,
|
||||||
AVG(fat_g) as fat_avg,
|
AVG(daily_fat) AS fat_avg,
|
||||||
COUNT(*) as data_points
|
COUNT(*)::int AS day_count
|
||||||
FROM nutrition_log
|
FROM (
|
||||||
WHERE profile_id=%s AND date >= %s""",
|
SELECT date,
|
||||||
(profile_id, cutoff)
|
COALESCE(SUM(kcal), 0)::float AS daily_kcal,
|
||||||
|
COALESCE(SUM(protein_g), 0)::float AS daily_protein,
|
||||||
|
COALESCE(SUM(carbs_g), 0)::float AS daily_carbs,
|
||||||
|
COALESCE(SUM(fat_g), 0)::float AS daily_fat
|
||||||
|
FROM nutrition_log
|
||||||
|
WHERE profile_id=%s AND date >= %s
|
||||||
|
GROUP BY date
|
||||||
|
) AS daily""",
|
||||||
|
(profile_id, cutoff),
|
||||||
)
|
)
|
||||||
row = cur.fetchone()
|
row = cur.fetchone()
|
||||||
|
|
||||||
if not row or row['data_points'] == 0:
|
if not row or row["day_count"] == 0:
|
||||||
return {
|
return {
|
||||||
"kcal_avg": 0.0,
|
"kcal_avg": 0.0,
|
||||||
"protein_avg": 0.0,
|
"protein_avg": 0.0,
|
||||||
@@ -80,7 +171,7 @@ def get_nutrition_average_data(
|
|||||||
"days_analyzed": days
|
"days_analyzed": days
|
||||||
}
|
}
|
||||||
|
|
||||||
data_points = row['data_points']
|
data_points = row["day_count"]
|
||||||
confidence = calculate_confidence(data_points, days, "general")
|
confidence = calculate_confidence(data_points, days, "general")
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -190,79 +281,73 @@ def get_energy_balance_data(
|
|||||||
days: int = 7
|
days: int = 7
|
||||||
) -> Dict:
|
) -> Dict:
|
||||||
"""
|
"""
|
||||||
Calculate energy balance (intake - estimated expenditure).
|
Energy balance (intake - estimated expenditure), kcal/day.
|
||||||
|
|
||||||
Note: This is a simplified calculation.
|
Intake: mean of daily total kcal (sum per calendar day).
|
||||||
For accurate TDEE, use profile-based calculations.
|
TDEE: estimate_tdee_kcal_from_latest_weight (MSJ × PAL oder kg-Fallback).
|
||||||
|
|
||||||
Args:
|
|
||||||
profile_id: User profile ID
|
|
||||||
days: Analysis window (default 7)
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
{
|
|
||||||
"energy_balance": float, # kcal/day (negative = deficit)
|
|
||||||
"avg_intake": float,
|
|
||||||
"estimated_tdee": float,
|
|
||||||
"status": str, # "deficit" | "surplus" | "maintenance"
|
|
||||||
"confidence": str,
|
|
||||||
"days_analyzed": int,
|
|
||||||
"data_points": int
|
|
||||||
}
|
|
||||||
"""
|
"""
|
||||||
with get_db() as conn:
|
with get_db() as conn:
|
||||||
cur = get_cursor(conn)
|
cur = get_cursor(conn)
|
||||||
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
||||||
|
|
||||||
# Get average intake
|
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"""SELECT AVG(kcal) as avg_kcal, COUNT(*) as cnt
|
"""SELECT date, SUM(kcal)::float AS daily_kcal
|
||||||
FROM nutrition_log
|
FROM nutrition_log
|
||||||
WHERE profile_id=%s AND date >= %s AND kcal IS NOT NULL""",
|
WHERE profile_id=%s AND date >= %s AND kcal IS NOT NULL
|
||||||
(profile_id, cutoff)
|
GROUP BY date
|
||||||
|
ORDER BY date""",
|
||||||
|
(profile_id, cutoff),
|
||||||
)
|
)
|
||||||
row = cur.fetchone()
|
daily_rows = cur.fetchall()
|
||||||
|
|
||||||
if not row or row['cnt'] == 0:
|
|
||||||
return {
|
|
||||||
"energy_balance": 0.0,
|
|
||||||
"avg_intake": 0.0,
|
|
||||||
"estimated_tdee": 0.0,
|
|
||||||
"status": "unknown",
|
|
||||||
"confidence": "insufficient",
|
|
||||||
"days_analyzed": days,
|
|
||||||
"data_points": 0
|
|
||||||
}
|
|
||||||
|
|
||||||
avg_intake = safe_float(row['avg_kcal'])
|
|
||||||
data_points = row['cnt']
|
|
||||||
|
|
||||||
# Simple TDEE estimation (this should be improved with profile data)
|
|
||||||
# For now, use a rough estimate: 2500 kcal for average adult
|
|
||||||
estimated_tdee = 2500.0 # TODO: Calculate from profile (weight, height, age, activity)
|
|
||||||
|
|
||||||
energy_balance = avg_intake - estimated_tdee
|
|
||||||
|
|
||||||
# Determine status
|
|
||||||
if energy_balance < -200:
|
|
||||||
status = "deficit"
|
|
||||||
elif energy_balance > 200:
|
|
||||||
status = "surplus"
|
|
||||||
else:
|
|
||||||
status = "maintenance"
|
|
||||||
|
|
||||||
confidence = calculate_confidence(data_points, days, "general")
|
|
||||||
|
|
||||||
|
if not daily_rows:
|
||||||
return {
|
return {
|
||||||
"energy_balance": energy_balance,
|
"energy_balance": 0.0,
|
||||||
|
"avg_intake": 0.0,
|
||||||
|
"estimated_tdee": 0.0,
|
||||||
|
"status": "unknown",
|
||||||
|
"confidence": "insufficient",
|
||||||
|
"days_analyzed": days,
|
||||||
|
"data_points": 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
daily_totals = [safe_float(r["daily_kcal"]) for r in daily_rows]
|
||||||
|
avg_intake = sum(daily_totals) / len(daily_totals)
|
||||||
|
data_points = len(daily_totals)
|
||||||
|
|
||||||
|
estimated_tdee = estimate_tdee_kcal_from_latest_weight(profile_id)
|
||||||
|
if estimated_tdee is None:
|
||||||
|
return {
|
||||||
|
"energy_balance": 0.0,
|
||||||
"avg_intake": avg_intake,
|
"avg_intake": avg_intake,
|
||||||
"estimated_tdee": estimated_tdee,
|
"estimated_tdee": 0.0,
|
||||||
"status": status,
|
"status": "unknown",
|
||||||
"confidence": confidence,
|
"confidence": "insufficient",
|
||||||
"days_analyzed": days,
|
"days_analyzed": days,
|
||||||
"data_points": data_points
|
"data_points": data_points
|
||||||
}
|
}
|
||||||
|
|
||||||
|
energy_balance = avg_intake - estimated_tdee
|
||||||
|
|
||||||
|
if energy_balance < -200:
|
||||||
|
status = "deficit"
|
||||||
|
elif energy_balance > 200:
|
||||||
|
status = "surplus"
|
||||||
|
else:
|
||||||
|
status = "maintenance"
|
||||||
|
|
||||||
|
confidence = calculate_confidence(data_points, days, "general")
|
||||||
|
|
||||||
|
return {
|
||||||
|
"energy_balance": energy_balance,
|
||||||
|
"avg_intake": avg_intake,
|
||||||
|
"estimated_tdee": estimated_tdee,
|
||||||
|
"status": status,
|
||||||
|
"confidence": confidence,
|
||||||
|
"days_analyzed": days,
|
||||||
|
"data_points": data_points
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def get_protein_adequacy_data(
|
def get_protein_adequacy_data(
|
||||||
profile_id: str,
|
profile_id: str,
|
||||||
@@ -291,7 +376,6 @@ def get_protein_adequacy_data(
|
|||||||
"confidence": str
|
"confidence": str
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
# Get protein targets
|
|
||||||
targets = get_protein_targets_data(profile_id)
|
targets = get_protein_targets_data(profile_id)
|
||||||
|
|
||||||
with get_db() as conn:
|
with get_db() as conn:
|
||||||
@@ -299,60 +383,55 @@ def get_protein_adequacy_data(
|
|||||||
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
||||||
|
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"""SELECT
|
"""SELECT COALESCE(SUM(protein_g), 0)::float AS daily_protein
|
||||||
AVG(protein_g) as avg_protein,
|
|
||||||
COUNT(*) as cnt,
|
|
||||||
SUM(CASE WHEN protein_g >= %s AND protein_g <= %s THEN 1 ELSE 0 END) as days_in_target
|
|
||||||
FROM nutrition_log
|
FROM nutrition_log
|
||||||
WHERE profile_id=%s AND date >= %s AND protein_g IS NOT NULL""",
|
WHERE profile_id=%s AND date >= %s
|
||||||
(targets['protein_target_low'], targets['protein_target_high'], profile_id, cutoff)
|
GROUP BY date""",
|
||||||
|
(profile_id, cutoff),
|
||||||
)
|
)
|
||||||
row = cur.fetchone()
|
rows = cur.fetchall()
|
||||||
|
|
||||||
if not row or row['cnt'] == 0:
|
|
||||||
return {
|
|
||||||
"adequacy_score": 0,
|
|
||||||
"avg_protein_g": 0.0,
|
|
||||||
"target_protein_low": targets['protein_target_low'],
|
|
||||||
"target_protein_high": targets['protein_target_high'],
|
|
||||||
"protein_g_per_kg": 0.0,
|
|
||||||
"days_in_target": 0,
|
|
||||||
"days_with_data": 0,
|
|
||||||
"confidence": "insufficient"
|
|
||||||
}
|
|
||||||
|
|
||||||
avg_protein = safe_float(row['avg_protein'])
|
|
||||||
days_with_data = row['cnt']
|
|
||||||
days_in_target = row['days_in_target']
|
|
||||||
|
|
||||||
protein_g_per_kg = avg_protein / targets['current_weight'] if targets['current_weight'] > 0 else 0.0
|
|
||||||
|
|
||||||
# Calculate adequacy score
|
|
||||||
# 100 = always in target range
|
|
||||||
# Scale based on percentage of days in target + average relative to target
|
|
||||||
target_pct = (days_in_target / days_with_data * 100) if days_with_data > 0 else 0
|
|
||||||
|
|
||||||
# Bonus/penalty for average protein level
|
|
||||||
target_mid = (targets['protein_target_low'] + targets['protein_target_high']) / 2
|
|
||||||
avg_vs_target = (avg_protein / target_mid) if target_mid > 0 else 0
|
|
||||||
|
|
||||||
# Weighted score: 70% target days, 30% average level
|
|
||||||
adequacy_score = int(target_pct * 0.7 + min(avg_vs_target * 100, 100) * 0.3)
|
|
||||||
adequacy_score = max(0, min(100, adequacy_score)) # Clamp to 0-100
|
|
||||||
|
|
||||||
confidence = calculate_confidence(days_with_data, days, "general")
|
|
||||||
|
|
||||||
|
if not rows or targets.get("confidence") == "insufficient" or targets["current_weight"] <= 0:
|
||||||
return {
|
return {
|
||||||
"adequacy_score": adequacy_score,
|
"adequacy_score": 0,
|
||||||
"avg_protein_g": avg_protein,
|
"avg_protein_g": 0.0,
|
||||||
"target_protein_low": targets['protein_target_low'],
|
"target_protein_low": targets['protein_target_low'],
|
||||||
"target_protein_high": targets['protein_target_high'],
|
"target_protein_high": targets['protein_target_high'],
|
||||||
"protein_g_per_kg": protein_g_per_kg,
|
"protein_g_per_kg": 0.0,
|
||||||
"days_in_target": days_in_target,
|
"days_in_target": 0,
|
||||||
"days_with_data": days_with_data,
|
"days_with_data": 0,
|
||||||
"confidence": confidence
|
"confidence": "insufficient"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
daily_totals = [safe_float(r["daily_protein"]) for r in rows]
|
||||||
|
days_with_data = len(daily_totals)
|
||||||
|
low = targets["protein_target_low"]
|
||||||
|
high = targets["protein_target_high"]
|
||||||
|
days_in_target = sum(1 for d in daily_totals if low <= d <= high)
|
||||||
|
|
||||||
|
avg_protein = sum(daily_totals) / days_with_data
|
||||||
|
protein_g_per_kg = avg_protein / targets["current_weight"] if targets["current_weight"] > 0 else 0.0
|
||||||
|
|
||||||
|
target_pct = (days_in_target / days_with_data * 100) if days_with_data > 0 else 0
|
||||||
|
target_mid = (low + high) / 2
|
||||||
|
avg_vs_target = (avg_protein / target_mid) if target_mid > 0 else 0
|
||||||
|
|
||||||
|
adequacy_score = int(target_pct * 0.7 + min(avg_vs_target * 100, 100) * 0.3)
|
||||||
|
adequacy_score = max(0, min(100, adequacy_score))
|
||||||
|
|
||||||
|
confidence = calculate_confidence(days_with_data, days, "general")
|
||||||
|
|
||||||
|
return {
|
||||||
|
"adequacy_score": adequacy_score,
|
||||||
|
"avg_protein_g": avg_protein,
|
||||||
|
"target_protein_low": targets['protein_target_low'],
|
||||||
|
"target_protein_high": targets['protein_target_high'],
|
||||||
|
"protein_g_per_kg": protein_g_per_kg,
|
||||||
|
"days_in_target": days_in_target,
|
||||||
|
"days_with_data": days_with_data,
|
||||||
|
"confidence": confidence
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def get_macro_consistency_data(
|
def get_macro_consistency_data(
|
||||||
profile_id: str,
|
profile_id: str,
|
||||||
@@ -387,16 +466,18 @@ def get_macro_consistency_data(
|
|||||||
|
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"""SELECT
|
"""SELECT
|
||||||
protein_g, carbs_g, fat_g, kcal
|
COALESCE(SUM(kcal), 0)::float AS kcal,
|
||||||
|
COALESCE(SUM(protein_g), 0)::float AS protein_g,
|
||||||
|
COALESCE(SUM(carbs_g), 0)::float AS carbs_g,
|
||||||
|
COALESCE(SUM(fat_g), 0)::float AS fat_g
|
||||||
FROM nutrition_log
|
FROM nutrition_log
|
||||||
WHERE profile_id=%s
|
WHERE profile_id=%s AND date >= %s
|
||||||
AND date >= %s
|
GROUP BY date
|
||||||
AND protein_g IS NOT NULL
|
HAVING COALESCE(SUM(kcal), 0) > 0
|
||||||
AND carbs_g IS NOT NULL
|
AND COALESCE(SUM(protein_g), 0) > 0
|
||||||
AND fat_g IS NOT NULL
|
AND COALESCE(SUM(carbs_g), 0) > 0
|
||||||
AND kcal > 0
|
AND COALESCE(SUM(fat_g), 0) > 0""",
|
||||||
ORDER BY date""",
|
(profile_id, cutoff),
|
||||||
(profile_id, cutoff)
|
|
||||||
)
|
)
|
||||||
rows = cur.fetchall()
|
rows = cur.fetchall()
|
||||||
|
|
||||||
@@ -413,7 +494,6 @@ def get_macro_consistency_data(
|
|||||||
"data_points": len(rows)
|
"data_points": len(rows)
|
||||||
}
|
}
|
||||||
|
|
||||||
# Calculate macro percentages for each day
|
|
||||||
import statistics
|
import statistics
|
||||||
|
|
||||||
protein_pcts = []
|
protein_pcts = []
|
||||||
@@ -425,7 +505,6 @@ def get_macro_consistency_data(
|
|||||||
if total_kcal == 0:
|
if total_kcal == 0:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Convert grams to kcal (protein=4, carbs=4, fat=9)
|
|
||||||
protein_kcal = safe_float(row['protein_g']) * 4
|
protein_kcal = safe_float(row['protein_g']) * 4
|
||||||
carbs_kcal = safe_float(row['carbs_g']) * 4
|
carbs_kcal = safe_float(row['carbs_g']) * 4
|
||||||
fat_kcal = safe_float(row['fat_g']) * 9
|
fat_kcal = safe_float(row['fat_g']) * 9
|
||||||
@@ -491,50 +570,15 @@ def get_macro_consistency_data(
|
|||||||
|
|
||||||
def calculate_energy_balance_7d(profile_id: str) -> Optional[float]:
|
def calculate_energy_balance_7d(profile_id: str) -> Optional[float]:
|
||||||
"""
|
"""
|
||||||
Calculate 7-day average energy balance (kcal/day)
|
7-day mean energy balance (kcal/day), same rules as get_energy_balance_data(..., 7).
|
||||||
Positive = surplus, Negative = deficit
|
|
||||||
|
|
||||||
Migration from Phase 0b:
|
|
||||||
Used by placeholders that need single balance value
|
|
||||||
"""
|
"""
|
||||||
with get_db() as conn:
|
data = get_energy_balance_data(profile_id, 7)
|
||||||
cur = get_cursor(conn)
|
if data["data_points"] < 4:
|
||||||
cur.execute("""
|
return None
|
||||||
SELECT kcal
|
tdee = data.get("estimated_tdee") or 0
|
||||||
FROM nutrition_log
|
if tdee <= 0:
|
||||||
WHERE profile_id = %s
|
return None
|
||||||
AND date >= CURRENT_DATE - INTERVAL '7 days'
|
return round(float(data["energy_balance"]), 0)
|
||||||
ORDER BY date DESC
|
|
||||||
""", (profile_id,))
|
|
||||||
|
|
||||||
calories = [row['kcal'] for row in cur.fetchall()]
|
|
||||||
|
|
||||||
if len(calories) < 4: # Need at least 4 days
|
|
||||||
return None
|
|
||||||
|
|
||||||
avg_intake = float(sum(calories) / len(calories))
|
|
||||||
|
|
||||||
# Get estimated TDEE (simplified - could use Harris-Benedict)
|
|
||||||
# For now, use weight-based estimate
|
|
||||||
cur.execute("""
|
|
||||||
SELECT weight
|
|
||||||
FROM weight_log
|
|
||||||
WHERE profile_id = %s
|
|
||||||
ORDER BY date DESC
|
|
||||||
LIMIT 1
|
|
||||||
""", (profile_id,))
|
|
||||||
|
|
||||||
weight_row = cur.fetchone()
|
|
||||||
if not weight_row:
|
|
||||||
return None
|
|
||||||
|
|
||||||
# Simple TDEE estimate: bodyweight (kg) × 30-35
|
|
||||||
# TODO: Improve with activity level, age, gender
|
|
||||||
estimated_tdee = float(weight_row['weight']) * 32.5
|
|
||||||
|
|
||||||
balance = avg_intake - estimated_tdee
|
|
||||||
|
|
||||||
return round(balance, 0)
|
|
||||||
|
|
||||||
|
|
||||||
def calculate_energy_deficit_surplus(profile_id: str, days: int = 7) -> Optional[str]:
|
def calculate_energy_deficit_surplus(profile_id: str, days: int = 7) -> Optional[str]:
|
||||||
@@ -654,15 +698,14 @@ def calculate_protein_days_in_target(profile_id: str, target_low: float = 1.6, t
|
|||||||
|
|
||||||
def calculate_protein_adequacy_28d(profile_id: str) -> Optional[int]:
|
def calculate_protein_adequacy_28d(profile_id: str) -> Optional[int]:
|
||||||
"""
|
"""
|
||||||
Protein adequacy score 0-100 (last 28 days)
|
Protein adequacy score 0-100 (last 28 days).
|
||||||
Based on consistency and target achievement
|
Uses per-calendar-day total protein vs. average weight in the window (g/kg per day).
|
||||||
"""
|
"""
|
||||||
import statistics
|
import statistics
|
||||||
|
|
||||||
with get_db() as conn:
|
with get_db() as conn:
|
||||||
cur = get_cursor(conn)
|
cur = get_cursor(conn)
|
||||||
|
|
||||||
# Get average weight (28d)
|
|
||||||
cur.execute("""
|
cur.execute("""
|
||||||
SELECT AVG(weight) as avg_weight
|
SELECT AVG(weight) as avg_weight
|
||||||
FROM weight_log
|
FROM weight_log
|
||||||
@@ -676,38 +719,29 @@ def calculate_protein_adequacy_28d(profile_id: str) -> Optional[int]:
|
|||||||
|
|
||||||
weight = float(weight_row['avg_weight'])
|
weight = float(weight_row['avg_weight'])
|
||||||
|
|
||||||
# Get protein intake (28d)
|
|
||||||
cur.execute("""
|
cur.execute("""
|
||||||
SELECT protein_g
|
SELECT COALESCE(SUM(protein_g), 0)::float AS daily_protein
|
||||||
FROM nutrition_log
|
FROM nutrition_log
|
||||||
WHERE profile_id = %s
|
WHERE profile_id = %s
|
||||||
AND date >= CURRENT_DATE - INTERVAL '28 days'
|
AND date >= CURRENT_DATE - INTERVAL '28 days'
|
||||||
AND protein_g IS NOT NULL
|
GROUP BY date
|
||||||
""", (profile_id,))
|
""", (profile_id,))
|
||||||
|
|
||||||
protein_values = [float(row['protein_g']) for row in cur.fetchall()]
|
daily_totals = [float(row['daily_protein']) for row in cur.fetchall()]
|
||||||
|
|
||||||
if len(protein_values) < 18: # 60% coverage
|
if len(daily_totals) < 18:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Calculate metrics
|
protein_per_kg_values = [p / weight for p in daily_totals]
|
||||||
protein_per_kg_values = [p / weight for p in protein_values]
|
|
||||||
avg_protein_per_kg = sum(protein_per_kg_values) / len(protein_per_kg_values)
|
avg_protein_per_kg = sum(protein_per_kg_values) / len(protein_per_kg_values)
|
||||||
|
|
||||||
# Target range: 1.6-2.2 g/kg for active individuals
|
|
||||||
target_mid = 1.9
|
|
||||||
|
|
||||||
# Score based on distance from target
|
|
||||||
if 1.6 <= avg_protein_per_kg <= 2.2:
|
if 1.6 <= avg_protein_per_kg <= 2.2:
|
||||||
base_score = 100
|
base_score = 100
|
||||||
elif avg_protein_per_kg < 1.6:
|
elif avg_protein_per_kg < 1.6:
|
||||||
# Below target
|
|
||||||
base_score = max(40, 100 - ((1.6 - avg_protein_per_kg) * 40))
|
base_score = max(40, 100 - ((1.6 - avg_protein_per_kg) * 40))
|
||||||
else:
|
else:
|
||||||
# Above target (less penalty)
|
|
||||||
base_score = max(80, 100 - ((avg_protein_per_kg - 2.2) * 10))
|
base_score = max(80, 100 - ((avg_protein_per_kg - 2.2) * 10))
|
||||||
|
|
||||||
# Consistency bonus/penalty
|
|
||||||
std_dev = statistics.stdev(protein_per_kg_values)
|
std_dev = statistics.stdev(protein_per_kg_values)
|
||||||
if std_dev < 0.3:
|
if std_dev < 0.3:
|
||||||
consistency_bonus = 10
|
consistency_bonus = 10
|
||||||
@@ -723,20 +757,24 @@ def calculate_protein_adequacy_28d(profile_id: str) -> Optional[int]:
|
|||||||
|
|
||||||
def calculate_macro_consistency_score(profile_id: str) -> Optional[int]:
|
def calculate_macro_consistency_score(profile_id: str) -> Optional[int]:
|
||||||
"""
|
"""
|
||||||
Macro consistency score 0-100 (last 28 days)
|
Macro consistency score 0-100 (last 28 days).
|
||||||
Lower variability = higher score
|
CV of daily totals (kcal and macros), not raw log rows.
|
||||||
"""
|
"""
|
||||||
import statistics
|
import statistics
|
||||||
|
|
||||||
with get_db() as conn:
|
with get_db() as conn:
|
||||||
cur = get_cursor(conn)
|
cur = get_cursor(conn)
|
||||||
cur.execute("""
|
cur.execute("""
|
||||||
SELECT kcal, protein_g, fat_g, carbs_g
|
SELECT
|
||||||
|
COALESCE(SUM(kcal), 0)::float AS dk,
|
||||||
|
COALESCE(SUM(protein_g), 0)::float AS dp,
|
||||||
|
COALESCE(SUM(fat_g), 0)::float AS df,
|
||||||
|
COALESCE(SUM(carbs_g), 0)::float AS dc
|
||||||
FROM nutrition_log
|
FROM nutrition_log
|
||||||
WHERE profile_id = %s
|
WHERE profile_id = %s
|
||||||
AND date >= CURRENT_DATE - INTERVAL '28 days'
|
AND date >= CURRENT_DATE - INTERVAL '28 days'
|
||||||
AND kcal IS NOT NULL
|
GROUP BY date
|
||||||
ORDER BY date DESC
|
HAVING COALESCE(SUM(kcal), 0) > 0
|
||||||
""", (profile_id,))
|
""", (profile_id,))
|
||||||
|
|
||||||
data = cur.fetchall()
|
data = cur.fetchall()
|
||||||
@@ -744,9 +782,7 @@ def calculate_macro_consistency_score(profile_id: str) -> Optional[int]:
|
|||||||
if len(data) < 18:
|
if len(data) < 18:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Calculate coefficient of variation for each macro
|
|
||||||
def cv(values):
|
def cv(values):
|
||||||
"""Coefficient of variation (std_dev / mean)"""
|
|
||||||
if not values or len(values) < 2:
|
if not values or len(values) < 2:
|
||||||
return None
|
return None
|
||||||
mean = sum(values) / len(values)
|
mean = sum(values) / len(values)
|
||||||
@@ -755,10 +791,10 @@ def calculate_macro_consistency_score(profile_id: str) -> Optional[int]:
|
|||||||
std_dev = statistics.stdev(values)
|
std_dev = statistics.stdev(values)
|
||||||
return std_dev / mean
|
return std_dev / mean
|
||||||
|
|
||||||
calories_cv = cv([d['kcal'] for d in data])
|
calories_cv = cv([d['dk'] for d in data])
|
||||||
protein_cv = cv([d['protein_g'] for d in data if d['protein_g']])
|
protein_cv = cv([d['dp'] for d in data if d['dp']])
|
||||||
fat_cv = cv([d['fat_g'] for d in data if d['fat_g']])
|
fat_cv = cv([d['df'] for d in data if d['df']])
|
||||||
carbs_cv = cv([d['carbs_g'] for d in data if d['carbs_g']])
|
carbs_cv = cv([d['dc'] for d in data if d['dc']])
|
||||||
|
|
||||||
cv_values = [v for v in [calories_cv, protein_cv, fat_cv, carbs_cv] if v is not None]
|
cv_values = [v for v in [calories_cv, protein_cv, fat_cv, carbs_cv] if v is not None]
|
||||||
|
|
||||||
@@ -767,9 +803,6 @@ def calculate_macro_consistency_score(profile_id: str) -> Optional[int]:
|
|||||||
|
|
||||||
avg_cv = sum(cv_values) / len(cv_values)
|
avg_cv = sum(cv_values) / len(cv_values)
|
||||||
|
|
||||||
# Score: lower CV = higher score
|
|
||||||
# CV < 0.2 = excellent consistency
|
|
||||||
# CV > 0.5 = poor consistency
|
|
||||||
if avg_cv < 0.2:
|
if avg_cv < 0.2:
|
||||||
score = 100
|
score = 100
|
||||||
elif avg_cv < 0.3:
|
elif avg_cv < 0.3:
|
||||||
@@ -811,14 +844,16 @@ def calculate_nutrition_score(profile_id: str, focus_weights: Optional[Dict] = N
|
|||||||
from data_layer.scores import get_user_focus_weights
|
from data_layer.scores import get_user_focus_weights
|
||||||
focus_weights = get_user_focus_weights(profile_id)
|
focus_weights = get_user_focus_weights(profile_id)
|
||||||
|
|
||||||
# Nutrition-related focus areas (English keys from DB)
|
# Nutrition-related focus areas (English keys from DB; Gewichte immer float)
|
||||||
protein_intake = focus_weights.get('protein_intake', 0)
|
protein_intake = float(focus_weights.get('protein_intake', 0) or 0)
|
||||||
calorie_balance = focus_weights.get('calorie_balance', 0)
|
calorie_balance = float(focus_weights.get('calorie_balance', 0) or 0)
|
||||||
macro_consistency = focus_weights.get('macro_consistency', 0)
|
macro_consistency = float(focus_weights.get('macro_consistency', 0) or 0)
|
||||||
meal_timing = focus_weights.get('meal_timing', 0)
|
meal_timing = float(focus_weights.get('meal_timing', 0) or 0)
|
||||||
hydration = focus_weights.get('hydration', 0)
|
hydration = float(focus_weights.get('hydration', 0) or 0)
|
||||||
|
|
||||||
total_nutrition_weight = protein_intake + calorie_balance + macro_consistency + meal_timing + hydration
|
total_nutrition_weight = (
|
||||||
|
protein_intake + calorie_balance + macro_consistency + meal_timing + hydration
|
||||||
|
)
|
||||||
|
|
||||||
if total_nutrition_weight == 0:
|
if total_nutrition_weight == 0:
|
||||||
return None # No nutrition goals
|
return None # No nutrition goals
|
||||||
@@ -853,40 +888,66 @@ def calculate_nutrition_score(profile_id: str, focus_weights: Optional[Dict] = N
|
|||||||
if not components:
|
if not components:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Weighted average
|
# Weighted average (float: DB-Werte können Decimal sein)
|
||||||
total_score = sum(score * weight for _, score, weight in components)
|
total_score = sum(float(score) * float(weight) for _, score, weight in components)
|
||||||
total_weight = sum(weight for _, _, weight in components)
|
total_weight = sum(float(weight) for _, _, weight in components)
|
||||||
|
|
||||||
return int(total_score / total_weight)
|
return int(total_score / total_weight)
|
||||||
|
|
||||||
|
|
||||||
def _score_calorie_adherence(profile_id: str) -> Optional[int]:
|
def _score_calorie_adherence(profile_id: str) -> Optional[int]:
|
||||||
"""Score calorie target adherence (0-100)"""
|
"""Score calorie target adherence (0–100) using 7d balance vs profiles.goal_mode."""
|
||||||
# Check for energy balance goal
|
|
||||||
# For now, use energy balance calculation
|
|
||||||
balance = calculate_energy_balance_7d(profile_id)
|
balance = calculate_energy_balance_7d(profile_id)
|
||||||
|
|
||||||
if balance is None:
|
if balance is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Score based on whether deficit/surplus aligns with goal
|
mode = _get_profile_goal_mode(profile_id)
|
||||||
# Simplified: assume weight loss goal = deficit is good
|
b = float(balance)
|
||||||
# TODO: Check actual goal type
|
|
||||||
|
|
||||||
abs_balance = abs(balance)
|
def _weight_loss(x: float) -> int:
|
||||||
|
if -550 <= x <= -250:
|
||||||
|
return 100
|
||||||
|
if x > 450:
|
||||||
|
return 38
|
||||||
|
if -750 <= x < -550 or -250 < x <= 120:
|
||||||
|
return 82
|
||||||
|
if x < -1200:
|
||||||
|
return 52
|
||||||
|
if -950 <= x < -750 or 120 < x <= 350:
|
||||||
|
return 68
|
||||||
|
return 58
|
||||||
|
|
||||||
# Moderate deficit/surplus = good
|
def _surplus_friendly(x: float) -> int:
|
||||||
if 200 <= abs_balance <= 500:
|
if 80 <= x <= 480:
|
||||||
return 100
|
return 100
|
||||||
elif 100 <= abs_balance <= 700:
|
if -120 <= x < 80 or 480 < x <= 700:
|
||||||
return 85
|
return 86
|
||||||
elif abs_balance <= 900:
|
if -380 <= x < -120:
|
||||||
return 70
|
return 68
|
||||||
elif abs_balance <= 1200:
|
if x > 850:
|
||||||
return 55
|
return 54
|
||||||
else:
|
if x < -650:
|
||||||
|
return 44
|
||||||
|
return 72
|
||||||
|
|
||||||
|
def _maintenance(x: float) -> int:
|
||||||
|
a = abs(x)
|
||||||
|
if a <= 200:
|
||||||
|
return 100
|
||||||
|
if a <= 400:
|
||||||
|
return 84
|
||||||
|
if a <= 650:
|
||||||
|
return 70
|
||||||
|
if a <= 900:
|
||||||
|
return 55
|
||||||
return 40
|
return 40
|
||||||
|
|
||||||
|
if mode == "weight_loss":
|
||||||
|
return _weight_loss(b)
|
||||||
|
if mode in ("strength", "recomposition"):
|
||||||
|
return _surplus_friendly(b)
|
||||||
|
return _maintenance(b)
|
||||||
|
|
||||||
|
|
||||||
def _score_macro_balance(profile_id: str) -> Optional[int]:
|
def _score_macro_balance(profile_id: str) -> Optional[int]:
|
||||||
"""Score macro balance (0-100)"""
|
"""Score macro balance (0-100)"""
|
||||||
|
|||||||
@@ -15,12 +15,50 @@ Phase 0c: Multi-Layer Architecture
|
|||||||
Version: 1.0
|
Version: 1.0
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from typing import Dict, List, Optional
|
import json
|
||||||
|
from typing import Dict, List, Optional, Any
|
||||||
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
|
||||||
from data_layer.utils import calculate_confidence, safe_float, safe_int
|
from data_layer.utils import calculate_confidence, safe_float, safe_int
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_sleep_segments(raw: Any) -> Optional[List[dict]]:
|
||||||
|
"""JSONB kann dict/list/str sein; ungültig → None."""
|
||||||
|
if raw is None:
|
||||||
|
return None
|
||||||
|
if isinstance(raw, str):
|
||||||
|
try:
|
||||||
|
raw = json.loads(raw)
|
||||||
|
except (json.JSONDecodeError, TypeError):
|
||||||
|
return None
|
||||||
|
if not isinstance(raw, list):
|
||||||
|
return None
|
||||||
|
return raw
|
||||||
|
|
||||||
|
|
||||||
|
def _segment_minutes(seg: Any) -> int:
|
||||||
|
if not isinstance(seg, dict):
|
||||||
|
return 0
|
||||||
|
for key in ("duration_min", "duration_minutes", "minutes"):
|
||||||
|
v = seg.get(key)
|
||||||
|
if v is not None:
|
||||||
|
return max(0, safe_int(v))
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def _normalize_sleep_phase(seg: dict) -> str:
|
||||||
|
"""Kleinbuchstaben; Apple „Core“-Schlaf wird wie light gewertet."""
|
||||||
|
if not isinstance(seg, dict):
|
||||||
|
return ""
|
||||||
|
p = seg.get("phase")
|
||||||
|
if p is None:
|
||||||
|
return ""
|
||||||
|
s = str(p).strip().lower()
|
||||||
|
if s in ("core", "asleep"):
|
||||||
|
return "light"
|
||||||
|
return s
|
||||||
|
|
||||||
|
|
||||||
def get_sleep_duration_data(
|
def get_sleep_duration_data(
|
||||||
profile_id: str,
|
profile_id: str,
|
||||||
days: int = 7
|
days: int = 7
|
||||||
@@ -51,7 +89,7 @@ def get_sleep_duration_data(
|
|||||||
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
||||||
|
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"""SELECT sleep_segments FROM sleep_log
|
"""SELECT sleep_segments, duration_minutes FROM sleep_log
|
||||||
WHERE profile_id=%s AND date >= %s
|
WHERE profile_id=%s AND date >= %s
|
||||||
ORDER BY date DESC""",
|
ORDER BY date DESC""",
|
||||||
(profile_id, cutoff)
|
(profile_id, cutoff)
|
||||||
@@ -72,12 +110,17 @@ def get_sleep_duration_data(
|
|||||||
nights_with_data = 0
|
nights_with_data = 0
|
||||||
|
|
||||||
for row in rows:
|
for row in rows:
|
||||||
segments = row['sleep_segments']
|
night_minutes = 0
|
||||||
|
segments = _parse_sleep_segments(row.get("sleep_segments"))
|
||||||
if segments:
|
if segments:
|
||||||
night_minutes = sum(seg.get('duration_min', 0) for seg in segments)
|
night_minutes = sum(_segment_minutes(seg) for seg in segments)
|
||||||
if night_minutes > 0:
|
if night_minutes <= 0:
|
||||||
total_minutes += night_minutes
|
dm = row.get("duration_minutes")
|
||||||
nights_with_data += 1
|
if dm is not None:
|
||||||
|
night_minutes = max(0, safe_int(dm))
|
||||||
|
if night_minutes > 0:
|
||||||
|
total_minutes += night_minutes
|
||||||
|
nights_with_data += 1
|
||||||
|
|
||||||
if nights_with_data == 0:
|
if nights_with_data == 0:
|
||||||
return {
|
return {
|
||||||
@@ -136,7 +179,9 @@ def get_sleep_quality_data(
|
|||||||
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
||||||
|
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"""SELECT sleep_segments FROM sleep_log
|
"""SELECT sleep_segments, duration_minutes, deep_minutes, rem_minutes,
|
||||||
|
light_minutes, awake_minutes
|
||||||
|
FROM sleep_log
|
||||||
WHERE profile_id=%s AND date >= %s
|
WHERE profile_id=%s AND date >= %s
|
||||||
ORDER BY date DESC""",
|
ORDER BY date DESC""",
|
||||||
(profile_id, cutoff)
|
(profile_id, cutoff)
|
||||||
@@ -163,15 +208,29 @@ def get_sleep_quality_data(
|
|||||||
count = 0
|
count = 0
|
||||||
|
|
||||||
for row in rows:
|
for row in rows:
|
||||||
segments = row['sleep_segments']
|
deep_rem_min = light_min = awake_min = 0
|
||||||
if segments:
|
total_min = 0
|
||||||
# Note: segments use 'phase' key, stored lowercase (deep, rem, light, awake)
|
used_segments = False
|
||||||
deep_rem_min = sum(s.get('duration_min', 0) for s in segments if s.get('phase') in ['deep', 'rem'])
|
|
||||||
light_min = sum(s.get('duration_min', 0) for s in segments if s.get('phase') == 'light')
|
|
||||||
awake_min = sum(s.get('duration_min', 0) for s in segments if s.get('phase') == 'awake')
|
|
||||||
total_min = sum(s.get('duration_min', 0) for s in segments)
|
|
||||||
|
|
||||||
|
segments = _parse_sleep_segments(row.get("sleep_segments"))
|
||||||
|
if segments:
|
||||||
|
total_min = sum(_segment_minutes(s) for s in segments)
|
||||||
if total_min > 0:
|
if total_min > 0:
|
||||||
|
deep_rem_min = sum(
|
||||||
|
_segment_minutes(s)
|
||||||
|
for s in segments
|
||||||
|
if _normalize_sleep_phase(s) in ("deep", "rem")
|
||||||
|
)
|
||||||
|
light_min = sum(
|
||||||
|
_segment_minutes(s)
|
||||||
|
for s in segments
|
||||||
|
if _normalize_sleep_phase(s) == "light"
|
||||||
|
)
|
||||||
|
awake_min = sum(
|
||||||
|
_segment_minutes(s)
|
||||||
|
for s in segments
|
||||||
|
if _normalize_sleep_phase(s) == "awake"
|
||||||
|
)
|
||||||
quality_pct = (deep_rem_min / total_min) * 100
|
quality_pct = (deep_rem_min / total_min) * 100
|
||||||
total_quality += quality_pct
|
total_quality += quality_pct
|
||||||
total_deep_rem += deep_rem_min
|
total_deep_rem += deep_rem_min
|
||||||
@@ -179,6 +238,28 @@ def get_sleep_quality_data(
|
|||||||
total_awake += awake_min
|
total_awake += awake_min
|
||||||
total_all += total_min
|
total_all += total_min
|
||||||
count += 1
|
count += 1
|
||||||
|
used_segments = True
|
||||||
|
|
||||||
|
if not used_segments:
|
||||||
|
d, r, l, a = (
|
||||||
|
row.get("deep_minutes"),
|
||||||
|
row.get("rem_minutes"),
|
||||||
|
row.get("light_minutes"),
|
||||||
|
row.get("awake_minutes"),
|
||||||
|
)
|
||||||
|
if d is not None or r is not None or l is not None:
|
||||||
|
di, ri, li = (d or 0), (r or 0), (l or 0)
|
||||||
|
phase_sum = di + ri + li
|
||||||
|
ai = (a or 0) if a is not None else 0
|
||||||
|
total_min = phase_sum + ai
|
||||||
|
if total_min > 0 and phase_sum > 0:
|
||||||
|
quality_pct = ((di + ri) / total_min) * 100
|
||||||
|
total_quality += quality_pct
|
||||||
|
total_deep_rem += di + ri
|
||||||
|
total_light += li
|
||||||
|
total_awake += ai
|
||||||
|
total_all += total_min
|
||||||
|
count += 1
|
||||||
|
|
||||||
if count == 0:
|
if count == 0:
|
||||||
return {
|
return {
|
||||||
@@ -351,8 +432,8 @@ def calculate_recovery_score_v2(profile_id: str) -> Optional[int]:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
# Weighted average
|
# Weighted average
|
||||||
total_score = sum(score * weight for _, score, weight in components)
|
total_score = sum(float(score) * float(weight) for _, score, weight in components)
|
||||||
total_weight = sum(weight for _, _, weight in components)
|
total_weight = sum(float(weight) for _, _, weight in components)
|
||||||
|
|
||||||
final_score = int(total_score / total_weight)
|
final_score = int(total_score / total_weight)
|
||||||
|
|
||||||
@@ -783,17 +864,24 @@ def calculate_sleep_quality_7d(profile_id: str) -> Optional[int]:
|
|||||||
|
|
||||||
quality_scores = []
|
quality_scores = []
|
||||||
for s in sleep_data:
|
for s in sleep_data:
|
||||||
if s['deep_minutes'] and s['rem_minutes']:
|
dur = s["duration_minutes"]
|
||||||
quality_pct = ((s['deep_minutes'] + s['rem_minutes']) / s['duration_minutes']) * 100
|
if not dur or dur <= 0:
|
||||||
# 40-60% deep+REM is good
|
continue
|
||||||
if quality_pct >= 45:
|
d = s["deep_minutes"]
|
||||||
quality_scores.append(100)
|
r = s["rem_minutes"]
|
||||||
elif quality_pct >= 35:
|
if d is None and r is None:
|
||||||
quality_scores.append(75)
|
continue
|
||||||
elif quality_pct >= 25:
|
di, ri = (d or 0), (r or 0)
|
||||||
quality_scores.append(50)
|
quality_pct = ((di + ri) / dur) * 100
|
||||||
else:
|
# 40-60% deep+REM is good
|
||||||
quality_scores.append(30)
|
if quality_pct >= 45:
|
||||||
|
quality_scores.append(100)
|
||||||
|
elif quality_pct >= 35:
|
||||||
|
quality_scores.append(75)
|
||||||
|
elif quality_pct >= 25:
|
||||||
|
quality_scores.append(50)
|
||||||
|
else:
|
||||||
|
quality_scores.append(30)
|
||||||
|
|
||||||
if not quality_scores:
|
if not quality_scores:
|
||||||
return None
|
return None
|
||||||
|
|||||||
@@ -202,29 +202,30 @@ def calculate_goal_progress_score(profile_id: str) -> Optional[int]:
|
|||||||
total_weight = 0.0
|
total_weight = 0.0
|
||||||
|
|
||||||
for focus_area_id, weight in focus_weights.items():
|
for focus_area_id, weight in focus_weights.items():
|
||||||
|
w = float(weight)
|
||||||
component = focus_to_component.get(focus_area_id)
|
component = focus_to_component.get(focus_area_id)
|
||||||
|
|
||||||
if component == 'body' and body_score is not None:
|
if component == 'body' and body_score is not None:
|
||||||
total_score += body_score * weight
|
total_score += float(body_score) * w
|
||||||
total_weight += weight
|
total_weight += w
|
||||||
elif component == 'nutrition' and nutrition_score is not None:
|
elif component == 'nutrition' and nutrition_score is not None:
|
||||||
total_score += nutrition_score * weight
|
total_score += float(nutrition_score) * w
|
||||||
total_weight += weight
|
total_weight += w
|
||||||
elif component == 'activity' and activity_score is not None:
|
elif component == 'activity' and activity_score is not None:
|
||||||
total_score += activity_score * weight
|
total_score += float(activity_score) * w
|
||||||
total_weight += weight
|
total_weight += w
|
||||||
elif component == 'recovery' and recovery_score is not None:
|
elif component == 'recovery' and recovery_score is not None:
|
||||||
total_score += recovery_score * weight
|
total_score += float(recovery_score) * w
|
||||||
total_weight += weight
|
total_weight += w
|
||||||
elif component == 'health' and health_risk_score is not None:
|
elif component == 'health' and health_risk_score is not None:
|
||||||
total_score += health_risk_score * weight
|
total_score += float(health_risk_score) * w
|
||||||
total_weight += weight
|
total_weight += w
|
||||||
|
|
||||||
if total_weight == 0:
|
if total_weight == 0:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Normalize to 0-100
|
# Normalize to 0-100 (Explizit float: Zwischensummen können Decimal aus DB sein)
|
||||||
final_score = total_score / total_weight
|
final_score = float(total_score) / float(total_weight)
|
||||||
|
|
||||||
return int(final_score)
|
return int(final_score)
|
||||||
|
|
||||||
@@ -282,9 +283,9 @@ def calculate_health_stability_score(profile_id: str) -> Optional[int]:
|
|||||||
|
|
||||||
activities = cur.fetchall()
|
activities = cur.fetchall()
|
||||||
if activities:
|
if activities:
|
||||||
total_minutes = sum(a['duration_min'] for a in activities)
|
total_minutes = float(sum(float(a['duration_min'] or 0) for a in activities))
|
||||||
# WHO recommends 150-300 min/week moderate activity
|
# WHO recommends 150-300 min/week moderate activity
|
||||||
movement_score = min(100, (total_minutes / 150) * 100)
|
movement_score = min(100.0, (total_minutes / 150) * 100)
|
||||||
components.append(('movement', movement_score, 20))
|
components.append(('movement', movement_score, 20))
|
||||||
|
|
||||||
# 4. Waist circumference risk (15%)
|
# 4. Waist circumference risk (15%)
|
||||||
@@ -328,8 +329,8 @@ def calculate_health_stability_score(profile_id: str) -> Optional[int]:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
# Weighted average
|
# Weighted average
|
||||||
total_score = sum(score * weight for _, score, weight in components)
|
total_score = sum(float(score) * float(weight) for _, score, weight in components)
|
||||||
total_weight = sum(weight for _, _, weight in components)
|
total_weight = sum(float(weight) for _, _, weight in components)
|
||||||
|
|
||||||
return int(total_score / total_weight)
|
return int(total_score / total_weight)
|
||||||
|
|
||||||
@@ -532,9 +533,19 @@ def calculate_focus_area_progress(profile_id: str, focus_area_id: str) -> Option
|
|||||||
if not goals:
|
if not goals:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Weighted average by contribution_weight
|
# Weighted average; progress_pct darf NULL sein (Ziele ohne quantitative Berechnung)
|
||||||
total_progress = sum(g['progress_pct'] * g['contribution_weight'] for g in goals)
|
parts: List[tuple] = []
|
||||||
total_weight = sum(g['contribution_weight'] for g in goals)
|
for g in goals:
|
||||||
|
pct = g['progress_pct']
|
||||||
|
if pct is None:
|
||||||
|
continue
|
||||||
|
parts.append((float(pct), float(g['contribution_weight'])))
|
||||||
|
|
||||||
|
if not parts:
|
||||||
|
return None
|
||||||
|
|
||||||
|
total_progress = sum(p * w for p, w in parts)
|
||||||
|
total_weight = sum(w for _, w in parts)
|
||||||
|
|
||||||
return int(total_progress / total_weight) if total_weight > 0 else None
|
return int(total_progress / total_weight) if total_weight > 0 else None
|
||||||
|
|
||||||
|
|||||||
+6
-4
@@ -29,7 +29,9 @@ def init_pool():
|
|||||||
user=os.getenv("DB_USER", "mitai"),
|
user=os.getenv("DB_USER", "mitai"),
|
||||||
password=os.getenv("DB_PASSWORD", "")
|
password=os.getenv("DB_PASSWORD", "")
|
||||||
)
|
)
|
||||||
print(f"✓ PostgreSQL connection pool initialized ({os.getenv('DB_HOST', 'postgres')}:{os.getenv('DB_PORT', '5432')})")
|
print(
|
||||||
|
f"[OK] PostgreSQL connection pool initialized ({os.getenv('DB_HOST', 'postgres')}:{os.getenv('DB_PORT', '5432')})"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
@@ -171,7 +173,7 @@ def init_db():
|
|||||||
) as table_exists
|
) as table_exists
|
||||||
""")
|
""")
|
||||||
if not cur.fetchone()['table_exists']:
|
if not cur.fetchone()['table_exists']:
|
||||||
print("⚠️ ai_prompts table doesn't exist yet - skipping pipeline prompt creation")
|
print("[WARN] ai_prompts table doesn't exist yet - skipping pipeline prompt creation")
|
||||||
return
|
return
|
||||||
|
|
||||||
# Ensure "pipeline" master prompt exists
|
# Ensure "pipeline" master prompt exists
|
||||||
@@ -189,7 +191,7 @@ def init_db():
|
|||||||
)
|
)
|
||||||
""")
|
""")
|
||||||
conn.commit()
|
conn.commit()
|
||||||
print("✓ Pipeline master prompt created")
|
print("[OK] Pipeline master prompt created")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"⚠️ Could not create pipeline prompt: {e}")
|
print(f"[WARN] Could not create pipeline prompt: {e}")
|
||||||
# Don't fail startup - prompt can be created manually
|
# Don't fail startup - prompt can be created manually
|
||||||
+9
-2
@@ -14,6 +14,10 @@ from slowapi.errors import RateLimitExceeded
|
|||||||
|
|
||||||
from db import init_db
|
from db import init_db
|
||||||
|
|
||||||
|
# Placeholder registry: load all register_placeholder() side-effects before any request
|
||||||
|
# so get_placeholder_catalog() and exports see consistent metadata (see Phase A plan).
|
||||||
|
import placeholder_registrations # noqa: F401
|
||||||
|
|
||||||
# Import routers
|
# Import routers
|
||||||
from routers import auth, profiles, weight, circumference, caliper
|
from routers import auth, profiles, weight, circumference, caliper
|
||||||
from routers import activity, nutrition, photos, insights, prompts
|
from routers import activity, nutrition, photos, insights, prompts
|
||||||
@@ -32,6 +36,7 @@ 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-Lab Layout
|
from routers import app_dashboard # Geschützter App-Bereich: Dashboard-Lab Layout
|
||||||
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
|
||||||
|
|
||||||
# ── App Configuration ─────────────────────────────────────────────────────────
|
# ── App Configuration ─────────────────────────────────────────────────────────
|
||||||
DATA_DIR = Path(os.getenv("DATA_DIR", "./data"))
|
DATA_DIR = Path(os.getenv("DATA_DIR", "./data"))
|
||||||
@@ -62,7 +67,7 @@ async def startup_event():
|
|||||||
try:
|
try:
|
||||||
init_db()
|
init_db()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"⚠️ init_db() failed (non-fatal): {e}")
|
print(f"[WARN] init_db() failed (non-fatal): {e}")
|
||||||
# Don't crash on startup - can be created manually
|
# Don't crash on startup - can be created manually
|
||||||
|
|
||||||
# Apply v9c migration if needed
|
# Apply v9c migration if needed
|
||||||
@@ -70,7 +75,7 @@ async def startup_event():
|
|||||||
from apply_v9c_migration import apply_migration
|
from apply_v9c_migration import apply_migration
|
||||||
apply_migration()
|
apply_migration()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"⚠️ v9c migration failed (non-fatal): {e}")
|
print(f"[WARN] v9c migration failed (non-fatal): {e}")
|
||||||
|
|
||||||
# ── Register Routers ──────────────────────────────────────────────────────────
|
# ── Register Routers ──────────────────────────────────────────────────────────
|
||||||
app.include_router(auth.router) # /api/auth/*
|
app.include_router(auth.router) # /api/auth/*
|
||||||
@@ -124,6 +129,8 @@ app.include_router(admin_reference_value_types.router) # /api/admin/reference-v
|
|||||||
app.include_router(app_dashboard.router) # /api/app/dashboard-layout
|
app.include_router(app_dashboard.router) # /api/app/dashboard-layout
|
||||||
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_activity_attribute_profiles.router) # /api/admin/training-*-parameters
|
||||||
|
|
||||||
# ── Health Check ──────────────────────────────────────────────────────────────
|
# ── Health Check ──────────────────────────────────────────────────────────────
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
|
|||||||
@@ -0,0 +1,80 @@
|
|||||||
|
-- Migration 054: Activity session metrics (EAV) + attribute profiles
|
||||||
|
-- Date: 2026-04-14
|
||||||
|
-- Additive only: safe for production (no data deletion).
|
||||||
|
-- Agent guide: .claude/docs/technical/ACTIVITY_SESSION_METRICS_EAV_AGENT_GUIDE.md
|
||||||
|
|
||||||
|
-- Session interval (nullable; optional backfill later)
|
||||||
|
ALTER TABLE activity_log
|
||||||
|
ADD COLUMN IF NOT EXISTS started_at TIMESTAMPTZ,
|
||||||
|
ADD COLUMN IF NOT EXISTS ended_at TIMESTAMPTZ;
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_activity_log_profile_started
|
||||||
|
ON activity_log (profile_id, started_at DESC)
|
||||||
|
WHERE started_at IS NOT NULL;
|
||||||
|
|
||||||
|
COMMENT ON COLUMN activity_log.started_at IS 'Training start (wall clock, TZ-aware); optional; for dedupe/analysis';
|
||||||
|
COMMENT ON COLUMN activity_log.ended_at IS 'Training end (wall clock, TZ-aware); optional';
|
||||||
|
|
||||||
|
-- Which parameters apply to which training category (training_types.category)
|
||||||
|
CREATE TABLE IF NOT EXISTS training_category_parameter (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
training_category VARCHAR(50) NOT NULL,
|
||||||
|
training_parameter_id INT NOT NULL REFERENCES training_parameters(id) ON DELETE CASCADE,
|
||||||
|
sort_order INT NOT NULL DEFAULT 0,
|
||||||
|
required BOOLEAN NOT NULL DEFAULT false,
|
||||||
|
ui_group VARCHAR(50),
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
CONSTRAINT uq_training_category_parameter UNIQUE (training_category, training_parameter_id)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_tcp_category ON training_category_parameter (training_category);
|
||||||
|
|
||||||
|
COMMENT ON TABLE training_category_parameter IS 'EAV schema: parameters enabled per training category';
|
||||||
|
|
||||||
|
-- Per training type: extra parameters or overrides (NULL sort/required/ui = inherit from category row if present)
|
||||||
|
CREATE TABLE IF NOT EXISTS training_type_parameter (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
training_type_id INT NOT NULL REFERENCES training_types(id) ON DELETE CASCADE,
|
||||||
|
training_parameter_id INT NOT NULL REFERENCES training_parameters(id) ON DELETE CASCADE,
|
||||||
|
sort_order INT,
|
||||||
|
required BOOLEAN,
|
||||||
|
ui_group VARCHAR(50),
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
CONSTRAINT uq_training_type_parameter UNIQUE (training_type_id, training_parameter_id)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_ttp_type ON training_type_parameter (training_type_id);
|
||||||
|
|
||||||
|
COMMENT ON TABLE training_type_parameter IS 'EAV schema: add/override parameters for a concrete training_types row';
|
||||||
|
|
||||||
|
-- EAV values per activity session
|
||||||
|
CREATE TABLE IF NOT EXISTS activity_session_metrics (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
activity_log_id UUID NOT NULL REFERENCES activity_log(id) ON DELETE CASCADE,
|
||||||
|
training_parameter_id INT NOT NULL REFERENCES training_parameters(id) ON DELETE RESTRICT,
|
||||||
|
value_num DOUBLE PRECISION,
|
||||||
|
value_int BIGINT,
|
||||||
|
value_text TEXT,
|
||||||
|
value_bool BOOLEAN,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
CONSTRAINT uq_activity_session_metric UNIQUE (activity_log_id, training_parameter_id),
|
||||||
|
CONSTRAINT chk_activity_session_metric_one_value CHECK (
|
||||||
|
(
|
||||||
|
(value_num IS NOT NULL)::int
|
||||||
|
+ (value_int IS NOT NULL)::int
|
||||||
|
+ (value_text IS NOT NULL)::int
|
||||||
|
+ (value_bool IS NOT NULL)::int
|
||||||
|
) = 1
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_asm_activity ON activity_session_metrics (activity_log_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_asm_parameter ON activity_session_metrics (training_parameter_id);
|
||||||
|
|
||||||
|
COMMENT ON TABLE activity_session_metrics IS 'EAV: one row per (session, training_parameter); exactly one value_* set';
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
RAISE NOTICE 'Migration 054: activity_session_metrics EAV + attribute profile tables + activity_log timestamps';
|
||||||
|
END $$;
|
||||||
@@ -0,0 +1,213 @@
|
|||||||
|
-- Migration 055: Seed training_category_parameter (all categories × parameters with activity_log source_field)
|
||||||
|
-- + idempotent backfill activity_log → activity_session_metrics (EAV)
|
||||||
|
-- Date: 2026-04-15
|
||||||
|
-- SAFE: INSERT … ON CONFLICT DO NOTHING only; no DELETE/TRUNCATE on activity_log.
|
||||||
|
-- Agent guide: .claude/docs/technical/ACTIVITY_SESSION_METRICS_EAV_AGENT_GUIDE.md
|
||||||
|
|
||||||
|
--1) Jede in training_types vorkommende Kategorie erhält alle aktiven Parameter mit source_field (Spalte in activity_log).
|
||||||
|
INSERT INTO training_category_parameter (
|
||||||
|
training_category,
|
||||||
|
training_parameter_id,
|
||||||
|
sort_order,
|
||||||
|
required,
|
||||||
|
ui_group
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
tc.training_category,
|
||||||
|
tp.id,
|
||||||
|
ROW_NUMBER() OVER (
|
||||||
|
PARTITION BY tc.training_category
|
||||||
|
ORDER BY tp.category, tp.id
|
||||||
|
),
|
||||||
|
false,
|
||||||
|
NULL
|
||||||
|
FROM (
|
||||||
|
SELECT DISTINCT category AS training_category
|
||||||
|
FROM training_types
|
||||||
|
WHERE category IS NOT NULL AND trim(category) <> ''
|
||||||
|
) tc
|
||||||
|
CROSS JOIN training_parameters tp
|
||||||
|
WHERE tp.is_active = true
|
||||||
|
AND tp.source_field IS NOT NULL
|
||||||
|
AND trim(tp.source_field) <> ''
|
||||||
|
ON CONFLICT (training_category, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
-- 2) Backfill: activity_log-Spalten → EAV (nur wenn noch keine Zeile existiert)
|
||||||
|
|
||||||
|
-- duration_min → integer
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
a.id,
|
||||||
|
tp.id,
|
||||||
|
NULL,
|
||||||
|
ROUND(a.duration_min::numeric)::bigint,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'duration_min' AND tp.is_active = true
|
||||||
|
WHERE a.duration_min IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
-- distance_km → float
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT a.id, tp.id, a.distance_km::double precision, NULL, NULL, NULL, NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'distance_km' AND tp.is_active = true
|
||||||
|
WHERE a.distance_km IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
-- kcal_active
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT a.id, tp.id, NULL, ROUND(a.kcal_active::numeric)::bigint, NULL, NULL, NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'kcal_active' AND tp.is_active = true
|
||||||
|
WHERE a.kcal_active IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT a.id, tp.id, NULL, ROUND(a.kcal_resting::numeric)::bigint, NULL, NULL, NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'kcal_resting' AND tp.is_active = true
|
||||||
|
WHERE a.kcal_resting IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
-- hr_avg / hr_max → keys avg_hr, max_hr
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT a.id, tp.id, NULL, ROUND(a.hr_avg::numeric)::bigint, NULL, NULL, NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'avg_hr' AND tp.is_active = true
|
||||||
|
WHERE a.hr_avg IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT a.id, tp.id, NULL, ROUND(a.hr_max::numeric)::bigint, NULL, NULL, NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'max_hr' AND tp.is_active = true
|
||||||
|
WHERE a.hr_max IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
-- rpe
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT a.id, tp.id, NULL, a.rpe::bigint, NULL, NULL, NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'rpe' AND tp.is_active = true
|
||||||
|
WHERE a.rpe IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
-- min_hr (Spalte hr_min nach Migration 014)
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT a.id, tp.id, NULL, a.hr_min, NULL, NULL, NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'min_hr' AND tp.is_active = true
|
||||||
|
WHERE a.hr_min IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT a.id, tp.id, a.pace_min_per_km::double precision, NULL, NULL, NULL, NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'pace_min_per_km' AND tp.is_active = true
|
||||||
|
WHERE a.pace_min_per_km IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT a.id, tp.id, NULL, a.cadence, NULL, NULL, NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'cadence' AND tp.is_active = true
|
||||||
|
WHERE a.cadence IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT a.id, tp.id, NULL, a.avg_power, NULL, NULL, NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'avg_power' AND tp.is_active = true
|
||||||
|
WHERE a.avg_power IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT a.id, tp.id, NULL, a.elevation_gain, NULL, NULL, NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'elevation_gain' AND tp.is_active = true
|
||||||
|
WHERE a.elevation_gain IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT a.id, tp.id, a.temperature_celsius::double precision, NULL, NULL, NULL, NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'temperature_celsius' AND tp.is_active = true
|
||||||
|
WHERE a.temperature_celsius IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT a.id, tp.id, NULL, a.humidity_percent, NULL, NULL, NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'humidity_percent' AND tp.is_active = true
|
||||||
|
WHERE a.humidity_percent IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT a.id, tp.id, a.avg_hr_percent::double precision, NULL, NULL, NULL, NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'avg_hr_percent' AND tp.is_active = true
|
||||||
|
WHERE a.avg_hr_percent IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT a.id, tp.id, a.kcal_per_km::double precision, NULL, NULL, NULL, NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'kcal_per_km' AND tp.is_active = true
|
||||||
|
WHERE a.kcal_per_km IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
RAISE NOTICE 'Migration 055: category parameter seed + EAV backfill from activity_log (no row deletes)';
|
||||||
|
END $$;
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
-- Migration 056: kcal_per_km Trigger — manuelles Leeren bei UPDATE erlauben
|
||||||
|
-- Problem: calculate_avg_hr_percent (014) setzte bei jedem UPDATE kcal_per_km aus
|
||||||
|
-- kcal_active/distance_km, sobald beide gesetzt waren — ein bewusst geleertes Feld
|
||||||
|
-- erschien sofort wieder.
|
||||||
|
-- Lösung: automatische Ableitung nur noch bei INSERT (wenn kcal_per_km noch NULL ist).
|
||||||
|
|
||||||
|
CREATE OR REPLACE FUNCTION calculate_avg_hr_percent()
|
||||||
|
RETURNS TRIGGER AS $$
|
||||||
|
DECLARE
|
||||||
|
user_max_hr INTEGER;
|
||||||
|
BEGIN
|
||||||
|
SELECT hf_max INTO user_max_hr
|
||||||
|
FROM profiles
|
||||||
|
WHERE id = NEW.profile_id;
|
||||||
|
|
||||||
|
IF NEW.hr_avg IS NOT NULL AND user_max_hr IS NOT NULL AND user_max_hr > 0 THEN
|
||||||
|
NEW.avg_hr_percent := (NEW.hr_avg::float / user_max_hr::float) * 100;
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
IF TG_OP = 'INSERT' THEN
|
||||||
|
IF NEW.kcal_active IS NOT NULL AND NEW.distance_km IS NOT NULL AND NEW.distance_km > 0 THEN
|
||||||
|
IF NEW.kcal_per_km IS NULL THEN
|
||||||
|
NEW.kcal_per_km := NEW.kcal_active::float / NEW.distance_km;
|
||||||
|
END IF;
|
||||||
|
END IF;
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
RETURN NEW;
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
RAISE NOTICE '✓ Migration 056: kcal_per_km nur noch bei INSERT auto-abgeleitet';
|
||||||
|
END $$;
|
||||||
@@ -0,0 +1,115 @@
|
|||||||
|
-- Migration 057: Kanon EAV-primär für erweiterte Trainingsmetriken
|
||||||
|
-- Date: 2026-04-15
|
||||||
|
-- activity_log-Spalten bleiben erhalten (Lesefallback / API); training_parameters.source_field
|
||||||
|
-- wird für diese Keys entfernt. Idempotenter EAV-Backfill aus Spalten (wie 055), dann source_field NULL.
|
||||||
|
-- Siehe: backend/data_layer/activity_data_canon.py
|
||||||
|
|
||||||
|
-- min_hr (Spalte hr_min)
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT a.id, tp.id, NULL, a.hr_min, NULL, NULL, NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'min_hr' AND tp.is_active = true
|
||||||
|
WHERE a.hr_min IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT a.id, tp.id, a.pace_min_per_km::double precision, NULL, NULL, NULL, NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'pace_min_per_km' AND tp.is_active = true
|
||||||
|
WHERE a.pace_min_per_km IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT a.id, tp.id, NULL, a.cadence, NULL, NULL, NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'cadence' AND tp.is_active = true
|
||||||
|
WHERE a.cadence IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT a.id, tp.id, NULL, a.avg_power, NULL, NULL, NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'avg_power' AND tp.is_active = true
|
||||||
|
WHERE a.avg_power IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT a.id, tp.id, NULL, a.elevation_gain, NULL, NULL, NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'elevation_gain' AND tp.is_active = true
|
||||||
|
WHERE a.elevation_gain IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT a.id, tp.id, a.temperature_celsius::double precision, NULL, NULL, NULL, NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'temperature_celsius' AND tp.is_active = true
|
||||||
|
WHERE a.temperature_celsius IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT a.id, tp.id, NULL, a.humidity_percent, NULL, NULL, NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'humidity_percent' AND tp.is_active = true
|
||||||
|
WHERE a.humidity_percent IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT a.id, tp.id, a.avg_hr_percent::double precision, NULL, NULL, NULL, NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'avg_hr_percent' AND tp.is_active = true
|
||||||
|
WHERE a.avg_hr_percent IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
INSERT INTO activity_session_metrics (
|
||||||
|
activity_log_id, training_parameter_id,
|
||||||
|
value_num, value_int, value_text, value_bool, updated_at
|
||||||
|
)
|
||||||
|
SELECT a.id, tp.id, a.kcal_per_km::double precision, NULL, NULL, NULL, NOW()
|
||||||
|
FROM activity_log a
|
||||||
|
JOIN training_parameters tp ON tp.key = 'kcal_per_km' AND tp.is_active = true
|
||||||
|
WHERE a.kcal_per_km IS NOT NULL
|
||||||
|
ON CONFLICT (activity_log_id, training_parameter_id) DO NOTHING;
|
||||||
|
|
||||||
|
UPDATE training_parameters
|
||||||
|
SET source_field = NULL
|
||||||
|
WHERE key IN (
|
||||||
|
'min_hr',
|
||||||
|
'pace_min_per_km',
|
||||||
|
'cadence',
|
||||||
|
'avg_power',
|
||||||
|
'elevation_gain',
|
||||||
|
'temperature_celsius',
|
||||||
|
'humidity_percent',
|
||||||
|
'avg_hr_percent',
|
||||||
|
'kcal_per_km'
|
||||||
|
);
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
RAISE NOTICE 'Migration 057: EAV-primary canon — backfill + source_field cleared for extended metrics';
|
||||||
|
END $$;
|
||||||
+23
-2
@@ -3,8 +3,9 @@ Pydantic Models for Mitai Jinkendo API
|
|||||||
|
|
||||||
Data validation schemas for request/response bodies.
|
Data validation schemas for request/response bodies.
|
||||||
"""
|
"""
|
||||||
from typing import Optional
|
from typing import Any, List, Optional
|
||||||
from pydantic import BaseModel
|
|
||||||
|
from pydantic import BaseModel, Field
|
||||||
|
|
||||||
|
|
||||||
# ── Profile Models ────────────────────────────────────────────────────────────
|
# ── Profile Models ────────────────────────────────────────────────────────────
|
||||||
@@ -82,8 +83,17 @@ class ActivityEntry(BaseModel):
|
|||||||
kcal_resting: Optional[float] = None
|
kcal_resting: Optional[float] = None
|
||||||
hr_avg: Optional[float] = None
|
hr_avg: Optional[float] = None
|
||||||
hr_max: Optional[float] = None
|
hr_max: Optional[float] = None
|
||||||
|
hr_min: Optional[int] = None # DB-Spalte hr_min (Parameter min_hr)
|
||||||
distance_km: Optional[float] = None
|
distance_km: Optional[float] = None
|
||||||
rpe: Optional[int] = None
|
rpe: Optional[int] = None
|
||||||
|
pace_min_per_km: Optional[float] = None
|
||||||
|
cadence: Optional[int] = None
|
||||||
|
avg_power: Optional[int] = None
|
||||||
|
elevation_gain: Optional[int] = None
|
||||||
|
temperature_celsius: Optional[float] = None
|
||||||
|
humidity_percent: Optional[int] = None
|
||||||
|
avg_hr_percent: Optional[float] = None
|
||||||
|
kcal_per_km: Optional[float] = None
|
||||||
source: Optional[str] = 'manual'
|
source: Optional[str] = 'manual'
|
||||||
notes: Optional[str] = None
|
notes: Optional[str] = None
|
||||||
training_type_id: Optional[int] = None # v9d: Training type categorization
|
training_type_id: Optional[int] = None # v9d: Training type categorization
|
||||||
@@ -91,6 +101,17 @@ class ActivityEntry(BaseModel):
|
|||||||
training_subcategory: Optional[str] = None # v9d: Denormalized subcategory
|
training_subcategory: Optional[str] = None # v9d: Denormalized subcategory
|
||||||
|
|
||||||
|
|
||||||
|
class ActivityMetricValue(BaseModel):
|
||||||
|
parameter_key: str
|
||||||
|
value: Any
|
||||||
|
|
||||||
|
|
||||||
|
class ActivityMetricsReplace(BaseModel):
|
||||||
|
"""Voller Ersatz der EAV-Metriken für eine Session (siehe Agent-Guide)."""
|
||||||
|
|
||||||
|
metrics: List[ActivityMetricValue] = Field(default_factory=list)
|
||||||
|
|
||||||
|
|
||||||
class NutritionDay(BaseModel):
|
class NutritionDay(BaseModel):
|
||||||
date: str
|
date: str
|
||||||
kcal: Optional[float] = None
|
kcal: Optional[float] = None
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
"""
|
"""
|
||||||
Complete Placeholder Metadata Definitions
|
Complete Placeholder Metadata Definitions (Legacy / Normativ v1)
|
||||||
|
|
||||||
This module contains manually curated, complete metadata for all 116 placeholders.
|
Hinweis (2026-04): **Verbindliche Metadaten-Pflege** erfolgt über
|
||||||
It combines automatic extraction with manual annotation to ensure 100% normative compliance.
|
`backend/placeholder_registrations/` + `placeholder_registry.py` (114 Keys, deckungsgleich
|
||||||
|
mit `PLACEHOLDER_MAP`). Dieses Modul bleibt für ältere Generator-/Export-Pfade und
|
||||||
IMPORTANT: This is the authoritative source for placeholder metadata.
|
Tests; neue Platzhalter hier nicht mehr duplizieren.
|
||||||
All new placeholders MUST be added here with complete metadata.
|
|
||||||
"""
|
"""
|
||||||
from placeholder_metadata import (
|
from placeholder_metadata import (
|
||||||
PlaceholderMetadata,
|
PlaceholderMetadata,
|
||||||
@@ -28,7 +27,7 @@ from typing import List
|
|||||||
|
|
||||||
def get_all_placeholder_metadata() -> List[PlaceholderMetadata]:
|
def get_all_placeholder_metadata() -> List[PlaceholderMetadata]:
|
||||||
"""
|
"""
|
||||||
Returns complete metadata for all 116 placeholders.
|
Returns complete metadata for all 114 placeholders (Registry ist maßgeblich).
|
||||||
|
|
||||||
This is the authoritative, manually curated source.
|
This is the authoritative, manually curated source.
|
||||||
"""
|
"""
|
||||||
@@ -476,7 +475,7 @@ def get_all_placeholder_metadata() -> List[PlaceholderMetadata]:
|
|||||||
notes=["Quadrant-Logik basiert auf FM/LBM Delta-Vorzeichen"],
|
notes=["Quadrant-Logik basiert auf FM/LBM Delta-Vorzeichen"],
|
||||||
),
|
),
|
||||||
|
|
||||||
# NOTE: Continuing with all 116 placeholders would make this file very long.
|
# NOTE: Continuing with all 114 placeholders would make this file very long.
|
||||||
# For brevity, I'll create a separate generator that fills all remaining placeholders.
|
# For brevity, I'll create a separate generator that fills all remaining placeholders.
|
||||||
# The pattern is established above - each placeholder gets full metadata.
|
# The pattern is established above - each placeholder gets full metadata.
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -29,14 +29,22 @@ def extract_value_raw(value_display: str, output_type: OutputType, placeholder_t
|
|||||||
|
|
||||||
Returns: (raw_value, success)
|
Returns: (raw_value, success)
|
||||||
"""
|
"""
|
||||||
if not value_display or value_display in ['nicht verfügbar', 'nicht genug Daten']:
|
s = (value_display or "").strip()
|
||||||
|
if (
|
||||||
|
not s
|
||||||
|
or s in ['nicht verfügbar', 'nicht genug Daten']
|
||||||
|
or s.startswith('nicht verfügbar —')
|
||||||
|
):
|
||||||
# V2 strict mode: missing/unavailable value is not a successful extraction
|
# V2 strict mode: missing/unavailable value is not a successful extraction
|
||||||
return None, False
|
return None, False
|
||||||
|
|
||||||
# JSON output type
|
# JSON output type
|
||||||
if output_type == OutputType.JSON:
|
if output_type == OutputType.JSON:
|
||||||
try:
|
try:
|
||||||
return json.loads(value_display), True
|
parsed = json.loads(value_display)
|
||||||
|
if isinstance(parsed, dict) and parsed.get('_available') is False:
|
||||||
|
return None, False
|
||||||
|
return parsed, True
|
||||||
except (json.JSONDecodeError, TypeError):
|
except (json.JSONDecodeError, TypeError):
|
||||||
# Try to find JSON in string
|
# Try to find JSON in string
|
||||||
json_match = re.search(r'(\{.*\}|\[.*\])', value_display, re.DOTALL)
|
json_match = re.search(r'(\{.*\}|\[.*\])', value_display, re.DOTALL)
|
||||||
|
|||||||
@@ -8,7 +8,31 @@ Auto-imports all placeholder registrations to populate the global registry.
|
|||||||
from . import nutrition_part_a
|
from . import nutrition_part_a
|
||||||
from . import nutrition_part_b
|
from . import nutrition_part_b
|
||||||
from . import nutrition_part_c
|
from . import nutrition_part_c
|
||||||
|
from . import nutrition_score
|
||||||
from . import body_metrics
|
from . import body_metrics
|
||||||
|
from . import body_extras
|
||||||
from . import activity_metrics
|
from . import activity_metrics
|
||||||
|
from . import activity_session_insights
|
||||||
|
from . import schlaf_erholung
|
||||||
|
from . import vitalwerte
|
||||||
|
from . import profil_zeitraum
|
||||||
|
from . import phase_0b_meta_scores
|
||||||
|
from . import phase_0b_ziele_fokus
|
||||||
|
from . import korrelationen
|
||||||
|
|
||||||
__all__ = ['nutrition_part_a', 'nutrition_part_b', 'nutrition_part_c', 'body_metrics', 'activity_metrics']
|
__all__ = [
|
||||||
|
'nutrition_part_a',
|
||||||
|
'nutrition_part_b',
|
||||||
|
'nutrition_part_c',
|
||||||
|
'nutrition_score',
|
||||||
|
'body_metrics',
|
||||||
|
'body_extras',
|
||||||
|
'activity_metrics',
|
||||||
|
'activity_session_insights',
|
||||||
|
'schlaf_erholung',
|
||||||
|
'vitalwerte',
|
||||||
|
'profil_zeitraum',
|
||||||
|
'phase_0b_meta_scores',
|
||||||
|
'phase_0b_ziele_fokus',
|
||||||
|
'korrelationen',
|
||||||
|
]
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
"""Gemeinsames Evidence-Tagging für Registry-Einträge."""
|
||||||
|
|
||||||
|
from placeholder_registry import EvidenceType, PlaceholderMetadata
|
||||||
|
|
||||||
|
STANDARD_FIELDS = (
|
||||||
|
"key", "category", "description", "resolver_module", "resolver_function",
|
||||||
|
"data_layer_module", "data_layer_function", "source_tables", "semantic_contract",
|
||||||
|
"unit", "time_window", "output_type", "placeholder_type", "format_hint",
|
||||||
|
"example_output", "minimum_data_requirements", "confidence_logic",
|
||||||
|
"missing_value_policy", "layer_1_decision", "layer_2a_decision",
|
||||||
|
"layer_2b_reuse_possible", "architecture_alignment", "issue_53_alignment",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def tag_standard_evidence(meta: PlaceholderMetadata) -> None:
|
||||||
|
for field in STANDARD_FIELDS:
|
||||||
|
meta.set_evidence(field, EvidenceType.CODE_DERIVED)
|
||||||
|
meta.set_evidence("business_meaning", EvidenceType.DRAFT_DERIVED)
|
||||||
|
meta.set_evidence("known_limitations", EvidenceType.MIXED)
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
"""
|
"""
|
||||||
Activity Metrics Placeholder Registrations
|
Activity Metrics Placeholder Registrations
|
||||||
|
|
||||||
Registers all 17 activity-related placeholders in the central placeholder registry.
|
Registers 17 Aktivitäts-Platzhalter hier; 3 weitere Keys in activity_session_insights.py (**20 gesamt** in PLACEHOLDER_MAP).
|
||||||
|
|
||||||
Evidence-based metadata with clear tagging of source.
|
Evidence-based metadata with clear tagging of source.
|
||||||
|
|
||||||
@@ -10,6 +10,9 @@ Groups:
|
|||||||
- Basic Metrics (7): training_minutes_week, training_frequency_7d, quality_sessions_pct,
|
- Basic Metrics (7): training_minutes_week, training_frequency_7d, quality_sessions_pct,
|
||||||
proxy_internal_load_7d, monotony_score, strain_score, rest_day_compliance
|
proxy_internal_load_7d, monotony_score, strain_score, rest_day_compliance
|
||||||
- Advanced Metrics (7): ability_balance_*, vo2max_trend_28d, activity_score
|
- Advanced Metrics (7): ability_balance_*, vo2max_trend_28d, activity_score
|
||||||
|
|
||||||
|
Resolver: alle Keys gebündelt unter „Training / Aktivität“ in PLACEHOLDER_MAP;
|
||||||
|
activity_score nicht unter „Meta Scores“.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from placeholder_registry import (
|
from placeholder_registry import (
|
||||||
@@ -40,7 +43,7 @@ def register_activity_group_1():
|
|||||||
category="Aktivität",
|
category="Aktivität",
|
||||||
description="Zusammenfassung der letzten 14 Tage Aktivität",
|
description="Zusammenfassung der letzten 14 Tage Aktivität",
|
||||||
resolver_module="backend/placeholder_resolver.py",
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
resolver_function="_format_activity_summary",
|
resolver_function="get_activity_summary",
|
||||||
data_layer_module=None,
|
data_layer_module=None,
|
||||||
data_layer_function=None,
|
data_layer_function=None,
|
||||||
source_tables=["activity_log", "training_types"],
|
source_tables=["activity_log", "training_types"],
|
||||||
@@ -124,16 +127,23 @@ def register_activity_group_1():
|
|||||||
activity_detail_metadata = PlaceholderMetadata(
|
activity_detail_metadata = PlaceholderMetadata(
|
||||||
key="activity_detail",
|
key="activity_detail",
|
||||||
category="Aktivität",
|
category="Aktivität",
|
||||||
description="Detaillierte Liste der letzten 14 Tage Aktivität",
|
description=(
|
||||||
|
"Letzte 14 Tage: pro Session Kopfzeile (activity_log) plus gemergte Profil-Metriken "
|
||||||
|
"(dynamische Keys je training_category / training_type_id)"
|
||||||
|
),
|
||||||
resolver_module="backend/placeholder_resolver.py",
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
resolver_function="_format_activity_detail",
|
resolver_function="get_activity_detail",
|
||||||
data_layer_module=None,
|
data_layer_module="backend/data_layer/activity_metrics.py",
|
||||||
data_layer_function=None,
|
data_layer_function="get_activity_detail_data",
|
||||||
source_tables=["activity_log", "training_types"],
|
source_tables=["activity_log", "activity_session_metrics", "training_parameters"],
|
||||||
semantic_contract=(
|
semantic_contract=(
|
||||||
"Liefert eine strukturierte Liste aller Trainingseinheiten der letzten 14 Tage. "
|
"Layer 1: get_activity_detail_data lädt Sessions, enrich_sessions_with_metrics fügt "
|
||||||
"Jede Einheit: Datum, Trainingstyp, Dauer (Minuten), optional Notizen. "
|
"session_metrics hinzu — effektive Liste aus merge_column_backed_and_eav_metrics: nur "
|
||||||
"Sortiert chronologisch absteigend (neueste zuerst)."
|
"Parameter aus dem Attributschema (tcp/ttp), sortiert nach key. "
|
||||||
|
"Leseregel Kanon: activity_log-Spalte (source_field, Registry-Feld, Legacy-Spalte für "
|
||||||
|
"EAV-primäre Keys) schlägt EAV, wenn beide Werte liefern. "
|
||||||
|
"Layer 2a: Zeilen mit „| EAV: key=value; …“ nur für nicht-leere session_metrics; "
|
||||||
|
"die Menge der Keys ist admin-/profilabhängig, kein festes Prompt-Schema."
|
||||||
),
|
),
|
||||||
business_meaning=(
|
business_meaning=(
|
||||||
"Detaillierte Trainingshistorie für KI-Prompts, die Muster, Progressionen "
|
"Detaillierte Trainingshistorie für KI-Prompts, die Muster, Progressionen "
|
||||||
@@ -144,7 +154,9 @@ def register_activity_group_1():
|
|||||||
time_window="14d",
|
time_window="14d",
|
||||||
output_type=OutputType.LIST,
|
output_type=OutputType.LIST,
|
||||||
placeholder_type=PlaceholderType.RAW_DATA,
|
placeholder_type=PlaceholderType.RAW_DATA,
|
||||||
format_hint="Liste von Strings, eine Zeile pro Einheit: 'YYYY-MM-DD: Typ (Dauer min)'",
|
format_hint=(
|
||||||
|
"Pro Zeile: Datum, Typ, Dauer, kcal, optional HF, optional „| EAV: …“ aus Session-Metriken"
|
||||||
|
),
|
||||||
example_output=(
|
example_output=(
|
||||||
"2026-03-28: Krafttraining (45 min)\\n"
|
"2026-03-28: Krafttraining (45 min)\\n"
|
||||||
"2026-03-27: Laufen (30 min)\\n"
|
"2026-03-27: Laufen (30 min)\\n"
|
||||||
@@ -160,19 +172,17 @@ def register_activity_group_1():
|
|||||||
legacy_display="Keine Aktivitätsdaten"
|
legacy_display="Keine Aktivitätsdaten"
|
||||||
),
|
),
|
||||||
known_limitations=(
|
known_limitations=(
|
||||||
"OLD RESOLVER PATTERN: Keine Data Layer Funktion. "
|
"Keine Profil-Qualitätsfilterung in dieser Liste. Max. 20 Zeilen im Prompt-Output "
|
||||||
"Formatierung direkt im Resolver. "
|
"(Hard-Limit Resolver). session_metrics kann leer sein (kein Typ, kein Profil, keine EAV-Zeilen). "
|
||||||
"CRITICAL: Keine Qualitätsfilterung - auch ungültige Einheiten (z.B. 0 min) "
|
"Keys und Anzahl Metriken variieren je Instanz/Admin — nicht von festen Platzhaltern in anderen "
|
||||||
"werden gelistet. JOIN mit training_types für Typ-Namen."
|
"Prompts ausgehen. Nur im effektiven Merge erscheinende Parameter; keine verwaisten EAV-Keys "
|
||||||
|
"außerhalb des Schemas."
|
||||||
),
|
),
|
||||||
layer_1_decision="NONE - Old resolver pattern (direct SQL in resolver)",
|
layer_1_decision="activity_metrics.get_activity_detail_data (+ enrich_sessions_with_metrics)",
|
||||||
layer_2a_decision="Placeholder Resolver (formatting + SQL query)",
|
layer_2a_decision="get_activity_detail (Formatierung)",
|
||||||
layer_2b_reuse_possible=False,
|
layer_2b_reuse_possible=True,
|
||||||
architecture_alignment=(
|
architecture_alignment="Phase 0c Layer 1 + EAV-Anreicherung",
|
||||||
"NOT ALIGNED with Phase 0c Multi-Layer Architecture. "
|
issue_53_alignment="Layer 1"
|
||||||
"Should be refactored to use data_layer function."
|
|
||||||
),
|
|
||||||
issue_53_alignment="NOT ALIGNED - no layer separation"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
activity_detail_metadata.set_evidence("key", EvidenceType.CODE_DERIVED)
|
activity_detail_metadata.set_evidence("key", EvidenceType.CODE_DERIVED)
|
||||||
@@ -209,56 +219,47 @@ def register_activity_group_1():
|
|||||||
trainingstyp_verteilung_metadata = PlaceholderMetadata(
|
trainingstyp_verteilung_metadata = PlaceholderMetadata(
|
||||||
key="trainingstyp_verteilung",
|
key="trainingstyp_verteilung",
|
||||||
category="Aktivität",
|
category="Aktivität",
|
||||||
description="Trainingstypen-Verteilung der letzten 14 Tage als JSON",
|
description="Verteilung nach training_category (14 Tage): Top 3 als kompakte Prozent-Textzeile",
|
||||||
resolver_module="backend/placeholder_resolver.py",
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
resolver_function="_format_trainingstyp_verteilung",
|
resolver_function="get_trainingstyp_verteilung",
|
||||||
data_layer_module=None,
|
data_layer_module="backend/data_layer/activity_metrics.py",
|
||||||
data_layer_function=None,
|
data_layer_function="get_training_type_distribution_data",
|
||||||
source_tables=["activity_log", "training_types"],
|
source_tables=["activity_log"],
|
||||||
semantic_contract=(
|
semantic_contract=(
|
||||||
"Liefert eine JSON-Struktur mit der Verteilung der Trainingstypen über 14 Tage. "
|
"Layer 1: get_training_type_distribution_data — Anteil je training_category am "
|
||||||
"Für jeden Trainingstyp: Anzahl Einheiten, Gesamtdauer (Minuten), "
|
"Gesamt-Session-Count im Fenster (auch unkategorisierte zählen im Nenner). "
|
||||||
"Prozentanteil an Gesamtdauer. Sortiert nach Dauer absteigend."
|
"Layer 2a: Top 3 Kategorien als „Name: p%“ kommagetrennt; bei fehlenden Daten Kurz-Hinweis."
|
||||||
),
|
),
|
||||||
business_meaning=(
|
business_meaning=(
|
||||||
"Analyse-Placeholder für Trainingsvielfalt und -schwerpunkte. "
|
"Analyse-Placeholder für Trainingsvielfalt und -schwerpunkte. "
|
||||||
"Erlaubt KI-Prompts, Imbalancen zu erkennen (z.B. nur Kraft, keine Ausdauer) "
|
"Erlaubt KI-Prompts, Imbalancen zu erkennen (z.B. nur Kraft, keine Ausdauer) "
|
||||||
"oder Zielkonformität zu prüfen (z.B. 'zu wenig Mobilität')."
|
"oder Zielkonformität zu prüfen (z.B. 'zu wenig Mobilität')."
|
||||||
),
|
),
|
||||||
unit="json",
|
unit="text",
|
||||||
time_window="14d",
|
time_window="14d",
|
||||||
output_type=OutputType.JSON,
|
output_type=OutputType.TEXT_SUMMARY,
|
||||||
placeholder_type=PlaceholderType.INTERPRETED,
|
placeholder_type=PlaceholderType.INTERPRETED,
|
||||||
format_hint="JSON Object mit Trainingstyp als Key, Value: {count, duration_min, percentage}",
|
format_hint="Eine Zeile: bis zu drei „Kategorie: Prozent%“, durch Komma getrennt",
|
||||||
example_output=(
|
example_output="cardio: 45%, strength: 30%, mobility: 15%",
|
||||||
'{"Krafttraining": {"count": 5, "duration_min": 180, "percentage": 57}, '
|
|
||||||
'"Ausdauer": {"count": 4, "duration_min": 90, "percentage": 29}, '
|
|
||||||
'"Mobilität": {"count": 3, "duration_min": 45, "percentage": 14}}'
|
|
||||||
),
|
|
||||||
minimum_data_requirements=None,
|
minimum_data_requirements=None,
|
||||||
quality_filter_policy=None,
|
quality_filter_policy=None,
|
||||||
confidence_logic="Keine Confidence-Berechnung. Aggregation basiert auf verfügbaren Daten.",
|
confidence_logic="Wie get_training_type_distribution_data (calculate_confidence über categorized_count)",
|
||||||
missing_value_policy=MissingValuePolicy(
|
missing_value_policy=MissingValuePolicy(
|
||||||
available=False,
|
available=False,
|
||||||
value_raw=None,
|
value_raw=None,
|
||||||
missing_reason="no_data",
|
missing_reason="no_data",
|
||||||
legacy_display="{}"
|
legacy_display="Keine kategorisierten Trainings"
|
||||||
),
|
),
|
||||||
known_limitations=(
|
known_limitations=(
|
||||||
"OLD RESOLVER PATTERN: Keine Data Layer Funktion. "
|
"Nur Sessions mit gesetztem training_category fließen in die Verteilungsliste; "
|
||||||
"Aggregation direkt im Resolver. "
|
"Prozente beziehen sich auf alle Sessions im Fenster (Nenner = total_sessions). "
|
||||||
"CRITICAL: Keine Qualitätsfilterung - auch ungültige Einheiten werden aggregiert. "
|
"Keine Qualitätsfilterung der Einheiten. Kein drill-down nach training_type_id in diesem Platzhalter."
|
||||||
"JOIN mit training_types für Typ-Namen. "
|
|
||||||
"EDGE CASE: Einheiten ohne training_type_id werden ignoriert (LEFT JOIN)."
|
|
||||||
),
|
),
|
||||||
layer_1_decision="NONE - Old resolver pattern (direct SQL aggregation in resolver)",
|
layer_1_decision="activity_metrics.get_training_type_distribution_data",
|
||||||
layer_2a_decision="Placeholder Resolver (aggregation + JSON formatting)",
|
layer_2a_decision="get_trainingstyp_verteilung (Top 3 als Text)",
|
||||||
layer_2b_reuse_possible=True,
|
layer_2b_reuse_possible=True,
|
||||||
architecture_alignment=(
|
architecture_alignment="Phase 0c — Layer 1 + Formatierung",
|
||||||
"PARTIALLY ALIGNED: JSON output structure suitable for chart endpoints, "
|
issue_53_alignment="Layer 1"
|
||||||
"but no data layer separation. Should be refactored."
|
|
||||||
),
|
|
||||||
issue_53_alignment="PARTIALLY ALIGNED - output format good, layer separation missing"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
trainingstyp_verteilung_metadata.set_evidence("key", EvidenceType.CODE_DERIVED)
|
trainingstyp_verteilung_metadata.set_evidence("key", EvidenceType.CODE_DERIVED)
|
||||||
@@ -938,9 +939,9 @@ def register_activity_group_3():
|
|||||||
description="VO2 Max Trend über 28 Tage",
|
description="VO2 Max Trend über 28 Tage",
|
||||||
category="Aktivität",
|
category="Aktivität",
|
||||||
resolver_module="backend/placeholder_resolver.py",
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
resolver_function="get_vo2max_trend_28d",
|
resolver_function="_safe_float",
|
||||||
data_layer_module="backend/data_layer/activity_metrics.py",
|
data_layer_module="backend/data_layer/activity_metrics.py",
|
||||||
data_layer_function="calculate_vo2max_trend",
|
data_layer_function="calculate_vo2max_trend_28d",
|
||||||
source_tables=["vitals_baseline"],
|
source_tables=["vitals_baseline"],
|
||||||
time_window="28d",
|
time_window="28d",
|
||||||
output_type=OutputType.NUMERIC,
|
output_type=OutputType.NUMERIC,
|
||||||
@@ -977,8 +978,8 @@ def register_activity_group_3():
|
|||||||
"EDGE CASE: Nur 1 Messung → kein Trend → missing_value. "
|
"EDGE CASE: Nur 1 Messung → kein Trend → missing_value. "
|
||||||
"EDGE CASE: Große Zeitlücken zwischen Messungen → Trend nicht aussagekräftig."
|
"EDGE CASE: Große Zeitlücken zwischen Messungen → Trend nicht aussagekräftig."
|
||||||
),
|
),
|
||||||
layer_1_decision="Data Layer (activity_metrics.calculate_vo2max_trend) - QUESTIONABLE",
|
layer_1_decision="Data Layer (activity_metrics.calculate_vo2max_trend_28d) — Kategorie diskutierbar",
|
||||||
layer_2a_decision="Placeholder Resolver (formatting only)",
|
layer_2a_decision="Placeholder Resolver (_safe_float)",
|
||||||
layer_2b_reuse_possible=True,
|
layer_2b_reuse_possible=True,
|
||||||
architecture_alignment="Phase 0c Multi-Layer Architecture conform",
|
architecture_alignment="Phase 0c Multi-Layer Architecture conform",
|
||||||
issue_53_alignment="Layer separation established"
|
issue_53_alignment="Layer separation established"
|
||||||
@@ -1020,8 +1021,8 @@ def register_activity_group_3():
|
|||||||
description="Gesamtaktivitäts-Score (gewichtet)",
|
description="Gesamtaktivitäts-Score (gewichtet)",
|
||||||
category="Aktivität",
|
category="Aktivität",
|
||||||
resolver_module="backend/placeholder_resolver.py",
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
resolver_function="get_activity_score",
|
resolver_function="_safe_int",
|
||||||
data_layer_module="backend/data_layer/scores.py",
|
data_layer_module="backend/data_layer/activity_metrics.py",
|
||||||
data_layer_function="calculate_activity_score",
|
data_layer_function="calculate_activity_score",
|
||||||
source_tables=["activity_log", "training_types", "rest_days", "vitals_baseline", "user_focus_area_weights"],
|
source_tables=["activity_log", "training_types", "rest_days", "vitals_baseline", "user_focus_area_weights"],
|
||||||
time_window="composite (7d, 14d, 28d mixed)",
|
time_window="composite (7d, 14d, 28d mixed)",
|
||||||
@@ -1065,8 +1066,8 @@ def register_activity_group_3():
|
|||||||
"QUESTIONABLE: Vermischt Metriken mit unterschiedlicher Verlässlichkeit "
|
"QUESTIONABLE: Vermischt Metriken mit unterschiedlicher Verlässlichkeit "
|
||||||
"(z.B. quality_sessions_pct hat TO_VERIFY Issues)."
|
"(z.B. quality_sessions_pct hat TO_VERIFY Issues)."
|
||||||
),
|
),
|
||||||
layer_1_decision="Data Layer (scores.calculate_activity_score)",
|
layer_1_decision="Data Layer (activity_metrics.calculate_activity_score)",
|
||||||
layer_2a_decision="Placeholder Resolver (formatting only)",
|
layer_2a_decision="Placeholder Resolver (_safe_int)",
|
||||||
layer_2b_reuse_possible=False,
|
layer_2b_reuse_possible=False,
|
||||||
architecture_alignment="Phase 0c Multi-Layer Architecture conform",
|
architecture_alignment="Phase 0c Multi-Layer Architecture conform",
|
||||||
issue_53_alignment="Layer separation established"
|
issue_53_alignment="Layer separation established"
|
||||||
|
|||||||
@@ -0,0 +1,254 @@
|
|||||||
|
"""
|
||||||
|
Registry: Trainings-Häufigkeit, Pausen zwischen Einheiten, wöchentliche Session-JSON (KI-Rohkontext).
|
||||||
|
"""
|
||||||
|
|
||||||
|
from placeholder_registry import (
|
||||||
|
PlaceholderMetadata,
|
||||||
|
MissingValuePolicy,
|
||||||
|
EvidenceType,
|
||||||
|
OutputType,
|
||||||
|
PlaceholderType,
|
||||||
|
register_placeholder,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _ev(meta: PlaceholderMetadata, field: str, et: EvidenceType = EvidenceType.CODE_DERIVED):
|
||||||
|
meta.set_evidence(field, et)
|
||||||
|
|
||||||
|
|
||||||
|
def register_activity_session_insights():
|
||||||
|
md_freq = PlaceholderMetadata(
|
||||||
|
key="training_frequency_by_type_md",
|
||||||
|
category="Aktivität",
|
||||||
|
description=(
|
||||||
|
"Markdown-Tabelle: pro Trainingsart (activity_type) Sessions, Ø/Woche, "
|
||||||
|
"Dauer, kcal, HF, RPE, kcal/min (Intensitätsproxy)"
|
||||||
|
),
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="get_training_frequency_by_type_md",
|
||||||
|
data_layer_module="backend/data_layer/activity_metrics.py",
|
||||||
|
data_layer_function="get_training_frequency_by_type_data",
|
||||||
|
source_tables=["activity_log"],
|
||||||
|
semantic_contract=(
|
||||||
|
"Aggregat über activity_log gruppiert nach activity_type (Roh-Label). "
|
||||||
|
"sessions_per_week = count / (days/7). avg_kcal_per_min = Summe kcal / Summe min."
|
||||||
|
),
|
||||||
|
business_meaning="KI: Häufigkeit & Belastung pro Sportart, Erholungs-/Überlastungs-Kontext",
|
||||||
|
unit="Markdown",
|
||||||
|
time_window="default 28 Tage",
|
||||||
|
output_type=OutputType.TEXT_SUMMARY,
|
||||||
|
placeholder_type=PlaceholderType.INTERPRETED,
|
||||||
|
format_hint="GitHub-Flavored Markdown-Tabelle",
|
||||||
|
example_output="| Art | n | Ø/Woche | … |",
|
||||||
|
minimum_data_requirements="Mindestens eine Session im Fenster",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="Wie calculate_confidence anhand Session-Anzahl",
|
||||||
|
missing_value_policy=MissingValuePolicy(
|
||||||
|
available=False,
|
||||||
|
value_raw=None,
|
||||||
|
missing_reason="no_data",
|
||||||
|
legacy_display="Keine Trainingsdaten",
|
||||||
|
),
|
||||||
|
known_limitations=(
|
||||||
|
"Gruppierung nach activity_type-String (Import-Namen), nicht nur training_type_id. "
|
||||||
|
"HF/RPE oft NULL je nach Quelle. Pausen-Analyse separater Platzhalter."
|
||||||
|
),
|
||||||
|
layer_1_decision="activity_metrics.get_training_frequency_by_type_data",
|
||||||
|
layer_2a_decision="get_training_frequency_by_type_md",
|
||||||
|
layer_2b_reuse_possible=True,
|
||||||
|
architecture_alignment="Phase 0c",
|
||||||
|
issue_53_alignment="Layer 1",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
for f in (
|
||||||
|
"key", "category", "description", "resolver_module", "resolver_function",
|
||||||
|
"data_layer_module", "data_layer_function", "source_tables", "semantic_contract",
|
||||||
|
"unit", "time_window", "output_type", "placeholder_type", "format_hint",
|
||||||
|
"example_output", "minimum_data_requirements", "confidence_logic",
|
||||||
|
"missing_value_policy", "layer_1_decision", "layer_2a_decision",
|
||||||
|
"layer_2b_reuse_possible", "architecture_alignment", "issue_53_alignment",
|
||||||
|
):
|
||||||
|
_ev(md_freq, f)
|
||||||
|
_ev(md_freq, "business_meaning", EvidenceType.DRAFT_DERIVED)
|
||||||
|
_ev(md_freq, "known_limitations", EvidenceType.MIXED)
|
||||||
|
register_placeholder(md_freq)
|
||||||
|
|
||||||
|
md_gap = PlaceholderMetadata(
|
||||||
|
key="training_inter_session_gap_md",
|
||||||
|
category="Aktivität",
|
||||||
|
description="Median/Mittel/Min der Stunden zwischen aufeinanderfolgenden Trainingseinheiten",
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="get_training_inter_session_gap_md",
|
||||||
|
data_layer_module="backend/data_layer/activity_metrics.py",
|
||||||
|
data_layer_function="get_training_inter_session_gap_data",
|
||||||
|
source_tables=["activity_log"],
|
||||||
|
semantic_contract=(
|
||||||
|
"Sessions chronologisch; Zeitstempel = date + start_time oder 12:00. "
|
||||||
|
"Lücken in Stunden zwischen aufeinanderfolgenden Starts."
|
||||||
|
),
|
||||||
|
business_meaning="KI: ausreichend Erholung zwischen Belastungen? Doppelbelastung?",
|
||||||
|
unit="Markdown",
|
||||||
|
time_window="default 28 Tage",
|
||||||
|
output_type=OutputType.TEXT_SUMMARY,
|
||||||
|
placeholder_type=PlaceholderType.INTERPRETED,
|
||||||
|
format_hint="Kurzer Markdown-Fließtext",
|
||||||
|
example_output="**Pause zwischen Trainings** …",
|
||||||
|
minimum_data_requirements="Mindestens 2 Sessions",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="calculate_confidence über Session-Anzahl",
|
||||||
|
missing_value_policy=MissingValuePolicy(
|
||||||
|
available=False,
|
||||||
|
value_raw=None,
|
||||||
|
missing_reason="insufficient_data",
|
||||||
|
legacy_display="Zu wenige Trainings",
|
||||||
|
),
|
||||||
|
known_limitations=(
|
||||||
|
"Kein Unterscheidung aktiv/passiv außerhalb activity_log. "
|
||||||
|
"Fehlende Uhrzeit verzerrt Reihenfolge am selben Tag nicht (nur ein künstlicher Mittag)."
|
||||||
|
),
|
||||||
|
layer_1_decision="activity_metrics.get_training_inter_session_gap_data",
|
||||||
|
layer_2a_decision="get_training_inter_session_gap_md",
|
||||||
|
layer_2b_reuse_possible=True,
|
||||||
|
architecture_alignment="Phase 0c",
|
||||||
|
issue_53_alignment="Layer 1",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
for f in (
|
||||||
|
"key", "category", "description", "resolver_module", "resolver_function",
|
||||||
|
"data_layer_module", "data_layer_function", "source_tables", "semantic_contract",
|
||||||
|
"unit", "time_window", "output_type", "placeholder_type", "format_hint",
|
||||||
|
"example_output", "minimum_data_requirements", "confidence_logic",
|
||||||
|
"missing_value_policy", "layer_1_decision", "layer_2a_decision",
|
||||||
|
"layer_2b_reuse_possible", "architecture_alignment", "issue_53_alignment",
|
||||||
|
):
|
||||||
|
_ev(md_gap, f)
|
||||||
|
_ev(md_gap, "business_meaning", EvidenceType.DRAFT_DERIVED)
|
||||||
|
_ev(md_gap, "known_limitations", EvidenceType.MIXED)
|
||||||
|
register_placeholder(md_gap)
|
||||||
|
|
||||||
|
pj = PlaceholderMetadata(
|
||||||
|
key="training_sessions_recent_json",
|
||||||
|
category="Aktivität",
|
||||||
|
description=(
|
||||||
|
"JSON: ISO-Wochen mit Sessions (activity_log-Kopf) plus session_metrics[] — gemergte Profil-Metriken "
|
||||||
|
"(dynamische Keys)"
|
||||||
|
),
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="_safe_json",
|
||||||
|
data_layer_module="backend/data_layer/activity_metrics.py",
|
||||||
|
data_layer_function="get_training_sessions_recent_weeks_data",
|
||||||
|
source_tables=["activity_log", "training_types", "activity_session_metrics", "training_parameters"],
|
||||||
|
semantic_contract=(
|
||||||
|
"Root: weeks[] mit week_iso; sessions[] pro Einheit u. a. id, date, activity_type, "
|
||||||
|
"duration_min, kcal_active, hr_avg, hr_max, rpe, training_category, training_type_name, "
|
||||||
|
"session_metrics[]. "
|
||||||
|
"session_metrics: effektive Liste nach merge_column_backed_and_eav_metrics — Einträge mit "
|
||||||
|
"training_parameter_id, key, data_type, unit, value, name_de/name_en, description_de/description_en; "
|
||||||
|
"nur Parameter aus Attributschema "
|
||||||
|
"(training_category_parameter + training_type_parameter Overrides), keys sortiert. "
|
||||||
|
"Kanon Lesen: activity_log-Spalte vor EAV bei Konflikt. "
|
||||||
|
"meta: weeks_requested, days_loaded, session_count, confidence. "
|
||||||
|
"Default ca. 4 ISO-Wochen (28 Tage Rohdatenfenster)."
|
||||||
|
),
|
||||||
|
business_meaning="Rohkontext für wochenweise Auswertung (Erholung, Intensität) in der KI",
|
||||||
|
unit="JSON string",
|
||||||
|
time_window="4 ISO-Wochen (28 Tage Datenfenster)",
|
||||||
|
output_type=OutputType.JSON,
|
||||||
|
placeholder_type=PlaceholderType.RAW_DATA,
|
||||||
|
format_hint="JSON-Objekt als String",
|
||||||
|
example_output='{"weeks":[...],"meta":{...}}',
|
||||||
|
minimum_data_requirements="Optional Sessions; meta.confidence bei leer insufficient",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="meta.confidence aus Session-Anzahl",
|
||||||
|
missing_value_policy=MissingValuePolicy(
|
||||||
|
available=False,
|
||||||
|
value_raw=None,
|
||||||
|
missing_reason="no_data",
|
||||||
|
legacy_display="{}",
|
||||||
|
),
|
||||||
|
known_limitations=(
|
||||||
|
"Token-Länge bei vielen Sessions. training_type_name nur bei gesetztem training_type_id. "
|
||||||
|
"session_metrics oft [] (kein Typ, kein Profil, keine gespeicherten Werte). "
|
||||||
|
"Anzahl und Namen der Metrik-Keys sind instanz-/adminabhängig — JSON nicht als festes Schema "
|
||||||
|
"für Downstream-Parsing harter Logik verwenden. "
|
||||||
|
"Für KI-Semantik zusätzlich {{training_parameters_glossary_md}} (gesamter aktiver Katalog) in den Prompt legen. "
|
||||||
|
"Composite-Parameter (JSON in EAV) noch nicht im MVP expandiert; ggf. Roh-value_text in späterer Phase."
|
||||||
|
),
|
||||||
|
layer_1_decision="activity_metrics.get_training_sessions_recent_weeks_data",
|
||||||
|
layer_2a_decision="_safe_json('training_sessions_recent_json')",
|
||||||
|
layer_2b_reuse_possible=True,
|
||||||
|
architecture_alignment="Phase 0c",
|
||||||
|
issue_53_alignment="Layer 1",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
for f in (
|
||||||
|
"key", "category", "description", "resolver_module", "resolver_function",
|
||||||
|
"data_layer_module", "data_layer_function", "source_tables", "semantic_contract",
|
||||||
|
"unit", "time_window", "output_type", "placeholder_type", "format_hint",
|
||||||
|
"example_output", "minimum_data_requirements", "confidence_logic",
|
||||||
|
"missing_value_policy", "layer_1_decision", "layer_2a_decision",
|
||||||
|
"layer_2b_reuse_possible", "architecture_alignment", "issue_53_alignment",
|
||||||
|
):
|
||||||
|
_ev(pj, f)
|
||||||
|
_ev(pj, "business_meaning", EvidenceType.DRAFT_DERIVED)
|
||||||
|
_ev(pj, "known_limitations", EvidenceType.MIXED)
|
||||||
|
register_placeholder(pj)
|
||||||
|
|
||||||
|
md_gloss = PlaceholderMetadata(
|
||||||
|
key="training_parameters_glossary_md",
|
||||||
|
category="Aktivität",
|
||||||
|
description=(
|
||||||
|
"Markdown-Tabelle: alle aktiven training_parameters (key, DE/EN, Beschreibungen, Typ, Einheit, Kategorie). "
|
||||||
|
"Ergänzung zu training_sessions_recent_json für KI (Bedeutung dynamischer Metrik-Keys)."
|
||||||
|
),
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="get_training_parameters_glossary_md",
|
||||||
|
data_layer_module="backend/data_layer/activity_metrics.py",
|
||||||
|
data_layer_function="get_training_parameters_ki_glossary_data",
|
||||||
|
source_tables=["training_parameters"],
|
||||||
|
semantic_contract=(
|
||||||
|
"SELECT auf training_parameters WHERE is_active; sortiert category, key. "
|
||||||
|
"profile_id-Parameter im Resolver reserviert, aktuell globaler Katalog."
|
||||||
|
),
|
||||||
|
business_meaning="KI: Legende zu session_metrics-Keys und Custom-Parametern",
|
||||||
|
unit="Markdown",
|
||||||
|
time_window="n/a (Katalog-Snapshot)",
|
||||||
|
output_type=OutputType.TEXT_SUMMARY,
|
||||||
|
placeholder_type=PlaceholderType.INTERPRETED,
|
||||||
|
format_hint="GitHub-Flavored Markdown-Tabelle",
|
||||||
|
example_output="| Feld (key) | DE | EN | Beschreibung DE | … |",
|
||||||
|
minimum_data_requirements="Optional leer → Kurztext statt Tabelle",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="Immer verfügbar wenn DB erreichbar",
|
||||||
|
missing_value_policy=MissingValuePolicy(
|
||||||
|
available=False,
|
||||||
|
value_raw=None,
|
||||||
|
missing_reason="no_data",
|
||||||
|
legacy_display="Keine aktiven Trainingsparameter im Katalog.",
|
||||||
|
),
|
||||||
|
known_limitations=(
|
||||||
|
"Keine profil-spezifische Einschränkung auf tatsächlich genutzte Keys (V2). "
|
||||||
|
"Tabellen können bei großem Katalog lang werden."
|
||||||
|
),
|
||||||
|
layer_1_decision="activity_metrics.get_training_parameters_ki_glossary_data",
|
||||||
|
layer_2a_decision="get_training_parameters_glossary_md",
|
||||||
|
layer_2b_reuse_possible=True,
|
||||||
|
architecture_alignment="Phase 0c",
|
||||||
|
issue_53_alignment="Layer 2a",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
for f in (
|
||||||
|
"key", "category", "description", "resolver_module", "resolver_function",
|
||||||
|
"data_layer_module", "data_layer_function", "source_tables", "semantic_contract",
|
||||||
|
"unit", "time_window", "output_type", "placeholder_type", "format_hint",
|
||||||
|
"example_output", "minimum_data_requirements", "confidence_logic",
|
||||||
|
"missing_value_policy", "layer_1_decision", "layer_2a_decision",
|
||||||
|
"layer_2b_reuse_possible", "architecture_alignment", "issue_53_alignment",
|
||||||
|
):
|
||||||
|
_ev(md_gloss, f)
|
||||||
|
_ev(md_gloss, "business_meaning", EvidenceType.DRAFT_DERIVED)
|
||||||
|
_ev(md_gloss, "known_limitations", EvidenceType.MIXED)
|
||||||
|
register_placeholder(md_gloss)
|
||||||
|
|
||||||
|
|
||||||
|
register_activity_session_insights()
|
||||||
@@ -0,0 +1,237 @@
|
|||||||
|
"""
|
||||||
|
Registry: BMI, Profil-Ziele (goal_weight, goal_bf_pct), body_progress_score.
|
||||||
|
|
||||||
|
Profilfelder sind unabhängig von der goals-Tabelle; operative Ziele über andere Keys.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from placeholder_registry import (
|
||||||
|
PlaceholderMetadata,
|
||||||
|
MissingValuePolicy,
|
||||||
|
EvidenceType,
|
||||||
|
OutputType,
|
||||||
|
PlaceholderType,
|
||||||
|
register_placeholder,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def register_body_extras():
|
||||||
|
bmi = PlaceholderMetadata(
|
||||||
|
key="bmi",
|
||||||
|
category="Körper",
|
||||||
|
description="Body-Mass-Index aus letztem Gewicht und Profilgröße (cm)",
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="calculate_bmi",
|
||||||
|
data_layer_module="backend/data_layer/body_metrics.py",
|
||||||
|
data_layer_function="get_bmi_data",
|
||||||
|
source_tables=["profiles", "weight_log"],
|
||||||
|
semantic_contract=(
|
||||||
|
"BMI = Gewicht_kg / (Größe_m)² mit Größe_m = profiles.height / 100 "
|
||||||
|
"und Gewicht = jüngster Eintrag in weight_log."
|
||||||
|
),
|
||||||
|
business_meaning="Standard-Körpermaß für Coaching und Risiko-Kontext",
|
||||||
|
unit="kg/m²",
|
||||||
|
time_window="latest weight + aktuelle Profilgröße",
|
||||||
|
output_type=OutputType.NUMERIC,
|
||||||
|
placeholder_type=PlaceholderType.RAW_DATA,
|
||||||
|
format_hint="Eine Dezimalstelle, ohne Einheit im String",
|
||||||
|
example_output="24.3",
|
||||||
|
minimum_data_requirements="Profil mit height > 0 und mindestens ein weight_log",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="high nur wenn BMI berechenbar; sonst insufficient / Anzeige nicht verfügbar",
|
||||||
|
missing_value_policy=MissingValuePolicy(
|
||||||
|
available=False,
|
||||||
|
value_raw=None,
|
||||||
|
missing_reason="no_data",
|
||||||
|
legacy_display="nicht verfügbar",
|
||||||
|
),
|
||||||
|
known_limitations=(
|
||||||
|
"Keine ethnischen Referenzkurven; Profilgröße kann veraltet sein. "
|
||||||
|
"Unterscheidet nicht Muskelmasse vs. Fett."
|
||||||
|
),
|
||||||
|
layer_1_decision="body_metrics.get_bmi_data",
|
||||||
|
layer_2a_decision="placeholder_resolver.calculate_bmi (Format)",
|
||||||
|
layer_2b_reuse_possible=True,
|
||||||
|
architecture_alignment="Phase 0c",
|
||||||
|
issue_53_alignment="Layer 1 als Quelle",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
for field in (
|
||||||
|
"key", "category", "description", "resolver_module", "resolver_function",
|
||||||
|
"data_layer_module", "data_layer_function", "source_tables",
|
||||||
|
"semantic_contract", "business_meaning", "unit", "time_window",
|
||||||
|
"output_type", "placeholder_type", "format_hint", "example_output",
|
||||||
|
"minimum_data_requirements", "confidence_logic", "missing_value_policy",
|
||||||
|
"known_limitations", "layer_1_decision", "layer_2a_decision",
|
||||||
|
"layer_2b_reuse_possible", "architecture_alignment", "issue_53_alignment",
|
||||||
|
):
|
||||||
|
bmi.set_evidence(field, EvidenceType.CODE_DERIVED)
|
||||||
|
bmi.set_evidence("business_meaning", EvidenceType.DRAFT_DERIVED)
|
||||||
|
bmi.set_evidence("known_limitations", EvidenceType.MIXED)
|
||||||
|
register_placeholder(bmi)
|
||||||
|
|
||||||
|
gw = PlaceholderMetadata(
|
||||||
|
key="goal_weight",
|
||||||
|
category="Körper",
|
||||||
|
description="Zielgewicht aus Profilfeld profiles.goal_weight (kg)",
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="get_goal_weight",
|
||||||
|
data_layer_module="backend/data_layer/body_metrics.py",
|
||||||
|
data_layer_function="get_profile_goal_weight_data",
|
||||||
|
source_tables=["profiles"],
|
||||||
|
semantic_contract=(
|
||||||
|
"Strategisches Soll-Gewicht im Profil; unabhängig von der goals-Tabelle "
|
||||||
|
"(dort detaillierte Ziele mit Fortschritt)."
|
||||||
|
),
|
||||||
|
business_meaning="Schneller Abgleich Prompt vs. Profil-Default-Zielgewicht",
|
||||||
|
unit="kg",
|
||||||
|
time_window="Profil-Snapshot",
|
||||||
|
output_type=OutputType.NUMERIC,
|
||||||
|
placeholder_type=PlaceholderType.RAW_DATA,
|
||||||
|
format_hint="Eine Dezimalstelle oder Text „nicht gesetzt“",
|
||||||
|
example_output="82.0",
|
||||||
|
minimum_data_requirements="profiles.goal_weight IS NOT NULL",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="high wenn gesetzt",
|
||||||
|
missing_value_policy=MissingValuePolicy(
|
||||||
|
available=False,
|
||||||
|
value_raw=None,
|
||||||
|
missing_reason="not_set",
|
||||||
|
legacy_display="nicht gesetzt",
|
||||||
|
),
|
||||||
|
known_limitations="Kann von aktiven goals.weight-Zielen abweichen.",
|
||||||
|
layer_1_decision="body_metrics.get_profile_goal_weight_data",
|
||||||
|
layer_2a_decision="placeholder_resolver.get_goal_weight",
|
||||||
|
layer_2b_reuse_possible=True,
|
||||||
|
architecture_alignment="Phase 0c",
|
||||||
|
issue_53_alignment="Layer 1 als Quelle",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
for field in (
|
||||||
|
"key", "category", "description", "resolver_module", "resolver_function",
|
||||||
|
"data_layer_module", "data_layer_function", "source_tables",
|
||||||
|
"semantic_contract", "unit", "time_window", "output_type",
|
||||||
|
"placeholder_type", "format_hint", "example_output",
|
||||||
|
"minimum_data_requirements", "confidence_logic", "missing_value_policy",
|
||||||
|
"layer_1_decision", "layer_2a_decision", "layer_2b_reuse_possible",
|
||||||
|
"architecture_alignment", "issue_53_alignment",
|
||||||
|
):
|
||||||
|
gw.set_evidence(field, EvidenceType.CODE_DERIVED)
|
||||||
|
gw.set_evidence("business_meaning", EvidenceType.DRAFT_DERIVED)
|
||||||
|
gw.set_evidence("known_limitations", EvidenceType.MIXED)
|
||||||
|
register_placeholder(gw)
|
||||||
|
|
||||||
|
gbf = PlaceholderMetadata(
|
||||||
|
key="goal_bf_pct",
|
||||||
|
category="Körper",
|
||||||
|
description="Ziel-Körperfettanteil aus Profilfeld profiles.goal_bf_pct (%)",
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="get_goal_bf_pct",
|
||||||
|
data_layer_module="backend/data_layer/body_metrics.py",
|
||||||
|
data_layer_function="get_profile_goal_bf_pct_data",
|
||||||
|
source_tables=["profiles"],
|
||||||
|
semantic_contract="Strategisches Ziel-KFA im Profil.",
|
||||||
|
business_meaning="Prompt-Abgleich mit Profil-Ziel-KFA",
|
||||||
|
unit="%",
|
||||||
|
time_window="Profil-Snapshot",
|
||||||
|
output_type=OutputType.NUMERIC,
|
||||||
|
placeholder_type=PlaceholderType.RAW_DATA,
|
||||||
|
format_hint="Eine Dezimalstelle oder Text „nicht gesetzt“",
|
||||||
|
example_output="15.0",
|
||||||
|
minimum_data_requirements="profiles.goal_bf_pct IS NOT NULL",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="high wenn gesetzt",
|
||||||
|
missing_value_policy=MissingValuePolicy(
|
||||||
|
available=False,
|
||||||
|
value_raw=None,
|
||||||
|
missing_reason="not_set",
|
||||||
|
legacy_display="nicht gesetzt",
|
||||||
|
),
|
||||||
|
known_limitations="Kann von goals body_fat abweichen.",
|
||||||
|
layer_1_decision="body_metrics.get_profile_goal_bf_pct_data",
|
||||||
|
layer_2a_decision="placeholder_resolver.get_goal_bf_pct",
|
||||||
|
layer_2b_reuse_possible=True,
|
||||||
|
architecture_alignment="Phase 0c",
|
||||||
|
issue_53_alignment="Layer 1 als Quelle",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
for field in (
|
||||||
|
"key", "category", "description", "resolver_module", "resolver_function",
|
||||||
|
"data_layer_module", "data_layer_function", "source_tables",
|
||||||
|
"semantic_contract", "unit", "time_window", "output_type",
|
||||||
|
"placeholder_type", "format_hint", "example_output",
|
||||||
|
"minimum_data_requirements", "confidence_logic", "missing_value_policy",
|
||||||
|
"layer_1_decision", "layer_2a_decision", "layer_2b_reuse_possible",
|
||||||
|
"architecture_alignment", "issue_53_alignment",
|
||||||
|
):
|
||||||
|
gbf.set_evidence(field, EvidenceType.CODE_DERIVED)
|
||||||
|
gbf.set_evidence("business_meaning", EvidenceType.DRAFT_DERIVED)
|
||||||
|
gbf.set_evidence("known_limitations", EvidenceType.MIXED)
|
||||||
|
register_placeholder(gbf)
|
||||||
|
|
||||||
|
bps = PlaceholderMetadata(
|
||||||
|
key="body_progress_score",
|
||||||
|
category="Körper",
|
||||||
|
description="Körper-Fortschritts-Score 0–100, gewichtet nach Focus (Abnehmen, Muskelaufbau, Recomp)",
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="_safe_int",
|
||||||
|
data_layer_module="backend/data_layer/body_metrics.py",
|
||||||
|
data_layer_function="calculate_body_progress_score",
|
||||||
|
source_tables=[
|
||||||
|
"user_focus_area_weights",
|
||||||
|
"focus_area_definitions",
|
||||||
|
"goals",
|
||||||
|
"weight_log",
|
||||||
|
"caliper_log",
|
||||||
|
"circumference_log",
|
||||||
|
],
|
||||||
|
semantic_contract=(
|
||||||
|
"Gewichteter Mittelwert aus bis zu drei Komponenten: Trend vs. Gewichtsziel, "
|
||||||
|
"Körperzusammensetzung (FM/LBM/Recomp-Quadrant), Taille-Trend. "
|
||||||
|
"Komponenten nur aktiv, wenn passende Focus-Gewichte > 0."
|
||||||
|
),
|
||||||
|
business_meaning="Meta-KPI: passt dokumentierter Körperfortschritt zur gewichteten Körper-Priorität?",
|
||||||
|
unit="Score (0–100)",
|
||||||
|
time_window="composite (u. a. 28d Deltas, Ziel-Fortschritt)",
|
||||||
|
output_type=OutputType.NUMERIC,
|
||||||
|
placeholder_type=PlaceholderType.SCORE,
|
||||||
|
format_hint="Ganzzahl oder „nicht verfügbar“",
|
||||||
|
example_output="72",
|
||||||
|
minimum_data_requirements=(
|
||||||
|
"Summe der Körper-Focus-Gewichte (weight_loss + muscle_gain + body_recomposition) > 0 "
|
||||||
|
"und mindestens eine bewertbare Komponente mit Daten."
|
||||||
|
),
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="Kein separates Confidence-Feld; None wenn keine Körper-Gewichtung oder keine Teilscores.",
|
||||||
|
missing_value_policy=MissingValuePolicy(
|
||||||
|
available=False,
|
||||||
|
value_raw=None,
|
||||||
|
missing_reason="not_applicable",
|
||||||
|
legacy_display="nicht verfügbar",
|
||||||
|
),
|
||||||
|
known_limitations=(
|
||||||
|
"Abhängig von user_focus_area_weights und aktiven weight-goals für Gewichts-Teilscore. "
|
||||||
|
"Taille-Score wird mit festem Basisgewicht 20+ eingemischt und kann dominieren."
|
||||||
|
),
|
||||||
|
layer_1_decision="body_metrics.calculate_body_progress_score",
|
||||||
|
layer_2a_decision="placeholder_resolver._safe_int('body_progress_score', …)",
|
||||||
|
layer_2b_reuse_possible=True,
|
||||||
|
architecture_alignment="Phase 0c",
|
||||||
|
issue_53_alignment="Layer 1 als Quelle",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
for field in (
|
||||||
|
"key", "category", "description", "resolver_module", "resolver_function",
|
||||||
|
"data_layer_module", "data_layer_function", "source_tables",
|
||||||
|
"semantic_contract", "unit", "time_window", "output_type",
|
||||||
|
"placeholder_type", "format_hint", "example_output",
|
||||||
|
"minimum_data_requirements", "confidence_logic", "missing_value_policy",
|
||||||
|
"layer_1_decision", "layer_2a_decision", "layer_2b_reuse_possible",
|
||||||
|
"architecture_alignment", "issue_53_alignment",
|
||||||
|
):
|
||||||
|
bps.set_evidence(field, EvidenceType.CODE_DERIVED)
|
||||||
|
bps.set_evidence("business_meaning", EvidenceType.DRAFT_DERIVED)
|
||||||
|
bps.set_evidence("known_limitations", EvidenceType.MIXED)
|
||||||
|
register_placeholder(bps)
|
||||||
|
|
||||||
|
|
||||||
|
register_body_extras()
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
"""
|
"""
|
||||||
Body Metrics Placeholder Registrations
|
Body Metrics Placeholder Registrations
|
||||||
|
|
||||||
Registers 17 body composition and measurement placeholders:
|
Registers 17 Körper-Metriken in diesem Modul; insgesamt 21 Körper-Keys in der Registry
|
||||||
|
(zusätzlich body_extras.py: bmi, goal_weight, goal_bf_pct, body_progress_score).
|
||||||
|
|
||||||
Weight & Trends (7):
|
Weight & Trends (7):
|
||||||
- weight_aktuell
|
- weight_aktuell
|
||||||
@@ -29,7 +30,7 @@ Summaries (2):
|
|||||||
- circ_summary
|
- circ_summary
|
||||||
|
|
||||||
Evidence-based metadata with comprehensive formula documentation.
|
Evidence-based metadata with comprehensive formula documentation.
|
||||||
Code inspection: backend/data_layer/body_metrics.py (830 lines)
|
Siehe backend/data_layer/body_metrics.py als Layer-1-Implementierung.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from placeholder_registry import (
|
from placeholder_registry import (
|
||||||
|
|||||||
@@ -0,0 +1,96 @@
|
|||||||
|
"""Registry: Korrelations- und Treiber-Metriken (Data Layer correlations)."""
|
||||||
|
|
||||||
|
from placeholder_registry import (
|
||||||
|
PlaceholderMetadata,
|
||||||
|
MissingValuePolicy,
|
||||||
|
OutputType,
|
||||||
|
PlaceholderType,
|
||||||
|
register_placeholder,
|
||||||
|
)
|
||||||
|
from ._evidence import tag_standard_evidence
|
||||||
|
|
||||||
|
CAT = "Korrelationen"
|
||||||
|
MVP = lambda reason, disp: MissingValuePolicy(
|
||||||
|
available=False, value_raw=None, missing_reason=reason, legacy_display=disp
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def register_korrelationen():
|
||||||
|
for key, dl_fn, desc, tables, sem in [
|
||||||
|
(
|
||||||
|
"correlation_energy_weight_lag",
|
||||||
|
"calculate_lag_correlation",
|
||||||
|
"JSON: Lag-Korrelation Energiebilanz ↔ Gewicht",
|
||||||
|
["nutrition_log", "weight_log"],
|
||||||
|
"correlations.calculate_lag_correlation(pid, 'energy', 'weight')",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"correlation_protein_lbm",
|
||||||
|
"calculate_lag_correlation",
|
||||||
|
"JSON: Lag-Korrelation Protein ↔ Magermasse",
|
||||||
|
["nutrition_log", "weight_log", "caliper_log"],
|
||||||
|
"correlations.calculate_lag_correlation(pid, 'protein', 'lbm')",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"correlation_load_hrv",
|
||||||
|
"calculate_lag_correlation",
|
||||||
|
"JSON: Lag-Korrelation Trainingslast ↔ HRV",
|
||||||
|
["activity_log", "vitals_baseline"],
|
||||||
|
"correlations.calculate_lag_correlation(pid, 'training_load', 'hrv')",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"correlation_load_rhr",
|
||||||
|
"calculate_lag_correlation",
|
||||||
|
"JSON: Lag-Korrelation Trainingslast ↔ Ruhepuls",
|
||||||
|
["activity_log", "vitals_baseline"],
|
||||||
|
"correlations.calculate_lag_correlation(pid, 'training_load', 'rhr')",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"plateau_detected",
|
||||||
|
"calculate_plateau_detected",
|
||||||
|
"JSON: Platten-Erkennung (Gewicht/Körper)",
|
||||||
|
["weight_log", "caliper_log"],
|
||||||
|
"correlations.calculate_plateau_detected",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"top_drivers",
|
||||||
|
"calculate_top_drivers",
|
||||||
|
"JSON: Top Treiber für Ziel-/Score-Variablen",
|
||||||
|
["weight_log", "nutrition_log", "activity_log", "vitals_baseline", "sleep_log"],
|
||||||
|
"correlations.calculate_top_drivers",
|
||||||
|
),
|
||||||
|
]:
|
||||||
|
m = PlaceholderMetadata(
|
||||||
|
key=key,
|
||||||
|
category=CAT,
|
||||||
|
description=desc,
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="_safe_json",
|
||||||
|
data_layer_module="backend/data_layer/correlations.py",
|
||||||
|
data_layer_function=dl_fn,
|
||||||
|
source_tables=tables,
|
||||||
|
semantic_contract=sem,
|
||||||
|
business_meaning="Strukturierte Korrelationsausgabe für KI",
|
||||||
|
unit="JSON",
|
||||||
|
time_window="funktionsintern",
|
||||||
|
output_type=OutputType.JSON,
|
||||||
|
placeholder_type=PlaceholderType.RAW_DATA,
|
||||||
|
format_hint="JSON-String",
|
||||||
|
example_output="{}",
|
||||||
|
minimum_data_requirements="Ausreichend gekoppelte Zeitreihen",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="Wie correlations.*",
|
||||||
|
missing_value_policy=MVP("insufficient_data", "{}"),
|
||||||
|
known_limitations="Bei wenigen Daten leer oder wenig robust",
|
||||||
|
layer_1_decision=f"correlations.{dl_fn}",
|
||||||
|
layer_2a_decision="_safe_json",
|
||||||
|
layer_2b_reuse_possible=True,
|
||||||
|
architecture_alignment="Phase 0c",
|
||||||
|
issue_53_alignment="Layer 1",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
tag_standard_evidence(m)
|
||||||
|
register_placeholder(m)
|
||||||
|
|
||||||
|
|
||||||
|
register_korrelationen()
|
||||||
@@ -53,6 +53,13 @@ def register_nutrition_part_a():
|
|||||||
"layer_1_decision": "Data Layer (nutrition_metrics.get_nutrition_average_data)",
|
"layer_1_decision": "Data Layer (nutrition_metrics.get_nutrition_average_data)",
|
||||||
"layer_2a_decision": "Placeholder Resolver (formatting only)",
|
"layer_2a_decision": "Placeholder Resolver (formatting only)",
|
||||||
"architecture_alignment": "Phase 0c Multi-Layer Architecture conform",
|
"architecture_alignment": "Phase 0c Multi-Layer Architecture conform",
|
||||||
|
"minimum_data_requirements": (
|
||||||
|
"Mind. ein Kalendertag mit nutrition_log im Fenster; Mittelwerte aus täglicher Aggregation. "
|
||||||
|
"Confidence über calculate_confidence(day_count, days) in get_nutrition_average_data."
|
||||||
|
),
|
||||||
|
"quality_filter_policy": (
|
||||||
|
"Kein Outlier-Filter auf Tagesaggregaten; leere Tage fehlen in der Aggregation (kein Imputing)."
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
# Common evidence for shared fields
|
# Common evidence for shared fields
|
||||||
@@ -73,8 +80,8 @@ def register_nutrition_part_a():
|
|||||||
"layer_2b_reuse_possible": EvidenceType.TO_VERIFY, # not verified in charts
|
"layer_2b_reuse_possible": EvidenceType.TO_VERIFY, # not verified in charts
|
||||||
"architecture_alignment": EvidenceType.CODE_DERIVED, # imports from data_layer
|
"architecture_alignment": EvidenceType.CODE_DERIVED, # imports from data_layer
|
||||||
"issue_53_alignment": EvidenceType.MIXED, # layer separation visible, issue conformity derived
|
"issue_53_alignment": EvidenceType.MIXED, # layer separation visible, issue conformity derived
|
||||||
"minimum_data_requirements": EvidenceType.UNRESOLVED, # not explicit in code
|
"minimum_data_requirements": EvidenceType.CODE_DERIVED,
|
||||||
"quality_filter_policy": EvidenceType.UNRESOLVED, # not implemented
|
"quality_filter_policy": EvidenceType.CODE_DERIVED,
|
||||||
}
|
}
|
||||||
|
|
||||||
# ── kcal_avg ──────────────────────────────────────────────────────────────
|
# ── kcal_avg ──────────────────────────────────────────────────────────────
|
||||||
@@ -94,8 +101,6 @@ def register_nutrition_part_a():
|
|||||||
known_limitations="nur Intake, kein Bedarf; sagt allein nichts über Zielpassung",
|
known_limitations="nur Intake, kein Bedarf; sagt allein nichts über Zielpassung",
|
||||||
layer_2b_reuse_possible=None, # to_verify - not checked in chart code
|
layer_2b_reuse_possible=None, # to_verify - not checked in chart code
|
||||||
issue_53_alignment="Layer separation established",
|
issue_53_alignment="Layer separation established",
|
||||||
minimum_data_requirements=None, # unresolved
|
|
||||||
quality_filter_policy=None, # unresolved
|
|
||||||
**common_metadata
|
**common_metadata
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -131,8 +136,6 @@ def register_nutrition_part_a():
|
|||||||
),
|
),
|
||||||
layer_2b_reuse_possible=None,
|
layer_2b_reuse_possible=None,
|
||||||
issue_53_alignment="Layer separation established",
|
issue_53_alignment="Layer separation established",
|
||||||
minimum_data_requirements=None,
|
|
||||||
quality_filter_policy=None,
|
|
||||||
**common_metadata
|
**common_metadata
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -165,8 +168,6 @@ def register_nutrition_part_a():
|
|||||||
),
|
),
|
||||||
layer_2b_reuse_possible=None,
|
layer_2b_reuse_possible=None,
|
||||||
issue_53_alignment="Layer separation established",
|
issue_53_alignment="Layer separation established",
|
||||||
minimum_data_requirements=None,
|
|
||||||
quality_filter_policy=None,
|
|
||||||
**common_metadata
|
**common_metadata
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -196,8 +197,6 @@ def register_nutrition_part_a():
|
|||||||
known_limitations="meist im Gesamtkontext der Makroverteilung relevant",
|
known_limitations="meist im Gesamtkontext der Makroverteilung relevant",
|
||||||
layer_2b_reuse_possible=None,
|
layer_2b_reuse_possible=None,
|
||||||
issue_53_alignment="Layer separation established",
|
issue_53_alignment="Layer separation established",
|
||||||
minimum_data_requirements=None,
|
|
||||||
quality_filter_policy=None,
|
|
||||||
**common_metadata
|
**common_metadata
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
"""
|
"""
|
||||||
Placeholder Registrations - Nutrition Part C
|
Placeholder Registrations - Nutrition Part C
|
||||||
|
|
||||||
Registers 5 nutrition-related placeholders with complete metadata:
|
Registers 5 nutrition-related placeholders in this file (nutrition_score: siehe nutrition_score.py):
|
||||||
- macro_consistency_score
|
- macro_consistency_score
|
||||||
- energy_balance_7d
|
- energy_balance_7d
|
||||||
- energy_deficit_surplus
|
- energy_deficit_surplus
|
||||||
@@ -113,7 +113,7 @@ energy_balance_metadata = PlaceholderMetadata(
|
|||||||
resolver_function="_safe_float('energy_balance_7d', pid, decimals=0)",
|
resolver_function="_safe_float('energy_balance_7d', pid, decimals=0)",
|
||||||
data_layer_module="backend/data_layer/nutrition_metrics.py",
|
data_layer_module="backend/data_layer/nutrition_metrics.py",
|
||||||
data_layer_function="calculate_energy_balance_7d",
|
data_layer_function="calculate_energy_balance_7d",
|
||||||
source_tables=["nutrition_log", "weight_log"],
|
source_tables=["nutrition_log", "weight_log", "profiles"],
|
||||||
|
|
||||||
# Semantic
|
# Semantic
|
||||||
semantic_contract="Liefert die geschätzte Energiebilanz über 7 Tage als Differenz zwischen durchschnittlicher Energieaufnahme und geschätztem TDEE (Total Daily Energy Expenditure). Positiver Wert = Überschuss, Negativer Wert = Defizit.",
|
semantic_contract="Liefert die geschätzte Energiebilanz über 7 Tage als Differenz zwischen durchschnittlicher Energieaufnahme und geschätztem TDEE (Total Daily Energy Expenditure). Positiver Wert = Überschuss, Negativer Wert = Defizit.",
|
||||||
@@ -127,11 +127,14 @@ energy_balance_metadata = PlaceholderMetadata(
|
|||||||
|
|
||||||
# Quality
|
# Quality
|
||||||
minimum_data_requirements="Mindestens 4 Tage mit Kalorienerfassung in 7-Tage-Fenster. Aktuelles Gewicht aus weight_log erforderlich.",
|
minimum_data_requirements="Mindestens 4 Tage mit Kalorienerfassung in 7-Tage-Fenster. Aktuelles Gewicht aus weight_log erforderlich.",
|
||||||
quality_filter_policy="Unvollständige Intake-Daten und fehlende Gewichtsmessung reduzieren Verlässlichkeit. TDEE-Schätzung ist vereinfacht (weight_kg × 32.5).",
|
quality_filter_policy=(
|
||||||
|
"Unvollständige Intake-Daten und fehlende Gewichtsmessung reduzieren Verlässlichkeit. "
|
||||||
|
"TDEE: Mifflin–St Jeor × PAL 1.55 wenn Höhe, Geschlecht, DOB und Gewicht vorhanden, sonst kg×32.5."
|
||||||
|
),
|
||||||
confidence_logic=(
|
confidence_logic=(
|
||||||
"Kombiniert Intake-Abdeckung und Robustheit des Verbrauchsmodells. "
|
"Kombiniert Intake-Abdeckung und Robustheit des Verbrauchsmodells. "
|
||||||
"Niedrigere Confidence bei <7 Tagen Daten oder fehlendem Gewicht. "
|
"Niedrigere Confidence bei <7 Tagen Daten oder fehlendem Gewicht. "
|
||||||
"TDEE-Modell ist vereinfacht → inherent uncertainty."
|
"PAL=1.55 ist ein Festwert (moderate Aktivität), kein individuelles Aktivitätslogging."
|
||||||
),
|
),
|
||||||
missing_value_policy=MissingValuePolicy(
|
missing_value_policy=MissingValuePolicy(
|
||||||
available=False,
|
available=False,
|
||||||
@@ -140,11 +143,10 @@ energy_balance_metadata = PlaceholderMetadata(
|
|||||||
legacy_display="nicht verfügbar"
|
legacy_display="nicht verfügbar"
|
||||||
),
|
),
|
||||||
known_limitations=(
|
known_limitations=(
|
||||||
"TDEE-MODELL: Vereinfacht als bodyweight_kg × 32.5 (mittlerer Multiplikator). "
|
"TDEE: Bei vollständigem Profil (Größe, Geschlecht, DOB, Gewicht) Mifflin–St Jeor BMR × 1.55; "
|
||||||
"NICHT berücksichtigt: Aktivitätslevel, Alter, Geschlecht, Stoffwechselanpassungen. "
|
"sonst Fallback kg×32.5. PAL ist nicht nutzerkonfigurierbar. "
|
||||||
"TODO in Code: Harris-Benedict oder Mifflin-St Jeor für präzisere TDEE-Schätzung. "
|
"Energiebilanz ist modellbasiert, nicht gemessen. "
|
||||||
"ACHTUNG: Energiebilanz ist modellbasiert, nicht direkt gemessen. "
|
"Einheit kcal/Tag (Tagesmittel), nicht 7-Tage-Summe."
|
||||||
"Einheit ist kcal/Tag (daily average), NICHT 7d-Total."
|
|
||||||
),
|
),
|
||||||
|
|
||||||
# Architecture
|
# Architecture
|
||||||
@@ -435,8 +437,9 @@ Part C Registration Complete:
|
|||||||
Total Nutrition Cluster:
|
Total Nutrition Cluster:
|
||||||
- Part A: 4 placeholders (kcal_avg, protein_avg, carb_avg, fat_avg)
|
- Part A: 4 placeholders (kcal_avg, protein_avg, carb_avg, fat_avg)
|
||||||
- Part B: 5 placeholders (protein targets + adequacy)
|
- Part B: 5 placeholders (protein targets + adequacy)
|
||||||
- Part C: 5 placeholders (consistency + balance + meta)
|
- Part C: 5 placeholders in dieser Datei (consistency + balance + meta)
|
||||||
→ 14 nutrition placeholders total
|
- nutrition_score: eigenes Modul nutrition_score.py
|
||||||
|
→ 15 Ernährungs-Platzhalter gesamt (A+B+C+nutrition_score)
|
||||||
|
|
||||||
All registrations follow Phase 0c Multi-Layer Architecture:
|
All registrations follow Phase 0c Multi-Layer Architecture:
|
||||||
- Layer 1 (Data Layer): Calculations
|
- Layer 1 (Data Layer): Calculations
|
||||||
|
|||||||
@@ -0,0 +1,102 @@
|
|||||||
|
"""
|
||||||
|
Placeholder registration: nutrition_score
|
||||||
|
|
||||||
|
Focus-gewichteter Ernährungs-Meta-Score (separates Modul, um nutrition_part_c schlank zu halten).
|
||||||
|
"""
|
||||||
|
|
||||||
|
from placeholder_registry import (
|
||||||
|
PlaceholderMetadata,
|
||||||
|
MissingValuePolicy,
|
||||||
|
EvidenceType,
|
||||||
|
OutputType,
|
||||||
|
PlaceholderType,
|
||||||
|
register_placeholder,
|
||||||
|
)
|
||||||
|
|
||||||
|
nutrition_score_metadata = PlaceholderMetadata(
|
||||||
|
key="nutrition_score",
|
||||||
|
category="Ernährung",
|
||||||
|
description="Ernährungs-Score (0–100), gewichtet nach Focus Areas",
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="_safe_int",
|
||||||
|
data_layer_module="backend/data_layer/nutrition_metrics.py",
|
||||||
|
data_layer_function="calculate_nutrition_score",
|
||||||
|
source_tables=[
|
||||||
|
"nutrition_log",
|
||||||
|
"weight_log",
|
||||||
|
"user_focus_area_weights",
|
||||||
|
"focus_area_definitions",
|
||||||
|
],
|
||||||
|
semantic_contract=(
|
||||||
|
"Gewichteter Score 0–100 aus Komponenten, die nur einfließen, wenn der Nutzer "
|
||||||
|
"passende Ernährungs-Focus-Gewichte gesetzt hat (z. B. protein_intake, "
|
||||||
|
"calorie_balance, macro_consistency). Nutzt u. a. Protein-Adequacy, "
|
||||||
|
"Makro-Konsistenz, Kalorien-Adhärenz (über Energiebilanz) und Makro-Balance."
|
||||||
|
),
|
||||||
|
business_meaning=(
|
||||||
|
"Verdichteter KPI für Prompts: passt die dokumentierte Ernährung zur "
|
||||||
|
"gewichteten strategischen Priorität des Nutzers?"
|
||||||
|
),
|
||||||
|
unit="score (0-100)",
|
||||||
|
time_window="composite (7d / 28d je Komponente)",
|
||||||
|
output_type=OutputType.NUMERIC,
|
||||||
|
placeholder_type=PlaceholderType.SCORE,
|
||||||
|
format_hint="Ganzzahl; bei fehlender Ernährungs-Gewichtung oft nicht verfügbar",
|
||||||
|
example_output="72",
|
||||||
|
minimum_data_requirements=(
|
||||||
|
"Mindestens eine Ernährungs-Focus-Komponente mit Gewicht > 0; "
|
||||||
|
"sowie je nach Komponente ausreichende nutrition_log-/weight_log-Abdeckung."
|
||||||
|
),
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic=(
|
||||||
|
"Kein separates Confidence-Feld im Resolver; fehlende Komponenten werden "
|
||||||
|
"aus der Gewichtung ausgeschlossen. total_nutrition_weight == 0 ergibt keinen Score."
|
||||||
|
),
|
||||||
|
missing_value_policy=MissingValuePolicy(
|
||||||
|
available=False,
|
||||||
|
value_raw=None,
|
||||||
|
missing_reason="not_applicable",
|
||||||
|
legacy_display="nicht verfügbar",
|
||||||
|
),
|
||||||
|
known_limitations=(
|
||||||
|
"Abhängig von user_focus_area_weights; ohne Ernährungs-Fokus liefert die "
|
||||||
|
"Funktion None. Kalorien-Adhärenz nutzt 7d-Energiebilanz vs. profiles.goal_mode "
|
||||||
|
"(weight_loss / strength+recomposition / sonst maintenance). "
|
||||||
|
"_score_macro_balance nutzt zeilenbasierte 28d-Abfrage (langfristig an "
|
||||||
|
"Tagesaggregation angleichen)."
|
||||||
|
),
|
||||||
|
layer_1_decision="Data Layer (nutrition_metrics.calculate_nutrition_score)",
|
||||||
|
layer_2a_decision="Placeholder Resolver (_safe_int)",
|
||||||
|
layer_2b_reuse_possible=True,
|
||||||
|
architecture_alignment="Phase 0c: Berechnung in nutrition_metrics",
|
||||||
|
issue_53_alignment="Layer 1 als Quelle; Komponenten nutzen weitere Layer-1-Funktionen",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
|
||||||
|
nutrition_score_metadata.set_evidence("key", EvidenceType.CODE_DERIVED)
|
||||||
|
nutrition_score_metadata.set_evidence("category", EvidenceType.CODE_DERIVED)
|
||||||
|
nutrition_score_metadata.set_evidence("description", EvidenceType.MIXED)
|
||||||
|
nutrition_score_metadata.set_evidence("resolver_module", EvidenceType.CODE_DERIVED)
|
||||||
|
nutrition_score_metadata.set_evidence("resolver_function", EvidenceType.CODE_DERIVED)
|
||||||
|
nutrition_score_metadata.set_evidence("data_layer_module", EvidenceType.CODE_DERIVED)
|
||||||
|
nutrition_score_metadata.set_evidence("data_layer_function", EvidenceType.CODE_DERIVED)
|
||||||
|
nutrition_score_metadata.set_evidence("source_tables", EvidenceType.CODE_DERIVED)
|
||||||
|
nutrition_score_metadata.set_evidence("semantic_contract", EvidenceType.CODE_DERIVED)
|
||||||
|
nutrition_score_metadata.set_evidence("business_meaning", EvidenceType.DRAFT_DERIVED)
|
||||||
|
nutrition_score_metadata.set_evidence("unit", EvidenceType.CODE_DERIVED)
|
||||||
|
nutrition_score_metadata.set_evidence("time_window", EvidenceType.CODE_DERIVED)
|
||||||
|
nutrition_score_metadata.set_evidence("output_type", EvidenceType.CODE_DERIVED)
|
||||||
|
nutrition_score_metadata.set_evidence("placeholder_type", EvidenceType.CODE_DERIVED)
|
||||||
|
nutrition_score_metadata.set_evidence("format_hint", EvidenceType.CODE_DERIVED)
|
||||||
|
nutrition_score_metadata.set_evidence("example_output", EvidenceType.CODE_DERIVED)
|
||||||
|
nutrition_score_metadata.set_evidence("minimum_data_requirements", EvidenceType.MIXED)
|
||||||
|
nutrition_score_metadata.set_evidence("confidence_logic", EvidenceType.CODE_DERIVED)
|
||||||
|
nutrition_score_metadata.set_evidence("missing_value_policy", EvidenceType.CODE_DERIVED)
|
||||||
|
nutrition_score_metadata.set_evidence("known_limitations", EvidenceType.MIXED)
|
||||||
|
nutrition_score_metadata.set_evidence("layer_1_decision", EvidenceType.CODE_DERIVED)
|
||||||
|
nutrition_score_metadata.set_evidence("layer_2a_decision", EvidenceType.CODE_DERIVED)
|
||||||
|
nutrition_score_metadata.set_evidence("layer_2b_reuse_possible", EvidenceType.TO_VERIFY)
|
||||||
|
nutrition_score_metadata.set_evidence("architecture_alignment", EvidenceType.CODE_DERIVED)
|
||||||
|
nutrition_score_metadata.set_evidence("issue_53_alignment", EvidenceType.MIXED)
|
||||||
|
|
||||||
|
register_placeholder(nutrition_score_metadata)
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
"""Registry: Meta-Scores (Phase 0b) — Ziel-Fortschritt und Datenqualität."""
|
||||||
|
|
||||||
|
from placeholder_registry import (
|
||||||
|
PlaceholderMetadata,
|
||||||
|
MissingValuePolicy,
|
||||||
|
OutputType,
|
||||||
|
PlaceholderType,
|
||||||
|
register_placeholder,
|
||||||
|
)
|
||||||
|
from ._evidence import tag_standard_evidence
|
||||||
|
|
||||||
|
CAT = "Scores (Phase 0b)"
|
||||||
|
MVP = lambda reason, disp: MissingValuePolicy(
|
||||||
|
available=False, value_raw=None, missing_reason=reason, legacy_display=disp
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def register_phase_0b_meta_scores():
|
||||||
|
for key, dl_fn, desc, unit in [
|
||||||
|
(
|
||||||
|
"goal_progress_score",
|
||||||
|
"calculate_goal_progress_score",
|
||||||
|
"Aggregierter Ziel-Fortschritt 0–100",
|
||||||
|
"0–100",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"data_quality_score",
|
||||||
|
"calculate_data_quality_score",
|
||||||
|
"Datenqualitäts-Score 0–100",
|
||||||
|
"0–100",
|
||||||
|
),
|
||||||
|
]:
|
||||||
|
m = PlaceholderMetadata(
|
||||||
|
key=key,
|
||||||
|
category=CAT,
|
||||||
|
description=desc,
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="_safe_int",
|
||||||
|
data_layer_module="backend/data_layer/scores.py",
|
||||||
|
data_layer_function=dl_fn,
|
||||||
|
source_tables=["goals", "weight_log", "nutrition_log", "activity_log", "profiles"],
|
||||||
|
semantic_contract=f"scores.{dl_fn} (siehe Data Layer).",
|
||||||
|
business_meaning="Meta-KPI für Prompt-Gewichtung",
|
||||||
|
unit=unit,
|
||||||
|
time_window="composite",
|
||||||
|
output_type=OutputType.NUMERIC,
|
||||||
|
placeholder_type=PlaceholderType.SCORE,
|
||||||
|
format_hint="Ganzzahl als String",
|
||||||
|
example_output="72",
|
||||||
|
minimum_data_requirements="Abhängig von Score-Implementierung",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="Wie calculate_* in scores.py",
|
||||||
|
missing_value_policy=MVP("insufficient_data", "nicht verfügbar"),
|
||||||
|
known_limitations="Bei dünnen Daten weniger aussagekräftig",
|
||||||
|
layer_1_decision=f"scores.{dl_fn}",
|
||||||
|
layer_2a_decision="_safe_int",
|
||||||
|
layer_2b_reuse_possible=True,
|
||||||
|
architecture_alignment="Phase 0b",
|
||||||
|
issue_53_alignment="Layer 1",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
tag_standard_evidence(m)
|
||||||
|
register_placeholder(m)
|
||||||
|
|
||||||
|
|
||||||
|
register_phase_0b_meta_scores()
|
||||||
@@ -0,0 +1,392 @@
|
|||||||
|
"""Registry: Ziele, Fokusbereiche, Kategorie-Scores und formatierte Listen (Phase 0b)."""
|
||||||
|
|
||||||
|
from placeholder_registry import (
|
||||||
|
PlaceholderMetadata,
|
||||||
|
MissingValuePolicy,
|
||||||
|
OutputType,
|
||||||
|
PlaceholderType,
|
||||||
|
register_placeholder,
|
||||||
|
)
|
||||||
|
from ._evidence import tag_standard_evidence
|
||||||
|
|
||||||
|
CAT = "Ziele & Fokus (Phase 0b)"
|
||||||
|
MVP = lambda reason, disp: MissingValuePolicy(
|
||||||
|
available=False, value_raw=None, missing_reason=reason, legacy_display=disp
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def register_phase_0b_ziele_fokus():
|
||||||
|
# Top-Ziel / Top-Fokusbereich
|
||||||
|
m = PlaceholderMetadata(
|
||||||
|
key="top_goal_name",
|
||||||
|
category=CAT,
|
||||||
|
description="Name/Typ des höchstpriorisierten Ziels",
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="_safe_str",
|
||||||
|
data_layer_module="backend/data_layer/scores.py",
|
||||||
|
data_layer_function="get_top_priority_goal",
|
||||||
|
source_tables=["goals"],
|
||||||
|
semantic_contract="Feld name oder goal_type aus get_top_priority_goal",
|
||||||
|
business_meaning="Priorisierung für KI-Empfehlungen",
|
||||||
|
unit="text",
|
||||||
|
time_window="aktuell",
|
||||||
|
output_type=OutputType.STRING,
|
||||||
|
placeholder_type=PlaceholderType.INTERPRETED,
|
||||||
|
format_hint="Kurztext",
|
||||||
|
example_output="Gewicht 80kg",
|
||||||
|
minimum_data_requirements="Mindestens ein aktives Ziel",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="scores.get_top_priority_goal",
|
||||||
|
missing_value_policy=MVP("insufficient_data", "nicht verfügbar"),
|
||||||
|
known_limitations=None,
|
||||||
|
layer_1_decision="scores.get_top_priority_goal",
|
||||||
|
layer_2a_decision="_safe_str('top_goal_name')",
|
||||||
|
layer_2b_reuse_possible=False,
|
||||||
|
architecture_alignment="Phase 0b",
|
||||||
|
issue_53_alignment="Layer 1",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
tag_standard_evidence(m)
|
||||||
|
register_placeholder(m)
|
||||||
|
|
||||||
|
m = PlaceholderMetadata(
|
||||||
|
key="top_goal_progress_pct",
|
||||||
|
category=CAT,
|
||||||
|
description="Fortschritt Top-Ziel (%)",
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="_safe_int",
|
||||||
|
data_layer_module="backend/data_layer/scores.py",
|
||||||
|
data_layer_function="get_top_priority_goal",
|
||||||
|
source_tables=["goals"],
|
||||||
|
semantic_contract="progress_pct aus get_top_priority_goal",
|
||||||
|
business_meaning="Priorisierung für KI-Empfehlungen",
|
||||||
|
unit="%",
|
||||||
|
time_window="aktuell",
|
||||||
|
output_type=OutputType.NUMERIC,
|
||||||
|
placeholder_type=PlaceholderType.SCORE,
|
||||||
|
format_hint="Ganzzahl",
|
||||||
|
example_output="65",
|
||||||
|
minimum_data_requirements="Mindestens ein aktives Ziel",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="scores.get_top_priority_goal",
|
||||||
|
missing_value_policy=MVP("insufficient_data", "nicht verfügbar"),
|
||||||
|
known_limitations=None,
|
||||||
|
layer_1_decision="scores.get_top_priority_goal",
|
||||||
|
layer_2a_decision="_safe_int('top_goal_progress_pct')",
|
||||||
|
layer_2b_reuse_possible=False,
|
||||||
|
architecture_alignment="Phase 0b",
|
||||||
|
issue_53_alignment="Layer 1",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
tag_standard_evidence(m)
|
||||||
|
register_placeholder(m)
|
||||||
|
|
||||||
|
m = PlaceholderMetadata(
|
||||||
|
key="top_goal_status",
|
||||||
|
category=CAT,
|
||||||
|
description="Status-Label Top-Ziel",
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="_safe_str",
|
||||||
|
data_layer_module="backend/data_layer/scores.py",
|
||||||
|
data_layer_function="get_top_priority_goal",
|
||||||
|
source_tables=["goals"],
|
||||||
|
semantic_contract="status aus get_top_priority_goal",
|
||||||
|
business_meaning="Priorisierung für KI-Empfehlungen",
|
||||||
|
unit="text",
|
||||||
|
time_window="aktuell",
|
||||||
|
output_type=OutputType.STRING,
|
||||||
|
placeholder_type=PlaceholderType.INTERPRETED,
|
||||||
|
format_hint="Kurztext",
|
||||||
|
example_output="active",
|
||||||
|
minimum_data_requirements="Mindestens ein aktives Ziel",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="scores.get_top_priority_goal",
|
||||||
|
missing_value_policy=MVP("insufficient_data", "nicht verfügbar"),
|
||||||
|
known_limitations=None,
|
||||||
|
layer_1_decision="scores.get_top_priority_goal",
|
||||||
|
layer_2a_decision="_safe_str('top_goal_status')",
|
||||||
|
layer_2b_reuse_possible=False,
|
||||||
|
architecture_alignment="Phase 0b",
|
||||||
|
issue_53_alignment="Layer 1",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
tag_standard_evidence(m)
|
||||||
|
register_placeholder(m)
|
||||||
|
|
||||||
|
m = PlaceholderMetadata(
|
||||||
|
key="top_focus_area_name",
|
||||||
|
category=CAT,
|
||||||
|
description="Bezeichnung des gewichtet stärksten Fokusbereichs",
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="_safe_str",
|
||||||
|
data_layer_module="backend/data_layer/scores.py",
|
||||||
|
data_layer_function="get_top_focus_area",
|
||||||
|
source_tables=["user_focus_area_weights", "focus_area_definitions"],
|
||||||
|
semantic_contract="label aus get_top_focus_area",
|
||||||
|
business_meaning="Priorisierung für KI-Empfehlungen",
|
||||||
|
unit="text",
|
||||||
|
time_window="aktuell",
|
||||||
|
output_type=OutputType.STRING,
|
||||||
|
placeholder_type=PlaceholderType.INTERPRETED,
|
||||||
|
format_hint="Kurztext",
|
||||||
|
example_output="Kraft",
|
||||||
|
minimum_data_requirements="Gewichtete Fokusbereiche",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="scores.get_top_focus_area",
|
||||||
|
missing_value_policy=MVP("insufficient_data", "nicht verfügbar"),
|
||||||
|
known_limitations=None,
|
||||||
|
layer_1_decision="scores.get_top_focus_area",
|
||||||
|
layer_2a_decision="_safe_str('top_focus_area_name')",
|
||||||
|
layer_2b_reuse_possible=False,
|
||||||
|
architecture_alignment="Phase 0b",
|
||||||
|
issue_53_alignment="Layer 1",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
tag_standard_evidence(m)
|
||||||
|
register_placeholder(m)
|
||||||
|
|
||||||
|
m = PlaceholderMetadata(
|
||||||
|
key="top_focus_area_progress",
|
||||||
|
category=CAT,
|
||||||
|
description="Fortschritt Top-Fokusbereich (%)",
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="_safe_int",
|
||||||
|
data_layer_module="backend/data_layer/scores.py",
|
||||||
|
data_layer_function="get_top_focus_area",
|
||||||
|
source_tables=["user_focus_area_weights", "focus_area_definitions", "goals"],
|
||||||
|
semantic_contract="progress aus get_top_focus_area",
|
||||||
|
business_meaning="Priorisierung für KI-Empfehlungen",
|
||||||
|
unit="%",
|
||||||
|
time_window="aktuell",
|
||||||
|
output_type=OutputType.NUMERIC,
|
||||||
|
placeholder_type=PlaceholderType.SCORE,
|
||||||
|
format_hint="Ganzzahl",
|
||||||
|
example_output="58",
|
||||||
|
minimum_data_requirements="Gewichtete Fokusbereiche",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="scores.get_top_focus_area",
|
||||||
|
missing_value_policy=MVP("insufficient_data", "nicht verfügbar"),
|
||||||
|
known_limitations=None,
|
||||||
|
layer_1_decision="scores.get_top_focus_area",
|
||||||
|
layer_2a_decision="_safe_int('top_focus_area_progress')",
|
||||||
|
layer_2b_reuse_possible=False,
|
||||||
|
architecture_alignment="Phase 0b",
|
||||||
|
issue_53_alignment="Layer 1",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
tag_standard_evidence(m)
|
||||||
|
register_placeholder(m)
|
||||||
|
|
||||||
|
# Kategorie Progress / Weight (7 Kategorien)
|
||||||
|
for slug in (
|
||||||
|
"körper",
|
||||||
|
"ernährung",
|
||||||
|
"aktivität",
|
||||||
|
"recovery",
|
||||||
|
"vitalwerte",
|
||||||
|
"mental",
|
||||||
|
"lebensstil",
|
||||||
|
):
|
||||||
|
key_p = f"focus_cat_{slug}_progress"
|
||||||
|
key_w = f"focus_cat_{slug}_weight"
|
||||||
|
m_p = PlaceholderMetadata(
|
||||||
|
key=key_p,
|
||||||
|
category=CAT,
|
||||||
|
description=f"Aggregierter Fortschritt Kategorie „{slug}“ (%)",
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="_safe_int",
|
||||||
|
data_layer_module="backend/data_layer/scores.py",
|
||||||
|
data_layer_function="calculate_category_progress",
|
||||||
|
source_tables=["goals", "focus_area_definitions", "user_focus_area_weights"],
|
||||||
|
semantic_contract=f"scores.calculate_category_progress(pid, '{slug}')",
|
||||||
|
business_meaning="Focus-Area-Kategorie-Score",
|
||||||
|
unit="%",
|
||||||
|
time_window="aktuell",
|
||||||
|
output_type=OutputType.NUMERIC,
|
||||||
|
placeholder_type=PlaceholderType.SCORE,
|
||||||
|
format_hint="Ganzzahl",
|
||||||
|
example_output="55",
|
||||||
|
minimum_data_requirements="Gewichtete Bereiche in Kategorie",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="scores.calculate_category_progress",
|
||||||
|
missing_value_policy=MVP("insufficient_data", "nicht verfügbar"),
|
||||||
|
known_limitations=None,
|
||||||
|
layer_1_decision="scores.calculate_category_progress",
|
||||||
|
layer_2a_decision="_safe_int",
|
||||||
|
layer_2b_reuse_possible=True,
|
||||||
|
architecture_alignment="Phase 0b",
|
||||||
|
issue_53_alignment="Layer 1",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
tag_standard_evidence(m_p)
|
||||||
|
register_placeholder(m_p)
|
||||||
|
|
||||||
|
m_w = PlaceholderMetadata(
|
||||||
|
key=key_w,
|
||||||
|
category=CAT,
|
||||||
|
description=f"Nutzer-Gewichtung Kategorie „{slug}“ (Anteil 0–1)",
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="_safe_float",
|
||||||
|
data_layer_module="backend/data_layer/scores.py",
|
||||||
|
data_layer_function="calculate_category_weight",
|
||||||
|
source_tables=["user_focus_area_weights", "focus_area_definitions"],
|
||||||
|
semantic_contract=f"scores.calculate_category_weight(pid, '{slug}')",
|
||||||
|
business_meaning="Kategorie-Gewichtung im Fokusmodell",
|
||||||
|
unit="0–1",
|
||||||
|
time_window="aktuell",
|
||||||
|
output_type=OutputType.NUMERIC,
|
||||||
|
placeholder_type=PlaceholderType.INTERPRETED,
|
||||||
|
format_hint="Dezimal",
|
||||||
|
example_output="0.25",
|
||||||
|
minimum_data_requirements="user_focus_area_weights",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="scores.calculate_category_weight",
|
||||||
|
missing_value_policy=MVP("insufficient_data", "nicht verfügbar"),
|
||||||
|
known_limitations=None,
|
||||||
|
layer_1_decision="scores.calculate_category_weight",
|
||||||
|
layer_2a_decision="_safe_float",
|
||||||
|
layer_2b_reuse_possible=False,
|
||||||
|
architecture_alignment="Phase 0b",
|
||||||
|
issue_53_alignment="Layer 1",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
tag_standard_evidence(m_w)
|
||||||
|
register_placeholder(m_w)
|
||||||
|
|
||||||
|
# Strukturierte Ziele / Fokus
|
||||||
|
for key, res_fn, dl_mod, dl_fn, desc, out, ptype in [
|
||||||
|
(
|
||||||
|
"active_goals_json",
|
||||||
|
"_safe_json",
|
||||||
|
"backend/goal_utils.py",
|
||||||
|
"get_active_goals",
|
||||||
|
"Aktive Ziele als JSON",
|
||||||
|
OutputType.JSON,
|
||||||
|
PlaceholderType.RAW_DATA,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"active_goals_md",
|
||||||
|
"_safe_str",
|
||||||
|
"backend/placeholder_resolver.py",
|
||||||
|
"_format_goals_as_markdown",
|
||||||
|
"Aktive Ziele als Markdown-Tabelle",
|
||||||
|
OutputType.TEXT_SUMMARY,
|
||||||
|
PlaceholderType.INTERPRETED,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"focus_areas_weighted_json",
|
||||||
|
"_safe_json",
|
||||||
|
"backend/placeholder_resolver.py",
|
||||||
|
"_get_focus_areas_weighted_json",
|
||||||
|
"Gewichtete Fokusbereiche mit Namen (JSON)",
|
||||||
|
OutputType.JSON,
|
||||||
|
PlaceholderType.RAW_DATA,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"focus_areas_weighted_md",
|
||||||
|
"_safe_str",
|
||||||
|
"backend/placeholder_resolver.py",
|
||||||
|
"_format_focus_areas_as_markdown",
|
||||||
|
"Gewichtete Fokusbereiche als Markdown",
|
||||||
|
OutputType.TEXT_SUMMARY,
|
||||||
|
PlaceholderType.INTERPRETED,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"focus_area_weights_json",
|
||||||
|
"_safe_json",
|
||||||
|
"backend/data_layer/scores.py",
|
||||||
|
"get_user_focus_weights",
|
||||||
|
"Rohe Gewichtungen key→Anteil (JSON)",
|
||||||
|
OutputType.JSON,
|
||||||
|
PlaceholderType.RAW_DATA,
|
||||||
|
),
|
||||||
|
]:
|
||||||
|
m = PlaceholderMetadata(
|
||||||
|
key=key,
|
||||||
|
category=CAT,
|
||||||
|
description=desc,
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function=res_fn,
|
||||||
|
data_layer_module=dl_mod,
|
||||||
|
data_layer_function=dl_fn,
|
||||||
|
source_tables=["goals", "focus_area_definitions", "user_focus_area_weights"],
|
||||||
|
semantic_contract=f"{dl_fn} (siehe Modul {dl_mod})",
|
||||||
|
business_meaning="Strukturierte Übersicht für Prompts",
|
||||||
|
unit="JSON" if out == OutputType.JSON else "markdown",
|
||||||
|
time_window="aktuell",
|
||||||
|
output_type=out,
|
||||||
|
placeholder_type=ptype,
|
||||||
|
format_hint="String aus Resolver",
|
||||||
|
example_output="[]" if out == OutputType.JSON else "—",
|
||||||
|
minimum_data_requirements="Ziele bzw. Fokusgewichte",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="Resolver + goal_utils / scores",
|
||||||
|
missing_value_policy=MVP("insufficient_data", "[]" if out == OutputType.JSON else "nicht verfügbar"),
|
||||||
|
known_limitations=None,
|
||||||
|
layer_1_decision=dl_fn,
|
||||||
|
layer_2a_decision=res_fn,
|
||||||
|
layer_2b_reuse_possible=False,
|
||||||
|
architecture_alignment="Phase 0b",
|
||||||
|
issue_53_alignment="Layer 1",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
tag_standard_evidence(m)
|
||||||
|
register_placeholder(m)
|
||||||
|
|
||||||
|
for key, res_fn, dl_fn, desc, ex in [
|
||||||
|
(
|
||||||
|
"top_3_focus_areas",
|
||||||
|
"_safe_str",
|
||||||
|
"_format_top_focus_areas",
|
||||||
|
"Top-3 Fokusbereiche als formatierter Text",
|
||||||
|
"1. Kraft …",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"top_3_goals_behind_schedule",
|
||||||
|
"_safe_str",
|
||||||
|
"_format_goals_behind",
|
||||||
|
"Bis zu drei Ziele hinter Zeitplan",
|
||||||
|
"—",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"top_3_goals_on_track",
|
||||||
|
"_safe_str",
|
||||||
|
"_format_goals_on_track",
|
||||||
|
"Bis zu drei Ziele im Plan",
|
||||||
|
"—",
|
||||||
|
),
|
||||||
|
]:
|
||||||
|
m = PlaceholderMetadata(
|
||||||
|
key=key,
|
||||||
|
category=CAT,
|
||||||
|
description=desc,
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function=res_fn,
|
||||||
|
data_layer_module="backend/goal_utils.py",
|
||||||
|
data_layer_function="get_active_goals",
|
||||||
|
source_tables=["goals", "focus_area_definitions"],
|
||||||
|
semantic_contract=f"Resolver {dl_fn}",
|
||||||
|
business_meaning="Kurzlisten für Coaching-Prompts",
|
||||||
|
unit="text",
|
||||||
|
time_window="aktuell",
|
||||||
|
output_type=OutputType.TEXT_SUMMARY,
|
||||||
|
placeholder_type=PlaceholderType.INTERPRETED,
|
||||||
|
format_hint="Freitext / Aufzählung",
|
||||||
|
example_output=ex,
|
||||||
|
minimum_data_requirements="Ziele / Fokusdaten",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic=dl_fn,
|
||||||
|
missing_value_policy=MVP("insufficient_data", "nicht verfügbar"),
|
||||||
|
known_limitations=None,
|
||||||
|
layer_1_decision="goals + focus aggregation",
|
||||||
|
layer_2a_decision=dl_fn,
|
||||||
|
layer_2b_reuse_possible=False,
|
||||||
|
architecture_alignment="Phase 0b",
|
||||||
|
issue_53_alignment="Layer 2a",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
tag_standard_evidence(m)
|
||||||
|
register_placeholder(m)
|
||||||
|
|
||||||
|
|
||||||
|
register_phase_0b_ziele_fokus()
|
||||||
@@ -0,0 +1,139 @@
|
|||||||
|
"""
|
||||||
|
Registry: Profil-Stammdaten und statische Zeitraum-Labels für Prompts.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from placeholder_registry import (
|
||||||
|
PlaceholderMetadata,
|
||||||
|
MissingValuePolicy,
|
||||||
|
OutputType,
|
||||||
|
PlaceholderType,
|
||||||
|
register_placeholder,
|
||||||
|
)
|
||||||
|
from ._evidence import tag_standard_evidence
|
||||||
|
|
||||||
|
MVP = lambda reason, disp: MissingValuePolicy(
|
||||||
|
available=False, value_raw=None, missing_reason=reason, legacy_display=disp
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def register_profil_zeitraum():
|
||||||
|
cat_profil = "Profil"
|
||||||
|
for key, desc, res_fn, unit, ptype, out, hint, ex, sem in [
|
||||||
|
(
|
||||||
|
"name",
|
||||||
|
"Anzeigename aus profiles.name",
|
||||||
|
"get_profile_name",
|
||||||
|
"text",
|
||||||
|
PlaceholderType.ATOMIC,
|
||||||
|
OutputType.STRING,
|
||||||
|
"Kurzname",
|
||||||
|
"Max",
|
||||||
|
"profiles.name, Fallback „Nutzer“.",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"age",
|
||||||
|
"Alter in Jahren aus profiles.dob",
|
||||||
|
"get_profile_age_display",
|
||||||
|
"Jahre",
|
||||||
|
PlaceholderType.ATOMIC,
|
||||||
|
OutputType.STRING,
|
||||||
|
"Ganzzahl oder unbekannt",
|
||||||
|
"42",
|
||||||
|
"Berechnung aus Geburtsdatum; PostgreSQL date oder ISO-String.",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"height",
|
||||||
|
"Körpergröße (cm) aus profiles.height",
|
||||||
|
"get_profile_height_display",
|
||||||
|
"cm",
|
||||||
|
PlaceholderType.ATOMIC,
|
||||||
|
OutputType.STRING,
|
||||||
|
"Zahl oder unbekannt",
|
||||||
|
"180",
|
||||||
|
"profiles.height.",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"geschlecht",
|
||||||
|
"Geschlecht (männlich/weiblich) aus profiles.sex",
|
||||||
|
"get_profile_geschlecht_display",
|
||||||
|
"Kategorie",
|
||||||
|
PlaceholderType.ATOMIC,
|
||||||
|
OutputType.STRING,
|
||||||
|
"m/w-Mapping",
|
||||||
|
"männlich",
|
||||||
|
"sex == 'm' → männlich, sonst weiblich.",
|
||||||
|
),
|
||||||
|
]:
|
||||||
|
m = PlaceholderMetadata(
|
||||||
|
key=key,
|
||||||
|
category=cat_profil,
|
||||||
|
description=desc,
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function=res_fn,
|
||||||
|
data_layer_module=None,
|
||||||
|
data_layer_function=None,
|
||||||
|
source_tables=["profiles"],
|
||||||
|
semantic_contract=sem,
|
||||||
|
business_meaning="Profil-Kontext für KI-Prompts",
|
||||||
|
unit=unit,
|
||||||
|
time_window="latest profile row",
|
||||||
|
output_type=out,
|
||||||
|
placeholder_type=ptype,
|
||||||
|
format_hint=hint,
|
||||||
|
example_output=ex,
|
||||||
|
minimum_data_requirements="Profilzeile",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="Row vorhanden",
|
||||||
|
missing_value_policy=MVP("no_data", "unbekannt" if key != "name" else "Nutzer"),
|
||||||
|
known_limitations="Keine diversen Geschlechtsoptionen im Platzhalter",
|
||||||
|
layer_1_decision="profiles",
|
||||||
|
layer_2a_decision=res_fn,
|
||||||
|
layer_2b_reuse_possible=False,
|
||||||
|
architecture_alignment="Phase 0b",
|
||||||
|
issue_53_alignment="Resolver",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
tag_standard_evidence(m)
|
||||||
|
register_placeholder(m)
|
||||||
|
|
||||||
|
cat_zeit = "Zeitraum"
|
||||||
|
for key, desc, res_fn, sem, ex_out in [
|
||||||
|
("datum_heute", "Heutiges Datum (lokal)", "get_datum_heute", "datetime.now, Format dd.mm.yyyy", "11.04.2026"),
|
||||||
|
("zeitraum_7d", "Label „letzte 7 Tage“", "get_zeitraum_label_7d", "Statisches UI/Prompt-Label", "letzte 7 Tage"),
|
||||||
|
("zeitraum_30d", "Label „letzte 30 Tage“", "get_zeitraum_label_30d", "Statisches UI/Prompt-Label", "letzte 30 Tage"),
|
||||||
|
("zeitraum_90d", "Label „letzte 90 Tage“", "get_zeitraum_label_90d", "Statisches UI/Prompt-Label", "letzte 90 Tage"),
|
||||||
|
]:
|
||||||
|
m = PlaceholderMetadata(
|
||||||
|
key=key,
|
||||||
|
category=cat_zeit,
|
||||||
|
description=desc,
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function=res_fn,
|
||||||
|
data_layer_module=None,
|
||||||
|
data_layer_function=None,
|
||||||
|
source_tables=[],
|
||||||
|
semantic_contract=sem,
|
||||||
|
business_meaning="Zeitlicher Bezug im Prompt ohne Datenabfrage",
|
||||||
|
unit="label",
|
||||||
|
time_window="n/a",
|
||||||
|
output_type=OutputType.STRING,
|
||||||
|
placeholder_type=PlaceholderType.META,
|
||||||
|
format_hint="Kurzdeutsch",
|
||||||
|
example_output=ex_out,
|
||||||
|
minimum_data_requirements=None,
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="Immer verfügbar",
|
||||||
|
missing_value_policy=None,
|
||||||
|
known_limitations="Kein kalender-basierter Datenfilter allein durch Platzhalter",
|
||||||
|
layer_1_decision="n/a",
|
||||||
|
layer_2a_decision=res_fn,
|
||||||
|
layer_2b_reuse_possible=False,
|
||||||
|
architecture_alignment="Phase 0b",
|
||||||
|
issue_53_alignment="Resolver",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
tag_standard_evidence(m)
|
||||||
|
register_placeholder(m)
|
||||||
|
|
||||||
|
|
||||||
|
register_profil_zeitraum()
|
||||||
@@ -0,0 +1,236 @@
|
|||||||
|
"""
|
||||||
|
Registry: Schlaf, Ruhetage, Recovery-Score, Schlaf-Metriken, Schlaf-Erholungs-Korrelation.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from placeholder_registry import (
|
||||||
|
PlaceholderMetadata,
|
||||||
|
MissingValuePolicy,
|
||||||
|
EvidenceType,
|
||||||
|
OutputType,
|
||||||
|
PlaceholderType,
|
||||||
|
register_placeholder,
|
||||||
|
)
|
||||||
|
|
||||||
|
CAT = "Schlaf & Erholung"
|
||||||
|
MVP = lambda reason, disp: MissingValuePolicy(
|
||||||
|
available=False, value_raw=None, missing_reason=reason, legacy_display=disp
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _tag(m: PlaceholderMetadata):
|
||||||
|
for f in (
|
||||||
|
"key", "category", "description", "resolver_module", "resolver_function",
|
||||||
|
"data_layer_module", "data_layer_function", "source_tables", "semantic_contract",
|
||||||
|
"unit", "time_window", "output_type", "placeholder_type", "format_hint",
|
||||||
|
"example_output", "minimum_data_requirements", "confidence_logic",
|
||||||
|
"missing_value_policy", "layer_1_decision", "layer_2a_decision",
|
||||||
|
"layer_2b_reuse_possible", "architecture_alignment", "issue_53_alignment",
|
||||||
|
):
|
||||||
|
m.set_evidence(f, EvidenceType.CODE_DERIVED)
|
||||||
|
m.set_evidence("business_meaning", EvidenceType.DRAFT_DERIVED)
|
||||||
|
m.set_evidence("known_limitations", EvidenceType.MIXED)
|
||||||
|
|
||||||
|
|
||||||
|
def register_schlaf_erholung():
|
||||||
|
# ── formatierte Schlaf-/Ruhetage-Snapshots ───────────────────────────────
|
||||||
|
m = PlaceholderMetadata(
|
||||||
|
key="sleep_avg_duration",
|
||||||
|
category=CAT,
|
||||||
|
description="Durchschnittliche Schlafdauer (Stunden), formatiert",
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="get_sleep_avg_duration",
|
||||||
|
data_layer_module="backend/data_layer/recovery_metrics.py",
|
||||||
|
data_layer_function="get_sleep_duration_data",
|
||||||
|
source_tables=["sleep_log"],
|
||||||
|
semantic_contract="Mittel aus Schlafphasen im Fenster (siehe get_sleep_duration_data).",
|
||||||
|
business_meaning="KI-Kontext Schlafdauer",
|
||||||
|
unit="h (Anzeige mit Einheit)",
|
||||||
|
time_window="7d default im Resolver",
|
||||||
|
output_type=OutputType.STRING,
|
||||||
|
placeholder_type=PlaceholderType.INTERPRETED,
|
||||||
|
format_hint="z. B. 7.2h",
|
||||||
|
example_output="7.2h",
|
||||||
|
minimum_data_requirements="sleep_log im Fenster",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="data['confidence'] im Layer1",
|
||||||
|
missing_value_policy=MVP("no_data", "nicht verfügbar"),
|
||||||
|
known_limitations="Abhängig von Import/Qualität der Phasen",
|
||||||
|
layer_1_decision="recovery_metrics.get_sleep_duration_data",
|
||||||
|
layer_2a_decision="get_sleep_avg_duration",
|
||||||
|
layer_2b_reuse_possible=True,
|
||||||
|
architecture_alignment="Phase 0c",
|
||||||
|
issue_53_alignment="Layer 1",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
_tag(m)
|
||||||
|
register_placeholder(m)
|
||||||
|
|
||||||
|
m = PlaceholderMetadata(
|
||||||
|
key="sleep_avg_quality",
|
||||||
|
category=CAT,
|
||||||
|
description="Schlafqualität (Deep+REM %), formatiert",
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="get_sleep_avg_quality",
|
||||||
|
data_layer_module="backend/data_layer/recovery_metrics.py",
|
||||||
|
data_layer_function="get_sleep_quality_data",
|
||||||
|
source_tables=["sleep_log"],
|
||||||
|
semantic_contract="Anteil Deep+REM aus Segmenten (siehe get_sleep_quality_data).",
|
||||||
|
business_meaning="KI-Kontext Schlafqualität",
|
||||||
|
unit="%",
|
||||||
|
time_window="7d default",
|
||||||
|
output_type=OutputType.STRING,
|
||||||
|
placeholder_type=PlaceholderType.INTERPRETED,
|
||||||
|
format_hint="Prozent oder nicht verfügbar",
|
||||||
|
example_output="24%",
|
||||||
|
minimum_data_requirements="sleep_log mit Phasen",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="Layer-1-Confidence",
|
||||||
|
missing_value_policy=MVP("no_data", "nicht verfügbar"),
|
||||||
|
known_limitations="Segment-Schreibweise case-sensitiv normalisiert",
|
||||||
|
layer_1_decision="recovery_metrics.get_sleep_quality_data",
|
||||||
|
layer_2a_decision="get_sleep_avg_quality",
|
||||||
|
layer_2b_reuse_possible=True,
|
||||||
|
architecture_alignment="Phase 0c",
|
||||||
|
issue_53_alignment="Layer 1",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
_tag(m)
|
||||||
|
register_placeholder(m)
|
||||||
|
|
||||||
|
m = PlaceholderMetadata(
|
||||||
|
key="rest_days_count",
|
||||||
|
category=CAT,
|
||||||
|
description="Anzahl dokumentierter Ruhetage (30d default)",
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="get_rest_days_count",
|
||||||
|
data_layer_module="backend/data_layer/recovery_metrics.py",
|
||||||
|
data_layer_function="get_rest_days_data",
|
||||||
|
source_tables=["rest_days"],
|
||||||
|
semantic_contract="Count rest_days im Zeitraum",
|
||||||
|
business_meaning="Aktive/passive Erholungstags-Übersicht",
|
||||||
|
unit="count",
|
||||||
|
time_window="30d default",
|
||||||
|
output_type=OutputType.STRING,
|
||||||
|
placeholder_type=PlaceholderType.RAW_DATA,
|
||||||
|
format_hint="z. B. 2 Ruhetage",
|
||||||
|
example_output="2 Ruhetage",
|
||||||
|
minimum_data_requirements="rest_days",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="Immer Zählung, 0 möglich",
|
||||||
|
missing_value_policy=MVP("no_data", "0 Ruhetage"),
|
||||||
|
known_limitations="Nur explizit erfasste Ruhetage",
|
||||||
|
layer_1_decision="recovery_metrics.get_rest_days_data",
|
||||||
|
layer_2a_decision="get_rest_days_count",
|
||||||
|
layer_2b_reuse_possible=True,
|
||||||
|
architecture_alignment="Phase 0c",
|
||||||
|
issue_53_alignment="Layer 1",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
_tag(m)
|
||||||
|
register_placeholder(m)
|
||||||
|
|
||||||
|
m = PlaceholderMetadata(
|
||||||
|
key="recovery_score",
|
||||||
|
category=CAT,
|
||||||
|
description="Recovery-Score 0–100 (v2, komposit)",
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="_safe_int",
|
||||||
|
data_layer_module="backend/data_layer/recovery_metrics.py",
|
||||||
|
data_layer_function="calculate_recovery_score_v2",
|
||||||
|
source_tables=["sleep_log", "vitals_baseline", "activity_log"],
|
||||||
|
semantic_contract="Gewichteter Score aus Schlaf, Vitaltrends, optional Load (siehe Implementierung).",
|
||||||
|
business_meaning="Gesamt-Recovery-KPI für Prompts",
|
||||||
|
unit="0–100",
|
||||||
|
time_window="composite",
|
||||||
|
output_type=OutputType.NUMERIC,
|
||||||
|
placeholder_type=PlaceholderType.SCORE,
|
||||||
|
format_hint="Ganzzahl-String",
|
||||||
|
example_output="72",
|
||||||
|
minimum_data_requirements="Teilkomponenten je nach Gewichtung",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="Wie calculate_recovery_score_v2",
|
||||||
|
missing_value_policy=MVP("insufficient_data", "nicht verfügbar"),
|
||||||
|
known_limitations="Abhängig von Datenabdeckung HF/HRV/Schlaf",
|
||||||
|
layer_1_decision="recovery_metrics.calculate_recovery_score_v2",
|
||||||
|
layer_2a_decision="_safe_int('recovery_score_v2')",
|
||||||
|
layer_2b_reuse_possible=True,
|
||||||
|
architecture_alignment="Phase 0c",
|
||||||
|
issue_53_alignment="Layer 1",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
_tag(m)
|
||||||
|
register_placeholder(m)
|
||||||
|
|
||||||
|
for key, dl_fn, desc, unit, tbls, res_fn in [
|
||||||
|
("sleep_avg_duration_7d", "calculate_sleep_avg_duration_7d", "Durchschnittliche Schlafdauer 7d (h)", "h", ["sleep_log"], "_safe_float"),
|
||||||
|
("sleep_debt_hours", "calculate_sleep_debt_hours", "Kumulative Schlafschuld (h)", "h", ["sleep_log"], "_safe_float"),
|
||||||
|
("sleep_regularity_proxy", "calculate_sleep_regularity_proxy", "Schlaf-Regularität (Proxy)", "min", ["sleep_log"], "_safe_float"),
|
||||||
|
("recent_load_balance_3d", "calculate_recent_load_balance_3d", "Load-Balance 3d (Score)", "score", ["activity_log"], "_safe_int"),
|
||||||
|
("sleep_quality_7d", "calculate_sleep_quality_7d", "Schlafqualität 7d (0–100)", "0-100", ["sleep_log"], "_safe_int"),
|
||||||
|
]:
|
||||||
|
m = PlaceholderMetadata(
|
||||||
|
key=key,
|
||||||
|
category=CAT,
|
||||||
|
description=desc,
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function=res_fn,
|
||||||
|
data_layer_module="backend/data_layer/recovery_metrics.py",
|
||||||
|
data_layer_function=dl_fn,
|
||||||
|
source_tables=tbls,
|
||||||
|
semantic_contract=f"Berechnung {dl_fn} in recovery_metrics.",
|
||||||
|
business_meaning="Erholungs-Detailmetrik",
|
||||||
|
unit=unit,
|
||||||
|
time_window="siehe Funktion",
|
||||||
|
output_type=OutputType.NUMERIC,
|
||||||
|
placeholder_type=PlaceholderType.INTERPRETED,
|
||||||
|
format_hint="numerischer String",
|
||||||
|
example_output="1.0",
|
||||||
|
minimum_data_requirements="wie Funktion",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="Funktionsintern",
|
||||||
|
missing_value_policy=MVP("insufficient_data", "nicht verfügbar"),
|
||||||
|
known_limitations=None,
|
||||||
|
layer_1_decision=f"recovery_metrics.{dl_fn}",
|
||||||
|
layer_2a_decision="Resolver _safe_float/_safe_int",
|
||||||
|
layer_2b_reuse_possible=True,
|
||||||
|
architecture_alignment="Phase 0c",
|
||||||
|
issue_53_alignment="Layer 1",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
_tag(m)
|
||||||
|
register_placeholder(m)
|
||||||
|
|
||||||
|
m = PlaceholderMetadata(
|
||||||
|
key="correlation_sleep_recovery",
|
||||||
|
category=CAT,
|
||||||
|
description="JSON: Korrelation Schlaf ↔ Recovery-Indikatoren",
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="_safe_json",
|
||||||
|
data_layer_module="backend/data_layer/correlations.py",
|
||||||
|
data_layer_function="calculate_correlation_sleep_recovery",
|
||||||
|
source_tables=["sleep_log", "vitals_baseline", "activity_log"],
|
||||||
|
semantic_contract="Strukturierte Korrelationsauswertung (siehe correlations).",
|
||||||
|
business_meaning="KI: Zusammenhänge Schlaf und Erholung",
|
||||||
|
unit="JSON",
|
||||||
|
time_window="funktionsabhängig",
|
||||||
|
output_type=OutputType.JSON,
|
||||||
|
placeholder_type=PlaceholderType.RAW_DATA,
|
||||||
|
format_hint="JSON-String",
|
||||||
|
example_output="{}",
|
||||||
|
minimum_data_requirements="Ausreichend gekoppelte Datenpunkte",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="Wie correlation_metrics",
|
||||||
|
missing_value_policy=MVP("insufficient_data", "{}"),
|
||||||
|
known_limitations="Bei wenig Daten leer oder schwach",
|
||||||
|
layer_1_decision="correlations.calculate_correlation_sleep_recovery",
|
||||||
|
layer_2a_decision="_safe_json",
|
||||||
|
layer_2b_reuse_possible=True,
|
||||||
|
architecture_alignment="Phase 0c",
|
||||||
|
issue_53_alignment="Layer 1",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
_tag(m)
|
||||||
|
register_placeholder(m)
|
||||||
|
|
||||||
|
|
||||||
|
register_schlaf_erholung()
|
||||||
@@ -0,0 +1,180 @@
|
|||||||
|
"""
|
||||||
|
Registry: Baseline-Vitals (Ruhepuls, HRV, VO2 Max) und Abweichung vs. persönlicher Baseline.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from placeholder_registry import (
|
||||||
|
PlaceholderMetadata,
|
||||||
|
MissingValuePolicy,
|
||||||
|
EvidenceType,
|
||||||
|
OutputType,
|
||||||
|
PlaceholderType,
|
||||||
|
register_placeholder,
|
||||||
|
)
|
||||||
|
|
||||||
|
CAT = "Vitalwerte"
|
||||||
|
MVP = lambda reason, disp: MissingValuePolicy(
|
||||||
|
available=False, value_raw=None, missing_reason=reason, legacy_display=disp
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _tag(m: PlaceholderMetadata):
|
||||||
|
for f in (
|
||||||
|
"key", "category", "description", "resolver_module", "resolver_function",
|
||||||
|
"data_layer_module", "data_layer_function", "source_tables", "semantic_contract",
|
||||||
|
"unit", "time_window", "output_type", "placeholder_type", "format_hint",
|
||||||
|
"example_output", "minimum_data_requirements", "confidence_logic",
|
||||||
|
"missing_value_policy", "layer_1_decision", "layer_2a_decision",
|
||||||
|
"layer_2b_reuse_possible", "architecture_alignment", "issue_53_alignment",
|
||||||
|
):
|
||||||
|
m.set_evidence(f, EvidenceType.CODE_DERIVED)
|
||||||
|
m.set_evidence("business_meaning", EvidenceType.DRAFT_DERIVED)
|
||||||
|
m.set_evidence("known_limitations", EvidenceType.MIXED)
|
||||||
|
|
||||||
|
|
||||||
|
def register_vitalwerte():
|
||||||
|
m = PlaceholderMetadata(
|
||||||
|
key="vitals_avg_hr",
|
||||||
|
category=CAT,
|
||||||
|
description="Durchschnittlicher Ruhepuls (7d), formatiert",
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="get_vitals_avg_hr",
|
||||||
|
data_layer_module="backend/data_layer/health_metrics.py",
|
||||||
|
data_layer_function="get_resting_heart_rate_data",
|
||||||
|
source_tables=["vitals_baseline"],
|
||||||
|
semantic_contract="Mittel RHR aus vitals_baseline im Fenster (siehe health_metrics).",
|
||||||
|
business_meaning="KI-Kontext kardiovaskuläre Ruhelage",
|
||||||
|
unit="bpm (Anzeige mit Einheit)",
|
||||||
|
time_window="7d default im Resolver",
|
||||||
|
output_type=OutputType.STRING,
|
||||||
|
placeholder_type=PlaceholderType.INTERPRETED,
|
||||||
|
format_hint="z. B. 58 bpm",
|
||||||
|
example_output="58 bpm",
|
||||||
|
minimum_data_requirements="vitals_baseline im Fenster",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="data['confidence'] im Layer1",
|
||||||
|
missing_value_policy=MVP("no_data", "nicht verfügbar"),
|
||||||
|
known_limitations="Nur erfasste Morgen-Baseline-Messungen",
|
||||||
|
layer_1_decision="health_metrics.get_resting_heart_rate_data",
|
||||||
|
layer_2a_decision="get_vitals_avg_hr",
|
||||||
|
layer_2b_reuse_possible=True,
|
||||||
|
architecture_alignment="Phase 0c",
|
||||||
|
issue_53_alignment="Layer 1",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
_tag(m)
|
||||||
|
register_placeholder(m)
|
||||||
|
|
||||||
|
m = PlaceholderMetadata(
|
||||||
|
key="vitals_avg_hrv",
|
||||||
|
category=CAT,
|
||||||
|
description="Durchschnittliche HRV (7d), formatiert",
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="get_vitals_avg_hrv",
|
||||||
|
data_layer_module="backend/data_layer/health_metrics.py",
|
||||||
|
data_layer_function="get_heart_rate_variability_data",
|
||||||
|
source_tables=["vitals_baseline"],
|
||||||
|
semantic_contract="Mittel HRV aus vitals_baseline im Fenster.",
|
||||||
|
business_meaning="KI-Kontext autonome Regulation / Erholung",
|
||||||
|
unit="ms (Anzeige mit Einheit)",
|
||||||
|
time_window="7d default",
|
||||||
|
output_type=OutputType.STRING,
|
||||||
|
placeholder_type=PlaceholderType.INTERPRETED,
|
||||||
|
format_hint="z. B. 45 ms",
|
||||||
|
example_output="45 ms",
|
||||||
|
minimum_data_requirements="vitals_baseline mit HRV",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="data['confidence'] im Layer1",
|
||||||
|
missing_value_policy=MVP("no_data", "nicht verfügbar"),
|
||||||
|
known_limitations="Geräte-/Messprotokoll kann streuen",
|
||||||
|
layer_1_decision="health_metrics.get_heart_rate_variability_data",
|
||||||
|
layer_2a_decision="get_vitals_avg_hrv",
|
||||||
|
layer_2b_reuse_possible=True,
|
||||||
|
architecture_alignment="Phase 0c",
|
||||||
|
issue_53_alignment="Layer 1",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
_tag(m)
|
||||||
|
register_placeholder(m)
|
||||||
|
|
||||||
|
m = PlaceholderMetadata(
|
||||||
|
key="vitals_vo2_max",
|
||||||
|
category=CAT,
|
||||||
|
description="Aktueller VO2 Max (letzte Messung), formatiert",
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function="get_vitals_vo2_max",
|
||||||
|
data_layer_module="backend/data_layer/health_metrics.py",
|
||||||
|
data_layer_function="get_vo2_max_data",
|
||||||
|
source_tables=["vitals_baseline"],
|
||||||
|
semantic_contract="Jüngster vo2_max aus vitals_baseline.",
|
||||||
|
business_meaning="Ausdauer-/Fitness-Kontext",
|
||||||
|
unit="ml/kg/min",
|
||||||
|
time_window="latest",
|
||||||
|
output_type=OutputType.STRING,
|
||||||
|
placeholder_type=PlaceholderType.INTERPRETED,
|
||||||
|
format_hint="eine Dezimalstelle + Einheit",
|
||||||
|
example_output="42.0 ml/kg/min",
|
||||||
|
minimum_data_requirements="mindestens eine VO2-Messung",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="data['confidence'] im Layer1",
|
||||||
|
missing_value_policy=MVP("no_data", "nicht verfügbar"),
|
||||||
|
known_limitations="Schätzung vs. Labortest je nach Quelle",
|
||||||
|
layer_1_decision="health_metrics.get_vo2_max_data",
|
||||||
|
layer_2a_decision="get_vitals_vo2_max",
|
||||||
|
layer_2b_reuse_possible=True,
|
||||||
|
architecture_alignment="Phase 0c",
|
||||||
|
issue_53_alignment="Layer 1",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
_tag(m)
|
||||||
|
register_placeholder(m)
|
||||||
|
|
||||||
|
for key, dl_fn, desc, unit, res_fn in [
|
||||||
|
(
|
||||||
|
"hrv_vs_baseline_pct",
|
||||||
|
"calculate_hrv_vs_baseline_pct",
|
||||||
|
"HRV vs. persönlicher Baseline (%)",
|
||||||
|
"%",
|
||||||
|
"_safe_float",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"rhr_vs_baseline_pct",
|
||||||
|
"calculate_rhr_vs_baseline_pct",
|
||||||
|
"Ruhepuls vs. persönlicher Baseline (%)",
|
||||||
|
"%",
|
||||||
|
"_safe_float",
|
||||||
|
),
|
||||||
|
]:
|
||||||
|
m = PlaceholderMetadata(
|
||||||
|
key=key,
|
||||||
|
category=CAT,
|
||||||
|
description=desc,
|
||||||
|
resolver_module="backend/placeholder_resolver.py",
|
||||||
|
resolver_function=res_fn,
|
||||||
|
data_layer_module="backend/data_layer/recovery_metrics.py",
|
||||||
|
data_layer_function=dl_fn,
|
||||||
|
source_tables=["vitals_baseline"],
|
||||||
|
semantic_contract=f"Vergleich aktueller Wert zu Baseline (siehe {dl_fn}).",
|
||||||
|
business_meaning="Erholungs- und Belastungsindikator relativ zur Norm des Nutzers",
|
||||||
|
unit=unit,
|
||||||
|
time_window="funktionsintern",
|
||||||
|
output_type=OutputType.NUMERIC,
|
||||||
|
placeholder_type=PlaceholderType.INTERPRETED,
|
||||||
|
format_hint="numerischer Prozent-String",
|
||||||
|
example_output="5.2",
|
||||||
|
minimum_data_requirements="Ausreichend Baseline-Historie",
|
||||||
|
quality_filter_policy=None,
|
||||||
|
confidence_logic="Funktionsintern",
|
||||||
|
missing_value_policy=MVP("insufficient_data", "nicht verfügbar"),
|
||||||
|
known_limitations="Baseline braucht ausreichend Vorlauf",
|
||||||
|
layer_1_decision=f"recovery_metrics.{dl_fn}",
|
||||||
|
layer_2a_decision=f"Resolver {res_fn}",
|
||||||
|
layer_2b_reuse_possible=True,
|
||||||
|
architecture_alignment="Phase 0c",
|
||||||
|
issue_53_alignment="Layer 1",
|
||||||
|
evidence={},
|
||||||
|
)
|
||||||
|
_tag(m)
|
||||||
|
register_placeholder(m)
|
||||||
|
|
||||||
|
|
||||||
|
register_vitalwerte()
|
||||||
@@ -258,6 +258,42 @@ class PlaceholderRegistry:
|
|||||||
return metadata._resolver_func(profile_id)
|
return metadata._resolver_func(profile_id)
|
||||||
|
|
||||||
|
|
||||||
|
def build_ai_placeholder_caption(metadata: PlaceholderMetadata, max_len: int = 400) -> str:
|
||||||
|
"""
|
||||||
|
Kurzerklärung / Einordnung für {{key|x}} und Exportfeld ``ai_caption`` (ohne Wert, ohne Einheit).
|
||||||
|
|
||||||
|
Inhalt: business_meaning oder gekürzter semantic_contract; bei SCORE-Zeilen die 0–100-Skala.
|
||||||
|
Nicht enthalten: description (die nur bei {{key|d}} angehängt wird) und keine „Technischer Bezug: …“-Zeile.
|
||||||
|
"""
|
||||||
|
desc = (metadata.description or "").strip()
|
||||||
|
bm = (metadata.business_meaning or "").strip()
|
||||||
|
sc = (metadata.semantic_contract or "").strip()
|
||||||
|
|
||||||
|
chunks: List[str] = []
|
||||||
|
|
||||||
|
interpret = bm
|
||||||
|
if not interpret and sc:
|
||||||
|
interpret = sc if len(sc) <= max_len else sc[: max_len - 1] + "…"
|
||||||
|
|
||||||
|
if interpret:
|
||||||
|
il = interpret.lower()
|
||||||
|
redundant = bool(
|
||||||
|
desc
|
||||||
|
and len(desc) >= 10
|
||||||
|
and desc.lower() in il
|
||||||
|
)
|
||||||
|
if not redundant:
|
||||||
|
chunks.append(interpret)
|
||||||
|
|
||||||
|
if metadata.placeholder_type == PlaceholderType.SCORE:
|
||||||
|
chunks.append("Skala 0–100: höher = im Modell günstiger / besser abgestimmt.")
|
||||||
|
|
||||||
|
out = " ".join(c for c in chunks if c).strip()
|
||||||
|
if len(out) > max_len + 120:
|
||||||
|
out = out[: max_len + 60] + "…"
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
# Global registry instance
|
# Global registry instance
|
||||||
_global_registry = PlaceholderRegistry()
|
_global_registry = PlaceholderRegistry()
|
||||||
|
|
||||||
|
|||||||
+667
-198
File diff suppressed because it is too large.
Load diff
+78
-44
@@ -12,14 +12,17 @@ import re
|
|||||||
from typing import Dict, Any, Optional
|
from typing import Dict, Any, Optional
|
||||||
from db import get_db, get_cursor, r2d
|
from db import get_db, get_cursor, r2d
|
||||||
from fastapi import HTTPException
|
from fastapi import HTTPException
|
||||||
|
from placeholder_resolver import get_catalog_row_for_key
|
||||||
|
|
||||||
|
|
||||||
def resolve_placeholders(template: str, variables: Dict[str, Any], debug_info: Optional[Dict] = None, catalog: Optional[Dict] = None) -> str:
|
def resolve_placeholders(template: str, variables: Dict[str, Any], debug_info: Optional[Dict] = None, catalog: Optional[Dict] = None) -> str:
|
||||||
"""
|
"""
|
||||||
Replace {{placeholder}} with values from variables dict.
|
Replace {{placeholder}} with values from variables dict.
|
||||||
|
|
||||||
Supports modifiers:
|
Modifiers (Katalog aus get_placeholder_catalog empfohlen):
|
||||||
- {{key|d}} - Include description in parentheses (requires catalog)
|
- {{key|d}} — Wert — description (kurz)
|
||||||
|
- {{key|x}} — nur Erklärung (Katalogfeld ai_caption), ohne Zahlenwert
|
||||||
|
- {{key|d,x}} — Wert — description — Erklärung
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
template: String with {{key}} or {{key|modifiers}} placeholders
|
template: String with {{key}} or {{key|modifiers}} placeholders
|
||||||
@@ -40,46 +43,66 @@ def resolve_placeholders(template: str, variables: Dict[str, Any], debug_info: O
|
|||||||
parts = full_placeholder.split('|')
|
parts = full_placeholder.split('|')
|
||||||
key = parts[0].strip()
|
key = parts[0].strip()
|
||||||
modifiers = parts[1].strip() if len(parts) > 1 else ''
|
modifiers = parts[1].strip() if len(parts) > 1 else ''
|
||||||
|
mods = {x.strip().lower() for x in modifiers.split(",") if x.strip()}
|
||||||
|
want_d = "d" in mods
|
||||||
|
want_x = "x" in mods
|
||||||
|
|
||||||
if key in variables:
|
def _warn(msg: str):
|
||||||
value = variables[key]
|
|
||||||
# Convert dict/list to JSON string
|
|
||||||
if isinstance(value, (dict, list)):
|
|
||||||
resolved_value = json.dumps(value, ensure_ascii=False)
|
|
||||||
else:
|
|
||||||
resolved_value = str(value)
|
|
||||||
|
|
||||||
# Apply modifiers
|
|
||||||
if 'd' in modifiers:
|
|
||||||
if catalog:
|
|
||||||
# Add description from catalog
|
|
||||||
description = None
|
|
||||||
for cat_items in catalog.values():
|
|
||||||
matching = [item for item in cat_items if item['key'] == key]
|
|
||||||
if matching:
|
|
||||||
description = matching[0].get('description', '')
|
|
||||||
break
|
|
||||||
|
|
||||||
if description:
|
|
||||||
resolved_value = f"{resolved_value} ({description})"
|
|
||||||
else:
|
|
||||||
# Catalog not available - log warning in debug
|
|
||||||
if debug_info is not None:
|
|
||||||
if 'warnings' not in debug_info:
|
|
||||||
debug_info['warnings'] = []
|
|
||||||
debug_info['warnings'].append(f"Modifier |d used but catalog not available for {key}")
|
|
||||||
|
|
||||||
# Track resolution for debug
|
|
||||||
if debug_info is not None:
|
if debug_info is not None:
|
||||||
resolved[key] = resolved_value[:100] + ('...' if len(resolved_value) > 100 else '')
|
debug_info.setdefault("warnings", []).append(msg)
|
||||||
|
|
||||||
return resolved_value
|
row = get_catalog_row_for_key(catalog, key) if catalog else None
|
||||||
else:
|
|
||||||
# Keep placeholder if no value found
|
if want_x and not want_d:
|
||||||
|
if key not in variables:
|
||||||
|
if debug_info is not None:
|
||||||
|
unresolved.append(key)
|
||||||
|
return match.group(0)
|
||||||
|
expl = (row.get("ai_caption") or "").strip() if row else ""
|
||||||
|
if not expl and catalog is None:
|
||||||
|
_warn(f"Modifier |x für {key}: Katalog fehlt (ai_caption).")
|
||||||
|
out = expl
|
||||||
|
if debug_info is not None:
|
||||||
|
resolved[key] = out[:100] + ("..." if len(out) > 100 else "")
|
||||||
|
return out
|
||||||
|
|
||||||
|
if key not in variables:
|
||||||
if debug_info is not None:
|
if debug_info is not None:
|
||||||
unresolved.append(key)
|
unresolved.append(key)
|
||||||
return match.group(0)
|
return match.group(0)
|
||||||
|
|
||||||
|
value = variables[key]
|
||||||
|
if isinstance(value, (dict, list)):
|
||||||
|
resolved_value = json.dumps(value, ensure_ascii=False)
|
||||||
|
else:
|
||||||
|
resolved_value = str(value)
|
||||||
|
|
||||||
|
if not want_d and not want_x:
|
||||||
|
out = resolved_value
|
||||||
|
if debug_info is not None:
|
||||||
|
resolved[key] = out[:100] + ("..." if len(out) > 100 else "")
|
||||||
|
return out
|
||||||
|
|
||||||
|
parts = [resolved_value]
|
||||||
|
if want_d:
|
||||||
|
if row:
|
||||||
|
desc = (row.get("description") or "").strip()
|
||||||
|
if desc:
|
||||||
|
parts.append(desc)
|
||||||
|
else:
|
||||||
|
_warn(f"Modifier |d für {key}: Katalog fehlt (description).")
|
||||||
|
if want_x:
|
||||||
|
expl = (row.get("ai_caption") or "").strip() if row else ""
|
||||||
|
if expl:
|
||||||
|
parts.append(expl)
|
||||||
|
elif catalog is not None:
|
||||||
|
_warn(f"Modifier |x (mit |d) für {key}: ai_caption leer.")
|
||||||
|
|
||||||
|
out = " — ".join(parts)
|
||||||
|
if debug_info is not None:
|
||||||
|
resolved[key] = out[:100] + ("..." if len(out) > 100 else "")
|
||||||
|
return out
|
||||||
|
|
||||||
result = re.sub(r'\{\{([^}]+)\}\}', replacer, template)
|
result = re.sub(r'\{\{([^}]+)\}\}', replacer, template)
|
||||||
|
|
||||||
# Store debug info
|
# Store debug info
|
||||||
@@ -144,7 +167,8 @@ async def execute_prompt(
|
|||||||
prompt_slug: str,
|
prompt_slug: str,
|
||||||
variables: Dict[str, Any],
|
variables: Dict[str, Any],
|
||||||
openrouter_call_func,
|
openrouter_call_func,
|
||||||
enable_debug: bool = False
|
enable_debug: bool = False,
|
||||||
|
progress_callback = None # NEW: Optional callback für SSE Progress-Updates
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
Execute a single prompt (base or pipeline type).
|
Execute a single prompt (base or pipeline type).
|
||||||
@@ -194,7 +218,7 @@ async def execute_prompt(
|
|||||||
|
|
||||||
elif prompt_type == 'workflow':
|
elif prompt_type == 'workflow':
|
||||||
# Workflow prompt: graph-based execution (Phase 0: Foundation)
|
# Workflow prompt: graph-based execution (Phase 0: Foundation)
|
||||||
return await execute_workflow_prompt(prompt, variables, openrouter_call_func, enable_debug, catalog)
|
return await execute_workflow_prompt(prompt, variables, openrouter_call_func, enable_debug, catalog, progress_callback)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise HTTPException(400, f"Unknown prompt type: {prompt_type}")
|
raise HTTPException(400, f"Unknown prompt type: {prompt_type}")
|
||||||
@@ -255,7 +279,11 @@ async def execute_base_prompt(
|
|||||||
|
|
||||||
if enable_debug:
|
if enable_debug:
|
||||||
debug_info['template'] = template
|
debug_info['template'] = template
|
||||||
debug_info['final_prompt'] = prompt_text[:500] + ('...' if len(prompt_text) > 500 else '')
|
# Volltext für Test-UI (Admin); sehr große Prompts nur weich begrenzen
|
||||||
|
_max = 512 * 1024
|
||||||
|
debug_info['final_prompt'] = (
|
||||||
|
prompt_text if len(prompt_text) <= _max else prompt_text[:_max] + "\n… [gekürzt, >512KB]"
|
||||||
|
)
|
||||||
debug_info['available_variables'] = list(variables.keys())
|
debug_info['available_variables'] = list(variables.keys())
|
||||||
|
|
||||||
# Call AI
|
# Call AI
|
||||||
@@ -374,7 +402,10 @@ async def execute_pipeline_prompt(
|
|||||||
if enable_debug:
|
if enable_debug:
|
||||||
prompt_debug['source'] = 'inline'
|
prompt_debug['source'] = 'inline'
|
||||||
prompt_debug['template'] = template
|
prompt_debug['template'] = template
|
||||||
prompt_debug['final_prompt'] = prompt_text[:500] + ('...' if len(prompt_text) > 500 else '')
|
_max = 512 * 1024
|
||||||
|
prompt_debug['final_prompt'] = (
|
||||||
|
prompt_text if len(prompt_text) <= _max else prompt_text[:_max] + "\n… [gekürzt, >512KB]"
|
||||||
|
)
|
||||||
prompt_debug.update(placeholder_debug)
|
prompt_debug.update(placeholder_debug)
|
||||||
|
|
||||||
response = await openrouter_call_func(prompt_text)
|
response = await openrouter_call_func(prompt_text)
|
||||||
@@ -439,7 +470,8 @@ async def execute_prompt_with_data(
|
|||||||
modules: Optional[Dict[str, bool]] = None,
|
modules: Optional[Dict[str, bool]] = None,
|
||||||
timeframes: Optional[Dict[str, int]] = None,
|
timeframes: Optional[Dict[str, int]] = None,
|
||||||
openrouter_call_func = None,
|
openrouter_call_func = None,
|
||||||
enable_debug: bool = False
|
enable_debug: bool = False,
|
||||||
|
progress_callback = None # NEW: Optional callback für SSE Progress-Updates
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
Execute prompt with data loaded from database.
|
Execute prompt with data loaded from database.
|
||||||
@@ -464,7 +496,7 @@ async def execute_prompt_with_data(
|
|||||||
'today': datetime.now().strftime('%Y-%m-%d')
|
'today': datetime.now().strftime('%Y-%m-%d')
|
||||||
}
|
}
|
||||||
|
|
||||||
# Load placeholder catalog for |d modifier support
|
# Load placeholder catalog for |d / |x Modifier
|
||||||
try:
|
try:
|
||||||
catalog = get_placeholder_catalog(profile_id)
|
catalog = get_placeholder_catalog(profile_id)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -575,7 +607,7 @@ async def execute_prompt_with_data(
|
|||||||
variables['goals_data'] = []
|
variables['goals_data'] = []
|
||||||
|
|
||||||
# Execute prompt
|
# Execute prompt
|
||||||
return await execute_prompt(prompt_slug, variables, openrouter_call_func, enable_debug)
|
return await execute_prompt(prompt_slug, variables, openrouter_call_func, enable_debug, progress_callback)
|
||||||
|
|
||||||
|
|
||||||
async def execute_workflow_prompt(
|
async def execute_workflow_prompt(
|
||||||
@@ -583,7 +615,8 @@ async def execute_workflow_prompt(
|
|||||||
variables: Dict[str, Any],
|
variables: Dict[str, Any],
|
||||||
openrouter_call_func,
|
openrouter_call_func,
|
||||||
enable_debug: bool = False,
|
enable_debug: bool = False,
|
||||||
catalog: Optional[Dict] = None
|
catalog: Optional[Dict] = None,
|
||||||
|
progress_callback = None # NEW: Optional callback für SSE Progress-Updates
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
Execute a workflow-type prompt (graph-based execution).
|
Execute a workflow-type prompt (graph-based execution).
|
||||||
@@ -622,7 +655,8 @@ async def execute_workflow_prompt(
|
|||||||
profile_id=variables.get('profile_id', 'unknown'), # From context
|
profile_id=variables.get('profile_id', 'unknown'), # From context
|
||||||
variables=variables,
|
variables=variables,
|
||||||
openrouter_call_func=openrouter_call_func,
|
openrouter_call_func=openrouter_call_func,
|
||||||
enable_debug=enable_debug
|
enable_debug=enable_debug,
|
||||||
|
progress_callback=progress_callback # NEW: Progress-Callbacks durchreichen
|
||||||
)
|
)
|
||||||
|
|
||||||
# Convert ExecutionResult to dict for API response
|
# Convert ExecutionResult to dict for API response
|
||||||
|
|||||||
@@ -158,7 +158,8 @@ def parse_decision_questions(section_text: str) -> Dict[str, str]:
|
|||||||
for pattern in patterns:
|
for pattern in patterns:
|
||||||
matches = re.finditer(pattern, section_text, re.MULTILINE | re.IGNORECASE)
|
matches = re.finditer(pattern, section_text, re.MULTILINE | re.IGNORECASE)
|
||||||
for match in matches:
|
for match in matches:
|
||||||
question_type = match.group(1).strip().lower()
|
# Preserve original case for question IDs (e.g., "qAnalyst" not "qanalyst")
|
||||||
|
question_type = match.group(1).strip()
|
||||||
answer = match.group(2).strip()
|
answer = match.group(2).strip()
|
||||||
|
|
||||||
# Entferne Klammern und Whitespace
|
# Entferne Klammern und Whitespace
|
||||||
|
|||||||
+448
-208
@@ -7,16 +7,76 @@ import csv
|
|||||||
import io
|
import io
|
||||||
import uuid
|
import uuid
|
||||||
import logging
|
import logging
|
||||||
|
import re
|
||||||
|
import calendar
|
||||||
|
from datetime import date
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from fastapi import APIRouter, HTTPException, UploadFile, File, Header, Depends, Query
|
from fastapi import APIRouter, HTTPException, UploadFile, File, Header, Depends, Query
|
||||||
|
|
||||||
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
|
||||||
from models import ActivityEntry
|
from models import ActivityEntry, ActivityMetricsReplace
|
||||||
from routers.profiles import get_pid
|
from routers.profiles import get_pid
|
||||||
from feature_logger import log_feature_usage
|
from feature_logger import log_feature_usage
|
||||||
from quality_filter import get_quality_filter_sql
|
from quality_filter import get_quality_filter_sql
|
||||||
|
from data_layer.activity_persistence_orchestrator import (
|
||||||
|
get_mappable_activity_field_catalog,
|
||||||
|
insert_activity_from_entry,
|
||||||
|
run_activity_post_write_hooks,
|
||||||
|
update_activity_from_entry,
|
||||||
|
find_activity_duplicate_id,
|
||||||
|
update_activity_columns,
|
||||||
|
insert_activity_csv_minimal,
|
||||||
|
run_activity_post_write_hooks_import,
|
||||||
|
new_activity_id,
|
||||||
|
)
|
||||||
|
from data_layer.activity_time_normalize import normalize_activity_start
|
||||||
|
from data_layer.activity_session_metrics import enrich_sessions_with_metrics
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/api/activity", tags=["activity"])
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
_MONTH_RE = re.compile(r"^(\d{4})-(\d{2})$")
|
||||||
|
|
||||||
|
|
||||||
|
def _month_date_bounds(ym: str) -> tuple[date, date]:
|
||||||
|
m = _MONTH_RE.match((ym or "").strip())
|
||||||
|
if not m:
|
||||||
|
raise HTTPException(status_code=400, detail="month muss YYYY-MM sein")
|
||||||
|
y, mo = int(m.group(1)), int(m.group(2))
|
||||||
|
if mo < 1 or mo > 12:
|
||||||
|
raise HTTPException(status_code=400, detail="Ungültiger Monat")
|
||||||
|
last = calendar.monthrange(y, mo)[1]
|
||||||
|
return date(y, mo, 1), date(y, mo, last)
|
||||||
|
|
||||||
|
|
||||||
|
_ACTIVITY_DEDUP_WINDOW = """
|
||||||
|
PARTITION BY al.profile_id, al.date,
|
||||||
|
COALESCE(al.activity_type, ''),
|
||||||
|
COALESCE(al.start_time::text, ''),
|
||||||
|
COALESCE(ROUND(al.duration_min::numeric, 1), '-999999'::numeric),
|
||||||
|
COALESCE(ROUND(al.kcal_active::numeric, 1), '-999999'::numeric)
|
||||||
|
ORDER BY al.created DESC NULLS LAST, al.id DESC
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def _activity_rows_after_list_query(cur):
|
||||||
|
rows = []
|
||||||
|
for r in cur.fetchall():
|
||||||
|
d = r2d(r)
|
||||||
|
if not d:
|
||||||
|
continue
|
||||||
|
d.pop("_dup_rn", None)
|
||||||
|
rows.append(d)
|
||||||
|
return rows
|
||||||
|
|
||||||
|
|
||||||
|
def _return_activity_list_rows(cur, rows: list) -> list:
|
||||||
|
"""Layer-1: gemergte session_metrics wie Detail-Pfad (Batch)."""
|
||||||
|
enrich_sessions_with_metrics(cur, rows)
|
||||||
|
return rows
|
||||||
|
|
||||||
|
|
||||||
# Evaluation import with error handling (Phase 1.2)
|
# Evaluation import with error handling (Phase 1.2)
|
||||||
try:
|
try:
|
||||||
@@ -27,51 +87,143 @@ except Exception as e:
|
|||||||
EVALUATION_AVAILABLE = False
|
EVALUATION_AVAILABLE = False
|
||||||
evaluate_and_save_activity = None
|
evaluate_and_save_activity = None
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/activity", tags=["activity"])
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("")
|
@router.get("")
|
||||||
def list_activity(
|
def list_activity(
|
||||||
limit: int = Query(200, ge=1, le=50_000),
|
limit: int = Query(200, ge=1, le=50_000),
|
||||||
|
offset: int = Query(0, ge=0, le=100_000, description="SQL OFFSET für Pagination"),
|
||||||
days: Optional[int] = Query(None, ge=1, le=4000, description="Nur Einträge mit date >= HEUTE − days (Kalendertage)"),
|
days: Optional[int] = Query(None, ge=1, le=4000, description="Nur Einträge mit date >= HEUTE − days (Kalendertage)"),
|
||||||
x_profile_id: Optional[str] = Header(default=None),
|
month: Optional[str] = Query(
|
||||||
|
None,
|
||||||
|
description='Kalendermonat "YYYY-MM" (ganzer Monat; schließt days und offset aus)',
|
||||||
|
),
|
||||||
|
skip_quality_filter: bool = Query(
|
||||||
|
False,
|
||||||
|
description="True = alle Einträge des Profils (ohne quality_label-Filter). Für /activity Erfassung.",
|
||||||
|
),
|
||||||
|
collapse_duplicate_sessions: bool = Query(
|
||||||
|
False,
|
||||||
|
description="True = Sessions mit gleichem Datum/Typ/Startzeit/Dauer/Kcal falten (neueste Zeile behalten).",
|
||||||
|
),
|
||||||
session: dict = Depends(require_auth),
|
session: dict = Depends(require_auth),
|
||||||
):
|
):
|
||||||
"""Get activity entries for current profile. Optional *days* filter by calendar window (not the same as *limit*)."""
|
"""Get activity entries for current profile. Optional *days* filter by calendar window (not the same as *limit*)."""
|
||||||
pid = get_pid(x_profile_id)
|
# Immer das Profil der gültigen Session (X-Profile-Id wird hier nicht verwendet).
|
||||||
|
pid = str(session["profile_id"])
|
||||||
with get_db() as conn:
|
with get_db() as conn:
|
||||||
cur = get_cursor(conn)
|
cur = get_cursor(conn)
|
||||||
|
|
||||||
# Issue #31: Apply global quality filter (profile from DB = saved level)
|
# Issue #31: Qualitätsfilter — auf der Erfassungsseite /activity abschaltbar (skip_quality_filter)
|
||||||
cur.execute("SELECT * FROM profiles WHERE id=%s", (pid,))
|
if skip_quality_filter:
|
||||||
profile = r2d(cur.fetchone())
|
quality_filter = ""
|
||||||
quality_filter = get_quality_filter_sql(profile)
|
quality_filter_al = ""
|
||||||
|
else:
|
||||||
|
cur.execute("SELECT * FROM profiles WHERE id=%s", (pid,))
|
||||||
|
profile = r2d(cur.fetchone())
|
||||||
|
quality_filter = get_quality_filter_sql(profile or {}, "")
|
||||||
|
quality_filter_al = get_quality_filter_sql(profile or {}, "al.")
|
||||||
|
|
||||||
|
if month:
|
||||||
|
if days is not None:
|
||||||
|
raise HTTPException(status_code=400, detail="month und days schließen sich aus")
|
||||||
|
if offset != 0:
|
||||||
|
raise HTTPException(status_code=400, detail="month und offset schließen sich aus")
|
||||||
|
d0, d1 = _month_date_bounds(month)
|
||||||
|
if collapse_duplicate_sessions:
|
||||||
|
cur.execute(
|
||||||
|
f"""
|
||||||
|
SELECT d.* FROM (
|
||||||
|
SELECT al.*, ROW_NUMBER() OVER (
|
||||||
|
{_ACTIVITY_DEDUP_WINDOW}
|
||||||
|
) AS _dup_rn
|
||||||
|
FROM activity_log al
|
||||||
|
WHERE al.profile_id = %s
|
||||||
|
{quality_filter_al}
|
||||||
|
AND al.date >= %s AND al.date <= %s
|
||||||
|
) d
|
||||||
|
WHERE d._dup_rn = 1
|
||||||
|
ORDER BY d.date DESC, d.start_time DESC NULLS LAST, d.id DESC
|
||||||
|
LIMIT %s
|
||||||
|
""",
|
||||||
|
(pid, d0, d1, limit),
|
||||||
|
)
|
||||||
|
return _return_activity_list_rows(cur, _activity_rows_after_list_query(cur))
|
||||||
|
cur.execute(
|
||||||
|
f"""
|
||||||
|
SELECT * FROM activity_log
|
||||||
|
WHERE profile_id=%s
|
||||||
|
{quality_filter}
|
||||||
|
AND date >= %s AND date <= %s
|
||||||
|
ORDER BY date DESC, start_time DESC NULLS LAST, id DESC
|
||||||
|
LIMIT %s
|
||||||
|
""",
|
||||||
|
(pid, d0, d1, limit),
|
||||||
|
)
|
||||||
|
return _return_activity_list_rows(
|
||||||
|
cur, [r2d(r) for r in cur.fetchall()]
|
||||||
|
)
|
||||||
|
|
||||||
if days is not None:
|
if days is not None:
|
||||||
|
if collapse_duplicate_sessions:
|
||||||
|
cur.execute(
|
||||||
|
f"""
|
||||||
|
SELECT d.* FROM (
|
||||||
|
SELECT al.*, ROW_NUMBER() OVER (
|
||||||
|
{_ACTIVITY_DEDUP_WINDOW}
|
||||||
|
) AS _dup_rn
|
||||||
|
FROM activity_log al
|
||||||
|
WHERE al.profile_id = %s
|
||||||
|
{quality_filter_al}
|
||||||
|
AND al.date >= (CURRENT_DATE - %s::integer)
|
||||||
|
) d
|
||||||
|
WHERE d._dup_rn = 1
|
||||||
|
ORDER BY d.date DESC, d.start_time DESC NULLS LAST, d.id DESC
|
||||||
|
LIMIT %s OFFSET %s
|
||||||
|
""",
|
||||||
|
(pid, days, limit, offset),
|
||||||
|
)
|
||||||
|
return _return_activity_list_rows(cur, _activity_rows_after_list_query(cur))
|
||||||
cur.execute(
|
cur.execute(
|
||||||
f"""
|
f"""
|
||||||
SELECT * FROM activity_log
|
SELECT * FROM activity_log
|
||||||
WHERE profile_id=%s
|
WHERE profile_id=%s
|
||||||
{quality_filter}
|
{quality_filter}
|
||||||
AND date >= (CURRENT_DATE - %s::integer)
|
AND date >= (CURRENT_DATE - %s::integer)
|
||||||
ORDER BY date DESC, start_time DESC
|
ORDER BY date DESC, start_time DESC NULLS LAST, id DESC
|
||||||
LIMIT %s
|
LIMIT %s OFFSET %s
|
||||||
""",
|
""",
|
||||||
(pid, days, limit),
|
(pid, days, limit, offset),
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
if collapse_duplicate_sessions:
|
||||||
|
cur.execute(
|
||||||
|
f"""
|
||||||
|
SELECT d.* FROM (
|
||||||
|
SELECT al.*, ROW_NUMBER() OVER (
|
||||||
|
{_ACTIVITY_DEDUP_WINDOW}
|
||||||
|
) AS _dup_rn
|
||||||
|
FROM activity_log al
|
||||||
|
WHERE al.profile_id = %s
|
||||||
|
{quality_filter_al}
|
||||||
|
) d
|
||||||
|
WHERE d._dup_rn = 1
|
||||||
|
ORDER BY d.date DESC, d.start_time DESC NULLS LAST, d.id DESC
|
||||||
|
LIMIT %s OFFSET %s
|
||||||
|
""",
|
||||||
|
(pid, limit, offset),
|
||||||
|
)
|
||||||
|
return _return_activity_list_rows(cur, _activity_rows_after_list_query(cur))
|
||||||
cur.execute(
|
cur.execute(
|
||||||
f"""
|
f"""
|
||||||
SELECT * FROM activity_log
|
SELECT * FROM activity_log
|
||||||
WHERE profile_id=%s
|
WHERE profile_id=%s
|
||||||
{quality_filter}
|
{quality_filter}
|
||||||
ORDER BY date DESC, start_time DESC
|
ORDER BY date DESC, start_time DESC NULLS LAST, id DESC
|
||||||
LIMIT %s
|
LIMIT %s OFFSET %s
|
||||||
""",
|
""",
|
||||||
(pid, limit),
|
(pid, limit, offset),
|
||||||
)
|
)
|
||||||
return [r2d(r) for r in cur.fetchall()]
|
return _return_activity_list_rows(cur, [r2d(r) for r in cur.fetchall()])
|
||||||
|
|
||||||
|
|
||||||
@router.post("")
|
@router.post("")
|
||||||
@@ -95,37 +247,10 @@ def create_activity(e: ActivityEntry, x_profile_id: Optional[str]=Header(default
|
|||||||
)
|
)
|
||||||
|
|
||||||
eid = str(uuid.uuid4())
|
eid = str(uuid.uuid4())
|
||||||
d = e.model_dump()
|
|
||||||
with get_db() as conn:
|
with get_db() as conn:
|
||||||
cur = get_cursor(conn)
|
cur = get_cursor(conn)
|
||||||
cur.execute("""INSERT INTO activity_log
|
insert_activity_from_entry(cur, pid, eid, e)
|
||||||
(id,profile_id,date,start_time,end_time,activity_type,duration_min,kcal_active,kcal_resting,
|
run_activity_post_write_hooks(cur, pid, eid)
|
||||||
hr_avg,hr_max,distance_km,rpe,source,notes,created)
|
|
||||||
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,CURRENT_TIMESTAMP)""",
|
|
||||||
(eid,pid,d['date'],d['start_time'],d['end_time'],d['activity_type'],d['duration_min'],
|
|
||||||
d['kcal_active'],d['kcal_resting'],d['hr_avg'],d['hr_max'],d['distance_km'],
|
|
||||||
d['rpe'],d['source'],d['notes']))
|
|
||||||
|
|
||||||
# Phase 1.2: Auto-evaluation after INSERT
|
|
||||||
if EVALUATION_AVAILABLE:
|
|
||||||
# Load the activity data to evaluate
|
|
||||||
cur.execute("""
|
|
||||||
SELECT id, profile_id, date, training_type_id, duration_min,
|
|
||||||
hr_avg, hr_max, distance_km, kcal_active, kcal_resting,
|
|
||||||
rpe, pace_min_per_km, cadence, elevation_gain
|
|
||||||
FROM activity_log
|
|
||||||
WHERE id = %s
|
|
||||||
""", (eid,))
|
|
||||||
activity_row = cur.fetchone()
|
|
||||||
if activity_row:
|
|
||||||
activity_dict = dict(activity_row)
|
|
||||||
training_type_id = activity_dict.get("training_type_id")
|
|
||||||
if training_type_id:
|
|
||||||
try:
|
|
||||||
evaluate_and_save_activity(cur, eid, activity_dict, training_type_id, pid)
|
|
||||||
logger.info(f"[AUTO-EVAL] Evaluated activity {eid} on INSERT")
|
|
||||||
except Exception as eval_error:
|
|
||||||
logger.error(f"[AUTO-EVAL] Failed to evaluate activity {eid}: {eval_error}")
|
|
||||||
|
|
||||||
# Phase 2: Increment usage counter (always for new entries)
|
# Phase 2: Increment usage counter (always for new entries)
|
||||||
increment_feature_usage(pid, 'activity_entries')
|
increment_feature_usage(pid, 'activity_entries')
|
||||||
@@ -133,36 +258,146 @@ def create_activity(e: ActivityEntry, x_profile_id: Optional[str]=Header(default
|
|||||||
return {"id":eid,"date":e.date}
|
return {"id":eid,"date":e.date}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/stats")
|
||||||
|
def activity_stats(
|
||||||
|
skip_quality_filter: bool = Query(
|
||||||
|
False,
|
||||||
|
description="True = Statistik-Kacheln ohne Profil-Qualitätsfilter (passend zur /activity-Liste).",
|
||||||
|
),
|
||||||
|
session: dict = Depends(require_auth),
|
||||||
|
):
|
||||||
|
"""Get activity statistics (last 30 entries)."""
|
||||||
|
pid = str(session["profile_id"])
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
if skip_quality_filter:
|
||||||
|
quality_filter = ""
|
||||||
|
else:
|
||||||
|
cur.execute("SELECT * FROM profiles WHERE id=%s", (pid,))
|
||||||
|
profile = r2d(cur.fetchone())
|
||||||
|
quality_filter = get_quality_filter_sql(profile or {}, "")
|
||||||
|
cur.execute(
|
||||||
|
f"SELECT COUNT(*)::bigint AS c FROM activity_log WHERE profile_id=%s {quality_filter}",
|
||||||
|
(pid,),
|
||||||
|
)
|
||||||
|
total_in_profile = int(cur.fetchone()["c"])
|
||||||
|
if skip_quality_filter:
|
||||||
|
cur.execute(
|
||||||
|
f"""
|
||||||
|
SELECT d.* FROM (
|
||||||
|
SELECT al.*, ROW_NUMBER() OVER (
|
||||||
|
{_ACTIVITY_DEDUP_WINDOW}
|
||||||
|
) AS _dup_rn
|
||||||
|
FROM activity_log al
|
||||||
|
WHERE al.profile_id = %s
|
||||||
|
) d
|
||||||
|
WHERE d._dup_rn = 1
|
||||||
|
ORDER BY d.date DESC, d.start_time DESC NULLS LAST, d.id DESC
|
||||||
|
LIMIT 30
|
||||||
|
""",
|
||||||
|
(pid,),
|
||||||
|
)
|
||||||
|
rows = _activity_rows_after_list_query(cur)
|
||||||
|
else:
|
||||||
|
cur.execute(
|
||||||
|
f"""
|
||||||
|
SELECT * FROM activity_log
|
||||||
|
WHERE profile_id=%s {quality_filter}
|
||||||
|
ORDER BY date DESC, start_time DESC NULLS LAST, id DESC
|
||||||
|
LIMIT 30
|
||||||
|
""",
|
||||||
|
(pid,),
|
||||||
|
)
|
||||||
|
rows = [r2d(r) for r in cur.fetchall()]
|
||||||
|
if not rows:
|
||||||
|
return {
|
||||||
|
"count": 0,
|
||||||
|
"sample_size": 0,
|
||||||
|
"total_in_profile": total_in_profile,
|
||||||
|
"total_kcal": 0,
|
||||||
|
"total_min": 0,
|
||||||
|
"by_type": {},
|
||||||
|
}
|
||||||
|
total_kcal = sum(float(r.get("kcal_active") or 0) for r in rows)
|
||||||
|
total_min = sum(float(r.get("duration_min") or 0) for r in rows)
|
||||||
|
by_type = {}
|
||||||
|
for r in rows:
|
||||||
|
t = r["activity_type"]
|
||||||
|
by_type.setdefault(t, {"count": 0, "kcal": 0, "min": 0})
|
||||||
|
by_type[t]["count"] += 1
|
||||||
|
by_type[t]["kcal"] += float(r.get("kcal_active") or 0)
|
||||||
|
by_type[t]["min"] += float(r.get("duration_min") or 0)
|
||||||
|
return {
|
||||||
|
"count": len(rows),
|
||||||
|
"sample_size": len(rows),
|
||||||
|
"total_in_profile": total_in_profile,
|
||||||
|
"total_kcal": round(total_kcal),
|
||||||
|
"total_min": round(total_min),
|
||||||
|
"by_type": by_type,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/uncategorized")
|
||||||
|
def list_uncategorized_activities(
|
||||||
|
session: dict = Depends(require_auth),
|
||||||
|
):
|
||||||
|
"""Get activities without assigned training type, grouped by activity_type."""
|
||||||
|
pid = str(session["profile_id"])
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT activity_type, COUNT(*) as count,
|
||||||
|
MIN(date) as first_date, MAX(date) as last_date
|
||||||
|
FROM activity_log
|
||||||
|
WHERE profile_id=%s AND training_type_id IS NULL
|
||||||
|
GROUP BY activity_type
|
||||||
|
ORDER BY count DESC
|
||||||
|
""",
|
||||||
|
(pid,),
|
||||||
|
)
|
||||||
|
return [r2d(r) for r in cur.fetchall()]
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/mappable-fields")
|
||||||
|
def get_activity_mappable_fields(session: dict = Depends(require_auth)):
|
||||||
|
"""
|
||||||
|
Vollständiger Katalog für Import-Mappings (activity_log-Kernfelder + alle aktiven training_parameters).
|
||||||
|
Werte für Keys ohne Schema zur konkreten Session werden beim Import ignoriert.
|
||||||
|
"""
|
||||||
|
pid = str(session["profile_id"])
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
return get_mappable_activity_field_catalog(cur, pid)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/attribute-schema")
|
||||||
|
def get_activity_attribute_schema(
|
||||||
|
training_category: Optional[str] = Query(None),
|
||||||
|
training_type_id: Optional[int] = Query(None),
|
||||||
|
session: dict = Depends(require_auth),
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Aufgelöstes Attributprofil (tcp/ttp) für Erfassung ohne bestehende Session —
|
||||||
|
gleiche Logik wie resolve_activity_attribute_schema.
|
||||||
|
"""
|
||||||
|
from data_layer.activity_session_metrics import resolve_activity_attribute_schema
|
||||||
|
|
||||||
|
cat = (training_category or "").strip() or None
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
schema = resolve_activity_attribute_schema(cur, cat, training_type_id)
|
||||||
|
return {"schema": schema}
|
||||||
|
|
||||||
|
|
||||||
@router.put("/{eid}")
|
@router.put("/{eid}")
|
||||||
def update_activity(eid: str, e: ActivityEntry, x_profile_id: Optional[str]=Header(default=None), session: dict=Depends(require_auth)):
|
def update_activity(eid: str, e: ActivityEntry, x_profile_id: Optional[str]=Header(default=None), session: dict=Depends(require_auth)):
|
||||||
"""Update existing activity entry."""
|
"""Update existing activity entry."""
|
||||||
pid = get_pid(x_profile_id)
|
pid = get_pid(x_profile_id)
|
||||||
with get_db() as conn:
|
with get_db() as conn:
|
||||||
d = e.model_dump()
|
|
||||||
cur = get_cursor(conn)
|
cur = get_cursor(conn)
|
||||||
cur.execute(f"UPDATE activity_log SET {', '.join(f'{k}=%s' for k in d)} WHERE id=%s AND profile_id=%s",
|
update_activity_from_entry(cur, pid, eid, e)
|
||||||
list(d.values())+[eid,pid])
|
run_activity_post_write_hooks(cur, pid, eid)
|
||||||
|
|
||||||
# Phase 1.2: Auto-evaluation after UPDATE
|
|
||||||
if EVALUATION_AVAILABLE:
|
|
||||||
# Load the updated activity data to evaluate
|
|
||||||
cur.execute("""
|
|
||||||
SELECT id, profile_id, date, training_type_id, duration_min,
|
|
||||||
hr_avg, hr_max, distance_km, kcal_active, kcal_resting,
|
|
||||||
rpe, pace_min_per_km, cadence, elevation_gain
|
|
||||||
FROM activity_log
|
|
||||||
WHERE id = %s
|
|
||||||
""", (eid,))
|
|
||||||
activity_row = cur.fetchone()
|
|
||||||
if activity_row:
|
|
||||||
activity_dict = dict(activity_row)
|
|
||||||
training_type_id = activity_dict.get("training_type_id")
|
|
||||||
if training_type_id:
|
|
||||||
try:
|
|
||||||
evaluate_and_save_activity(cur, eid, activity_dict, training_type_id, pid)
|
|
||||||
logger.info(f"[AUTO-EVAL] Re-evaluated activity {eid} on UPDATE")
|
|
||||||
except Exception as eval_error:
|
|
||||||
logger.error(f"[AUTO-EVAL] Failed to re-evaluate activity {eid}: {eval_error}")
|
|
||||||
|
|
||||||
return {"id":eid}
|
return {"id":eid}
|
||||||
|
|
||||||
@@ -177,25 +412,66 @@ def delete_activity(eid: str, x_profile_id: Optional[str]=Header(default=None),
|
|||||||
return {"ok":True}
|
return {"ok":True}
|
||||||
|
|
||||||
|
|
||||||
@router.get("/stats")
|
@router.put("/{eid}/metrics")
|
||||||
def activity_stats(x_profile_id: Optional[str]=Header(default=None), session: dict=Depends(require_auth)):
|
def replace_activity_metrics(
|
||||||
"""Get activity statistics (last 30 entries)."""
|
eid: str,
|
||||||
pid = get_pid(x_profile_id)
|
body: ActivityMetricsReplace,
|
||||||
with get_db() as conn:
|
session: dict = Depends(require_auth),
|
||||||
cur = get_cursor(conn)
|
):
|
||||||
cur.execute(
|
"""
|
||||||
"SELECT * FROM activity_log WHERE profile_id=%s ORDER BY date DESC LIMIT 30", (pid,))
|
Voller Ersatz der EAV-Session-Metriken (siehe ACTIVITY_SESSION_METRICS_EAV_AGENT_GUIDE.md).
|
||||||
rows = [r2d(r) for r in cur.fetchall()]
|
"""
|
||||||
if not rows: return {"count":0,"total_kcal":0,"total_min":0,"by_type":{}}
|
from data_layer.activity_session_metrics import (
|
||||||
total_kcal=sum(float(r.get('kcal_active') or 0) for r in rows)
|
ActivitySessionMetricsError,
|
||||||
total_min=sum(float(r.get('duration_min') or 0) for r in rows)
|
replace_activity_session_metrics,
|
||||||
by_type={}
|
)
|
||||||
for r in rows:
|
|
||||||
t=r['activity_type']; by_type.setdefault(t,{'count':0,'kcal':0,'min':0})
|
pid = str(session["profile_id"])
|
||||||
by_type[t]['count']+=1
|
payload = [m.model_dump() for m in body.metrics]
|
||||||
by_type[t]['kcal']+=float(r.get('kcal_active') or 0)
|
try:
|
||||||
by_type[t]['min']+=float(r.get('duration_min') or 0)
|
with get_db() as conn:
|
||||||
return {"count":len(rows),"total_kcal":round(total_kcal),"total_min":round(total_min),"by_type":by_type}
|
cur = get_cursor(conn)
|
||||||
|
metrics = replace_activity_session_metrics(cur, pid, eid, payload)
|
||||||
|
conn.commit()
|
||||||
|
except ActivitySessionMetricsError as err:
|
||||||
|
raise HTTPException(err.status_code, err.detail) from err
|
||||||
|
return {"id": eid, "metrics": metrics}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/{eid}")
|
||||||
|
def get_activity_session(
|
||||||
|
eid: str,
|
||||||
|
use_form_schema: bool = Query(
|
||||||
|
False,
|
||||||
|
description="True: Schema aus Query training_category / training_type_id (Formular), nicht nur DB-Zeile",
|
||||||
|
),
|
||||||
|
training_category: Optional[str] = Query(None),
|
||||||
|
training_type_id: Optional[int] = Query(None),
|
||||||
|
session: dict = Depends(require_auth),
|
||||||
|
):
|
||||||
|
"""Session-Kopf + aufgelöstes Schema + EAV-Metriken (Layer 1)."""
|
||||||
|
from data_layer.activity_session_metrics import (
|
||||||
|
ActivitySessionMetricsError,
|
||||||
|
get_activity_session_logical_unit,
|
||||||
|
)
|
||||||
|
from data_layer.utils import serialize_dates
|
||||||
|
|
||||||
|
pid = str(session["profile_id"])
|
||||||
|
try:
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
unit = get_activity_session_logical_unit(
|
||||||
|
cur,
|
||||||
|
pid,
|
||||||
|
eid,
|
||||||
|
use_form_training_context=use_form_schema,
|
||||||
|
form_training_category=training_category,
|
||||||
|
form_training_type_id=training_type_id,
|
||||||
|
)
|
||||||
|
except ActivitySessionMetricsError as err:
|
||||||
|
raise HTTPException(err.status_code, err.detail) from err
|
||||||
|
unit["header"] = serialize_dates(unit["header"])
|
||||||
|
return unit
|
||||||
|
|
||||||
|
|
||||||
def get_training_type_for_activity_with_cursor(cur, activity_type: str, profile_id: str | None = None):
|
def get_training_type_for_activity_with_cursor(cur, activity_type: str, profile_id: str | None = None):
|
||||||
@@ -251,23 +527,6 @@ def get_training_type_for_activity(activity_type: str, profile_id: str = None):
|
|||||||
return get_training_type_for_activity_with_cursor(cur, activity_type, profile_id)
|
return get_training_type_for_activity_with_cursor(cur, activity_type, profile_id)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/uncategorized")
|
|
||||||
def list_uncategorized_activities(x_profile_id: Optional[str]=Header(default=None), session: dict=Depends(require_auth)):
|
|
||||||
"""Get activities without assigned training type, grouped by activity_type."""
|
|
||||||
pid = get_pid(x_profile_id)
|
|
||||||
with get_db() as conn:
|
|
||||||
cur = get_cursor(conn)
|
|
||||||
cur.execute("""
|
|
||||||
SELECT activity_type, COUNT(*) as count,
|
|
||||||
MIN(date) as first_date, MAX(date) as last_date
|
|
||||||
FROM activity_log
|
|
||||||
WHERE profile_id=%s AND training_type_id IS NULL
|
|
||||||
GROUP BY activity_type
|
|
||||||
ORDER BY count DESC
|
|
||||||
""", (pid,))
|
|
||||||
return [r2d(r) for r in cur.fetchall()]
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/bulk-categorize")
|
@router.post("/bulk-categorize")
|
||||||
def bulk_categorize_activities(
|
def bulk_categorize_activities(
|
||||||
data: dict,
|
data: dict,
|
||||||
@@ -353,7 +612,10 @@ def bulk_categorize_activities(
|
|||||||
|
|
||||||
@router.post("/import-csv")
|
@router.post("/import-csv")
|
||||||
async def import_activity_csv(file: UploadFile=File(...), x_profile_id: Optional[str]=Header(default=None), session: dict=Depends(require_auth)):
|
async def import_activity_csv(file: UploadFile=File(...), x_profile_id: Optional[str]=Header(default=None), session: dict=Depends(require_auth)):
|
||||||
"""Import Apple Health workout CSV with automatic training type mapping."""
|
"""
|
||||||
|
Legacy-Upload (Apple Health Workout-CSV-Spaltennamen).
|
||||||
|
Persistenz läuft über activity_persistence_orchestrator — gleiche Schicht wie Universal-CSV.
|
||||||
|
"""
|
||||||
pid = get_pid(x_profile_id)
|
pid = get_pid(x_profile_id)
|
||||||
raw = await file.read()
|
raw = await file.read()
|
||||||
try: text = raw.decode('utf-8')
|
try: text = raw.decode('utf-8')
|
||||||
@@ -367,9 +629,11 @@ async def import_activity_csv(file: UploadFile=File(...), x_profile_id: Optional
|
|||||||
for row in reader:
|
for row in reader:
|
||||||
wtype = row.get('Workout Type','').strip()
|
wtype = row.get('Workout Type','').strip()
|
||||||
start = row.get('Start','').strip()
|
start = row.get('Start','').strip()
|
||||||
if not wtype or not start: continue
|
if not wtype or not start:
|
||||||
try: date = start[:10]
|
continue
|
||||||
except: continue
|
workout_date, workout_start_t = normalize_activity_start(start)
|
||||||
|
if not workout_date:
|
||||||
|
continue
|
||||||
dur = row.get('Duration','').strip()
|
dur = row.get('Duration','').strip()
|
||||||
duration_min = None
|
duration_min = None
|
||||||
if dur:
|
if dur:
|
||||||
@@ -386,106 +650,82 @@ async def import_activity_csv(file: UploadFile=File(...), x_profile_id: Optional
|
|||||||
# Map activity_type to training_type_id using database mappings
|
# Map activity_type to training_type_id using database mappings
|
||||||
training_type_id, training_category, training_subcategory = get_training_type_for_activity(wtype, pid)
|
training_type_id, training_category, training_subcategory = get_training_type_for_activity(wtype, pid)
|
||||||
|
|
||||||
|
kcal_a = kj(row.get("Aktive Energie (kJ)", ""))
|
||||||
|
kcal_r = kj(row.get("Ruheeinträge (kJ)", ""))
|
||||||
|
hr_av = tf(row.get("Durchschn. Herzfrequenz (count/min)", ""))
|
||||||
|
hr_mx = tf(row.get("Max. Herzfrequenz (count/min)", ""))
|
||||||
|
dist_km = tf(row.get("Distanz (km)", ""))
|
||||||
try:
|
try:
|
||||||
# Check if entry already exists (duplicate detection by date + start_time)
|
existing_id = find_activity_duplicate_id(cur, pid, workout_date, workout_start_t)
|
||||||
cur.execute("""
|
if existing_id:
|
||||||
SELECT id FROM activity_log
|
update_activity_columns(
|
||||||
WHERE profile_id = %s AND date = %s AND start_time = %s
|
cur,
|
||||||
""", (pid, date, start))
|
pid,
|
||||||
existing = cur.fetchone()
|
str(existing_id),
|
||||||
|
{
|
||||||
if existing:
|
"start_time": workout_start_t,
|
||||||
# Update existing entry (e.g., to add training type mapping)
|
"end_time": row.get("End", "") or None,
|
||||||
existing_id = existing['id']
|
"activity_type": wtype,
|
||||||
cur.execute("""
|
"duration_min": duration_min,
|
||||||
UPDATE activity_log
|
"kcal_active": kcal_a,
|
||||||
SET end_time = %s,
|
"kcal_resting": kcal_r,
|
||||||
activity_type = %s,
|
"hr_avg": hr_av,
|
||||||
duration_min = %s,
|
"hr_max": hr_mx,
|
||||||
kcal_active = %s,
|
"distance_km": dist_km,
|
||||||
kcal_resting = %s,
|
"training_type_id": training_type_id,
|
||||||
hr_avg = %s,
|
"training_category": training_category,
|
||||||
hr_max = %s,
|
"training_subcategory": training_subcategory,
|
||||||
distance_km = %s,
|
},
|
||||||
training_type_id = %s,
|
)
|
||||||
training_category = %s,
|
skipped += 1
|
||||||
training_subcategory = %s
|
run_activity_post_write_hooks_import(
|
||||||
WHERE id = %s
|
cur,
|
||||||
""", (
|
pid,
|
||||||
row.get('End',''), wtype, duration_min,
|
str(existing_id),
|
||||||
kj(row.get('Aktive Energie (kJ)','')),
|
workout_date=workout_date,
|
||||||
kj(row.get('Ruheeinträge (kJ)','')),
|
training_type_id=training_type_id,
|
||||||
tf(row.get('Durchschn. Herzfrequenz (count/min)','')),
|
duration_min=duration_min,
|
||||||
tf(row.get('Max. Herzfrequenz (count/min)','')),
|
hr_avg=hr_av,
|
||||||
tf(row.get('Distanz (km)','')),
|
hr_max=hr_mx,
|
||||||
training_type_id, training_category, training_subcategory,
|
distance_km=dist_km,
|
||||||
existing_id
|
kcal_active=kcal_a,
|
||||||
))
|
kcal_resting=kcal_r,
|
||||||
skipped += 1 # Count as skipped (not newly inserted)
|
)
|
||||||
|
|
||||||
# Phase 1.2: Auto-evaluation after CSV import UPDATE
|
|
||||||
if EVALUATION_AVAILABLE and training_type_id:
|
|
||||||
try:
|
|
||||||
# Build activity dict for evaluation
|
|
||||||
activity_dict = {
|
|
||||||
"id": existing_id,
|
|
||||||
"profile_id": pid,
|
|
||||||
"date": date,
|
|
||||||
"training_type_id": training_type_id,
|
|
||||||
"duration_min": duration_min,
|
|
||||||
"hr_avg": tf(row.get('Durchschn. Herzfrequenz (count/min)','')),
|
|
||||||
"hr_max": tf(row.get('Max. Herzfrequenz (count/min)','')),
|
|
||||||
"distance_km": tf(row.get('Distanz (km)','')),
|
|
||||||
"kcal_active": kj(row.get('Aktive Energie (kJ)','')),
|
|
||||||
"kcal_resting": kj(row.get('Ruheeinträge (kJ)','')),
|
|
||||||
"rpe": None,
|
|
||||||
"pace_min_per_km": None,
|
|
||||||
"cadence": None,
|
|
||||||
"elevation_gain": None
|
|
||||||
}
|
|
||||||
evaluate_and_save_activity(cur, existing_id, activity_dict, training_type_id, pid)
|
|
||||||
logger.debug(f"[AUTO-EVAL] Re-evaluated updated activity {existing_id}")
|
|
||||||
except Exception as eval_error:
|
|
||||||
logger.warning(f"[AUTO-EVAL] Failed to re-evaluate updated activity {existing_id}: {eval_error}")
|
|
||||||
else:
|
else:
|
||||||
# Insert new entry
|
new_id = new_activity_id()
|
||||||
new_id = str(uuid.uuid4())
|
insert_activity_csv_minimal(
|
||||||
cur.execute("""INSERT INTO activity_log
|
cur,
|
||||||
(id,profile_id,date,start_time,end_time,activity_type,duration_min,kcal_active,kcal_resting,
|
pid,
|
||||||
hr_avg,hr_max,distance_km,source,training_type_id,training_category,training_subcategory,created)
|
new_id,
|
||||||
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,'apple_health',%s,%s,%s,CURRENT_TIMESTAMP)""",
|
date_iso=workout_date,
|
||||||
(new_id,pid,date,start,row.get('End',''),wtype,duration_min,
|
start_time=workout_start_t,
|
||||||
kj(row.get('Aktive Energie (kJ)','')),kj(row.get('Ruheeinträge (kJ)','')),
|
end_time=row.get("End", "") or None,
|
||||||
tf(row.get('Durchschn. Herzfrequenz (count/min)','')),
|
activity_type=wtype,
|
||||||
tf(row.get('Max. Herzfrequenz (count/min)','')),
|
duration_min=duration_min,
|
||||||
tf(row.get('Distanz (km)','')),
|
kcal_active=kcal_a,
|
||||||
training_type_id,training_category,training_subcategory))
|
kcal_resting=kcal_r,
|
||||||
inserted+=1
|
hr_avg=hr_av,
|
||||||
|
hr_max=hr_mx,
|
||||||
# Phase 1.2: Auto-evaluation after CSV import INSERT
|
distance_km=dist_km,
|
||||||
if EVALUATION_AVAILABLE and training_type_id:
|
training_type_id=training_type_id,
|
||||||
try:
|
training_category=training_category,
|
||||||
# Build activity dict for evaluation
|
training_subcategory=training_subcategory,
|
||||||
activity_dict = {
|
source="apple_health",
|
||||||
"id": new_id,
|
)
|
||||||
"profile_id": pid,
|
inserted += 1
|
||||||
"date": date,
|
run_activity_post_write_hooks_import(
|
||||||
"training_type_id": training_type_id,
|
cur,
|
||||||
"duration_min": duration_min,
|
pid,
|
||||||
"hr_avg": tf(row.get('Durchschn. Herzfrequenz (count/min)','')),
|
new_id,
|
||||||
"hr_max": tf(row.get('Max. Herzfrequenz (count/min)','')),
|
workout_date=workout_date,
|
||||||
"distance_km": tf(row.get('Distanz (km)','')),
|
training_type_id=training_type_id,
|
||||||
"kcal_active": kj(row.get('Aktive Energie (kJ)','')),
|
duration_min=duration_min,
|
||||||
"kcal_resting": kj(row.get('Ruheeinträge (kJ)','')),
|
hr_avg=hr_av,
|
||||||
"rpe": None,
|
hr_max=hr_mx,
|
||||||
"pace_min_per_km": None,
|
distance_km=dist_km,
|
||||||
"cadence": None,
|
kcal_active=kcal_a,
|
||||||
"elevation_gain": None
|
kcal_resting=kcal_r,
|
||||||
}
|
)
|
||||||
evaluate_and_save_activity(cur, new_id, activity_dict, training_type_id, pid)
|
|
||||||
logger.debug(f"[AUTO-EVAL] Evaluated imported activity {new_id}")
|
|
||||||
except Exception as eval_error:
|
|
||||||
logger.warning(f"[AUTO-EVAL] Failed to evaluate imported activity {new_id}: {eval_error}")
|
|
||||||
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
|
||||||
|
|||||||
@@ -0,0 +1,266 @@
|
|||||||
|
"""
|
||||||
|
Admin: training_category_parameter + training_type_parameter (attribute profiles).
|
||||||
|
|
||||||
|
Agent guide: .claude/docs/technical/ACTIVITY_SESSION_METRICS_EAV_AGENT_GUIDE.md
|
||||||
|
"""
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||||
|
from pydantic import BaseModel, Field
|
||||||
|
|
||||||
|
from auth import require_admin
|
||||||
|
from db import get_db, get_cursor, r2d
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/api/admin", tags=["admin", "activity-attribute-profiles"])
|
||||||
|
|
||||||
|
|
||||||
|
class CategoryParameterCreate(BaseModel):
|
||||||
|
training_category: str = Field(..., min_length=1, max_length=50)
|
||||||
|
training_parameter_id: int
|
||||||
|
sort_order: int = 0
|
||||||
|
required: bool = False
|
||||||
|
ui_group: Optional[str] = Field(None, max_length=50)
|
||||||
|
|
||||||
|
|
||||||
|
class TypeParameterCreate(BaseModel):
|
||||||
|
training_type_id: int
|
||||||
|
training_parameter_id: int
|
||||||
|
sort_order: Optional[int] = None
|
||||||
|
required: Optional[bool] = None
|
||||||
|
ui_group: Optional[str] = Field(None, max_length=50)
|
||||||
|
|
||||||
|
|
||||||
|
class CategoryParameterUpdate(BaseModel):
|
||||||
|
sort_order: Optional[int] = None
|
||||||
|
required: Optional[bool] = None
|
||||||
|
ui_group: Optional[str] = Field(None, max_length=50)
|
||||||
|
|
||||||
|
|
||||||
|
class TypeParameterUpdate(BaseModel):
|
||||||
|
sort_order: Optional[int] = None
|
||||||
|
required: Optional[bool] = None
|
||||||
|
ui_group: Optional[str] = Field(None, max_length=50)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/training-category-parameters")
|
||||||
|
def admin_list_category_parameters(
|
||||||
|
category: Optional[str] = Query(None, description="Filter: training_types.category"),
|
||||||
|
session: dict = Depends(require_admin),
|
||||||
|
):
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
if category:
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT tcp.*, tp.key AS parameter_key, tp.name_de AS parameter_name_de
|
||||||
|
FROM training_category_parameter tcp
|
||||||
|
JOIN training_parameters tp ON tp.id = tcp.training_parameter_id
|
||||||
|
WHERE tcp.training_category = %s
|
||||||
|
ORDER BY tcp.sort_order, tp.key
|
||||||
|
""",
|
||||||
|
(category.strip(),),
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT tcp.*, tp.key AS parameter_key, tp.name_de AS parameter_name_de
|
||||||
|
FROM training_category_parameter tcp
|
||||||
|
JOIN training_parameters tp ON tp.id = tcp.training_parameter_id
|
||||||
|
ORDER BY tcp.training_category, tcp.sort_order, tp.key
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
return [r2d(r) for r in cur.fetchall()]
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/training-category-parameters")
|
||||||
|
def admin_add_category_parameter(
|
||||||
|
body: CategoryParameterCreate,
|
||||||
|
session: dict = Depends(require_admin),
|
||||||
|
):
|
||||||
|
cat = body.training_category.strip()
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cur.execute("SELECT id FROM training_parameters WHERE id = %s", (body.training_parameter_id,))
|
||||||
|
if not cur.fetchone():
|
||||||
|
raise HTTPException(404, "training_parameter_id unbekannt")
|
||||||
|
try:
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO training_category_parameter (
|
||||||
|
training_category, training_parameter_id, sort_order, required, ui_group
|
||||||
|
) VALUES (%s,%s,%s,%s,%s)
|
||||||
|
RETURNING id
|
||||||
|
""",
|
||||||
|
(cat, body.training_parameter_id, body.sort_order, body.required, body.ui_group),
|
||||||
|
)
|
||||||
|
new_id = cur.fetchone()["id"]
|
||||||
|
conn.commit()
|
||||||
|
except Exception as e:
|
||||||
|
conn.rollback()
|
||||||
|
if "uq_training_category_parameter" in str(e).lower() or "unique" in str(e).lower():
|
||||||
|
raise HTTPException(409, "Zuordnung existiert bereits") from e
|
||||||
|
raise HTTPException(400, str(e)) from e
|
||||||
|
return {"id": new_id}
|
||||||
|
|
||||||
|
|
||||||
|
@router.put("/training-category-parameters/{link_id}")
|
||||||
|
def admin_update_category_parameter(
|
||||||
|
link_id: int,
|
||||||
|
body: CategoryParameterUpdate,
|
||||||
|
session: dict = Depends(require_admin),
|
||||||
|
):
|
||||||
|
patch = body.model_dump(exclude_unset=True)
|
||||||
|
if not patch:
|
||||||
|
raise HTTPException(400, "Keine Felder zum Aktualisieren")
|
||||||
|
cols: list[str] = []
|
||||||
|
vals: list = []
|
||||||
|
if "sort_order" in patch:
|
||||||
|
cols.append("sort_order = %s")
|
||||||
|
vals.append(patch["sort_order"])
|
||||||
|
if "required" in patch:
|
||||||
|
cols.append("required = %s")
|
||||||
|
vals.append(patch["required"])
|
||||||
|
if "ui_group" in patch:
|
||||||
|
cols.append("ui_group = %s")
|
||||||
|
vals.append(patch["ui_group"].strip() if patch["ui_group"] else None)
|
||||||
|
if not cols:
|
||||||
|
raise HTTPException(400, "Keine Felder zum Aktualisieren")
|
||||||
|
vals.append(link_id)
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cur.execute(
|
||||||
|
f"UPDATE training_category_parameter SET {', '.join(cols)} WHERE id = %s RETURNING id",
|
||||||
|
vals,
|
||||||
|
)
|
||||||
|
if not cur.fetchone():
|
||||||
|
raise HTTPException(404, "Eintrag nicht gefunden")
|
||||||
|
conn.commit()
|
||||||
|
return {"ok": True, "id": link_id}
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/training-category-parameters/{link_id}")
|
||||||
|
def admin_delete_category_parameter(
|
||||||
|
link_id: int,
|
||||||
|
session: dict = Depends(require_admin),
|
||||||
|
):
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cur.execute(
|
||||||
|
"DELETE FROM training_category_parameter WHERE id = %s RETURNING id",
|
||||||
|
(link_id,),
|
||||||
|
)
|
||||||
|
if not cur.fetchone():
|
||||||
|
raise HTTPException(404, "Eintrag nicht gefunden")
|
||||||
|
conn.commit()
|
||||||
|
return {"ok": True}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/training-type-parameters")
|
||||||
|
def admin_list_type_parameters(
|
||||||
|
training_type_id: int = Query(..., ge=1),
|
||||||
|
session: dict = Depends(require_admin),
|
||||||
|
):
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT ttp.*, tp.key AS parameter_key, tp.name_de AS parameter_name_de
|
||||||
|
FROM training_type_parameter ttp
|
||||||
|
JOIN training_parameters tp ON tp.id = ttp.training_parameter_id
|
||||||
|
WHERE ttp.training_type_id = %s
|
||||||
|
ORDER BY ttp.sort_order NULLS LAST, tp.key
|
||||||
|
""",
|
||||||
|
(training_type_id,),
|
||||||
|
)
|
||||||
|
return [r2d(r) for r in cur.fetchall()]
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/training-type-parameters")
|
||||||
|
def admin_add_type_parameter(
|
||||||
|
body: TypeParameterCreate,
|
||||||
|
session: dict = Depends(require_admin),
|
||||||
|
):
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cur.execute("SELECT id FROM training_types WHERE id = %s", (body.training_type_id,))
|
||||||
|
if not cur.fetchone():
|
||||||
|
raise HTTPException(404, "training_type_id unbekannt")
|
||||||
|
cur.execute("SELECT id FROM training_parameters WHERE id = %s", (body.training_parameter_id,))
|
||||||
|
if not cur.fetchone():
|
||||||
|
raise HTTPException(404, "training_parameter_id unbekannt")
|
||||||
|
try:
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO training_type_parameter (
|
||||||
|
training_type_id, training_parameter_id, sort_order, required, ui_group
|
||||||
|
) VALUES (%s,%s,%s,%s,%s)
|
||||||
|
RETURNING id
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
body.training_type_id,
|
||||||
|
body.training_parameter_id,
|
||||||
|
body.sort_order,
|
||||||
|
body.required,
|
||||||
|
body.ui_group,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
new_id = cur.fetchone()["id"]
|
||||||
|
conn.commit()
|
||||||
|
except Exception as e:
|
||||||
|
conn.rollback()
|
||||||
|
if "uq_training_type_parameter" in str(e).lower() or "unique" in str(e).lower():
|
||||||
|
raise HTTPException(409, "Zuordnung existiert bereits") from e
|
||||||
|
raise HTTPException(400, str(e)) from e
|
||||||
|
return {"id": new_id}
|
||||||
|
|
||||||
|
|
||||||
|
@router.put("/training-type-parameters/{link_id}")
|
||||||
|
def admin_update_type_parameter(
|
||||||
|
link_id: int,
|
||||||
|
body: TypeParameterUpdate,
|
||||||
|
session: dict = Depends(require_admin),
|
||||||
|
):
|
||||||
|
patch = body.model_dump(exclude_unset=True)
|
||||||
|
if not patch:
|
||||||
|
raise HTTPException(400, "Keine Felder zum Aktualisieren")
|
||||||
|
cols: list[str] = []
|
||||||
|
vals: list = []
|
||||||
|
if "sort_order" in patch:
|
||||||
|
cols.append("sort_order = %s")
|
||||||
|
vals.append(patch["sort_order"])
|
||||||
|
if "required" in patch:
|
||||||
|
cols.append("required = %s")
|
||||||
|
vals.append(patch["required"])
|
||||||
|
if "ui_group" in patch:
|
||||||
|
cols.append("ui_group = %s")
|
||||||
|
vals.append(patch["ui_group"].strip() if patch["ui_group"] else None)
|
||||||
|
if not cols:
|
||||||
|
raise HTTPException(400, "Keine Felder zum Aktualisieren")
|
||||||
|
vals.append(link_id)
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cur.execute(
|
||||||
|
f"UPDATE training_type_parameter SET {', '.join(cols)} WHERE id = %s RETURNING id",
|
||||||
|
vals,
|
||||||
|
)
|
||||||
|
if not cur.fetchone():
|
||||||
|
raise HTTPException(404, "Eintrag nicht gefunden")
|
||||||
|
conn.commit()
|
||||||
|
return {"ok": True, "id": link_id}
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/training-type-parameters/{link_id}")
|
||||||
|
def admin_delete_type_parameter(
|
||||||
|
link_id: int,
|
||||||
|
session: dict = Depends(require_admin),
|
||||||
|
):
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cur.execute(
|
||||||
|
"DELETE FROM training_type_parameter WHERE id = %s RETURNING id",
|
||||||
|
(link_id,),
|
||||||
|
)
|
||||||
|
if not cur.fetchone():
|
||||||
|
raise HTTPException(404, "Eintrag nicht gefunden")
|
||||||
|
conn.commit()
|
||||||
|
return {"ok": True}
|
||||||
@@ -25,6 +25,7 @@ from csv_parser.import_row_processing import (
|
|||||||
)
|
)
|
||||||
from csv_parser.module_registry import get_module_definition
|
from csv_parser.module_registry import get_module_definition
|
||||||
from csv_parser.template_validator import validate_csv_template
|
from csv_parser.template_validator import validate_csv_template
|
||||||
|
from data_layer.activity_persistence_orchestrator import merge_activity_csv_module_fields
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/admin/csv-templates", tags=["admin", "csv-import"])
|
router = APIRouter(prefix="/api/admin/csv-templates", tags=["admin", "csv-import"])
|
||||||
|
|
||||||
@@ -181,6 +182,8 @@ async def admin_analyze_csv_for_template(
|
|||||||
sig = column_signature(headers)
|
sig = column_signature(headers)
|
||||||
|
|
||||||
seed_row: dict | None = None
|
seed_row: dict | None = None
|
||||||
|
field_mappings: dict = {}
|
||||||
|
type_conversions: dict = {}
|
||||||
with get_db() as conn:
|
with get_db() as conn:
|
||||||
cur = get_cursor(conn)
|
cur = get_cursor(conn)
|
||||||
if seed_template_id is not None:
|
if seed_template_id is not None:
|
||||||
@@ -215,15 +218,30 @@ async def admin_analyze_csv_for_template(
|
|||||||
if best and best_key[0] > 0:
|
if best and best_key[0] > 0:
|
||||||
seed_row = best
|
seed_row = best
|
||||||
|
|
||||||
seed_fm = (seed_row or {}).get("field_mappings") or {}
|
mod_def = get_module_definition(module) or {}
|
||||||
if isinstance(seed_fm, str):
|
eff_fields = dict(mod_def.get("fields") or {})
|
||||||
seed_fm = {}
|
if module == "activity":
|
||||||
seed_tc = (seed_row or {}).get("type_conversions")
|
eff_fields = merge_activity_csv_module_fields(cur, eff_fields)
|
||||||
if not isinstance(seed_tc, dict):
|
|
||||||
seed_tc = {}
|
|
||||||
|
|
||||||
field_mappings = suggest_field_mappings(headers, module, seed_fm if seed_fm else None)
|
seed_fm = (seed_row or {}).get("field_mappings") or {}
|
||||||
type_conversions = build_type_conversions_for_mapping(module, field_mappings, seed_tc if seed_tc else None)
|
if isinstance(seed_fm, str):
|
||||||
|
seed_fm = {}
|
||||||
|
seed_tc = (seed_row or {}).get("type_conversions")
|
||||||
|
if not isinstance(seed_tc, dict):
|
||||||
|
seed_tc = {}
|
||||||
|
|
||||||
|
field_mappings = suggest_field_mappings(
|
||||||
|
headers,
|
||||||
|
module,
|
||||||
|
seed_fm if seed_fm else None,
|
||||||
|
effective_fields=eff_fields,
|
||||||
|
)
|
||||||
|
type_conversions = build_type_conversions_for_mapping(
|
||||||
|
module,
|
||||||
|
field_mappings,
|
||||||
|
seed_tc if seed_tc else None,
|
||||||
|
effective_fields=eff_fields,
|
||||||
|
)
|
||||||
|
|
||||||
seed_meta = None
|
seed_meta = None
|
||||||
if seed_row:
|
if seed_row:
|
||||||
@@ -270,13 +288,16 @@ def validate_system_template_dry_run(body: CsvTemplateValidateBody, session: dic
|
|||||||
"""
|
"""
|
||||||
if not get_module_definition(body.module):
|
if not get_module_definition(body.module):
|
||||||
raise HTTPException(400, f"Unbekanntes Modul: {body.module}")
|
raise HTTPException(400, f"Unbekanntes Modul: {body.module}")
|
||||||
return validate_csv_template(
|
with get_db() as conn:
|
||||||
body.module,
|
cur = get_cursor(conn)
|
||||||
body.field_mappings,
|
return validate_csv_template(
|
||||||
body.type_conversions,
|
body.module,
|
||||||
body.import_row_processing,
|
body.field_mappings,
|
||||||
body.column_signature,
|
body.type_conversions,
|
||||||
)
|
body.import_row_processing,
|
||||||
|
body.column_signature,
|
||||||
|
cur=cur,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{template_id}")
|
@router.get("/{template_id}")
|
||||||
@@ -297,18 +318,19 @@ def get_system_template(template_id: int, session: dict = Depends(require_admin)
|
|||||||
def create_system_template(body: CsvSystemTemplateCreate, session: dict = Depends(require_admin)):
|
def create_system_template(body: CsvSystemTemplateCreate, session: dict = Depends(require_admin)):
|
||||||
if not get_module_definition(body.module):
|
if not get_module_definition(body.module):
|
||||||
raise HTTPException(400, f"Unbekanntes Modul: {body.module}")
|
raise HTTPException(400, f"Unbekanntes Modul: {body.module}")
|
||||||
report = validate_csv_template(
|
|
||||||
body.module,
|
|
||||||
body.field_mappings,
|
|
||||||
body.type_conversions,
|
|
||||||
body.import_row_processing,
|
|
||||||
body.column_signature,
|
|
||||||
)
|
|
||||||
if not report["valid"]:
|
|
||||||
raise HTTPException(status_code=422, detail=report)
|
|
||||||
|
|
||||||
with get_db() as conn:
|
with get_db() as conn:
|
||||||
cur = get_cursor(conn)
|
cur = get_cursor(conn)
|
||||||
|
report = validate_csv_template(
|
||||||
|
body.module,
|
||||||
|
body.field_mappings,
|
||||||
|
body.type_conversions,
|
||||||
|
body.import_row_processing,
|
||||||
|
body.column_signature,
|
||||||
|
cur=cur,
|
||||||
|
)
|
||||||
|
if not report["valid"]:
|
||||||
|
raise HTTPException(status_code=422, detail=report)
|
||||||
|
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"""
|
"""
|
||||||
INSERT INTO csv_field_mappings (
|
INSERT INTO csv_field_mappings (
|
||||||
@@ -367,6 +389,7 @@ def update_system_template(
|
|||||||
tc_eff,
|
tc_eff,
|
||||||
irp_eff,
|
irp_eff,
|
||||||
col_eff if isinstance(col_eff, list) else None,
|
col_eff if isinstance(col_eff, list) else None,
|
||||||
|
cur=cur,
|
||||||
)
|
)
|
||||||
if not report["valid"]:
|
if not report["valid"]:
|
||||||
raise HTTPException(status_code=422, detail=report)
|
raise HTTPException(status_code=422, detail=report)
|
||||||
|
|||||||
@@ -0,0 +1,215 @@
|
|||||||
|
"""
|
||||||
|
Admin: training_parameters catalog (EAV keys for activity session metrics).
|
||||||
|
|
||||||
|
Agent guide: .claude/docs/technical/ACTIVITY_SESSION_METRICS_EAV_AGENT_GUIDE.md
|
||||||
|
"""
|
||||||
|
import re
|
||||||
|
from typing import Any, Optional
|
||||||
|
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||||
|
from pydantic import BaseModel, Field
|
||||||
|
from psycopg2 import errors as pg_errors
|
||||||
|
from psycopg2.extras import Json
|
||||||
|
|
||||||
|
from auth import require_admin
|
||||||
|
from db import get_db, get_cursor, r2d
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/api/admin/training-parameters", tags=["admin", "training-parameters"])
|
||||||
|
|
||||||
|
KEY_PATTERN = re.compile(r"^[a-z][a-z0-9_]{0,62}$")
|
||||||
|
|
||||||
|
PARAM_CATEGORY = {"physical", "physiological", "subjective", "environmental", "performance"}
|
||||||
|
DATA_TYPES = {"integer", "float", "string", "boolean"}
|
||||||
|
|
||||||
|
|
||||||
|
class TrainingParameterCreate(BaseModel):
|
||||||
|
key: str = Field(..., min_length=1, max_length=50)
|
||||||
|
name_de: str = Field(..., min_length=1, max_length=100)
|
||||||
|
name_en: str = Field(..., min_length=1, max_length=100)
|
||||||
|
category: str = Field(..., max_length=50)
|
||||||
|
data_type: str = Field(..., max_length=20)
|
||||||
|
unit: Optional[str] = Field(None, max_length=20)
|
||||||
|
description_de: Optional[str] = None
|
||||||
|
description_en: Optional[str] = None
|
||||||
|
source_field: Optional[str] = Field(None, max_length=100)
|
||||||
|
validation_rules: Optional[dict] = None
|
||||||
|
is_active: bool = True
|
||||||
|
|
||||||
|
|
||||||
|
class TrainingParameterUpdate(BaseModel):
|
||||||
|
name_de: Optional[str] = Field(None, min_length=1, max_length=100)
|
||||||
|
name_en: Optional[str] = Field(None, min_length=1, max_length=100)
|
||||||
|
category: Optional[str] = Field(None, max_length=50)
|
||||||
|
data_type: Optional[str] = Field(None, max_length=20)
|
||||||
|
unit: Optional[str] = Field(None, max_length=20)
|
||||||
|
description_de: Optional[str] = None
|
||||||
|
description_en: Optional[str] = None
|
||||||
|
source_field: Optional[str] = Field(None, max_length=100)
|
||||||
|
validation_rules: Optional[dict] = None
|
||||||
|
is_active: Optional[bool] = None
|
||||||
|
|
||||||
|
|
||||||
|
def _norm_key(key: str) -> str:
|
||||||
|
k = key.strip().lower()
|
||||||
|
if not KEY_PATTERN.match(k):
|
||||||
|
raise HTTPException(
|
||||||
|
400,
|
||||||
|
"Ungültiger key: nur Kleinbuchstaben, Ziffern, Unterstriche; muss mit Buchstabe beginnen.",
|
||||||
|
)
|
||||||
|
return k
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_category(cat: str) -> str:
|
||||||
|
c = cat.strip()
|
||||||
|
if c not in PARAM_CATEGORY:
|
||||||
|
raise HTTPException(400, f"category muss einer von {sorted(PARAM_CATEGORY)} sein")
|
||||||
|
return c
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_data_type(dt: str) -> str:
|
||||||
|
d = dt.strip().lower()
|
||||||
|
if d not in DATA_TYPES:
|
||||||
|
raise HTTPException(400, f"data_type muss einer von {sorted(DATA_TYPES)} sein")
|
||||||
|
return d
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("")
|
||||||
|
def admin_list_training_parameters(
|
||||||
|
include_inactive: bool = Query(False),
|
||||||
|
session: dict = Depends(require_admin),
|
||||||
|
):
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
if include_inactive:
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT * FROM training_parameters
|
||||||
|
ORDER BY category, key
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT * FROM training_parameters
|
||||||
|
WHERE is_active = true
|
||||||
|
ORDER BY category, key
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
return [r2d(r) for r in cur.fetchall()]
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("")
|
||||||
|
def admin_create_training_parameter(
|
||||||
|
body: TrainingParameterCreate,
|
||||||
|
session: dict = Depends(require_admin),
|
||||||
|
):
|
||||||
|
key = _norm_key(body.key)
|
||||||
|
cat = _validate_category(body.category)
|
||||||
|
dt = _validate_data_type(body.data_type)
|
||||||
|
rules = body.validation_rules if body.validation_rules is not None else {}
|
||||||
|
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
try:
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO training_parameters (
|
||||||
|
key, name_de, name_en, category, data_type, unit,
|
||||||
|
description_de, description_en, source_field, validation_rules, is_active
|
||||||
|
) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
|
||||||
|
RETURNING id
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
key,
|
||||||
|
body.name_de.strip(),
|
||||||
|
body.name_en.strip(),
|
||||||
|
cat,
|
||||||
|
dt,
|
||||||
|
body.unit.strip() if body.unit else None,
|
||||||
|
body.description_de,
|
||||||
|
body.description_en,
|
||||||
|
body.source_field.strip() if body.source_field else None,
|
||||||
|
Json(rules),
|
||||||
|
body.is_active,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
new_id = cur.fetchone()["id"]
|
||||||
|
conn.commit()
|
||||||
|
except pg_errors.UniqueViolation:
|
||||||
|
conn.rollback()
|
||||||
|
raise HTTPException(409, "Parameter-key existiert bereits") from None
|
||||||
|
return {"id": new_id, "key": key}
|
||||||
|
|
||||||
|
|
||||||
|
@router.put("/{param_id}")
|
||||||
|
def admin_update_training_parameter(
|
||||||
|
param_id: int,
|
||||||
|
body: TrainingParameterUpdate,
|
||||||
|
session: dict = Depends(require_admin),
|
||||||
|
):
|
||||||
|
cols: list[str] = []
|
||||||
|
vals: list[Any] = []
|
||||||
|
|
||||||
|
if body.name_de is not None:
|
||||||
|
cols.append("name_de = %s")
|
||||||
|
vals.append(body.name_de.strip())
|
||||||
|
if body.name_en is not None:
|
||||||
|
cols.append("name_en = %s")
|
||||||
|
vals.append(body.name_en.strip())
|
||||||
|
if body.category is not None:
|
||||||
|
cols.append("category = %s")
|
||||||
|
vals.append(_validate_category(body.category))
|
||||||
|
if body.data_type is not None:
|
||||||
|
cols.append("data_type = %s")
|
||||||
|
vals.append(_validate_data_type(body.data_type))
|
||||||
|
if body.unit is not None:
|
||||||
|
cols.append("unit = %s")
|
||||||
|
vals.append(body.unit.strip() or None)
|
||||||
|
if body.description_de is not None:
|
||||||
|
cols.append("description_de = %s")
|
||||||
|
vals.append(body.description_de)
|
||||||
|
if body.description_en is not None:
|
||||||
|
cols.append("description_en = %s")
|
||||||
|
vals.append(body.description_en)
|
||||||
|
if body.source_field is not None:
|
||||||
|
cols.append("source_field = %s")
|
||||||
|
vals.append(body.source_field.strip() or None)
|
||||||
|
if body.validation_rules is not None:
|
||||||
|
cols.append("validation_rules = %s")
|
||||||
|
vals.append(Json(body.validation_rules))
|
||||||
|
if body.is_active is not None:
|
||||||
|
cols.append("is_active = %s")
|
||||||
|
vals.append(body.is_active)
|
||||||
|
|
||||||
|
if not cols:
|
||||||
|
raise HTTPException(400, "Keine Felder zum Aktualisieren")
|
||||||
|
|
||||||
|
vals.append(param_id)
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cur.execute(
|
||||||
|
f"UPDATE training_parameters SET {', '.join(cols)} WHERE id = %s RETURNING id",
|
||||||
|
vals,
|
||||||
|
)
|
||||||
|
if not cur.fetchone():
|
||||||
|
raise HTTPException(404, "Parameter nicht gefunden")
|
||||||
|
conn.commit()
|
||||||
|
return {"ok": True, "id": param_id}
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/{param_id}")
|
||||||
|
def admin_deactivate_training_parameter(
|
||||||
|
param_id: int,
|
||||||
|
session: dict = Depends(require_admin),
|
||||||
|
):
|
||||||
|
"""Soft-delete: is_active = false (FK von session_metrics verhindert hartes Löschen)."""
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cur.execute(
|
||||||
|
"UPDATE training_parameters SET is_active = false WHERE id = %s RETURNING id",
|
||||||
|
(param_id,),
|
||||||
|
)
|
||||||
|
if not cur.fetchone():
|
||||||
|
raise HTTPException(404, "Parameter nicht gefunden")
|
||||||
|
conn.commit()
|
||||||
|
return {"ok": True, "id": param_id}
|
||||||
+56
-46
@@ -35,7 +35,8 @@ 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_protein_adequacy_data,
|
||||||
get_macro_consistency_data
|
get_macro_consistency_data,
|
||||||
|
get_energy_balance_data,
|
||||||
)
|
)
|
||||||
from data_layer.activity_metrics import (
|
from data_layer.activity_metrics import (
|
||||||
get_activity_summary_data,
|
get_activity_summary_data,
|
||||||
@@ -118,7 +119,7 @@ def get_weight_trend_chart(
|
|||||||
"""
|
"""
|
||||||
profile_id = session['profile_id']
|
profile_id = session['profile_id']
|
||||||
|
|
||||||
# Get structured data from data layer
|
# Get structured data from data layer (includes series — no second weight_log query)
|
||||||
trend_data = get_weight_trend_data(profile_id, days)
|
trend_data = get_weight_trend_data(profile_id, days)
|
||||||
|
|
||||||
# Early return if insufficient data
|
# Early return if insufficient data
|
||||||
@@ -136,22 +137,12 @@ def get_weight_trend_chart(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Get raw data points for chart
|
series = trend_data.get("series") or []
|
||||||
from db import get_db, get_cursor
|
labels = [
|
||||||
with get_db() as conn:
|
pt["date"].isoformat() if hasattr(pt["date"], "isoformat") else str(pt["date"])
|
||||||
cur = get_cursor(conn)
|
for pt in series
|
||||||
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
]
|
||||||
cur.execute(
|
values = [pt["weight"] for pt in series]
|
||||||
"""SELECT date, weight FROM weight_log
|
|
||||||
WHERE profile_id=%s AND date >= %s
|
|
||||||
ORDER BY date""",
|
|
||||||
(profile_id, cutoff)
|
|
||||||
)
|
|
||||||
rows = cur.fetchall()
|
|
||||||
|
|
||||||
# Format for Chart.js
|
|
||||||
labels = [row['date'].isoformat() for row in rows]
|
|
||||||
values = [float(row['weight']) for row in rows]
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"chart_type": "line",
|
"chart_type": "line",
|
||||||
@@ -346,17 +337,20 @@ def get_energy_balance_chart(
|
|||||||
"""
|
"""
|
||||||
profile_id = session['profile_id']
|
profile_id = session['profile_id']
|
||||||
|
|
||||||
|
balance_meta = get_energy_balance_data(profile_id, days)
|
||||||
|
|
||||||
from db import get_db, get_cursor
|
from db import get_db, get_cursor
|
||||||
with get_db() as conn:
|
with get_db() as conn:
|
||||||
cur = get_cursor(conn)
|
cur = get_cursor(conn)
|
||||||
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
||||||
|
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"""SELECT date, kcal
|
"""SELECT date, SUM(kcal)::float AS kcal
|
||||||
FROM nutrition_log
|
FROM nutrition_log
|
||||||
WHERE profile_id=%s AND date >= %s AND kcal IS NOT NULL
|
WHERE profile_id=%s AND date >= %s AND kcal IS NOT NULL
|
||||||
|
GROUP BY date
|
||||||
ORDER BY date""",
|
ORDER BY date""",
|
||||||
(profile_id, cutoff)
|
(profile_id, cutoff),
|
||||||
)
|
)
|
||||||
rows = cur.fetchall()
|
rows = cur.fetchall()
|
||||||
|
|
||||||
@@ -374,7 +368,21 @@ def get_energy_balance_chart(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Prepare data
|
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 = []
|
labels = []
|
||||||
daily_values = []
|
daily_values = []
|
||||||
avg_7d = []
|
avg_7d = []
|
||||||
@@ -384,23 +392,19 @@ def get_energy_balance_chart(
|
|||||||
labels.append(row['date'].isoformat())
|
labels.append(row['date'].isoformat())
|
||||||
daily_values.append(safe_float(row['kcal']))
|
daily_values.append(safe_float(row['kcal']))
|
||||||
|
|
||||||
# 7d rolling average
|
|
||||||
start_7d = max(0, i - 6)
|
start_7d = max(0, i - 6)
|
||||||
window_7d = [safe_float(rows[j]['kcal']) for j in range(start_7d, i + 1)]
|
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)
|
avg_7d.append(round(sum(window_7d) / len(window_7d), 1) if window_7d else None)
|
||||||
|
|
||||||
# 14d rolling average
|
|
||||||
start_14d = max(0, i - 13)
|
start_14d = max(0, i - 13)
|
||||||
window_14d = [safe_float(rows[j]['kcal']) for j in range(start_14d, i + 1)]
|
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_14d.append(round(sum(window_14d) / len(window_14d), 1) if window_14d else None)
|
||||||
|
|
||||||
# Calculate TDEE (estimated, should come from profile)
|
avg_intake = float(balance_meta.get("avg_intake") or (sum(daily_values) / len(daily_values) if daily_values else 0))
|
||||||
# TODO: Calculate from profile (weight, height, age, activity level)
|
energy_balance = float(balance_meta.get("energy_balance") or (avg_intake - estimated_tdee))
|
||||||
estimated_tdee = 2500.0
|
balance_status = balance_meta.get("status") or (
|
||||||
|
"deficit" if energy_balance < -200 else "surplus" if energy_balance > 200 else "maintenance"
|
||||||
# Calculate deficit/surplus
|
)
|
||||||
avg_intake = sum(daily_values) / len(daily_values) if daily_values else 0
|
|
||||||
energy_balance = avg_intake - estimated_tdee
|
|
||||||
|
|
||||||
datasets = [
|
datasets = [
|
||||||
{
|
{
|
||||||
@@ -443,8 +447,7 @@ def get_energy_balance_chart(
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
from data_layer.utils import calculate_confidence
|
confidence = balance_meta.get("confidence") or "low"
|
||||||
confidence = calculate_confidence(len(rows), days, "general")
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"chart_type": "line",
|
"chart_type": "line",
|
||||||
@@ -458,7 +461,7 @@ def get_energy_balance_chart(
|
|||||||
"avg_kcal": round(avg_intake, 1),
|
"avg_kcal": round(avg_intake, 1),
|
||||||
"estimated_tdee": estimated_tdee,
|
"estimated_tdee": estimated_tdee,
|
||||||
"energy_balance": round(energy_balance, 1),
|
"energy_balance": round(energy_balance, 1),
|
||||||
"balance_status": "deficit" if energy_balance < -200 else "surplus" if energy_balance > 200 else "maintenance",
|
"balance_status": balance_status,
|
||||||
"first_date": rows[0]['date'],
|
"first_date": rows[0]['date'],
|
||||||
"last_date": rows[-1]['date']
|
"last_date": rows[-1]['date']
|
||||||
})
|
})
|
||||||
@@ -587,11 +590,12 @@ def get_protein_adequacy_chart(
|
|||||||
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
cutoff = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
||||||
|
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"""SELECT date, protein_g
|
"""SELECT date, SUM(protein_g)::float AS protein_g
|
||||||
FROM nutrition_log
|
FROM nutrition_log
|
||||||
WHERE profile_id=%s AND date >= %s AND protein_g IS NOT NULL
|
WHERE profile_id=%s AND date >= %s AND protein_g IS NOT NULL
|
||||||
|
GROUP BY date
|
||||||
ORDER BY date""",
|
ORDER BY date""",
|
||||||
(profile_id, cutoff)
|
(profile_id, cutoff),
|
||||||
)
|
)
|
||||||
rows = cur.fetchall()
|
rows = cur.fetchall()
|
||||||
|
|
||||||
@@ -687,7 +691,6 @@ def get_protein_adequacy_chart(
|
|||||||
from data_layer.utils import calculate_confidence
|
from data_layer.utils import calculate_confidence
|
||||||
confidence = calculate_confidence(len(rows), days, "general")
|
confidence = calculate_confidence(len(rows), days, "general")
|
||||||
|
|
||||||
# Count days in target
|
|
||||||
days_in_target = sum(1 for v in daily_values if target_low <= v <= target_high)
|
days_in_target = sum(1 for v in daily_values if target_low <= v <= target_high)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -978,16 +981,23 @@ def get_nutrition_adherence_score(
|
|||||||
|
|
||||||
# Get nutrition data
|
# Get nutrition data
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"""SELECT COUNT(*) as cnt,
|
"""WITH daily AS (
|
||||||
AVG(kcal) as avg_kcal,
|
SELECT date,
|
||||||
STDDEV(kcal) as std_kcal,
|
COALESCE(SUM(kcal), 0)::float AS dk,
|
||||||
AVG(protein_g) as avg_protein,
|
COALESCE(SUM(protein_g), 0)::float AS dp,
|
||||||
AVG(carbs_g) as avg_carbs,
|
COALESCE(SUM(carbs_g), 0)::float AS dc,
|
||||||
AVG(fat_g) as avg_fat
|
COALESCE(SUM(fat_g), 0)::float AS df FROM nutrition_log
|
||||||
FROM nutrition_log
|
WHERE profile_id=%s AND date >= %s AND kcal IS NOT NULL
|
||||||
WHERE profile_id=%s AND date >= %s
|
GROUP BY date
|
||||||
AND kcal IS NOT NULL""",
|
)
|
||||||
(profile_id, cutoff)
|
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()
|
stats = cur.fetchone()
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ 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 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
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/csv", tags=["csv-import"])
|
router = APIRouter(prefix="/api/csv", tags=["csv-import"])
|
||||||
@@ -66,25 +67,30 @@ def _mapping_to_summary(m: dict) -> dict:
|
|||||||
def csv_modules(session: dict = Depends(require_auth)):
|
def csv_modules(session: dict = Depends(require_auth)):
|
||||||
"""Unterstützte Import-Module und Felddefinitionen."""
|
"""Unterstützte Import-Module und Felddefinitionen."""
|
||||||
out = []
|
out = []
|
||||||
for mid in list_modules():
|
with get_db() as conn:
|
||||||
d = get_module_definition(mid)
|
cur = get_cursor(conn)
|
||||||
if d:
|
for mid in list_modules():
|
||||||
fields_out = {}
|
d = get_module_definition(mid)
|
||||||
for fname, finfo in (d.get("fields") or {}).items():
|
if d:
|
||||||
fd = dict(finfo)
|
field_src = dict(d.get("fields") or {})
|
||||||
opts = source_unit_choices_for_field(mid, fname)
|
if mid == "activity":
|
||||||
if opts:
|
field_src = merge_activity_csv_module_fields(cur, field_src)
|
||||||
fd["source_unit_options"] = opts
|
fields_out = {}
|
||||||
fields_out[fname] = fd
|
for fname, finfo in field_src.items():
|
||||||
out.append(
|
fd = dict(finfo)
|
||||||
{
|
opts = source_unit_choices_for_field(mid, fname)
|
||||||
"id": mid,
|
if opts:
|
||||||
"table": d["table"],
|
fd["source_unit_options"] = opts
|
||||||
"fields": fields_out,
|
fields_out[fname] = fd
|
||||||
"import_mode": d.get("import_mode"),
|
out.append(
|
||||||
"import_row_processing_default": d.get("import_row_processing_default"),
|
{
|
||||||
}
|
"id": mid,
|
||||||
)
|
"table": d["table"],
|
||||||
|
"fields": fields_out,
|
||||||
|
"import_mode": d.get("import_mode"),
|
||||||
|
"import_row_processing_default": d.get("import_row_processing_default"),
|
||||||
|
}
|
||||||
|
)
|
||||||
return {"modules": out}
|
return {"modules": out}
|
||||||
|
|
||||||
|
|
||||||
@@ -257,6 +263,10 @@ async def analyze_csv(
|
|||||||
|
|
||||||
with get_db() as conn:
|
with get_db() as conn:
|
||||||
cur = get_cursor(conn)
|
cur = get_cursor(conn)
|
||||||
|
if module == "activity" and mod_def:
|
||||||
|
available_fields = merge_activity_csv_module_fields(
|
||||||
|
cur, dict(mod_def.get("fields") or {})
|
||||||
|
)
|
||||||
if module:
|
if module:
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ from auth import require_auth, check_feature_access, increment_feature_usage
|
|||||||
from routers.profiles import get_pid
|
from routers.profiles import get_pid
|
||||||
from feature_logger import log_feature_usage
|
from feature_logger import log_feature_usage
|
||||||
from caliper_composition import enrich_caliper_row_for_response, load_weight_rows
|
from caliper_composition import enrich_caliper_row_for_response, load_weight_rows
|
||||||
|
from data_layer.activity_session_metrics import enrich_sessions_with_metrics
|
||||||
|
from data_layer.utils import serialize_dates
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/export", tags=["export"])
|
router = APIRouter(prefix="/api/export", tags=["export"])
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -90,10 +92,23 @@ def export_csv(x_profile_id: Optional[str]=Header(default=None), session: dict=D
|
|||||||
for r in cur.fetchall():
|
for r in cur.fetchall():
|
||||||
writer.writerow(["Ernährung", r['date'], f"{float(r['kcal'])}kcal", f"Protein:{float(r['protein_g'])}g"])
|
writer.writerow(["Ernährung", r['date'], f"{float(r['kcal'])}kcal", f"Protein:{float(r['protein_g'])}g"])
|
||||||
|
|
||||||
# Activity
|
# Activity (Layer-1: gemergte session_metrics in Details)
|
||||||
cur.execute("SELECT date, activity_type, duration_min, kcal_active FROM activity_log WHERE profile_id=%s ORDER BY date", (pid,))
|
cur.execute(
|
||||||
for r in cur.fetchall():
|
"SELECT id, date, activity_type, duration_min, kcal_active FROM activity_log WHERE profile_id=%s ORDER BY date",
|
||||||
writer.writerow(["Training", r['date'], r['activity_type'], f"{float(r['duration_min'])}min {float(r['kcal_active'])}kcal"])
|
(pid,),
|
||||||
|
)
|
||||||
|
act_rows = [r2d(r) for r in cur.fetchall()]
|
||||||
|
enrich_sessions_with_metrics(cur, act_rows)
|
||||||
|
for r in act_rows:
|
||||||
|
base = f"{float(r['duration_min'])}min {float(r['kcal_active'])}kcal"
|
||||||
|
eav_parts = []
|
||||||
|
for m in r.get("session_metrics") or []:
|
||||||
|
k, v = m.get("key"), m.get("value")
|
||||||
|
if k is None or v is None:
|
||||||
|
continue
|
||||||
|
eav_parts.append(f"{k}={v}")
|
||||||
|
details = base + (" | " + "; ".join(eav_parts) if eav_parts else "")
|
||||||
|
writer.writerow(["Training", r["date"], r["activity_type"], details])
|
||||||
|
|
||||||
output.seek(0)
|
output.seek(0)
|
||||||
|
|
||||||
@@ -148,7 +163,9 @@ def export_json(x_profile_id: Optional[str]=Header(default=None), session: dict=
|
|||||||
data['nutrition'] = [r2d(r) for r in cur.fetchall()]
|
data['nutrition'] = [r2d(r) for r in cur.fetchall()]
|
||||||
|
|
||||||
cur.execute("SELECT * FROM activity_log WHERE profile_id=%s ORDER BY date", (pid,))
|
cur.execute("SELECT * FROM activity_log WHERE profile_id=%s ORDER BY date", (pid,))
|
||||||
data['activity'] = [r2d(r) for r in cur.fetchall()]
|
data["activity"] = [r2d(r) for r in cur.fetchall()]
|
||||||
|
enrich_sessions_with_metrics(cur, data["activity"])
|
||||||
|
data["activity"] = serialize_dates(data["activity"])
|
||||||
|
|
||||||
cur.execute("SELECT * FROM ai_insights WHERE profile_id=%s ORDER BY created DESC", (pid,))
|
cur.execute("SELECT * FROM ai_insights WHERE profile_id=%s ORDER BY created DESC", (pid,))
|
||||||
data['insights'] = [r2d(r) for r in cur.fetchall()]
|
data['insights'] = [r2d(r) for r in cur.fetchall()]
|
||||||
@@ -243,6 +260,9 @@ Dieser Export kann in Mitai Jinkendo unter
|
|||||||
Einstellungen → Import → "Mitai Backup importieren"
|
Einstellungen → Import → "Mitai Backup importieren"
|
||||||
wieder eingespielt werden.
|
wieder eingespielt werden.
|
||||||
|
|
||||||
|
activity.csv (optional): Spalte session_metrics_json (JSON-Array, Layer-1-merge)
|
||||||
|
wird beim Standard-Import ignoriert; für Vollständigkeit/externe Tools.
|
||||||
|
|
||||||
Format-Version 2 (ab v9b):
|
Format-Version 2 (ab v9b):
|
||||||
Alle CSV-Dateien sind UTF-8 mit BOM kodiert.
|
Alle CSV-Dateien sind UTF-8 mit BOM kodiert.
|
||||||
Trennzeichen: Semikolon (;)
|
Trennzeichen: Semikolon (;)
|
||||||
@@ -318,13 +338,41 @@ Datumsformat: YYYY-MM-DD
|
|||||||
r['fiber'] = None; r['note'] = ''
|
r['fiber'] = None; r['note'] = ''
|
||||||
write_csv(zf, "nutrition.csv", rows, ['id','date','meal_name','kcal','protein','fat','carbs','fiber','note','source','created'])
|
write_csv(zf, "nutrition.csv", rows, ['id','date','meal_name','kcal','protein','fat','carbs','fiber','note','source','created'])
|
||||||
|
|
||||||
cur.execute("SELECT id, date, activity_type, duration_min, kcal_active, hr_avg, hr_max, distance_km, notes, source, created FROM activity_log WHERE profile_id=%s ORDER BY date", (pid,))
|
cur.execute(
|
||||||
|
"SELECT id, date, activity_type, duration_min, kcal_active, hr_avg, hr_max, distance_km, notes, source, created FROM activity_log WHERE profile_id=%s ORDER BY date",
|
||||||
|
(pid,),
|
||||||
|
)
|
||||||
rows = [r2d(r) for r in cur.fetchall()]
|
rows = [r2d(r) for r in cur.fetchall()]
|
||||||
|
enrich_sessions_with_metrics(cur, rows)
|
||||||
for r in rows:
|
for r in rows:
|
||||||
r['name'] = r['activity_type']; r['type'] = r.pop('activity_type', None)
|
sm = r.pop("session_metrics", None) or []
|
||||||
r['kcal'] = r.pop('kcal_active', None); r['heart_rate_avg'] = r.pop('hr_avg', None)
|
r["session_metrics_json"] = json.dumps(sm, ensure_ascii=False, default=str)
|
||||||
r['heart_rate_max'] = r.pop('hr_max', None); r['note'] = r.pop('notes', None)
|
r["name"] = r["activity_type"]
|
||||||
write_csv(zf, "activity.csv", rows, ['id','date','name','type','duration_min','kcal','heart_rate_avg','heart_rate_max','distance_km','note','source','created'])
|
r["type"] = r.pop("activity_type", None)
|
||||||
|
r["kcal"] = r.pop("kcal_active", None)
|
||||||
|
r["heart_rate_avg"] = r.pop("hr_avg", None)
|
||||||
|
r["heart_rate_max"] = r.pop("hr_max", None)
|
||||||
|
r["note"] = r.pop("notes", None)
|
||||||
|
write_csv(
|
||||||
|
zf,
|
||||||
|
"activity.csv",
|
||||||
|
rows,
|
||||||
|
[
|
||||||
|
"id",
|
||||||
|
"date",
|
||||||
|
"name",
|
||||||
|
"type",
|
||||||
|
"duration_min",
|
||||||
|
"kcal",
|
||||||
|
"heart_rate_avg",
|
||||||
|
"heart_rate_max",
|
||||||
|
"distance_km",
|
||||||
|
"note",
|
||||||
|
"source",
|
||||||
|
"created",
|
||||||
|
"session_metrics_json",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
# 8. insights/ai_insights.json
|
# 8. insights/ai_insights.json
|
||||||
cur.execute("SELECT id, scope, content, created FROM ai_insights WHERE profile_id=%s ORDER BY created DESC", (pid,))
|
cur.execute("SELECT id, scope, content, created FROM ai_insights WHERE profile_id=%s ORDER BY created DESC", (pid,))
|
||||||
|
|||||||
@@ -699,6 +699,12 @@ def get_goals_grouped(session: dict = Depends(require_auth)):
|
|||||||
grouped[cat] = []
|
grouped[cat] = []
|
||||||
|
|
||||||
goal_dict = r2d(goal)
|
goal_dict = r2d(goal)
|
||||||
|
# Keep in sync with GET /goals/list: derive current_value & progress from live data
|
||||||
|
try:
|
||||||
|
_update_goal_progress(conn, pid, goal_dict)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[ERROR] Failed to update progress for goal {goal_dict.get('id')}: {e}")
|
||||||
|
|
||||||
goal_dict['focus_contributions'] = focus_map.get(goal['id'], [])
|
goal_dict['focus_contributions'] = focus_map.get(goal['id'], [])
|
||||||
grouped[cat].append(goal_dict)
|
grouped[cat].append(goal_dict)
|
||||||
|
|
||||||
|
|||||||
+241
-31
@@ -12,15 +12,16 @@ from fastapi import APIRouter, Depends, HTTPException, Query, Header
|
|||||||
from fastapi.responses import StreamingResponse
|
from fastapi.responses import StreamingResponse
|
||||||
|
|
||||||
from db import get_db, get_cursor, r2d
|
from db import get_db, get_cursor, r2d
|
||||||
from auth import require_auth, require_admin
|
from auth import require_auth, require_admin, require_auth_flexible
|
||||||
from models import (
|
from models import (
|
||||||
PromptCreate, PromptUpdate, PromptGenerateRequest,
|
PromptCreate, PromptUpdate, PromptGenerateRequest,
|
||||||
PipelineConfigCreate, PipelineConfigUpdate
|
PipelineConfigCreate, PipelineConfigUpdate
|
||||||
)
|
)
|
||||||
|
from prompt_executor import resolve_placeholders as resolve_prompt_placeholders
|
||||||
from placeholder_resolver import (
|
from placeholder_resolver import (
|
||||||
resolve_placeholders,
|
|
||||||
get_unknown_placeholders,
|
get_unknown_placeholders,
|
||||||
get_placeholder_example_values,
|
get_placeholder_example_values,
|
||||||
|
format_value_with_d_modifier,
|
||||||
get_available_placeholders,
|
get_available_placeholders,
|
||||||
get_placeholder_catalog
|
get_placeholder_catalog
|
||||||
)
|
)
|
||||||
@@ -431,7 +432,13 @@ def preview_prompt(data: dict, session: dict=Depends(require_auth)):
|
|||||||
template = data.get('template', '')
|
template = data.get('template', '')
|
||||||
profile_id = session['profile_id']
|
profile_id = session['profile_id']
|
||||||
|
|
||||||
resolved = resolve_placeholders(template, profile_id)
|
catalog = get_placeholder_catalog(profile_id)
|
||||||
|
processed = get_placeholder_example_values(profile_id)
|
||||||
|
variables = {
|
||||||
|
k.replace('{{', '').replace('}}', ''): v
|
||||||
|
for k, v in processed.items()
|
||||||
|
}
|
||||||
|
resolved = resolve_prompt_placeholders(template, variables, None, catalog)
|
||||||
unknown = get_unknown_placeholders(template)
|
unknown = get_unknown_placeholders(template)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -457,8 +464,8 @@ def export_placeholder_values(session: dict = Depends(require_auth)):
|
|||||||
"""
|
"""
|
||||||
Export all available placeholders with their current resolved values.
|
Export all available placeholders with their current resolved values.
|
||||||
|
|
||||||
Returns JSON export suitable for download with all placeholders
|
Pro Zeile: value = {{key}}, example = Vorschau {{key|d}} (Wert — description),
|
||||||
resolved for the current user's profile.
|
ai_caption = Text für {{key|x}} (Erklärung ohne Wert). JSON für das aktive Profil.
|
||||||
"""
|
"""
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
profile_id = session['profile_id']
|
profile_id = session['profile_id']
|
||||||
@@ -486,12 +493,16 @@ def export_placeholder_values(session: dict = Depends(require_auth)):
|
|||||||
export_data['placeholders_by_category'][category] = []
|
export_data['placeholders_by_category'][category] = []
|
||||||
for item in items:
|
for item in items:
|
||||||
key = item['key'].replace('{{', '').replace('}}', '')
|
key = item['key'].replace('{{', '').replace('}}', '')
|
||||||
export_data['placeholders_by_category'][category].append({
|
raw_val = cleaned_values.get(key, 'nicht verfügbar')
|
||||||
|
row = {
|
||||||
'key': item['key'],
|
'key': item['key'],
|
||||||
'description': item['description'],
|
'description': item['description'],
|
||||||
'value': cleaned_values.get(key, 'nicht verfügbar'),
|
'value': raw_val,
|
||||||
'example': item.get('example')
|
'example': format_value_with_d_modifier(str(raw_val), item),
|
||||||
})
|
}
|
||||||
|
if item.get('ai_caption'):
|
||||||
|
row['ai_caption'] = item['ai_caption']
|
||||||
|
export_data['placeholders_by_category'][category].append(row)
|
||||||
|
|
||||||
# Also include flat list for easy access
|
# Also include flat list for easy access
|
||||||
export_data['all_placeholders'] = cleaned_values
|
export_data['all_placeholders'] = cleaned_values
|
||||||
@@ -583,8 +594,14 @@ def export_placeholder_values_extended(
|
|||||||
if 'value_raw' not in metadata.unresolved_fields:
|
if 'value_raw' not in metadata.unresolved_fields:
|
||||||
metadata.unresolved_fields.append('value_raw')
|
metadata.unresolved_fields.append('value_raw')
|
||||||
|
|
||||||
# Check availability
|
# Check availability (Resolver liefert oft „nicht verfügbar — <Grund>“)
|
||||||
if value in ['nicht verfügbar', 'nicht genug Daten', '[Fehler:', '[Nicht']:
|
sv = str(value)
|
||||||
|
if (
|
||||||
|
sv in ['nicht verfügbar', 'nicht genug Daten']
|
||||||
|
or sv.startswith('nicht verfügbar —')
|
||||||
|
or sv.startswith('[Fehler:')
|
||||||
|
or sv.startswith('[Nicht')
|
||||||
|
):
|
||||||
metadata.available = False
|
metadata.available = False
|
||||||
metadata.missing_reason = value
|
metadata.missing_reason = value
|
||||||
else:
|
else:
|
||||||
@@ -653,11 +670,12 @@ def export_placeholder_values_extended(
|
|||||||
export_data['legacy']['placeholders_by_category'][category] = []
|
export_data['legacy']['placeholders_by_category'][category] = []
|
||||||
for item in items:
|
for item in items:
|
||||||
key = item['key'].replace('{{', '').replace('}}', '')
|
key = item['key'].replace('{{', '').replace('}}', '')
|
||||||
|
raw_val = cleaned_values.get(key, 'nicht verfügbar')
|
||||||
export_data['legacy']['placeholders_by_category'][category].append({
|
export_data['legacy']['placeholders_by_category'][category].append({
|
||||||
'key': item['key'],
|
'key': item['key'],
|
||||||
'description': item['description'],
|
'description': item['description'],
|
||||||
'value': cleaned_values.get(key, 'nicht verfügbar'),
|
'value': raw_val,
|
||||||
'example': item.get('example')
|
'example': format_value_with_d_modifier(str(raw_val), item),
|
||||||
})
|
})
|
||||||
|
|
||||||
# Fill metadata flat
|
# Fill metadata flat
|
||||||
@@ -919,27 +937,60 @@ def export_placeholder_catalog_zip(
|
|||||||
|
|
||||||
# ── KI-Assisted Prompt Engineering ───────────────────────────────────────────
|
# ── KI-Assisted Prompt Engineering ───────────────────────────────────────────
|
||||||
|
|
||||||
async def call_openrouter(prompt: str, max_tokens: int = 1500) -> str:
|
async def call_openrouter(prompt: str, max_tokens: int = 4096) -> str:
|
||||||
"""Call OpenRouter API to get AI response."""
|
"""Call OpenRouter API to get AI response."""
|
||||||
if not OPENROUTER_KEY:
|
if not OPENROUTER_KEY:
|
||||||
raise HTTPException(status_code=500, detail="OpenRouter API key not configured")
|
raise HTTPException(status_code=500, detail="OpenRouter API key not configured")
|
||||||
|
|
||||||
async with httpx.AsyncClient() as client:
|
try:
|
||||||
resp = await client.post(
|
async with httpx.AsyncClient() as client:
|
||||||
"https://openrouter.ai/api/v1/chat/completions",
|
resp = await client.post(
|
||||||
headers={"Authorization": f"Bearer {OPENROUTER_KEY}"},
|
"https://openrouter.ai/api/v1/chat/completions",
|
||||||
json={
|
headers={"Authorization": f"Bearer {OPENROUTER_KEY}"},
|
||||||
"model": OPENROUTER_MODEL,
|
json={
|
||||||
"messages": [{"role": "user", "content": prompt}],
|
"model": OPENROUTER_MODEL,
|
||||||
"max_tokens": max_tokens
|
"messages": [{"role": "user", "content": prompt}],
|
||||||
},
|
"max_tokens": max_tokens
|
||||||
timeout=60.0
|
},
|
||||||
|
timeout=120.0
|
||||||
|
)
|
||||||
|
except httpx.TimeoutException as e:
|
||||||
|
raise HTTPException(status_code=504, detail=f"OpenRouter-Zeitüberschreitung: {e}") from e
|
||||||
|
except httpx.RequestError as e:
|
||||||
|
raise HTTPException(status_code=502, detail=f"OpenRouter-Verbindungsfehler: {e}") from e
|
||||||
|
|
||||||
|
if resp.status_code != 200:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=resp.status_code,
|
||||||
|
detail=f"OpenRouter API error ({resp.status_code}): {resp.text[:2000]}",
|
||||||
)
|
)
|
||||||
|
|
||||||
if resp.status_code != 200:
|
try:
|
||||||
raise HTTPException(status_code=resp.status_code, detail=f"OpenRouter API error: {resp.text}")
|
data = resp.json()
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=502,
|
||||||
|
detail=f"OpenRouter lieferte kein JSON: {resp.text[:800]}",
|
||||||
|
)
|
||||||
|
|
||||||
return resp.json()['choices'][0]['message']['content'].strip()
|
choices = data.get("choices") or []
|
||||||
|
if not choices:
|
||||||
|
err_snip = json.dumps(data.get("error") or data, ensure_ascii=False)[:1200]
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=502,
|
||||||
|
detail=f"OpenRouter ohne choices in der Antwort: {err_snip}",
|
||||||
|
)
|
||||||
|
|
||||||
|
message = choices[0].get("message") or {}
|
||||||
|
content = message.get("content")
|
||||||
|
if content is None:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=502,
|
||||||
|
detail="OpenRouter-Antwort ohne message.content (z. B. Tool-Calls oder abweichendes Schema).",
|
||||||
|
)
|
||||||
|
if not isinstance(content, str):
|
||||||
|
content = str(content)
|
||||||
|
return content.strip()
|
||||||
|
|
||||||
|
|
||||||
def collect_example_data(profile_id: str, data_categories: list[str]) -> dict:
|
def collect_example_data(profile_id: str, data_categories: list[str]) -> dict:
|
||||||
@@ -1394,13 +1445,164 @@ from prompt_executor import execute_prompt_with_data
|
|||||||
from models import UnifiedPromptCreate, UnifiedPromptUpdate
|
from models import UnifiedPromptCreate, UnifiedPromptUpdate
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/execute-stream")
|
||||||
|
async def execute_unified_prompt_stream(
|
||||||
|
prompt_slug: str = Query(..., description="Slug of prompt to execute"),
|
||||||
|
debug: bool = Query(False, description="Include debug information (node_states, etc.)"),
|
||||||
|
save: bool = Query(False, description="Save result to ai_insights"),
|
||||||
|
session: dict = Depends(require_auth_flexible)
|
||||||
|
):
|
||||||
|
print("[EXECUTE_STREAM] Endpoint function called!") # DEBUG
|
||||||
|
"""
|
||||||
|
Execute a unified prompt with Server-Sent Events (SSE) streaming.
|
||||||
|
|
||||||
|
Returns live progress updates during workflow execution:
|
||||||
|
- execution_started: Workflow has begun
|
||||||
|
- node_complete: Each node completes
|
||||||
|
- execution_complete: Final result ready
|
||||||
|
- execution_failed: Error occurred
|
||||||
|
|
||||||
|
Use this endpoint for long-running workflows (>30s) to avoid gateway timeouts.
|
||||||
|
"""
|
||||||
|
print(f"[EXECUTE_STREAM] session={repr(session)}") # DEBUG
|
||||||
|
profile_id = session['profile_id']
|
||||||
|
|
||||||
|
# Use default modules/timeframes (SSE doesn't support complex params)
|
||||||
|
modules = {
|
||||||
|
'körper': True,
|
||||||
|
'ernährung': True,
|
||||||
|
'training': True,
|
||||||
|
'schlaf': True,
|
||||||
|
'vitalwerte': True
|
||||||
|
}
|
||||||
|
|
||||||
|
timeframes = {
|
||||||
|
'körper': 30,
|
||||||
|
'ernährung': 30,
|
||||||
|
'training': 14,
|
||||||
|
'schlaf': 14,
|
||||||
|
'vitalwerte': 7
|
||||||
|
}
|
||||||
|
|
||||||
|
# Wrapper function for OpenRouter calls
|
||||||
|
async def workflow_llm_call(prompt: str, model: str = None) -> str:
|
||||||
|
return await call_openrouter(prompt)
|
||||||
|
|
||||||
|
# SSE Event Generator
|
||||||
|
async def event_stream():
|
||||||
|
"""Generate Server-Sent Events during workflow execution."""
|
||||||
|
import asyncio
|
||||||
|
from asyncio import Queue
|
||||||
|
|
||||||
|
# Event queue for progress updates
|
||||||
|
event_queue = Queue()
|
||||||
|
|
||||||
|
# Flag to track execution completion
|
||||||
|
execution_complete = False
|
||||||
|
|
||||||
|
# Define progress callback for streaming updates
|
||||||
|
async def progress_callback(event_type: str, data: dict):
|
||||||
|
"""Queue SSE event for streaming to client."""
|
||||||
|
event_data = {
|
||||||
|
"type": event_type,
|
||||||
|
**data
|
||||||
|
}
|
||||||
|
await event_queue.put(event_data)
|
||||||
|
|
||||||
|
# Start workflow execution in background task
|
||||||
|
async def execute_workflow_async():
|
||||||
|
nonlocal execution_complete
|
||||||
|
try:
|
||||||
|
# Execute workflow with progress callbacks
|
||||||
|
result = await execute_prompt_with_data(
|
||||||
|
prompt_slug=prompt_slug,
|
||||||
|
profile_id=profile_id,
|
||||||
|
modules=modules,
|
||||||
|
timeframes=timeframes,
|
||||||
|
openrouter_call_func=workflow_llm_call,
|
||||||
|
enable_debug=debug or save,
|
||||||
|
progress_callback=progress_callback
|
||||||
|
)
|
||||||
|
|
||||||
|
# Save to ai_insights if requested (same logic as /execute)
|
||||||
|
if save:
|
||||||
|
if result['type'] == 'pipeline':
|
||||||
|
final_output = result.get('output', {})
|
||||||
|
if isinstance(final_output, dict) and len(final_output) == 1:
|
||||||
|
content = list(final_output.values())[0]
|
||||||
|
else:
|
||||||
|
content = json.dumps(final_output, ensure_ascii=False)
|
||||||
|
elif result['type'] == 'workflow':
|
||||||
|
content = _workflow_user_facing_content(result.get('aggregated_result'))
|
||||||
|
else:
|
||||||
|
content = result.get('output', '')
|
||||||
|
if isinstance(content, dict):
|
||||||
|
content = json.dumps(content, ensure_ascii=False)
|
||||||
|
|
||||||
|
# Save to database (minimal metadata for now)
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cur.execute(
|
||||||
|
"""INSERT INTO ai_insights (profile_id, scope, content, metadata, created)
|
||||||
|
VALUES (%s, %s, %s, %s, CURRENT_TIMESTAMP)""",
|
||||||
|
(profile_id, prompt_slug, content, json.dumps({"prompt_type": result['type']}))
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
# Queue error event
|
||||||
|
await event_queue.put({
|
||||||
|
"type": "execution_failed",
|
||||||
|
"error": str(e)
|
||||||
|
})
|
||||||
|
finally:
|
||||||
|
execution_complete = True
|
||||||
|
|
||||||
|
# Start workflow execution in background
|
||||||
|
import asyncio
|
||||||
|
execution_task = asyncio.create_task(execute_workflow_async())
|
||||||
|
|
||||||
|
# Stream events from queue
|
||||||
|
try:
|
||||||
|
while not execution_complete or not event_queue.empty():
|
||||||
|
try:
|
||||||
|
# Wait for event with timeout
|
||||||
|
event = await asyncio.wait_for(event_queue.get(), timeout=0.5)
|
||||||
|
yield f"data: {json.dumps(event, ensure_ascii=False)}\n\n"
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
# Send keepalive ping
|
||||||
|
yield f": keepalive\n\n"
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Wait for execution task to complete
|
||||||
|
await execution_task
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
# Send final error event
|
||||||
|
error_event = {
|
||||||
|
"type": "execution_failed",
|
||||||
|
"error": str(e)
|
||||||
|
}
|
||||||
|
yield f"data: {json.dumps(error_event, ensure_ascii=False)}\n\n"
|
||||||
|
|
||||||
|
return StreamingResponse(
|
||||||
|
event_stream(),
|
||||||
|
media_type="text/event-stream",
|
||||||
|
headers={
|
||||||
|
"Cache-Control": "no-cache",
|
||||||
|
"Connection": "keep-alive",
|
||||||
|
"X-Accel-Buffering": "no" # Disable nginx buffering
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.post("/execute")
|
@router.post("/execute")
|
||||||
async def execute_unified_prompt(
|
async def execute_unified_prompt(
|
||||||
prompt_slug: str,
|
prompt_slug: str = Query(..., description="Slug of prompt to execute"),
|
||||||
modules: Optional[dict] = None,
|
modules: Optional[dict] = None,
|
||||||
timeframes: Optional[dict] = None,
|
timeframes: Optional[dict] = None,
|
||||||
debug: bool = False,
|
debug: bool = Query(False, description="Include debug information (node_states, etc.)"),
|
||||||
save: bool = False,
|
save: bool = Query(False, description="Save result to ai_insights"),
|
||||||
session: dict = Depends(require_auth)
|
session: dict = Depends(require_auth)
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
@@ -1437,6 +1639,14 @@ async def execute_unified_prompt(
|
|||||||
'vitalwerte': 7
|
'vitalwerte': 7
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Wrapper function to match workflow_executor's expected signature: (prompt, model) -> str
|
||||||
|
# workflow_executor calls: openrouter_call_func(prompt, "anthropic/claude-sonnet-4")
|
||||||
|
# but call_openrouter expects: call_openrouter(prompt, max_tokens=4096)
|
||||||
|
async def workflow_llm_call(prompt: str, model: str = None) -> str:
|
||||||
|
# Ignore model parameter (already set in OPENROUTER_MODEL env var)
|
||||||
|
# Use default max_tokens=4096 from call_openrouter
|
||||||
|
return await call_openrouter(prompt)
|
||||||
|
|
||||||
# Execute with prompt_executor
|
# Execute with prompt_executor
|
||||||
# Always enable debug when saving to collect metadata for value table
|
# Always enable debug when saving to collect metadata for value table
|
||||||
result = await execute_prompt_with_data(
|
result = await execute_prompt_with_data(
|
||||||
@@ -1444,7 +1654,7 @@ async def execute_unified_prompt(
|
|||||||
profile_id=profile_id,
|
profile_id=profile_id,
|
||||||
modules=modules,
|
modules=modules,
|
||||||
timeframes=timeframes,
|
timeframes=timeframes,
|
||||||
openrouter_call_func=call_openrouter,
|
openrouter_call_func=workflow_llm_call, # Use wrapper with correct signature
|
||||||
enable_debug=debug or save # Enable debug if saving for metadata collection
|
enable_debug=debug or save # Enable debug if saving for metadata collection
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -86,11 +86,19 @@ async def execute_workflow_endpoint(
|
|||||||
"type": "workflow"
|
"type": "workflow"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Wrapper function to match workflow_executor's expected signature: (prompt, model) -> str
|
||||||
|
# workflow_executor calls: openrouter_call_func(prompt, "anthropic/claude-sonnet-4")
|
||||||
|
# but call_openrouter expects: call_openrouter(prompt, max_tokens=4096)
|
||||||
|
async def workflow_llm_call(prompt: str, model: str) -> str:
|
||||||
|
# Ignore model parameter (already set in OPENROUTER_MODEL env var)
|
||||||
|
# Use default max_tokens=4096 from call_openrouter
|
||||||
|
return await call_openrouter(prompt)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
result = await execute_workflow_prompt(
|
result = await execute_workflow_prompt(
|
||||||
prompt=workflow_prompt,
|
prompt=workflow_prompt,
|
||||||
variables=variables,
|
variables=variables,
|
||||||
openrouter_call_func=call_openrouter,
|
openrouter_call_func=workflow_llm_call, # Use wrapper with correct signature
|
||||||
enable_debug=request.enable_debug
|
enable_debug=request.enable_debug
|
||||||
)
|
)
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -0,0 +1,179 @@
|
|||||||
|
"""
|
||||||
|
Diagnose: Was liegt in activity_session_metrics (EAV) vs. activity_log?
|
||||||
|
|
||||||
|
Ausführung (mit gesetzten DB_*-Variablen wie die App, z. B. aus .env):
|
||||||
|
|
||||||
|
cd backend
|
||||||
|
python scripts/inspect_activity_eav.py
|
||||||
|
|
||||||
|
Lokal ohne Docker-Hostname: z. B. ``set DB_HOST=127.0.0.1`` (Windows) / ``export DB_HOST=127.0.0.1``,
|
||||||
|
Port/User/Pass wie in der laufenden Postgres-Instanz.
|
||||||
|
|
||||||
|
Im Backend-Container (Compose-Service meist ``backend``, Arbeitsverzeichnis ``/app``):
|
||||||
|
|
||||||
|
docker compose exec backend python /app/scripts/inspect_activity_eav.py
|
||||||
|
|
||||||
|
Optional:
|
||||||
|
python scripts/inspect_activity_eav.py --limit 30
|
||||||
|
python scripts/inspect_activity_eav.py --profile <uuid>
|
||||||
|
python scripts/inspect_activity_eav.py --activity <activity_log uuid>
|
||||||
|
|
||||||
|
Keine Schreibzugriffe — nur SELECT.
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
# backend/ als Import-Root
|
||||||
|
_BACKEND_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
||||||
|
if _BACKEND_ROOT not in sys.path:
|
||||||
|
sys.path.insert(0, _BACKEND_ROOT)
|
||||||
|
|
||||||
|
|
||||||
|
def _val_row(r: dict) -> str | None:
|
||||||
|
dt = r.get("data_type")
|
||||||
|
if dt == "integer":
|
||||||
|
v = r.get("value_int")
|
||||||
|
return str(v) if v is not None else None
|
||||||
|
if dt == "float":
|
||||||
|
v = r.get("value_num")
|
||||||
|
return str(v) if v is not None else None
|
||||||
|
if dt == "string":
|
||||||
|
v = r.get("value_text")
|
||||||
|
return repr(v) if v is not None else None
|
||||||
|
if dt == "boolean":
|
||||||
|
v = r.get("value_bool")
|
||||||
|
return str(v) if v is not None else None
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
parser = argparse.ArgumentParser(description="EAV activity_session_metrics inspizieren")
|
||||||
|
parser.add_argument("--limit", type=int, default=40, help="Zeilen Report A/B")
|
||||||
|
parser.add_argument("--profile", type=str, default=None, help="profile_id filtern")
|
||||||
|
parser.add_argument("--activity", type=str, default=None, help="activity_log.id (einzelne Session)")
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
from db import get_db, get_cursor
|
||||||
|
|
||||||
|
if args.activity:
|
||||||
|
with get_db() as conn:
|
||||||
|
with get_cursor(conn) as cur:
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT al.id, al.profile_id, al.date, al.start_time, al.source,
|
||||||
|
al.training_category, al.training_type_id, al.activity_type,
|
||||||
|
al.duration_min, al.kcal_active, al.hr_avg, al.hr_max, al.distance_km
|
||||||
|
FROM activity_log al
|
||||||
|
WHERE al.id = %s::uuid
|
||||||
|
""",
|
||||||
|
(args.activity,),
|
||||||
|
)
|
||||||
|
h = cur.fetchone()
|
||||||
|
if not h:
|
||||||
|
print("activity_log: keine Zeile für diese id")
|
||||||
|
return
|
||||||
|
print("=== activity_log (Kopfzeile) ===")
|
||||||
|
for k, v in dict(h).items():
|
||||||
|
print(f" {k}: {v}")
|
||||||
|
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT m.id AS metric_id, tp.key, tp.data_type, tp.source_field,
|
||||||
|
m.value_num, m.value_int, m.value_text, m.value_bool, m.updated_at
|
||||||
|
FROM activity_session_metrics m
|
||||||
|
JOIN training_parameters tp ON tp.id = m.training_parameter_id
|
||||||
|
WHERE m.activity_log_id = %s::uuid
|
||||||
|
ORDER BY tp.key
|
||||||
|
""",
|
||||||
|
(args.activity,),
|
||||||
|
)
|
||||||
|
rows = cur.fetchall()
|
||||||
|
print(f"\n=== activity_session_metrics ({len(rows)} Zeilen) ===")
|
||||||
|
for r in rows:
|
||||||
|
d = dict(r)
|
||||||
|
print(
|
||||||
|
f" {d['key']} ({d['data_type']}) "
|
||||||
|
f"value={_val_row(d)!r} source_field={d.get('source_field')!r} "
|
||||||
|
f"updated_at={d.get('updated_at')}"
|
||||||
|
)
|
||||||
|
if not rows:
|
||||||
|
print(" (keine EAV-Zeilen)")
|
||||||
|
return
|
||||||
|
|
||||||
|
prof_filter = ""
|
||||||
|
if args.profile:
|
||||||
|
prof_filter = " AND al.profile_id = %s::uuid "
|
||||||
|
params_a: tuple = (args.profile, args.limit) if args.profile else (args.limit,)
|
||||||
|
params_b: tuple = (args.profile, args.limit) if args.profile else (args.limit,)
|
||||||
|
|
||||||
|
q_recent_eav = f"""
|
||||||
|
SELECT
|
||||||
|
al.id AS activity_id,
|
||||||
|
al.profile_id,
|
||||||
|
al.date,
|
||||||
|
al.start_time,
|
||||||
|
al.source,
|
||||||
|
al.training_type_id,
|
||||||
|
al.training_category,
|
||||||
|
tp.key AS parameter_key,
|
||||||
|
tp.data_type,
|
||||||
|
tp.source_field AS tp_source_field,
|
||||||
|
m.value_num,
|
||||||
|
m.value_int,
|
||||||
|
m.value_text,
|
||||||
|
m.value_bool,
|
||||||
|
m.updated_at
|
||||||
|
FROM activity_session_metrics m
|
||||||
|
JOIN activity_log al ON al.id = m.activity_log_id
|
||||||
|
JOIN training_parameters tp ON tp.id = m.training_parameter_id
|
||||||
|
WHERE 1=1 {prof_filter}
|
||||||
|
ORDER BY m.updated_at DESC NULLS LAST, al.date DESC, al.start_time DESC
|
||||||
|
LIMIT %s
|
||||||
|
"""
|
||||||
|
|
||||||
|
q_csv_no_eav = f"""
|
||||||
|
SELECT
|
||||||
|
al.id AS activity_id,
|
||||||
|
al.profile_id,
|
||||||
|
al.date,
|
||||||
|
al.start_time,
|
||||||
|
al.source,
|
||||||
|
al.training_type_id,
|
||||||
|
al.training_category,
|
||||||
|
(SELECT COUNT(*) FROM activity_session_metrics m WHERE m.activity_log_id = al.id) AS eav_count
|
||||||
|
FROM activity_log al
|
||||||
|
WHERE al.source = 'csv' {prof_filter}
|
||||||
|
ORDER BY al.date DESC, al.start_time DESC
|
||||||
|
LIMIT %s
|
||||||
|
"""
|
||||||
|
|
||||||
|
with get_db() as conn:
|
||||||
|
with get_cursor(conn) as cur:
|
||||||
|
print("=== A) Neueste EAV-Zeilen (join activity_log + training_parameters) ===\n")
|
||||||
|
cur.execute(q_recent_eav, params_a)
|
||||||
|
for r in cur.fetchall():
|
||||||
|
d = dict(r)
|
||||||
|
v = _val_row(d)
|
||||||
|
print(
|
||||||
|
f"{d['date']} {d['start_time']} | {d['activity_id']} | src={d['source']!r} | "
|
||||||
|
f"type={d['training_type_id']} cat={d['training_category']!r} | "
|
||||||
|
f"{d['parameter_key']}={v!r} (tp.source_field={d.get('tp_source_field')!r})"
|
||||||
|
)
|
||||||
|
|
||||||
|
print("\n=== B) Neueste CSV-importierte Sessions: EAV-Anzahl pro Zeile ===\n")
|
||||||
|
cur.execute(q_csv_no_eav, params_b)
|
||||||
|
for r in cur.fetchall():
|
||||||
|
d = dict(r)
|
||||||
|
print(
|
||||||
|
f"{d['date']} {d['start_time']} | {d['activity_id']} | "
|
||||||
|
f"type={d['training_type_id']} | eav_count={d['eav_count']}"
|
||||||
|
)
|
||||||
|
|
||||||
|
print("\nFertig. Für eine Session im Detail: --activity <uuid>")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Test export-all endpoint"""
|
||||||
|
import sys
|
||||||
|
sys.path.insert(0, '/app')
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
|
from db import get_db, get_cursor, r2d
|
||||||
|
|
||||||
|
print("Testing export-all logic...")
|
||||||
|
|
||||||
|
with get_db() as conn:
|
||||||
|
cur = get_cursor(conn)
|
||||||
|
cur.execute("SELECT * FROM ai_prompts ORDER BY sort_order, slug")
|
||||||
|
prompts = [r2d(row) for row in cur.fetchall()]
|
||||||
|
|
||||||
|
print(f"Found {len(prompts)} prompts")
|
||||||
|
|
||||||
|
# Convert to export format (clean up DB-specific fields)
|
||||||
|
export_data = []
|
||||||
|
for idx, p in enumerate(prompts):
|
||||||
|
print(f"\nProcessing prompt {idx+1}: {p.get('slug')}")
|
||||||
|
try:
|
||||||
|
export_item = {
|
||||||
|
'slug': p['slug'],
|
||||||
|
'name': p['name'],
|
||||||
|
'display_name': p.get('display_name'),
|
||||||
|
'description': p.get('description'),
|
||||||
|
'type': p.get('type', 'pipeline'),
|
||||||
|
'category': p.get('category', 'ganzheitlich'),
|
||||||
|
'template': p.get('template'),
|
||||||
|
'stages': p.get('stages'),
|
||||||
|
'output_format': p.get('output_format', 'text'),
|
||||||
|
'output_schema': p.get('output_schema'),
|
||||||
|
'question_augmentations': p.get('question_augmentations'),
|
||||||
|
'graph_data': p.get('graph_data'),
|
||||||
|
'active': p.get('active', True),
|
||||||
|
'sort_order': p.get('sort_order', 0)
|
||||||
|
}
|
||||||
|
export_data.append(export_item)
|
||||||
|
print(f" ✓ OK")
|
||||||
|
except Exception as e:
|
||||||
|
print(f" ✗ ERROR: {type(e).__name__}: {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
break
|
||||||
|
|
||||||
|
print(f"\n\nSuccessfully processed {len(export_data)} prompts")
|
||||||
|
|
||||||
|
# Try to create the response dict
|
||||||
|
try:
|
||||||
|
result = {
|
||||||
|
'export_date': datetime.now().isoformat(),
|
||||||
|
'count': len(export_data),
|
||||||
|
'prompts': export_data
|
||||||
|
}
|
||||||
|
print("✓ Result dict created successfully")
|
||||||
|
|
||||||
|
# Try JSON serialization
|
||||||
|
import json
|
||||||
|
json_str = json.dumps(result)
|
||||||
|
print(f"✓ JSON serialization OK, length: {len(json_str)}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"✗ ERROR creating result: {type(e).__name__}: {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
@@ -0,0 +1,544 @@
|
|||||||
|
"""Unit tests for data_layer.activity_session_metrics (no DB for most cases)."""
|
||||||
|
|
||||||
|
import uuid
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from data_layer.activity_session_metrics import (
|
||||||
|
ActivitySessionMetricsError,
|
||||||
|
enrich_sessions_with_metrics,
|
||||||
|
merge_column_backed_and_eav_metrics,
|
||||||
|
merge_parameter_schema_rows,
|
||||||
|
resolve_activity_attribute_schema,
|
||||||
|
upsert_session_metrics_from_csv_mapped,
|
||||||
|
_row_value_tuple,
|
||||||
|
_validate_single_value,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _tp_row(
|
||||||
|
pid: int,
|
||||||
|
key: str,
|
||||||
|
*,
|
||||||
|
data_type: str = "integer",
|
||||||
|
cat_sort: int = 0,
|
||||||
|
cat_required: bool = False,
|
||||||
|
name_de: str = "X",
|
||||||
|
name_en: str = "X",
|
||||||
|
param_category: str = "physical",
|
||||||
|
unit: str | None = None,
|
||||||
|
validation_rules: dict | None = None,
|
||||||
|
source_field: str | None = None,
|
||||||
|
):
|
||||||
|
return {
|
||||||
|
"training_parameter_id": pid,
|
||||||
|
"cat_sort": cat_sort,
|
||||||
|
"cat_required": cat_required,
|
||||||
|
"cat_ui_group": None,
|
||||||
|
"key": key,
|
||||||
|
"name_de": name_de,
|
||||||
|
"name_en": name_en,
|
||||||
|
"param_category": param_category,
|
||||||
|
"data_type": data_type,
|
||||||
|
"unit": unit,
|
||||||
|
"validation_rules": validation_rules or {},
|
||||||
|
"source_field": source_field,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _ttp_row(
|
||||||
|
pid: int,
|
||||||
|
key: str,
|
||||||
|
*,
|
||||||
|
typ_sort: int | None = None,
|
||||||
|
typ_required: bool | None = None,
|
||||||
|
typ_ui_group: str | None = None,
|
||||||
|
data_type: str = "integer",
|
||||||
|
name_de: str = "X",
|
||||||
|
name_en: str = "X",
|
||||||
|
param_category: str = "physical",
|
||||||
|
unit: str | None = None,
|
||||||
|
validation_rules: dict | None = None,
|
||||||
|
source_field: str | None = None,
|
||||||
|
):
|
||||||
|
return {
|
||||||
|
"training_parameter_id": pid,
|
||||||
|
"typ_sort": typ_sort,
|
||||||
|
"typ_required": typ_required,
|
||||||
|
"typ_ui_group": typ_ui_group,
|
||||||
|
"key": key,
|
||||||
|
"name_de": name_de,
|
||||||
|
"name_en": name_en,
|
||||||
|
"param_category": param_category,
|
||||||
|
"data_type": data_type,
|
||||||
|
"unit": unit,
|
||||||
|
"validation_rules": validation_rules or {},
|
||||||
|
"source_field": source_field,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_merge_category_only_sorted_by_sort_order_then_key():
|
||||||
|
cat = [
|
||||||
|
_tp_row(2, "zebra", cat_sort=10),
|
||||||
|
_tp_row(1, "alpha", cat_sort=5),
|
||||||
|
]
|
||||||
|
merged = merge_parameter_schema_rows(cat, [])
|
||||||
|
assert [m["key"] for m in merged] == ["alpha", "zebra"]
|
||||||
|
assert merged[0]["required"] is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_merge_type_overrides_required_and_sort():
|
||||||
|
cat = [_tp_row(1, "rpe", cat_sort=0, cat_required=False)]
|
||||||
|
typ = [_ttp_row(1, "rpe", typ_sort=99, typ_required=True)]
|
||||||
|
merged = merge_parameter_schema_rows(cat, typ)
|
||||||
|
assert len(merged) == 1
|
||||||
|
assert merged[0]["sort_order"] == 99
|
||||||
|
assert merged[0]["required"] is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_merge_parameter_schema_includes_descriptions():
|
||||||
|
cat = [
|
||||||
|
{
|
||||||
|
"training_parameter_id": 1,
|
||||||
|
"cat_sort": 0,
|
||||||
|
"cat_required": False,
|
||||||
|
"cat_ui_group": None,
|
||||||
|
"key": "custom_w",
|
||||||
|
"name_de": "Leistung",
|
||||||
|
"name_en": "Watts",
|
||||||
|
"description_de": "Mittlere 5-Min-Leistung",
|
||||||
|
"description_en": "5 min average power",
|
||||||
|
"param_category": "performance",
|
||||||
|
"data_type": "integer",
|
||||||
|
"unit": "W",
|
||||||
|
"validation_rules": {},
|
||||||
|
"source_field": None,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
merged = merge_parameter_schema_rows(cat, [])
|
||||||
|
assert merged[0]["description_de"] == "Mittlere 5-Min-Leistung"
|
||||||
|
assert merged[0]["description_en"] == "5 min average power"
|
||||||
|
|
||||||
|
|
||||||
|
def test_merge_column_backed_includes_human_labels_from_schema():
|
||||||
|
schema = [
|
||||||
|
{
|
||||||
|
"training_parameter_id": 1,
|
||||||
|
"key": "watts",
|
||||||
|
"data_type": "integer",
|
||||||
|
"unit": "W",
|
||||||
|
"validation_rules": {},
|
||||||
|
"source_field": "avg_power",
|
||||||
|
"name_de": "Leistung",
|
||||||
|
"name_en": "Power",
|
||||||
|
"description_de": "Gerätewert",
|
||||||
|
"description_en": "Device reading",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
out = merge_column_backed_and_eav_metrics({"avg_power": 200}, schema, [])
|
||||||
|
assert len(out) == 1
|
||||||
|
assert out[0]["value"] == 200
|
||||||
|
assert out[0]["name_de"] == "Leistung"
|
||||||
|
assert out[0]["description_en"] == "Device reading"
|
||||||
|
|
||||||
|
|
||||||
|
def test_merge_type_adds_parameter_not_in_category():
|
||||||
|
typ = [_ttp_row(7, "cadence", typ_sort=1, typ_required=True, data_type="integer")]
|
||||||
|
merged = merge_parameter_schema_rows([], typ)
|
||||||
|
assert len(merged) == 1
|
||||||
|
assert merged[0]["key"] == "cadence"
|
||||||
|
assert merged[0]["required"] is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_integer_range():
|
||||||
|
_validate_single_value("integer", 5, {"min": 1, "max": 10})
|
||||||
|
with pytest.raises(ActivitySessionMetricsError) as ei:
|
||||||
|
_validate_single_value("integer", 0, {"min": 1})
|
||||||
|
assert ei.value.status_code == 400
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_float_accepts_int():
|
||||||
|
_validate_single_value("float", 3, {"min": 0, "max": 10})
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_boolean_rejects_int():
|
||||||
|
with pytest.raises(ActivitySessionMetricsError):
|
||||||
|
_validate_single_value("boolean", 1, {})
|
||||||
|
|
||||||
|
|
||||||
|
def test_row_value_tuple_mapping():
|
||||||
|
assert _row_value_tuple("integer", 42) == (None, 42, None, None)
|
||||||
|
assert _row_value_tuple("float", 1.5) == (1.5, None, None, None)
|
||||||
|
assert _row_value_tuple("string", "hi") == (None, None, "hi", None)
|
||||||
|
assert _row_value_tuple("boolean", True) == (None, None, None, True)
|
||||||
|
|
||||||
|
|
||||||
|
class _FakeCursor:
|
||||||
|
"""Sequences fetchone/fetchall for resolve_activity_attribute_schema."""
|
||||||
|
|
||||||
|
def __init__(self, fetchone_chain, fetchall_chain):
|
||||||
|
self._fetchone = list(fetchone_chain)
|
||||||
|
self._fetchall = list(fetchall_chain)
|
||||||
|
self.executes: list[tuple] = []
|
||||||
|
|
||||||
|
def execute(self, sql, params=None):
|
||||||
|
self.executes.append((sql, params))
|
||||||
|
|
||||||
|
def fetchone(self):
|
||||||
|
return self._fetchone.pop(0)
|
||||||
|
|
||||||
|
def fetchall(self):
|
||||||
|
return self._fetchall.pop(0)
|
||||||
|
|
||||||
|
|
||||||
|
def test_resolve_with_explicit_category_no_type():
|
||||||
|
cur = _FakeCursor(
|
||||||
|
fetchone_chain=[],
|
||||||
|
fetchall_chain=[
|
||||||
|
[
|
||||||
|
_tp_row(1, "rpe", cat_sort=0),
|
||||||
|
],
|
||||||
|
],
|
||||||
|
)
|
||||||
|
out = resolve_activity_attribute_schema(cur, "cardio", None)
|
||||||
|
assert len(out) == 1
|
||||||
|
assert out[0]["key"] == "rpe"
|
||||||
|
assert len(cur.executes) == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_resolve_loads_category_from_training_type_id():
|
||||||
|
cur = _FakeCursor(
|
||||||
|
fetchone_chain=[{"category": "strength"}],
|
||||||
|
fetchall_chain=[
|
||||||
|
[_tp_row(1, "rpe", cat_sort=0)],
|
||||||
|
[],
|
||||||
|
],
|
||||||
|
)
|
||||||
|
out = resolve_activity_attribute_schema(cur, None, 42)
|
||||||
|
assert len(out) == 1
|
||||||
|
assert cur.executes[0][1] == (42,)
|
||||||
|
|
||||||
|
|
||||||
|
@patch("data_layer.activity_session_metrics.resolve_activity_attribute_schema", return_value=[])
|
||||||
|
def test_enrich_sessions_batch(mock_resolve):
|
||||||
|
aid = str(uuid.uuid4())
|
||||||
|
bid = str(uuid.uuid4())
|
||||||
|
|
||||||
|
class _Cur:
|
||||||
|
def __init__(self):
|
||||||
|
self.params = None
|
||||||
|
self._fetch_n = 0
|
||||||
|
|
||||||
|
def execute(self, sql, params=None):
|
||||||
|
self.sql = sql
|
||||||
|
self.params = params
|
||||||
|
|
||||||
|
def fetchall(self):
|
||||||
|
self._fetch_n += 1
|
||||||
|
if self._fetch_n == 1:
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
"id": uuid.UUID(aid),
|
||||||
|
"training_category": None,
|
||||||
|
"training_type_id": None,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": uuid.UUID(bid),
|
||||||
|
"training_category": None,
|
||||||
|
"training_type_id": None,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
"activity_log_id": uuid.UUID(aid),
|
||||||
|
"training_parameter_id": 3,
|
||||||
|
"key": "rpe",
|
||||||
|
"data_type": "integer",
|
||||||
|
"unit": None,
|
||||||
|
"value_num": None,
|
||||||
|
"value_int": 7,
|
||||||
|
"value_text": None,
|
||||||
|
"value_bool": None,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
sessions = [{"id": aid}, {"id": bid}]
|
||||||
|
enrich_sessions_with_metrics(_Cur(), sessions)
|
||||||
|
assert sessions[0]["session_metrics"] == []
|
||||||
|
assert sessions[1]["session_metrics"] == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_merge_column_backed_prefers_column_over_stale_eav():
|
||||||
|
schema = [
|
||||||
|
{
|
||||||
|
"training_parameter_id": 1,
|
||||||
|
"key": "hr_avg",
|
||||||
|
"data_type": "float",
|
||||||
|
"unit": "bpm",
|
||||||
|
"validation_rules": {},
|
||||||
|
"source_field": "hr_avg",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
eav = [
|
||||||
|
{
|
||||||
|
"training_parameter_id": 1,
|
||||||
|
"key": "hr_avg",
|
||||||
|
"data_type": "float",
|
||||||
|
"unit": "bpm",
|
||||||
|
"value": 99.0,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
out = merge_column_backed_and_eav_metrics({"hr_avg": 140.0}, schema, eav)
|
||||||
|
assert len(out) == 1
|
||||||
|
assert out[0]["value"] == 140.0
|
||||||
|
|
||||||
|
|
||||||
|
def test_merge_falls_back_to_eav_when_column_empty():
|
||||||
|
schema = [
|
||||||
|
{
|
||||||
|
"training_parameter_id": 1,
|
||||||
|
"key": "hr_avg",
|
||||||
|
"data_type": "float",
|
||||||
|
"unit": "bpm",
|
||||||
|
"validation_rules": {},
|
||||||
|
"source_field": "hr_avg",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
eav = [
|
||||||
|
{
|
||||||
|
"training_parameter_id": 1,
|
||||||
|
"key": "hr_avg",
|
||||||
|
"data_type": "float",
|
||||||
|
"unit": "bpm",
|
||||||
|
"value": 99.0,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
out = merge_column_backed_and_eav_metrics({"hr_avg": None}, schema, eav)
|
||||||
|
assert len(out) == 1
|
||||||
|
assert out[0]["value"] == 99.0
|
||||||
|
|
||||||
|
|
||||||
|
def test_merge_ignores_eav_when_parameter_not_in_schema():
|
||||||
|
"""Nur tcp/ttp-Schema zählt: verwaiste EAV-Zeilen erscheinen nicht in der effektiven Liste."""
|
||||||
|
schema = []
|
||||||
|
eav = [
|
||||||
|
{
|
||||||
|
"training_parameter_id": 2,
|
||||||
|
"key": "custom_param",
|
||||||
|
"data_type": "string",
|
||||||
|
"unit": None,
|
||||||
|
"value": "x",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
out = merge_column_backed_and_eav_metrics({}, schema, eav)
|
||||||
|
assert out == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_merge_eav_primary_falls_back_to_legacy_hr_min_column():
|
||||||
|
"""Kanon: min_hr ohne source_field / ohne EAV — Lesefallback Spalte hr_min."""
|
||||||
|
schema = [
|
||||||
|
{
|
||||||
|
"training_parameter_id": 9,
|
||||||
|
"key": "min_hr",
|
||||||
|
"data_type": "integer",
|
||||||
|
"unit": "bpm",
|
||||||
|
"validation_rules": {},
|
||||||
|
"source_field": None,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
out = merge_column_backed_and_eav_metrics({"hr_min": 88}, schema, [])
|
||||||
|
assert len(out) == 1
|
||||||
|
assert out[0]["key"] == "min_hr"
|
||||||
|
assert out[0]["value"] == 88
|
||||||
|
|
||||||
|
|
||||||
|
def test_merge_eav_primary_prefers_legacy_column_over_eav_when_both():
|
||||||
|
"""Kanon: bei min_hr + hr_min und EAV-Zeile gewinnt activity_log (hr_min)."""
|
||||||
|
schema = [
|
||||||
|
{
|
||||||
|
"training_parameter_id": 9,
|
||||||
|
"key": "min_hr",
|
||||||
|
"data_type": "integer",
|
||||||
|
"unit": "bpm",
|
||||||
|
"validation_rules": {},
|
||||||
|
"source_field": None,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
eav = [
|
||||||
|
{
|
||||||
|
"training_parameter_id": 9,
|
||||||
|
"key": "min_hr",
|
||||||
|
"data_type": "integer",
|
||||||
|
"unit": "bpm",
|
||||||
|
"value": 100,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
out = merge_column_backed_and_eav_metrics({"hr_min": 88}, schema, eav)
|
||||||
|
assert len(out) == 1
|
||||||
|
assert out[0]["value"] == 88
|
||||||
|
|
||||||
|
|
||||||
|
def test_merge_registry_key_prefers_activity_log_column_over_eav():
|
||||||
|
"""Parameter-Key = Registry-Feld (z. B. duration_min): Spalte vor EAV."""
|
||||||
|
schema = [
|
||||||
|
{
|
||||||
|
"training_parameter_id": 3,
|
||||||
|
"key": "duration_min",
|
||||||
|
"data_type": "float",
|
||||||
|
"unit": "min",
|
||||||
|
"validation_rules": {},
|
||||||
|
"source_field": None,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
eav = [
|
||||||
|
{
|
||||||
|
"training_parameter_id": 3,
|
||||||
|
"key": "duration_min",
|
||||||
|
"data_type": "float",
|
||||||
|
"unit": "min",
|
||||||
|
"value": 99.0,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
out = merge_column_backed_and_eav_metrics({"duration_min": 45.0}, schema, eav)
|
||||||
|
assert len(out) == 1
|
||||||
|
assert out[0]["value"] == 45.0
|
||||||
|
|
||||||
|
|
||||||
|
@patch("data_layer.activity_session_metrics.resolve_activity_attribute_schema")
|
||||||
|
def test_upsert_csv_skips_eav_when_source_field_maps_activity_log(mock_schema):
|
||||||
|
"""Parameter mit source_field: kanonisch activity_log — kein doppeltes EAV (z. B. avg_hr → hr_avg)."""
|
||||||
|
mock_schema.return_value = [
|
||||||
|
{
|
||||||
|
"key": "avg_hr",
|
||||||
|
"training_parameter_id": 42,
|
||||||
|
"data_type": "integer",
|
||||||
|
"validation_rules": {"min": 30, "max": 220},
|
||||||
|
"source_field": "hr_avg",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
class Cur:
|
||||||
|
def __init__(self):
|
||||||
|
self.asm_inserts = 0
|
||||||
|
|
||||||
|
def execute(self, sql, params=None):
|
||||||
|
if "INSERT INTO activity_session_metrics" in sql:
|
||||||
|
self.asm_inserts += 1
|
||||||
|
|
||||||
|
def fetchone(self):
|
||||||
|
return {
|
||||||
|
"profile_id": "00000000-0000-0000-0000-000000000001",
|
||||||
|
"hr_avg": 130,
|
||||||
|
}
|
||||||
|
|
||||||
|
cur = Cur()
|
||||||
|
upsert_session_metrics_from_csv_mapped(
|
||||||
|
cur,
|
||||||
|
"00000000-0000-0000-0000-000000000001",
|
||||||
|
"00000000-0000-0000-0000-000000000002",
|
||||||
|
{"avg_hr": 130},
|
||||||
|
"cardio",
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
assert cur.asm_inserts == 0
|
||||||
|
|
||||||
|
|
||||||
|
@patch("data_layer.activity_session_metrics.resolve_activity_attribute_schema")
|
||||||
|
def test_upsert_csv_writes_eav_when_source_field_but_column_empty(mock_schema):
|
||||||
|
"""source_field gesetzt, activity_log-Spalte leer — Wert aus mapped nur in EAV (kein Registry-Patch)."""
|
||||||
|
mock_schema.return_value = [
|
||||||
|
{
|
||||||
|
"key": "avg_hr",
|
||||||
|
"training_parameter_id": 42,
|
||||||
|
"data_type": "integer",
|
||||||
|
"validation_rules": {"min": 30, "max": 220},
|
||||||
|
"source_field": "hr_avg",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
class Cur:
|
||||||
|
def __init__(self):
|
||||||
|
self.asm_inserts = 0
|
||||||
|
|
||||||
|
def execute(self, sql, params=None):
|
||||||
|
if "INSERT INTO activity_session_metrics" in sql:
|
||||||
|
self.asm_inserts += 1
|
||||||
|
|
||||||
|
def fetchone(self):
|
||||||
|
return {
|
||||||
|
"profile_id": "00000000-0000-0000-0000-000000000001",
|
||||||
|
"hr_avg": None,
|
||||||
|
}
|
||||||
|
|
||||||
|
cur = Cur()
|
||||||
|
upsert_session_metrics_from_csv_mapped(
|
||||||
|
cur,
|
||||||
|
"00000000-0000-0000-0000-000000000001",
|
||||||
|
"00000000-0000-0000-0000-000000000002",
|
||||||
|
{"avg_hr": 130},
|
||||||
|
"cardio",
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
assert cur.asm_inserts == 1
|
||||||
|
|
||||||
|
|
||||||
|
@patch("data_layer.activity_session_metrics.resolve_activity_attribute_schema")
|
||||||
|
def test_upsert_csv_writes_eav_when_no_source_field(mock_schema):
|
||||||
|
mock_schema.return_value = [
|
||||||
|
{
|
||||||
|
"key": "custom_note",
|
||||||
|
"training_parameter_id": 99,
|
||||||
|
"data_type": "string",
|
||||||
|
"validation_rules": {},
|
||||||
|
"source_field": None,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
class Cur:
|
||||||
|
def __init__(self):
|
||||||
|
self.asm_inserts = 0
|
||||||
|
|
||||||
|
def execute(self, sql, params=None):
|
||||||
|
if "INSERT INTO activity_session_metrics" in sql:
|
||||||
|
self.asm_inserts += 1
|
||||||
|
|
||||||
|
def fetchone(self):
|
||||||
|
return {"profile_id": "00000000-0000-0000-0000-000000000001", "hr_avg": None}
|
||||||
|
|
||||||
|
cur = Cur()
|
||||||
|
upsert_session_metrics_from_csv_mapped(
|
||||||
|
cur,
|
||||||
|
"00000000-0000-0000-0000-000000000001",
|
||||||
|
"00000000-0000-0000-0000-000000000002",
|
||||||
|
{"custom_note": "x"},
|
||||||
|
"cardio",
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
assert cur.asm_inserts == 1
|
||||||
|
|
||||||
|
|
||||||
|
@patch("data_layer.activity_session_metrics.resolve_activity_attribute_schema", return_value=[])
|
||||||
|
def test_upsert_csv_skips_eav_when_mapped_key_not_in_profile_schema(mock_resolve):
|
||||||
|
"""Import-Mapping allein legt kein EAV an — Key muss in tcp/ttp (resolve) vorkommen."""
|
||||||
|
class Cur:
|
||||||
|
def __init__(self):
|
||||||
|
self.asm_inserts = 0
|
||||||
|
|
||||||
|
def execute(self, sql, params=None):
|
||||||
|
if "INSERT INTO activity_session_metrics" in sql:
|
||||||
|
self.asm_inserts += 1
|
||||||
|
|
||||||
|
def fetchone(self):
|
||||||
|
return {"profile_id": "00000000-0000-0000-0000-000000000001", "hr_avg": None}
|
||||||
|
|
||||||
|
cur = Cur()
|
||||||
|
upsert_session_metrics_from_csv_mapped(
|
||||||
|
cur,
|
||||||
|
"00000000-0000-0000-0000-000000000001",
|
||||||
|
"00000000-0000-0000-0000-000000000002",
|
||||||
|
{"stola": 12},
|
||||||
|
"cardio",
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
assert cur.asm_inserts == 0
|
||||||
@@ -249,11 +249,18 @@ def test_run_universal_import_activity_garmin_time_plus_date_columns(monkeypatch
|
|||||||
cur = _SeqCursor([None, {"id": new_id}])
|
cur = _SeqCursor([None, {"id": new_id}])
|
||||||
out = run_universal_csv_import(cur, PID, "activity", text, "garmin.csv", mapping)
|
out = run_universal_csv_import(cur, PID, "activity", text, "garmin.csv", mapping)
|
||||||
assert out["rows_imported"] == 1
|
assert out["rows_imported"] == 1
|
||||||
# Duplicate-Key muss Datum + kombinierte Startzeit enthalten
|
# Duplicate-Check: Datum + TIME, IS NOT DISTINCT FROM (kein String-Vergleich für start_time)
|
||||||
assert any(
|
dup_sqls = [
|
||||||
params and "2024-01-20 08:30:00" in str(params)
|
(_sql, params)
|
||||||
for _sql, params in cur.executes
|
for _sql, params in cur.executes
|
||||||
if params
|
if params and "IS NOT DISTINCT FROM" in _sql and "activity_log" in _sql
|
||||||
|
]
|
||||||
|
assert dup_sqls, f"erwarteter Duplicate-SELECT fehlt; executes={cur.executes!r}"
|
||||||
|
assert any(
|
||||||
|
len(p) >= 3
|
||||||
|
and str(p[1]).startswith("2024-01-20")
|
||||||
|
and (getattr(p[2], "hour", None) == 8 and getattr(p[2], "minute", None) == 30)
|
||||||
|
for _, p in dup_sqls
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -38,6 +38,29 @@ def test_parse_csv_sample_header():
|
|||||||
assert rows[0]["kcal"] == "2000"
|
assert rows[0]["kcal"] == "2000"
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_csv_sample_nbsp_in_header_matches_normal_space_key():
|
||||||
|
"""Excel/Apple: NBSP (U+00A0) im Spaltennamen — gleicher Key wie normales Leerzeichen."""
|
||||||
|
text = "Aufgestiegene\u00a0Höhe (m);Wert\n12;3\n"
|
||||||
|
headers, rows, delim = parse_csv_sample(text, delimiter=";", max_data_rows=3)
|
||||||
|
assert headers == ["Aufgestiegene Höhe (m)", "Wert"]
|
||||||
|
assert rows[0]["Aufgestiegene Höhe (m)"] == "12"
|
||||||
|
|
||||||
|
|
||||||
|
def test_iter_csv_dict_rows_nbsp_header_canonical():
|
||||||
|
text = "col\u00a0one;b\n1;2\n"
|
||||||
|
rows = list(iter_csv_dict_rows(text, ";", has_header=True))
|
||||||
|
assert rows == [{"col one": "1", "b": "2"}]
|
||||||
|
|
||||||
|
|
||||||
|
def test_build_row_field_mapping_space_vs_nbsp_in_csv_header():
|
||||||
|
"""Vorlage (Dialog) mit normalem Leerzeichen, CSV mit NBSP — Zuordnung muss greifen."""
|
||||||
|
csv_row = {"Aufgestiegene\u00a0Höhe (m)": "10"}
|
||||||
|
fm = {"Aufgestiegene Höhe (m)": "stola"}
|
||||||
|
tc = {"stola": {"type": "float", "decimal_separator": ".", "flexible": True}}
|
||||||
|
out = build_row_after_mapping(csv_row, fm, tc, module="activity")
|
||||||
|
assert out.get("stola") == 10.0
|
||||||
|
|
||||||
|
|
||||||
def test_column_signature_sorted_unique():
|
def test_column_signature_sorted_unique():
|
||||||
sig = column_signature(["B", "a", "a"])
|
sig = column_signature(["B", "a", "a"])
|
||||||
assert sig == ["a", "b"]
|
assert sig == ["a", "b"]
|
||||||
@@ -81,6 +104,25 @@ def test_convert_kcal_via_source_unit_kj():
|
|||||||
assert abs(k - 1000.0) < 0.05
|
assert abs(k - 1000.0) < 0.05
|
||||||
|
|
||||||
|
|
||||||
|
def test_apple_health_long_decimal_dot_preserved():
|
||||||
|
"""Apple: Mittel-HF u. Energie mit vielen Nachkommastellen; Punkt ist Dezimaltrenner."""
|
||||||
|
hr_spec = {"type": "int", "flexible": True}
|
||||||
|
r = convert_value("96.8749374730629", "hr_avg", hr_spec, module="activity")
|
||||||
|
assert r == 97
|
||||||
|
|
||||||
|
rest_spec = {"type": "float", "decimal_separator": ".", "flexible": True, "source_unit": "kj"}
|
||||||
|
kcal = convert_value("596.6689047323086", "kcal_resting", rest_spec, module="activity")
|
||||||
|
assert 140.0 < kcal < 145.0
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_float_auto_us_thousands_comma():
|
||||||
|
"""«12,345» ohne Dezimalpunkt weiter als Tausender möglich."""
|
||||||
|
v = convert_value("12345", "x", {"type": "float", "decimal_separator": "auto"})
|
||||||
|
assert v == 12345.0
|
||||||
|
v2 = convert_value("12,345", "x", {"type": "float", "decimal_separator": "auto"})
|
||||||
|
assert abs(v2 - 12345.0) < 0.01
|
||||||
|
|
||||||
|
|
||||||
def test_convert_protein_kg_to_g():
|
def test_convert_protein_kg_to_g():
|
||||||
spec = {"type": "float", "source_unit": "kg", "decimal_separator": "."}
|
spec = {"type": "float", "source_unit": "kg", "decimal_separator": "."}
|
||||||
g = convert_value("0.1", "protein_g", spec, module="nutrition")
|
g = convert_value("0.1", "protein_g", spec, module="nutrition")
|
||||||
@@ -164,6 +206,16 @@ def test_build_row_fddb_raw_header_keys_match_normalized_template():
|
|||||||
assert out["kcal"] is not None and abs(float(out["kcal"]) - (42000 / 4.184)) < 0.1
|
assert out["kcal"] is not None and abs(float(out["kcal"]) - (42000 / 4.184)) < 0.1
|
||||||
|
|
||||||
|
|
||||||
|
def test_build_row_apple_workout_elevation_header_prefix_matches_shorter_mapping_key():
|
||||||
|
"""Apple Workouts: „Aufgestiegene Höhe (m)“ normalisiert anders als manuell „aufgestiegene Höhe“."""
|
||||||
|
csv_row = {"Aufgestiegene Höhe (m)": "47.13", "Workout Type": "Outdoor Spaziergang"}
|
||||||
|
fm = {"aufgestiegene Höhe": "stola", "Workout Type": "activity_type"}
|
||||||
|
tc = {"stola": {"type": "string"}}
|
||||||
|
out = build_row_after_mapping(csv_row, fm, tc, module="activity")
|
||||||
|
assert out.get("stola") == "47.13"
|
||||||
|
assert out.get("activity_type") == "Outdoor Spaziergang"
|
||||||
|
|
||||||
|
|
||||||
def test_convert_date_ddmm_with_seconds():
|
def test_convert_date_ddmm_with_seconds():
|
||||||
d = convert_value(
|
d = convert_value(
|
||||||
"15.01.2024 14:30:00",
|
"15.01.2024 14:30:00",
|
||||||
|
|||||||
@@ -44,6 +44,15 @@ def test_value_raw_json():
|
|||||||
assert not success
|
assert not success
|
||||||
assert val is None
|
assert val is None
|
||||||
|
|
||||||
|
# Resolver-Fehlerhülle (kein verwertbares JSON für Charts)
|
||||||
|
val, success = extract_value_raw(
|
||||||
|
'{"_available": false, "_reason": "test"}',
|
||||||
|
OutputType.JSON,
|
||||||
|
PlaceholderType.RAW_DATA,
|
||||||
|
)
|
||||||
|
assert not success
|
||||||
|
assert val is None
|
||||||
|
|
||||||
|
|
||||||
def test_value_raw_number():
|
def test_value_raw_number():
|
||||||
"""Numeric outputs must extract numbers without units."""
|
"""Numeric outputs must extract numbers without units."""
|
||||||
@@ -66,6 +75,13 @@ def test_value_raw_number():
|
|||||||
val, success = extract_value_raw('nicht verfügbar', OutputType.NUMBER, PlaceholderType.ATOMIC)
|
val, success = extract_value_raw('nicht verfügbar', OutputType.NUMBER, PlaceholderType.ATOMIC)
|
||||||
assert not success
|
assert not success
|
||||||
|
|
||||||
|
val, success = extract_value_raw(
|
||||||
|
'nicht verfügbar — Keine Messungen (detail)',
|
||||||
|
OutputType.NUMBER,
|
||||||
|
PlaceholderType.ATOMIC,
|
||||||
|
)
|
||||||
|
assert not success
|
||||||
|
|
||||||
|
|
||||||
def test_value_raw_markdown():
|
def test_value_raw_markdown():
|
||||||
"""Markdown outputs keep as string."""
|
"""Markdown outputs keep as string."""
|
||||||
|
|||||||
@@ -0,0 +1,104 @@
|
|||||||
|
"""Tests für {{key|d}}, {{key|x}}, ai_caption und Unbekannt-Erkennung."""
|
||||||
|
from placeholder_registry import (
|
||||||
|
PlaceholderMetadata,
|
||||||
|
PlaceholderType,
|
||||||
|
OutputType,
|
||||||
|
build_ai_placeholder_caption,
|
||||||
|
)
|
||||||
|
import placeholder_resolver as pr
|
||||||
|
from placeholder_resolver import format_value_with_d_modifier
|
||||||
|
from prompt_executor import resolve_placeholders
|
||||||
|
|
||||||
|
|
||||||
|
def test_build_ai_caption_is_explanation_only():
|
||||||
|
m = PlaceholderMetadata(
|
||||||
|
key="test_x",
|
||||||
|
category="Test",
|
||||||
|
description="Kurzbeschreibung",
|
||||||
|
resolver_module="m",
|
||||||
|
resolver_function="f",
|
||||||
|
semantic_contract="Lang Vertrag " * 50,
|
||||||
|
business_meaning="Kernbedeutung für die KI.",
|
||||||
|
unit="g/day",
|
||||||
|
placeholder_type=PlaceholderType.INTERPRETED,
|
||||||
|
output_type=OutputType.NUMERIC,
|
||||||
|
)
|
||||||
|
cap = build_ai_placeholder_caption(m)
|
||||||
|
assert "Kernbedeutung" in cap
|
||||||
|
assert "Kurzbeschreibung" not in cap
|
||||||
|
|
||||||
|
|
||||||
|
def test_build_ai_caption_protein_avg_no_description_prefix():
|
||||||
|
m = PlaceholderMetadata(
|
||||||
|
key="protein_avg",
|
||||||
|
category="Ernährung",
|
||||||
|
description="Durchschn. Protein in g (30d)",
|
||||||
|
resolver_module="m",
|
||||||
|
resolver_function="f",
|
||||||
|
business_meaning="Zentraler Placeholder für Muskelerhalt.",
|
||||||
|
unit="g/day",
|
||||||
|
placeholder_type=PlaceholderType.INTERPRETED,
|
||||||
|
output_type=OutputType.NUMERIC,
|
||||||
|
)
|
||||||
|
cap = build_ai_placeholder_caption(m)
|
||||||
|
assert cap.startswith("Zentraler Placeholder")
|
||||||
|
assert "Durchschn. Protein" not in cap
|
||||||
|
assert "Technischer Bezug" not in cap
|
||||||
|
|
||||||
|
|
||||||
|
def test_build_ai_caption_score_adds_scale():
|
||||||
|
m = PlaceholderMetadata(
|
||||||
|
key="test_score",
|
||||||
|
category="Test",
|
||||||
|
description="Score",
|
||||||
|
resolver_module="m",
|
||||||
|
resolver_function="f",
|
||||||
|
business_meaning="Gewichteter Gesamtscore.",
|
||||||
|
unit="Score (0-100)",
|
||||||
|
placeholder_type=PlaceholderType.SCORE,
|
||||||
|
output_type=OutputType.NUMERIC,
|
||||||
|
)
|
||||||
|
cap = build_ai_placeholder_caption(m)
|
||||||
|
assert "0–100" in cap or "0-100" in cap
|
||||||
|
assert "Gewichteter" in cap
|
||||||
|
|
||||||
|
|
||||||
|
def test_placeholder_token_regex_optional_modifier():
|
||||||
|
m0 = pr._PLACEHOLDER_TOKEN_RE.search("{{fat_avg}}")
|
||||||
|
assert m0 and m0.group(1) == "fat_avg" and m0.group(2) is None
|
||||||
|
m1 = pr._PLACEHOLDER_TOKEN_RE.search("{{fat_avg|d}}")
|
||||||
|
assert m1 and m1.group(1) == "fat_avg" and m1.group(2).strip() == "d"
|
||||||
|
m2 = pr._PLACEHOLDER_TOKEN_RE.search("{{ protein_avg | d }}")
|
||||||
|
assert m2 and m2.group(1) == "protein_avg" and m2.group(2).strip() == "d"
|
||||||
|
m3 = pr._PLACEHOLDER_TOKEN_RE.search("{{k|d,x}}")
|
||||||
|
assert m3 and m3.group(1) == "k" and m3.group(2).strip() == "d,x"
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_unknown_placeholders_strips_modifier():
|
||||||
|
unk = pr.get_unknown_placeholders("{{not_a_real_key|d}}")
|
||||||
|
assert set(unk) == {"not_a_real_key"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_format_value_with_d_modifier_uses_description_only():
|
||||||
|
row = {
|
||||||
|
"key": "protein_avg",
|
||||||
|
"description": "Durchschn. Protein in g (30d)",
|
||||||
|
"ai_caption": "Nur für |x",
|
||||||
|
}
|
||||||
|
out = format_value_with_d_modifier("119g/Tag", row)
|
||||||
|
assert out == "119g/Tag — Durchschn. Protein in g (30d)"
|
||||||
|
assert "Nur für |x" not in out
|
||||||
|
|
||||||
|
|
||||||
|
def test_format_value_with_d_modifier_falls_back_to_description():
|
||||||
|
row = {"description": "Nur Beschreibung", "key": "x"}
|
||||||
|
assert format_value_with_d_modifier("42", row) == "42 — Nur Beschreibung"
|
||||||
|
|
||||||
|
|
||||||
|
def test_prompt_executor_modifiers_d_x_combined():
|
||||||
|
catalog = {"E": [{"key": "p", "description": "Desc", "ai_caption": "Expl"}]}
|
||||||
|
v = {"p": "99"}
|
||||||
|
assert resolve_placeholders("{{p|d}}", v, None, catalog) == "99 — Desc"
|
||||||
|
assert resolve_placeholders("{{p|x}}", v, None, catalog) == "Expl"
|
||||||
|
assert resolve_placeholders("{{p|d,x}}", v, None, catalog) == "99 — Desc — Expl"
|
||||||
|
assert resolve_placeholders("{{p}}", v, None, catalog) == "99"
|
||||||
+138
-11
@@ -42,7 +42,8 @@ async def execute_workflow(
|
|||||||
profile_id: str = None,
|
profile_id: str = None,
|
||||||
variables: Dict[str, Any] = None,
|
variables: Dict[str, Any] = None,
|
||||||
openrouter_call_func = None, # Callback für LLM-Calls: async (prompt, model) -> str
|
openrouter_call_func = None, # Callback für LLM-Calls: async (prompt, model) -> str
|
||||||
enable_debug: bool = False
|
enable_debug: bool = False,
|
||||||
|
progress_callback = None # NEW: Optional callback für Progress-Updates: async (event_type, data) -> None
|
||||||
) -> ExecutionResult:
|
) -> ExecutionResult:
|
||||||
"""
|
"""
|
||||||
Führt einen Workflow aus (mit conditional branching und path consolidation).
|
Führt einen Workflow aus (mit conditional branching und path consolidation).
|
||||||
@@ -76,6 +77,13 @@ async def execute_workflow(
|
|||||||
|
|
||||||
logger.info(f"Starting workflow execution: {execution_id}")
|
logger.info(f"Starting workflow execution: {execution_id}")
|
||||||
|
|
||||||
|
# NEW: Progress-Callback für Start
|
||||||
|
if progress_callback:
|
||||||
|
await progress_callback("execution_started", {
|
||||||
|
"execution_id": execution_id,
|
||||||
|
"started_at": started_at
|
||||||
|
})
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 1. Lade Workflow-Definition
|
# 1. Lade Workflow-Definition
|
||||||
if graph_data:
|
if graph_data:
|
||||||
@@ -158,9 +166,29 @@ async def execute_workflow(
|
|||||||
enable_debug=enable_debug
|
enable_debug=enable_debug
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Add human-readable label to node_state for debug UI
|
||||||
|
# Priority: node.label (user-defined) > prompt_slug > node_type-id
|
||||||
|
node_label = node.label if hasattr(node, 'label') and node.label else (
|
||||||
|
node.prompt_slug if hasattr(node, 'prompt_slug') and node.prompt_slug else f"{node.type.value}-{node_id[:8]}"
|
||||||
|
)
|
||||||
|
if not node_state.debug_prompt_slug:
|
||||||
|
node_state.debug_prompt_slug = node_label
|
||||||
|
|
||||||
node_states.append(node_state)
|
node_states.append(node_state)
|
||||||
context["node_results"][node_id] = node_state
|
context["node_results"][node_id] = node_state
|
||||||
|
|
||||||
|
# NEW: Progress-Callback aufrufen (für SSE Streaming)
|
||||||
|
if progress_callback:
|
||||||
|
await progress_callback("node_complete", {
|
||||||
|
"node_id": node_id,
|
||||||
|
"node_type": node.type,
|
||||||
|
"node_label": node_label,
|
||||||
|
"status": node_state.status.value,
|
||||||
|
"total_nodes": len(graph.nodes),
|
||||||
|
"completed_nodes": len([ns for ns in node_states if ns.status in [NodeStatus.EXECUTED, NodeStatus.SKIPPED]]),
|
||||||
|
"error": node_state.error if node_state.status == NodeStatus.FAILED else None
|
||||||
|
})
|
||||||
|
|
||||||
# Füge Nachfolger zur Queue hinzu
|
# Füge Nachfolger zur Queue hinzu
|
||||||
outgoing_edges = [e for e in graph.edges if e.from_node == node_id]
|
outgoing_edges = [e for e in graph.edges if e.from_node == node_id]
|
||||||
for edge in outgoing_edges:
|
for edge in outgoing_edges:
|
||||||
@@ -185,6 +213,19 @@ async def execute_workflow(
|
|||||||
|
|
||||||
logger.info(f"Workflow execution completed: {execution_id}")
|
logger.info(f"Workflow execution completed: {execution_id}")
|
||||||
|
|
||||||
|
# NEW: Progress-Callback für erfolgreiche Fertigstellung
|
||||||
|
if progress_callback:
|
||||||
|
await progress_callback("execution_complete", {
|
||||||
|
"execution_id": execution_id,
|
||||||
|
"status": "completed",
|
||||||
|
"aggregated_result": aggregated,
|
||||||
|
"total_nodes": len(node_states),
|
||||||
|
"completed_nodes": len([ns for ns in node_states if ns.status == NodeStatus.EXECUTED]),
|
||||||
|
"skipped_nodes": len([ns for ns in node_states if ns.status == NodeStatus.SKIPPED]),
|
||||||
|
"failed_nodes": len([ns for ns in node_states if ns.status == NodeStatus.FAILED]),
|
||||||
|
"completed_at": completed_at
|
||||||
|
})
|
||||||
|
|
||||||
return ExecutionResult(
|
return ExecutionResult(
|
||||||
execution_id=execution_id,
|
execution_id=execution_id,
|
||||||
workflow_id=workflow_id or "N/A", # Placeholder when graph_data is used directly
|
workflow_id=workflow_id or "N/A", # Placeholder when graph_data is used directly
|
||||||
@@ -198,6 +239,15 @@ async def execute_workflow(
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Workflow execution failed: {e}", exc_info=True)
|
logger.error(f"Workflow execution failed: {e}", exc_info=True)
|
||||||
|
|
||||||
|
# NEW: Progress-Callback für Fehler
|
||||||
|
if progress_callback:
|
||||||
|
await progress_callback("execution_failed", {
|
||||||
|
"execution_id": execution_id,
|
||||||
|
"status": "failed",
|
||||||
|
"error": str(e),
|
||||||
|
"completed_at": datetime.utcnow().isoformat()
|
||||||
|
})
|
||||||
|
|
||||||
# Speichere Failed State
|
# Speichere Failed State
|
||||||
completed_at = datetime.utcnow().isoformat()
|
completed_at = datetime.utcnow().isoformat()
|
||||||
save_execution_state(
|
save_execution_state(
|
||||||
@@ -270,11 +320,11 @@ async def execute_node(
|
|||||||
|
|
||||||
# Logic Nodes (Phase 3)
|
# Logic Nodes (Phase 3)
|
||||||
if node.type == "logic":
|
if node.type == "logic":
|
||||||
return execute_logic_node(node, context, graph)
|
return execute_logic_node(node, context, graph, enable_debug)
|
||||||
|
|
||||||
# Join Nodes (Phase 4)
|
# Join Nodes (Phase 4)
|
||||||
if node.type == "join":
|
if node.type == "join":
|
||||||
return execute_join_node(node, context, graph)
|
return execute_join_node(node, context, graph, enable_debug)
|
||||||
|
|
||||||
# Analysis Nodes
|
# Analysis Nodes
|
||||||
if node.type == "analysis":
|
if node.type == "analysis":
|
||||||
@@ -354,6 +404,11 @@ async def execute_node(
|
|||||||
decision_signals=parsed["decision_signals"],
|
decision_signals=parsed["decision_signals"],
|
||||||
normalized_signals=normalized_signals,
|
normalized_signals=normalized_signals,
|
||||||
reasoning_anchors=parsed.get("reasoning_anchors"),
|
reasoning_anchors=parsed.get("reasoning_anchors"),
|
||||||
|
# Debug information (nur wenn enable_debug=True)
|
||||||
|
debug_prompt=augmented_prompt if enable_debug else None,
|
||||||
|
debug_raw_response=llm_response if enable_debug else None,
|
||||||
|
debug_node_type="analysis",
|
||||||
|
debug_prompt_slug=node.prompt_slug if enable_debug else None,
|
||||||
started_at=started_at,
|
started_at=started_at,
|
||||||
completed_at=datetime.utcnow().isoformat()
|
completed_at=datetime.utcnow().isoformat()
|
||||||
)
|
)
|
||||||
@@ -375,7 +430,8 @@ async def execute_node(
|
|||||||
def execute_logic_node(
|
def execute_logic_node(
|
||||||
node,
|
node,
|
||||||
context: Dict[str, Any],
|
context: Dict[str, Any],
|
||||||
graph: WorkflowGraph
|
graph: WorkflowGraph,
|
||||||
|
enable_debug: bool = False
|
||||||
) -> NodeExecutionState:
|
) -> NodeExecutionState:
|
||||||
"""
|
"""
|
||||||
Führt Logic Node aus (Phase 3).
|
Führt Logic Node aus (Phase 3).
|
||||||
@@ -399,10 +455,49 @@ def execute_logic_node(
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
if not node.condition:
|
if not node.condition:
|
||||||
raise ValueError(f"Logic node {node.id} has no condition")
|
error_msg = f"Logic node {node.id} has no condition defined"
|
||||||
|
logger.error(error_msg)
|
||||||
|
return NodeExecutionState(
|
||||||
|
node_id=node.id,
|
||||||
|
status=NodeStatus.FAILED,
|
||||||
|
error=error_msg,
|
||||||
|
started_at=started_at,
|
||||||
|
completed_at=datetime.utcnow().isoformat()
|
||||||
|
)
|
||||||
|
|
||||||
|
# Handle both formats (thanks to Union[LogicExpression, Condition] type):
|
||||||
|
# 1. Direct LogicExpression (UI format): node.condition is LogicExpression
|
||||||
|
# 2. Wrapped in Condition (legacy): node.condition is Condition with .expression
|
||||||
|
from workflow_models import LogicExpression, Condition
|
||||||
|
|
||||||
|
expression = None
|
||||||
|
|
||||||
|
if isinstance(node.condition, LogicExpression):
|
||||||
|
# UI format: direct LogicExpression
|
||||||
|
expression = node.condition
|
||||||
|
elif isinstance(node.condition, Condition):
|
||||||
|
# Legacy format: wrapped in Condition
|
||||||
|
expression = node.condition.expression
|
||||||
|
else:
|
||||||
|
# Fallback: try to detect format manually
|
||||||
|
if hasattr(node.condition, 'operator') and hasattr(node.condition, 'operands'):
|
||||||
|
expression = node.condition # Looks like LogicExpression
|
||||||
|
elif hasattr(node.condition, 'expression'):
|
||||||
|
expression = node.condition.expression # Looks like Condition
|
||||||
|
|
||||||
|
if expression is None:
|
||||||
|
error_msg = f"Logic node {node.id} has no valid condition/expression defined"
|
||||||
|
logger.error(error_msg)
|
||||||
|
return NodeExecutionState(
|
||||||
|
node_id=node.id,
|
||||||
|
status=NodeStatus.FAILED,
|
||||||
|
error=error_msg,
|
||||||
|
started_at=started_at,
|
||||||
|
completed_at=datetime.utcnow().isoformat()
|
||||||
|
)
|
||||||
|
|
||||||
# 1. Evaluiere Bedingung
|
# 1. Evaluiere Bedingung
|
||||||
result, error = evaluate_logic_expression(node.condition.expression, context)
|
result, error = evaluate_logic_expression(expression, context)
|
||||||
|
|
||||||
if error:
|
if error:
|
||||||
# Fehler bei Evaluation → Fallback anwenden
|
# Fehler bei Evaluation → Fallback anwenden
|
||||||
@@ -436,7 +531,15 @@ def execute_logic_node(
|
|||||||
"evaluation_result": result if not error else None,
|
"evaluation_result": result if not error else None,
|
||||||
"error": error,
|
"error": error,
|
||||||
"activated_edges": activated_edges
|
"activated_edges": activated_edges
|
||||||
})
|
}),
|
||||||
|
# Debug information
|
||||||
|
debug_prompt=json.dumps(expression.model_dump() if hasattr(expression, 'model_dump') else str(expression), ensure_ascii=False) if enable_debug and expression else None,
|
||||||
|
debug_raw_response=json.dumps({
|
||||||
|
"result": result,
|
||||||
|
"error": error,
|
||||||
|
"activated_edges": activated_edges
|
||||||
|
}, ensure_ascii=False) if enable_debug else None,
|
||||||
|
debug_node_type="logic"
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -453,7 +556,8 @@ def execute_logic_node(
|
|||||||
def execute_join_node(
|
def execute_join_node(
|
||||||
node,
|
node,
|
||||||
context: Dict[str, Any],
|
context: Dict[str, Any],
|
||||||
graph: WorkflowGraph
|
graph: WorkflowGraph,
|
||||||
|
enable_debug: bool = False
|
||||||
) -> NodeExecutionState:
|
) -> NodeExecutionState:
|
||||||
"""
|
"""
|
||||||
Führt Join Node aus (Phase 4).
|
Führt Join Node aus (Phase 4).
|
||||||
@@ -525,7 +629,16 @@ def execute_join_node(
|
|||||||
normalized_signals=consolidated_signals_list,
|
normalized_signals=consolidated_signals_list,
|
||||||
metadata=join_result.metadata,
|
metadata=join_result.metadata,
|
||||||
started_at=started_at,
|
started_at=started_at,
|
||||||
completed_at=datetime.utcnow().isoformat()
|
completed_at=datetime.utcnow().isoformat(),
|
||||||
|
# Debug information
|
||||||
|
debug_prompt=f"Join Strategy: {node.join_strategy.value if node.join_strategy else 'wait_all'}\nPaths: {total_count}\nMinimum Required: {node.min_paths if node.min_paths else 'all'}" if enable_debug else None,
|
||||||
|
debug_raw_response=json.dumps({
|
||||||
|
"executed_paths": executed_count,
|
||||||
|
"total_paths": total_count,
|
||||||
|
"strategy": node.join_strategy.value if node.join_strategy else 'wait_all',
|
||||||
|
"consolidated_nodes": list(join_result.consolidated_analysis_core.keys())
|
||||||
|
}, ensure_ascii=False) if enable_debug else None,
|
||||||
|
debug_node_type="join"
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -777,17 +890,31 @@ def _get_edges_by_label(node_id: str, label: str, graph: WorkflowGraph) -> List[
|
|||||||
"""
|
"""
|
||||||
Findet alle ausgehenden Edges mit bestimmtem Label.
|
Findet alle ausgehenden Edges mit bestimmtem Label.
|
||||||
|
|
||||||
|
Unterstützt beide Formate:
|
||||||
|
- Legacy: e.label == label (z.B. "then", "else")
|
||||||
|
- UI: e.source_handle == label (z.B. "true", "false")
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
node_id: Node-ID
|
node_id: Node-ID
|
||||||
label: Edge-Label (z.B. "then", "else", "uncertainty")
|
label: Edge-Label oder sourceHandle (z.B. "then"/"true", "else"/"false")
|
||||||
graph: WorkflowGraph
|
graph: WorkflowGraph
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Liste von Edge-IDs
|
Liste von Edge-IDs
|
||||||
"""
|
"""
|
||||||
|
# Map label to sourceHandle equivalents
|
||||||
|
label_to_handle = {
|
||||||
|
"then": "true",
|
||||||
|
"else": "false"
|
||||||
|
}
|
||||||
|
handle_equivalent = label_to_handle.get(label, label)
|
||||||
|
|
||||||
matching_edges = [
|
matching_edges = [
|
||||||
e.id for e in graph.edges
|
e.id for e in graph.edges
|
||||||
if e.from_node == node_id and e.label == label
|
if e.from_node == node_id and (
|
||||||
|
e.label == label or # Legacy format
|
||||||
|
(hasattr(e, 'source_handle') and e.source_handle == handle_equivalent) # UI format
|
||||||
|
)
|
||||||
]
|
]
|
||||||
return matching_edges
|
return matching_edges
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ Data validation schemas for Workflow-Graph, Knoten, Kanten, Bedingungen.
|
|||||||
Konzept-Basis: konzept_workflow_engine_konsolidated.md
|
Konzept-Basis: konzept_workflow_engine_konsolidated.md
|
||||||
Anforderungsanalyse: anforderungsanalyse_umsetzungsplan.md
|
Anforderungsanalyse: anforderungsanalyse_umsetzungsplan.md
|
||||||
"""
|
"""
|
||||||
from typing import Optional, List, Dict, Any
|
from typing import Optional, List, Dict, Any, Union
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
@@ -148,18 +148,27 @@ class LogicExpression(BaseModel):
|
|||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
operator: LogicOperator = Field(..., description="Logischer Operator (and, or, not) oder Vergleichsoperator")
|
operator: LogicOperator = Field(..., description="Logischer Operator (and, or, not) oder Vergleichsoperator")
|
||||||
operands: Optional[List[Any]] = Field(None, description="Liste von Operanden (LogicOperand oder verschachtelte LogicExpression)")
|
operands: Optional[List['LogicExpression']] = Field(None, description="Liste von Operanden (LogicOperand oder verschachtelte LogicExpression)")
|
||||||
# Bei einfachem Vergleich:
|
# Bei einfachem Vergleich:
|
||||||
ref: Optional[str] = Field(None, description="Signal-Referenz (nur bei Vergleichsoperatoren)")
|
ref: Optional[str] = Field(None, description="Signal-Referenz (nur bei Vergleichsoperatoren)")
|
||||||
value: Optional[Any] = Field(None, description="Vergleichswert (nur bei Vergleichsoperatoren)")
|
value: Optional[Any] = Field(None, description="Vergleichswert (nur bei Vergleichsoperatoren)")
|
||||||
|
|
||||||
|
# Enable forward reference resolution for recursive model
|
||||||
|
LogicExpression.model_rebuild()
|
||||||
|
|
||||||
|
|
||||||
class Condition(BaseModel):
|
class Condition(BaseModel):
|
||||||
"""
|
"""
|
||||||
Bedingung für einen Logik-Knoten.
|
Bedingung für einen Logik-Knoten.
|
||||||
|
|
||||||
Unterstützt if/else-if/else-Logik.
|
Unterstützt if/else-if/else-Logik.
|
||||||
|
|
||||||
|
Note: Uses extra='forbid' to ensure proper Union resolution with LogicExpression.
|
||||||
|
If unknown fields are present (like 'operator', 'operands'), deserialization fails
|
||||||
|
and Pydantic tries LogicExpression instead.
|
||||||
"""
|
"""
|
||||||
|
model_config = {'extra': 'forbid'}
|
||||||
|
|
||||||
type: str = Field(default="if", description="Bedingungstyp: if, else-if, else")
|
type: str = Field(default="if", description="Bedingungstyp: if, else-if, else")
|
||||||
expression: Optional[LogicExpression] = Field(None, description="Logischer Ausdruck (null bei 'else')")
|
expression: Optional[LogicExpression] = Field(None, description="Logischer Ausdruck (null bei 'else')")
|
||||||
then_path: Optional[str] = Field(None, description="Edge-ID für 'then'-Pfad")
|
then_path: Optional[str] = Field(None, description="Edge-ID für 'then'-Pfad")
|
||||||
@@ -187,6 +196,7 @@ class WorkflowNode(BaseModel):
|
|||||||
"""
|
"""
|
||||||
id: str = Field(..., description="Eindeutige Knoten-ID")
|
id: str = Field(..., description="Eindeutige Knoten-ID")
|
||||||
type: NodeType = Field(..., description="Knotentyp")
|
type: NodeType = Field(..., description="Knotentyp")
|
||||||
|
label: Optional[str] = Field(None, description="Node-Label (vom Editor, z.B. 'Qualitätseinschätzung')")
|
||||||
position: Optional[Position] = Field(None, description="Position im visuellen Editor")
|
position: Optional[Position] = Field(None, description="Position im visuellen Editor")
|
||||||
|
|
||||||
# ANALYSIS-Knoten
|
# ANALYSIS-Knoten
|
||||||
@@ -195,7 +205,8 @@ class WorkflowNode(BaseModel):
|
|||||||
question_augmentations: Optional[List[QuestionAugmentation]] = Field(None, description="Fragenergänzungen (knotengebunden, überschreiben Prompt-Defaults)")
|
question_augmentations: Optional[List[QuestionAugmentation]] = Field(None, description="Fragenergänzungen (knotengebunden, überschreiben Prompt-Defaults)")
|
||||||
|
|
||||||
# LOGIC-Knoten
|
# LOGIC-Knoten
|
||||||
condition: Optional[Condition] = Field(None, description="Bedingung für Pfad-Routing")
|
# Support both formats: direct LogicExpression (UI) or wrapped in Condition (legacy)
|
||||||
|
condition: Optional[Union[LogicExpression, Condition]] = Field(None, description="Bedingung für Pfad-Routing")
|
||||||
fallback: Optional[FallbackConfig] = Field(None, description="Fallback-Konfiguration")
|
fallback: Optional[FallbackConfig] = Field(None, description="Fallback-Konfiguration")
|
||||||
|
|
||||||
# JOIN-Knoten (Phase 4)
|
# JOIN-Knoten (Phase 4)
|
||||||
@@ -220,6 +231,10 @@ class WorkflowEdge(BaseModel):
|
|||||||
to_node: str = Field(..., alias="to", description="Ziel-Knoten-ID")
|
to_node: str = Field(..., alias="to", description="Ziel-Knoten-ID")
|
||||||
label: Optional[str] = Field(None, description="Label für visuelle Darstellung (z.B. 'then', 'else')")
|
label: Optional[str] = Field(None, description="Label für visuelle Darstellung (z.B. 'then', 'else')")
|
||||||
|
|
||||||
|
# UI-Format fields (React Flow)
|
||||||
|
source_handle: Optional[str] = Field(None, alias="sourceHandle", description="Source handle ID (UI format: 'true', 'false', 'out')")
|
||||||
|
target_handle: Optional[str] = Field(None, alias="targetHandle", description="Target handle ID (UI format: 'in', 'path_1', etc.)")
|
||||||
|
|
||||||
|
|
||||||
class WorkflowGraph(BaseModel):
|
class WorkflowGraph(BaseModel):
|
||||||
"""
|
"""
|
||||||
@@ -335,6 +350,15 @@ class NodeExecutionState(BaseModel):
|
|||||||
normalized_signals: List[NormalizedSignal] = Field(default_factory=list, description="Normalisierte Signale (Phase 2)")
|
normalized_signals: List[NormalizedSignal] = Field(default_factory=list, description="Normalisierte Signale (Phase 2)")
|
||||||
reasoning_anchors: Optional[str] = Field(None, description="Begründungsanker aus ## Begründung")
|
reasoning_anchors: Optional[str] = Field(None, description="Begründungsanker aus ## Begründung")
|
||||||
|
|
||||||
|
# Debug Information (nur wenn enable_debug=True)
|
||||||
|
debug_prompt: Optional[str] = Field(None, description="Vollständiger Prompt der an die KI gesendet wurde")
|
||||||
|
debug_raw_response: Optional[str] = Field(None, description="Rohe KI-Antwort (ungeparst)")
|
||||||
|
debug_node_type: Optional[str] = Field(None, description="Node-Typ (analysis, logic, join, etc.)")
|
||||||
|
debug_prompt_slug: Optional[str] = Field(None, description="Verwendeter Prompt-Slug (bei ANALYSIS nodes)")
|
||||||
|
|
||||||
|
# Metadata (für Join Nodes und andere Zusatzinfos)
|
||||||
|
metadata: Optional[Dict[str, Any]] = Field(None, description="Zusätzliche Metadaten (z.B. Join-Statistiken)")
|
||||||
|
|
||||||
# Error & Timing
|
# Error & Timing
|
||||||
error: Optional[str] = Field(None, description="Fehlermeldung bei failed")
|
error: Optional[str] = Field(None, description="Fehlermeldung bei failed")
|
||||||
started_at: Optional[str] = Field(None, description="Start-Timestamp (ISO)")
|
started_at: Optional[str] = Field(None, description="Start-Timestamp (ISO)")
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ This document establishes **mandatory governance rules** for placeholder managem
|
|||||||
## 2. Scope
|
## 2. Scope
|
||||||
|
|
||||||
These guidelines apply to:
|
These guidelines apply to:
|
||||||
- All 116 existing placeholders
|
- All 114 existing placeholders (canonical: `PLACEHOLDER_MAP`)
|
||||||
- All new placeholders
|
- All new placeholders
|
||||||
- All modifications to existing placeholders
|
- All modifications to existing placeholders
|
||||||
- All placeholder deprecations
|
- All placeholder deprecations
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ curl -s -H "X-Auth-Token: $TOKEN" \
|
|||||||
**Expected response:**
|
**Expected response:**
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"total_placeholders": 116,
|
"total_placeholders": 114,
|
||||||
"available": 98,
|
"available": 98,
|
||||||
"missing": 18,
|
"missing": 18,
|
||||||
"by_type": {
|
"by_type": {
|
||||||
|
|||||||
@@ -9,12 +9,12 @@
|
|||||||
|
|
||||||
## Executive Summary
|
## Executive Summary
|
||||||
|
|
||||||
This document summarizes the complete implementation of the normative placeholder metadata system for Mitai Jinkendo. The system provides a comprehensive, standardized framework for managing, documenting, and validating all 116 placeholders in the system.
|
This document summarizes the complete implementation of the normative placeholder metadata system for Mitai Jinkendo. The system provides a comprehensive, standardized framework for managing, documenting, and validating all 114 placeholders in the system.
|
||||||
|
|
||||||
**Key Achievements:**
|
**Key Achievements:**
|
||||||
- ✅ Complete metadata schema (normative compliant)
|
- ✅ Complete metadata schema (normative compliant)
|
||||||
- ✅ Automatic metadata extraction
|
- ✅ Automatic metadata extraction
|
||||||
- ✅ Manual curation for 116 placeholders
|
- ✅ Manual curation for 114 placeholders
|
||||||
- ✅ Extended export API (non-breaking)
|
- ✅ Extended export API (non-breaking)
|
||||||
- ✅ Catalog generator (4 documentation files)
|
- ✅ Catalog generator (4 documentation files)
|
||||||
- ✅ Validation & testing framework
|
- ✅ Validation & testing framework
|
||||||
@@ -75,7 +75,7 @@ This document summarizes the complete implementation of the normative placeholde
|
|||||||
|
|
||||||
### 1.3 Complete Metadata Definitions
|
### 1.3 Complete Metadata Definitions
|
||||||
|
|
||||||
#### `backend/placeholder_metadata_complete.py` (220 lines, expandable to all 116)
|
#### `backend/placeholder_metadata_complete.py` (220 lines, expandable to all 114)
|
||||||
|
|
||||||
**Purpose:** Manually curated, authoritative metadata for all placeholders
|
**Purpose:** Manually curated, authoritative metadata for all placeholders
|
||||||
|
|
||||||
@@ -106,7 +106,7 @@ PlaceholderMetadata(
|
|||||||
|
|
||||||
**Key Features:**
|
**Key Features:**
|
||||||
- Hand-curated for accuracy
|
- Hand-curated for accuracy
|
||||||
- Complete for all 116 placeholders
|
- Complete for all 114 placeholders
|
||||||
- Serves as authoritative source
|
- Serves as authoritative source
|
||||||
- Normative compliant
|
- Normative compliant
|
||||||
|
|
||||||
@@ -285,7 +285,7 @@ pytest backend/tests/test_placeholder_metadata.py -v
|
|||||||
v
|
v
|
||||||
┌─────────────────────────────────────────────────────────────┐
|
┌─────────────────────────────────────────────────────────────┐
|
||||||
│ Complete Registry │
|
│ Complete Registry │
|
||||||
│ (116 placeholders with full metadata) │
|
│ (114 placeholders with full metadata) │
|
||||||
└──────────┬──────────────────────────────────────────────────┘
|
└──────────┬──────────────────────────────────────────────────┘
|
||||||
│
|
│
|
||||||
├──> Generation Scripts (generate_*.py)
|
├──> Generation Scripts (generate_*.py)
|
||||||
@@ -309,7 +309,7 @@ pytest backend/tests/test_placeholder_metadata.py -v
|
|||||||
### 3.1 Metadata Extraction Flow
|
### 3.1 Metadata Extraction Flow
|
||||||
|
|
||||||
```
|
```
|
||||||
1. PLACEHOLDER_MAP (116 entries)
|
1. PLACEHOLDER_MAP (114 entries)
|
||||||
└─> extract_resolver_name()
|
└─> extract_resolver_name()
|
||||||
└─> analyze_data_layer_usage()
|
└─> analyze_data_layer_usage()
|
||||||
└─> infer_type/time_window/output_type()
|
└─> infer_type/time_window/output_type()
|
||||||
@@ -468,7 +468,7 @@ curl -H "X-Auth-Token: <token>" \
|
|||||||
|
|
||||||
# Output:
|
# Output:
|
||||||
{
|
{
|
||||||
"total_placeholders": 116,
|
"total_placeholders": 114,
|
||||||
"available": 98,
|
"available": 98,
|
||||||
"missing": 18,
|
"missing": 18,
|
||||||
"by_type": {
|
"by_type": {
|
||||||
@@ -599,7 +599,7 @@ The system is designed for extensibility:
|
|||||||
## 8. Compliance Checklist
|
## 8. Compliance Checklist
|
||||||
|
|
||||||
✅ **Normative Standard Compliance:**
|
✅ **Normative Standard Compliance:**
|
||||||
- All 116 placeholders inventoried
|
- All 114 placeholders inventoried
|
||||||
- Complete metadata schema implemented
|
- Complete metadata schema implemented
|
||||||
- Validation framework in place
|
- Validation framework in place
|
||||||
- Non-breaking export API
|
- Non-breaking export API
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ Dieser Ordner ist **immer mit Git versioniert**. Er ergänzt **`.claude/docs/`**
|
|||||||
| `issue-53-phase-0c-multi-layer-architecture.md` |
|
| `issue-53-phase-0c-multi-layer-architecture.md` |
|
||||||
| `issue-54-dynamic-placeholder-system.md` |
|
| `issue-54-dynamic-placeholder-system.md` |
|
||||||
| `issue-55-dynamic-aggregation-methods.md` |
|
| `issue-55-dynamic-aggregation-methods.md` |
|
||||||
|
| `issue-76-training-quality-goal-list-filter.md` |
|
||||||
| `PHASE_PLAN_RESPONSIVE_UI.md` |
|
| `PHASE_PLAN_RESPONSIVE_UI.md` |
|
||||||
| `REVIEW_OPEN_ISSUES_2026-04-04.md` |
|
| `REVIEW_OPEN_ISSUES_2026-04-04.md` |
|
||||||
| `UMSETZUNGSPLAN_TRAININGSPROFILE_SPORTSPEZIFISCH_2026-04-05.md` |
|
| `UMSETZUNGSPLAN_TRAININGSPROFILE_SPORTSPEZIFISCH_2026-04-05.md` |
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
# Issue #76: Trainings-Qualität – Zielbezug + Listen-Filter
|
||||||
|
|
||||||
|
**Gitea:** http://192.168.2.144:3000/Lars/mitai-jinkendo/issues/76
|
||||||
|
**Status:** OFFEN
|
||||||
|
**Erstellt:** 11.04.2026
|
||||||
|
**Typ:** Enhancement (Backend, Frontend, Produkt)
|
||||||
|
**Verwandt:** #31 (globaler Quality-Filter), #15 (Quality-Filter KI/Charts – historisch)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Kurzfassung
|
||||||
|
|
||||||
|
Die heutige **`quality_label` / `quality_filter_level`**-Logik soll überdacht werden: nicht nur **Akzeptanz** (formales Label), sondern langfristig **Nutzen für die Zielerreichung**. Parallel: der **globale Profil-Filter** in der **Aktivitätsliste** versteckt u. a. **fehlende Einstufungen** (`NULL`) – das erschwert Datenpflege und Transparenz.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Ist-Zustand (technisch)
|
||||||
|
|
||||||
|
| Bereich | Filter nach `quality_filter_level`? |
|
||||||
|
|---------|--------------------------------------|
|
||||||
|
| `GET` Aktivitätsliste (`routers/activity.py`) | Ja (`get_quality_filter_sql`) |
|
||||||
|
| KI-Rohkontext (`routers/insights.py`, `_get_profile_data`) | Ja (letzte 90 Zeilen `activity_log`) |
|
||||||
|
| Data-Layer / Platzhalter / Chart-Aggregate (`data_layer/activity_metrics.py`) | Nein – alle Einträge im Zeitfenster |
|
||||||
|
|
||||||
|
Bei Stufen ≠ `all` schließt SQL-`IN` typischerweise **`NULL`** und nicht erlaubte Labels aus.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Ziele
|
||||||
|
|
||||||
|
1. **Konzept „Qualität“:** Richtung Ziel-/Focus-/Phasenbezug; Umsetzung kann in Folge-Issues zerlegt werden.
|
||||||
|
2. **Listendarstellung:** Standard soll **alle** relevanten Einträge zeigen; **optionale lokale** Filter (inkl. „ohne Einstufung“), kein unbemerktes globales Ausblenden.
|
||||||
|
3. **KI/Insights:** Klarstellen, ob gefilterte vs. vollständige Aktivitätsmenge; ggf. konfigurierbar oder im Prompt kenntlich.
|
||||||
|
4. **Dokumentation:** Kurz festhalten, wo `get_quality_filter_sql` greift und wie das zu den Aggregationen passt.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Code-Referenzen
|
||||||
|
|
||||||
|
- `backend/quality_filter.py`
|
||||||
|
- `backend/routers/activity.py` (`list_activity`)
|
||||||
|
- `backend/routers/insights.py` (`_get_profile_data`)
|
||||||
|
- `backend/data_layer/activity_metrics.py`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Akzeptanz (aus Gitea, Checkliste)
|
||||||
|
|
||||||
|
Siehe Issue-Body in Gitea #76; bei Umsetzung dort Häkchen setzen und diese Datei ggf. um **Umsetzungsnotizen / Abnahme** ergänzen.
|
||||||
@@ -8,6 +8,9 @@ server {
|
|||||||
proxy_set_header Host $host;
|
proxy_set_header Host $host;
|
||||||
proxy_set_header X-Real-IP $remote_addr;
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
client_max_body_size 20M;
|
client_max_body_size 20M;
|
||||||
|
proxy_read_timeout 300s;
|
||||||
|
proxy_connect_timeout 60s;
|
||||||
|
proxy_send_timeout 60s;
|
||||||
}
|
}
|
||||||
|
|
||||||
location / {
|
location / {
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ import AdminCouponsPage from './pages/AdminCouponsPage'
|
|||||||
import AdminUserRestrictionsPage from './pages/AdminUserRestrictionsPage'
|
import AdminUserRestrictionsPage from './pages/AdminUserRestrictionsPage'
|
||||||
import AdminTrainingTypesPage from './pages/AdminTrainingTypesPage'
|
import AdminTrainingTypesPage from './pages/AdminTrainingTypesPage'
|
||||||
import AdminActivityMappingsPage from './pages/AdminActivityMappingsPage'
|
import AdminActivityMappingsPage from './pages/AdminActivityMappingsPage'
|
||||||
|
import AdminActivityAttributeProfilesPage from './pages/AdminActivityAttributeProfilesPage'
|
||||||
import AdminTrainingProfiles from './pages/AdminTrainingProfiles'
|
import AdminTrainingProfiles from './pages/AdminTrainingProfiles'
|
||||||
import AdminPromptsPage from './pages/AdminPromptsPage'
|
import AdminPromptsPage from './pages/AdminPromptsPage'
|
||||||
import AdminGoalTypesPage from './pages/AdminGoalTypesPage'
|
import AdminGoalTypesPage from './pages/AdminGoalTypesPage'
|
||||||
@@ -255,6 +256,7 @@ function AppShell() {
|
|||||||
<Route path="widget-features" element={<AdminWidgetFeatureAssignmentsPage />} />
|
<Route path="widget-features" element={<AdminWidgetFeatureAssignmentsPage />} />
|
||||||
<Route path="training-types" element={<AdminTrainingTypesPage/>}/>
|
<Route path="training-types" element={<AdminTrainingTypesPage/>}/>
|
||||||
<Route path="activity-mappings" element={<AdminActivityMappingsPage/>}/>
|
<Route path="activity-mappings" element={<AdminActivityMappingsPage/>}/>
|
||||||
|
<Route path="activity-attribute-profiles" element={<AdminActivityAttributeProfilesPage />} />
|
||||||
<Route path="training-profiles" element={<AdminTrainingProfiles/>}/>
|
<Route path="training-profiles" element={<AdminTrainingProfiles/>}/>
|
||||||
<Route path="prompts" element={<AdminPromptsPage/>}/>
|
<Route path="prompts" element={<AdminPromptsPage/>}/>
|
||||||
<Route path="goal-types" element={<AdminGoalTypesPage/>}/>
|
<Route path="goal-types" element={<AdminGoalTypesPage/>}/>
|
||||||
|
|||||||
@@ -440,6 +440,141 @@ a.analysis-split__nav-item {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Admin: Session-Metriken / Attributprofile — volle Breite, linksbündig (nicht globale 90px-Zahlfelder) */
|
||||||
|
.activity-attribute-profiles .aaf-stack {
|
||||||
|
max-width: 42rem;
|
||||||
|
}
|
||||||
|
.activity-attribute-profiles .aaf-field {
|
||||||
|
margin-bottom: 1rem;
|
||||||
|
}
|
||||||
|
.activity-attribute-profiles .aaf-label {
|
||||||
|
display: block;
|
||||||
|
font-size: 14px;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--text1);
|
||||||
|
text-align: left;
|
||||||
|
margin-bottom: 6px;
|
||||||
|
line-height: 1.35;
|
||||||
|
}
|
||||||
|
.activity-attribute-profiles .aaf-sublabel {
|
||||||
|
display: block;
|
||||||
|
font-size: 12px;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--text2);
|
||||||
|
text-align: left;
|
||||||
|
margin-bottom: 4px;
|
||||||
|
}
|
||||||
|
.activity-attribute-profiles .aaf-hint {
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--text3);
|
||||||
|
text-align: left;
|
||||||
|
margin: 6px 0 0;
|
||||||
|
line-height: 1.45;
|
||||||
|
}
|
||||||
|
.activity-attribute-profiles .aaf-input,
|
||||||
|
.activity-attribute-profiles textarea.aaf-input {
|
||||||
|
display: block;
|
||||||
|
width: 100%;
|
||||||
|
box-sizing: border-box;
|
||||||
|
padding: 10px 12px;
|
||||||
|
text-align: left;
|
||||||
|
font-family: var(--font);
|
||||||
|
font-size: 15px;
|
||||||
|
font-weight: 500;
|
||||||
|
color: var(--text1);
|
||||||
|
background: var(--surface2);
|
||||||
|
border: 1.5px solid var(--border2);
|
||||||
|
border-radius: 8px;
|
||||||
|
transition: border-color 0.15s;
|
||||||
|
}
|
||||||
|
.activity-attribute-profiles textarea.aaf-input {
|
||||||
|
resize: vertical;
|
||||||
|
min-height: 4.5rem;
|
||||||
|
font-weight: 400;
|
||||||
|
}
|
||||||
|
.activity-attribute-profiles .aaf-input:focus {
|
||||||
|
outline: none;
|
||||||
|
border-color: var(--accent);
|
||||||
|
}
|
||||||
|
.activity-attribute-profiles .aaf-split {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: 1fr 1fr;
|
||||||
|
gap: 12px;
|
||||||
|
}
|
||||||
|
@media (max-width: 560px) {
|
||||||
|
.activity-attribute-profiles .aaf-split {
|
||||||
|
grid-template-columns: 1fr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.activity-attribute-profiles .aaf-field-select {
|
||||||
|
padding: 12px 0;
|
||||||
|
border-bottom: 1px solid var(--border);
|
||||||
|
}
|
||||||
|
.activity-attribute-profiles .aaf-field-select:last-child {
|
||||||
|
border-bottom: none;
|
||||||
|
}
|
||||||
|
.activity-attribute-profiles .aaf-field-select .form-label {
|
||||||
|
display: block;
|
||||||
|
text-align: left;
|
||||||
|
margin-bottom: 6px;
|
||||||
|
font-weight: 600;
|
||||||
|
flex: unset;
|
||||||
|
}
|
||||||
|
.activity-attribute-profiles .aaf-field-select .form-input,
|
||||||
|
.activity-attribute-profiles .aaf-field-select select.form-input {
|
||||||
|
width: 100%;
|
||||||
|
max-width: none;
|
||||||
|
min-width: 0;
|
||||||
|
text-align: left;
|
||||||
|
box-sizing: border-box;
|
||||||
|
padding: 10px 12px;
|
||||||
|
}
|
||||||
|
.activity-attribute-profiles .aaf-toolbar {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
align-items: flex-end;
|
||||||
|
gap: 12px;
|
||||||
|
padding: 12px 0;
|
||||||
|
border-bottom: 1px solid var(--border);
|
||||||
|
}
|
||||||
|
.activity-attribute-profiles .aaf-toolbar .form-label {
|
||||||
|
display: block;
|
||||||
|
text-align: left;
|
||||||
|
margin-bottom: 6px;
|
||||||
|
font-weight: 600;
|
||||||
|
flex: unset;
|
||||||
|
}
|
||||||
|
.activity-attribute-profiles .aaf-toolbar__grow {
|
||||||
|
flex: 1 1 240px;
|
||||||
|
min-width: 0;
|
||||||
|
}
|
||||||
|
.activity-attribute-profiles .aaf-toolbar .form-input,
|
||||||
|
.activity-attribute-profiles .aaf-toolbar select.form-input {
|
||||||
|
width: 100%;
|
||||||
|
min-width: 140px;
|
||||||
|
max-width: none;
|
||||||
|
text-align: left;
|
||||||
|
box-sizing: border-box;
|
||||||
|
padding: 10px 12px;
|
||||||
|
}
|
||||||
|
.activity-attribute-profiles .aaf-toolbar__compact {
|
||||||
|
flex: 0 0 auto;
|
||||||
|
}
|
||||||
|
.activity-attribute-profiles .aaf-toolbar__compact .form-input,
|
||||||
|
.activity-attribute-profiles .aaf-toolbar__compact select.form-input {
|
||||||
|
width: 100%;
|
||||||
|
min-width: 5rem;
|
||||||
|
}
|
||||||
|
.activity-attribute-profiles .aaf-inline-edit .form-input,
|
||||||
|
.activity-attribute-profiles .aaf-inline-edit select.form-input {
|
||||||
|
text-align: left;
|
||||||
|
min-width: 4.5rem;
|
||||||
|
width: auto;
|
||||||
|
max-width: none;
|
||||||
|
box-sizing: border-box;
|
||||||
|
padding: 8px 10px;
|
||||||
|
}
|
||||||
|
|
||||||
/* Erfassung: Sub-Navigation (Mobil = Chips, Desktop = linke Spalte) */
|
/* Erfassung: Sub-Navigation (Mobil = Chips, Desktop = linke Spalte) */
|
||||||
.capture-shell {
|
.capture-shell {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
|
|||||||
@@ -1,7 +1,264 @@
|
|||||||
import { useState, useEffect, useRef } from 'react'
|
import { useState, useEffect, useRef, useLayoutEffect } from 'react'
|
||||||
import { api } from '../utils/api'
|
import { api } from '../utils/api'
|
||||||
import { X, Plus, Trash2, MoveUp, MoveDown, Code } from 'lucide-react'
|
import { X, Plus, Trash2, MoveUp, MoveDown, Code } from 'lucide-react'
|
||||||
import PlaceholderPicker from './PlaceholderPicker'
|
import PlaceholderPicker from './PlaceholderPicker'
|
||||||
|
import Markdown from '../utils/Markdown'
|
||||||
|
|
||||||
|
/** Debug aus erfolgreicher Ausführung oder aus FastAPI-Fehlerdetail */
|
||||||
|
function extractExecutionDebug(testResult) {
|
||||||
|
if (!testResult) return null
|
||||||
|
const raw = testResult.debug ?? testResult
|
||||||
|
if (raw && typeof raw === 'object' && raw.detail && raw.detail.debug) {
|
||||||
|
return { ...raw.detail.debug, ...raw.detail }
|
||||||
|
}
|
||||||
|
return raw
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Vollständiger Prompt-Text für die Anzeige (Basis oder Pipeline-Stufen) */
|
||||||
|
function buildPromptTextForDisplay(testResult) {
|
||||||
|
if (!testResult || testResult.error) return ''
|
||||||
|
const dbg = extractExecutionDebug(testResult)
|
||||||
|
const t = testResult.type
|
||||||
|
if (t === 'base' && dbg?.final_prompt) return dbg.final_prompt
|
||||||
|
if (t === 'pipeline' && dbg?.stages?.length) {
|
||||||
|
const parts = []
|
||||||
|
for (const st of dbg.stages) {
|
||||||
|
for (const p of st.prompts || []) {
|
||||||
|
const label =
|
||||||
|
p.source === 'reference'
|
||||||
|
? `Stage ${st.stage} · Ref: ${p.ref_slug || '?'}`
|
||||||
|
: `Stage ${st.stage} · Inline`
|
||||||
|
const text =
|
||||||
|
p.final_prompt ||
|
||||||
|
(p.ref_debug && typeof p.ref_debug === 'object' ? p.ref_debug.final_prompt : '') ||
|
||||||
|
''
|
||||||
|
if (text) parts.push(`── ${label} ──\n${text}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return parts.join('\n\n')
|
||||||
|
}
|
||||||
|
return dbg?.final_prompt || ''
|
||||||
|
}
|
||||||
|
|
||||||
|
function sanitizeDebugForPanel(dbg, resultType) {
|
||||||
|
if (!dbg) return {}
|
||||||
|
const o = { ...dbg }
|
||||||
|
delete o.final_prompt
|
||||||
|
delete o.template
|
||||||
|
delete o.ai_response_preview
|
||||||
|
if (o.stages && resultType === 'pipeline') {
|
||||||
|
o.stages = o.stages.map((s) => ({
|
||||||
|
stage: s.stage,
|
||||||
|
available_variables: s.available_variables,
|
||||||
|
output: s.output,
|
||||||
|
prompts: (s.prompts || []).map((p) => ({
|
||||||
|
source: p.source,
|
||||||
|
ref_slug: p.ref_slug,
|
||||||
|
output_key: p.output_key,
|
||||||
|
context_var_key: p.context_var_key,
|
||||||
|
resolved_placeholders: p.resolved_placeholders || p.ref_debug?.resolved_placeholders,
|
||||||
|
unresolved_placeholders: p.unresolved_placeholders || p.ref_debug?.unresolved_placeholders,
|
||||||
|
warnings: p.warnings || p.ref_debug?.warnings,
|
||||||
|
ai_response_length: p.ai_response_length
|
||||||
|
}))
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Langer Inhalt: zunächst auf maxRem eingeklappt, „Mehr/Weniger anzeigen“ */
|
||||||
|
function ExpandableCollapsible({ children, contentKey, maxRem = 28 }) {
|
||||||
|
const [expanded, setExpanded] = useState(false)
|
||||||
|
const wrapRef = useRef(null)
|
||||||
|
const [showToggle, setShowToggle] = useState(false)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setExpanded(false)
|
||||||
|
}, [contentKey])
|
||||||
|
|
||||||
|
useLayoutEffect(() => {
|
||||||
|
const el = wrapRef.current
|
||||||
|
if (!el) return
|
||||||
|
if (expanded) {
|
||||||
|
setShowToggle(true)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
setShowToggle(el.scrollHeight > el.clientHeight + 2)
|
||||||
|
}, [contentKey, expanded])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<div
|
||||||
|
ref={wrapRef}
|
||||||
|
style={{
|
||||||
|
maxHeight: expanded ? 'none' : `${maxRem}rem`,
|
||||||
|
overflow: expanded ? 'visible' : 'hidden'
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</div>
|
||||||
|
{showToggle && (
|
||||||
|
<div style={{ marginTop: 10, textAlign: 'center' }}>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="btn btn-secondary"
|
||||||
|
style={{ fontSize: 12, padding: '6px 14px' }}
|
||||||
|
onClick={() => setExpanded((e) => !e)}
|
||||||
|
>
|
||||||
|
{expanded ? 'Weniger anzeigen' : 'Mehr anzeigen'}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderTestOutput(testResult) {
|
||||||
|
if (!testResult) return null
|
||||||
|
if (testResult.error) {
|
||||||
|
const d = testResult.debug?.detail
|
||||||
|
const msg =
|
||||||
|
typeof d === 'string'
|
||||||
|
? d
|
||||||
|
: d?.error || d?.message || testResult.error_message || 'Unbekannter Fehler'
|
||||||
|
return (
|
||||||
|
<div style={{ color: 'var(--danger)', fontSize: 14, lineHeight: 1.5 }}>
|
||||||
|
{typeof msg === 'string' ? msg : JSON.stringify(msg, null, 2)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const out = testResult.output
|
||||||
|
const fmt = testResult.output_format
|
||||||
|
const typ = testResult.type
|
||||||
|
|
||||||
|
if (out == null) {
|
||||||
|
return <span style={{ color: 'var(--text3)' }}>Keine Ausgabe</span>
|
||||||
|
}
|
||||||
|
|
||||||
|
const proseBox = (content, contentKey) => (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
padding: 16,
|
||||||
|
background: 'var(--bg)',
|
||||||
|
borderRadius: 8,
|
||||||
|
border: '1px solid var(--border)',
|
||||||
|
fontSize: 14,
|
||||||
|
lineHeight: 1.55,
|
||||||
|
color: 'var(--text1)',
|
||||||
|
textAlign: 'left'
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{contentKey != null ? (
|
||||||
|
<ExpandableCollapsible contentKey={contentKey}>{content}</ExpandableCollapsible>
|
||||||
|
) : (
|
||||||
|
content
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
|
||||||
|
if (typ === 'pipeline' && out && typeof out === 'object' && !Array.isArray(out)) {
|
||||||
|
return (
|
||||||
|
<div style={{ display: 'grid', gap: 16 }}>
|
||||||
|
{Object.entries(out).map(([key, val]) => (
|
||||||
|
<div key={key}>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
fontSize: 12,
|
||||||
|
fontWeight: 600,
|
||||||
|
color: 'var(--text2)',
|
||||||
|
marginBottom: 8,
|
||||||
|
textAlign: 'left'
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{key}
|
||||||
|
</div>
|
||||||
|
{typeof val === 'string' ? (
|
||||||
|
proseBox(
|
||||||
|
<Markdown text={val} />,
|
||||||
|
`pipe-${key}-${val.length}-${val.slice(0, 120)}`
|
||||||
|
)
|
||||||
|
) : (
|
||||||
|
<ExpandableCollapsible contentKey={`pipe-json-${key}-${JSON.stringify(val).slice(0, 200)}`} maxRem={22}>
|
||||||
|
<pre
|
||||||
|
style={{
|
||||||
|
margin: 0,
|
||||||
|
padding: 12,
|
||||||
|
background: 'var(--bg)',
|
||||||
|
borderRadius: 8,
|
||||||
|
border: '1px solid var(--border)',
|
||||||
|
fontSize: 12,
|
||||||
|
overflow: 'auto',
|
||||||
|
textAlign: 'left'
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{JSON.stringify(val, null, 2)}
|
||||||
|
</pre>
|
||||||
|
</ExpandableCollapsible>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof out === 'string') {
|
||||||
|
if (fmt === 'json') {
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(out)
|
||||||
|
const jsonStr = JSON.stringify(parsed, null, 2)
|
||||||
|
return (
|
||||||
|
<ExpandableCollapsible contentKey={`json-out-${jsonStr.length}-${jsonStr.slice(0, 80)}`} maxRem={26}>
|
||||||
|
<pre
|
||||||
|
style={{
|
||||||
|
margin: 0,
|
||||||
|
padding: 12,
|
||||||
|
background: 'var(--bg)',
|
||||||
|
borderRadius: 8,
|
||||||
|
border: '1px solid var(--border)',
|
||||||
|
fontSize: 12,
|
||||||
|
overflow: 'auto',
|
||||||
|
textAlign: 'left'
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{jsonStr}
|
||||||
|
</pre>
|
||||||
|
</ExpandableCollapsible>
|
||||||
|
)
|
||||||
|
} catch {
|
||||||
|
return proseBox(
|
||||||
|
<pre style={{ margin: 0, whiteSpace: 'pre-wrap' }}>{out}</pre>,
|
||||||
|
`json-fail-${out.length}-${out.slice(0, 80)}`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return proseBox(<Markdown text={out} />, `text-out-${out.length}-${out.slice(0, 120)}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof out === 'object') {
|
||||||
|
const jsonStr = JSON.stringify(out, null, 2)
|
||||||
|
return (
|
||||||
|
<ExpandableCollapsible contentKey={`obj-out-${jsonStr.length}-${jsonStr.slice(0, 120)}`} maxRem={26}>
|
||||||
|
<pre
|
||||||
|
style={{
|
||||||
|
margin: 0,
|
||||||
|
padding: 12,
|
||||||
|
background: 'var(--bg)',
|
||||||
|
borderRadius: 8,
|
||||||
|
border: '1px solid var(--border)',
|
||||||
|
fontSize: 12,
|
||||||
|
overflow: 'auto',
|
||||||
|
textAlign: 'left'
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{jsonStr}
|
||||||
|
</pre>
|
||||||
|
</ExpandableCollapsible>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return <span>{String(out)}</span>
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unified Prompt Editor Modal (Issue #28 Phase 3)
|
* Unified Prompt Editor Modal (Issue #28 Phase 3)
|
||||||
@@ -47,6 +304,8 @@ export default function UnifiedPromptModal({ prompt, onSave, onClose }) {
|
|||||||
const [testing, setTesting] = useState(false)
|
const [testing, setTesting] = useState(false)
|
||||||
const [testResult, setTestResult] = useState(null)
|
const [testResult, setTestResult] = useState(null)
|
||||||
const [showDebug, setShowDebug] = useState(false)
|
const [showDebug, setShowDebug] = useState(false)
|
||||||
|
/** 'response' | 'prompt' | 'debug' */
|
||||||
|
const [testResultTab, setTestResultTab] = useState('response')
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadAvailablePrompts()
|
loadAvailablePrompts()
|
||||||
@@ -280,6 +539,7 @@ export default function UnifiedPromptModal({ prompt, onSave, onClose }) {
|
|||||||
try {
|
try {
|
||||||
const result = await api.executeUnifiedPrompt(prompt.slug, null, null, true)
|
const result = await api.executeUnifiedPrompt(prompt.slug, null, null, true)
|
||||||
setTestResult(result)
|
setTestResult(result)
|
||||||
|
setTestResultTab('response')
|
||||||
setShowDebug(true)
|
setShowDebug(true)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Show error AND try to extract debug info from error
|
// Show error AND try to extract debug info from error
|
||||||
@@ -290,7 +550,12 @@ export default function UnifiedPromptModal({ prompt, onSave, onClose }) {
|
|||||||
try {
|
try {
|
||||||
const parsed = JSON.parse(errorMsg)
|
const parsed = JSON.parse(errorMsg)
|
||||||
if (parsed.detail) {
|
if (parsed.detail) {
|
||||||
setError('Test-Fehler: ' + parsed.detail)
|
const d = parsed.detail
|
||||||
|
const msg =
|
||||||
|
typeof d === 'string'
|
||||||
|
? d
|
||||||
|
: d.error || d.message || d.msg || JSON.stringify(d).slice(0, 500)
|
||||||
|
setError('Test-Fehler: ' + msg)
|
||||||
debugData = parsed
|
debugData = parsed
|
||||||
} else {
|
} else {
|
||||||
setError('Test-Fehler: ' + errorMsg)
|
setError('Test-Fehler: ' + errorMsg)
|
||||||
@@ -305,6 +570,7 @@ export default function UnifiedPromptModal({ prompt, onSave, onClose }) {
|
|||||||
error_message: errorMsg,
|
error_message: errorMsg,
|
||||||
debug: debugData || { error: errorMsg }
|
debug: debugData || { error: errorMsg }
|
||||||
})
|
})
|
||||||
|
setTestResultTab('response')
|
||||||
setShowDebug(true) // ALWAYS show debug on test, even on error
|
setShowDebug(true) // ALWAYS show debug on test, even on error
|
||||||
} finally {
|
} finally {
|
||||||
setTesting(false)
|
setTesting(false)
|
||||||
@@ -315,7 +581,12 @@ export default function UnifiedPromptModal({ prompt, onSave, onClose }) {
|
|||||||
if (!testResult) return
|
if (!testResult) return
|
||||||
|
|
||||||
// Extract all placeholder data from test result
|
// Extract all placeholder data from test result
|
||||||
const debug = testResult.debug || testResult
|
const raw = testResult.debug || testResult
|
||||||
|
// API-Fehler: FastAPI liefert { detail: { error, debug: { resolved_placeholders, ... } } }
|
||||||
|
const debug =
|
||||||
|
raw && typeof raw === 'object' && raw.detail && raw.detail.debug
|
||||||
|
? { ...raw.detail.debug, ...raw.detail }
|
||||||
|
: raw
|
||||||
const exportData = {
|
const exportData = {
|
||||||
export_date: new Date().toISOString(),
|
export_date: new Date().toISOString(),
|
||||||
prompt_slug: prompt?.slug || 'unknown',
|
prompt_slug: prompt?.slug || 'unknown',
|
||||||
@@ -711,7 +982,7 @@ export default function UnifiedPromptModal({ prompt, onSave, onClose }) {
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* Debug Output */}
|
{/* Test-Ergebnis: Antwort · Prompt · Debug */}
|
||||||
{showDebug && testResult && (
|
{showDebug && testResult && (
|
||||||
<div style={{
|
<div style={{
|
||||||
marginTop: 16,
|
marginTop: 16,
|
||||||
@@ -724,12 +995,14 @@ export default function UnifiedPromptModal({ prompt, onSave, onClose }) {
|
|||||||
display: 'flex',
|
display: 'flex',
|
||||||
justifyContent: 'space-between',
|
justifyContent: 'space-between',
|
||||||
alignItems: 'center',
|
alignItems: 'center',
|
||||||
marginBottom: 12
|
marginBottom: 12,
|
||||||
|
flexWrap: 'wrap',
|
||||||
|
gap: 8
|
||||||
}}>
|
}}>
|
||||||
<h3 style={{ margin: 0, fontSize: 14, fontWeight: 600 }}>
|
<h3 style={{ margin: 0, fontSize: 14, fontWeight: 600 }}>
|
||||||
🔬 Debug-Info
|
Test-Ergebnis
|
||||||
</h3>
|
</h3>
|
||||||
<div style={{ display: 'flex', gap: 8, alignItems: 'center' }}>
|
<div style={{ display: 'flex', gap: 8, alignItems: 'center', flexWrap: 'wrap' }}>
|
||||||
<button
|
<button
|
||||||
className="btn"
|
className="btn"
|
||||||
onClick={handleExportPlaceholders}
|
onClick={handleExportPlaceholders}
|
||||||
@@ -739,31 +1012,120 @@ export default function UnifiedPromptModal({ prompt, onSave, onClose }) {
|
|||||||
background: 'var(--accent)',
|
background: 'var(--accent)',
|
||||||
color: 'white'
|
color: 'white'
|
||||||
}}
|
}}
|
||||||
title="Exportiere alle Platzhalter mit Werten als JSON"
|
title="Exportiere Platzhalter / Kontext als JSON"
|
||||||
>
|
>
|
||||||
📋 Platzhalter exportieren
|
📋 Kontext exportieren
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
onClick={() => setShowDebug(false)}
|
onClick={() => setShowDebug(false)}
|
||||||
style={{ background: 'none', border: 'none', cursor: 'pointer', padding: 4 }}
|
style={{ background: 'none', border: 'none', cursor: 'pointer', padding: 4 }}
|
||||||
|
aria-label="Schließen"
|
||||||
>
|
>
|
||||||
<X size={16} color="var(--text3)" />
|
<X size={16} color="var(--text3)" />
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<pre style={{
|
|
||||||
fontSize: 11,
|
<div
|
||||||
fontFamily: 'monospace',
|
role="tablist"
|
||||||
background: 'var(--bg)',
|
style={{
|
||||||
padding: 12,
|
display: 'flex',
|
||||||
borderRadius: 6,
|
gap: 6,
|
||||||
overflow: 'auto',
|
marginBottom: 12,
|
||||||
maxHeight: 400,
|
flexWrap: 'wrap',
|
||||||
lineHeight: 1.5,
|
borderBottom: '1px solid var(--border)',
|
||||||
color: 'var(--text2)'
|
paddingBottom: 8
|
||||||
}}>
|
}}
|
||||||
{JSON.stringify(testResult.debug || testResult, null, 2)}
|
>
|
||||||
</pre>
|
{[
|
||||||
|
{ id: 'response', label: 'Antwort' },
|
||||||
|
{ id: 'prompt', label: 'Prompt (an LLM)' },
|
||||||
|
{ id: 'debug', label: 'Debug' }
|
||||||
|
].map((tab) => (
|
||||||
|
<button
|
||||||
|
key={tab.id}
|
||||||
|
type="button"
|
||||||
|
role="tab"
|
||||||
|
aria-selected={testResultTab === tab.id}
|
||||||
|
onClick={() => setTestResultTab(tab.id)}
|
||||||
|
style={{
|
||||||
|
padding: '6px 12px',
|
||||||
|
fontSize: 12,
|
||||||
|
borderRadius: 6,
|
||||||
|
border: '1px solid var(--border)',
|
||||||
|
background: testResultTab === tab.id ? 'var(--accent)' : 'var(--surface)',
|
||||||
|
color: testResultTab === tab.id ? '#fff' : 'var(--text2)',
|
||||||
|
cursor: 'pointer',
|
||||||
|
fontWeight: testResultTab === tab.id ? 600 : 400
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{tab.label}
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div
|
||||||
|
role="tabpanel"
|
||||||
|
style={{
|
||||||
|
minHeight: 120,
|
||||||
|
maxHeight: testResultTab === 'prompt' ? 480 : 420,
|
||||||
|
overflow: 'auto',
|
||||||
|
textAlign: 'left'
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{testResultTab === 'response' && (
|
||||||
|
<div style={{ paddingRight: 4 }}>
|
||||||
|
{renderTestOutput(testResult)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{testResultTab === 'prompt' && (
|
||||||
|
<pre
|
||||||
|
style={{
|
||||||
|
margin: 0,
|
||||||
|
fontSize: 12,
|
||||||
|
fontFamily: 'ui-monospace, monospace',
|
||||||
|
lineHeight: 1.45,
|
||||||
|
whiteSpace: 'pre-wrap',
|
||||||
|
wordBreak: 'break-word',
|
||||||
|
color: 'var(--text1)',
|
||||||
|
padding: 12,
|
||||||
|
background: 'var(--bg)',
|
||||||
|
borderRadius: 8,
|
||||||
|
border: '1px solid var(--border)'
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{buildPromptTextForDisplay(testResult) || '(Kein Prompt-Text in der Antwort — evtl. Fehler vor Ausführung.)'}
|
||||||
|
</pre>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{testResultTab === 'debug' && (
|
||||||
|
<pre
|
||||||
|
style={{
|
||||||
|
margin: 0,
|
||||||
|
fontSize: 11,
|
||||||
|
fontFamily: 'ui-monospace, monospace',
|
||||||
|
lineHeight: 1.45,
|
||||||
|
padding: 12,
|
||||||
|
background: 'var(--bg)',
|
||||||
|
borderRadius: 8,
|
||||||
|
border: '1px solid var(--border)',
|
||||||
|
color: 'var(--text2)',
|
||||||
|
whiteSpace: 'pre-wrap',
|
||||||
|
wordBreak: 'break-word'
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{JSON.stringify(
|
||||||
|
sanitizeDebugForPanel(
|
||||||
|
extractExecutionDebug(testResult),
|
||||||
|
testResult.error ? null : testResult.type
|
||||||
|
),
|
||||||
|
null,
|
||||||
|
2
|
||||||
|
)}
|
||||||
|
</pre>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,356 @@
|
|||||||
|
import React, { useState } from 'react'
|
||||||
|
import { ChevronDown, ChevronUp } from 'lucide-react'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WorkflowDebugPanel - Zeigt Debug-Informationen für jeden Node eines Workflows
|
||||||
|
*
|
||||||
|
* @param {Object} props
|
||||||
|
* @param {Array} props.nodeStates - Array von NodeExecutionState Objekten
|
||||||
|
*/
|
||||||
|
export default function WorkflowDebugPanel({ nodeStates }) {
|
||||||
|
const [expandedNodes, setExpandedNodes] = useState(new Set())
|
||||||
|
const [showAll, setShowAll] = useState(false)
|
||||||
|
|
||||||
|
if (!nodeStates || nodeStates.length === 0) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter nodes that have debug information
|
||||||
|
const debugNodes = nodeStates.filter(ns =>
|
||||||
|
ns.debug_prompt || ns.debug_raw_response || ns.debug_node_type
|
||||||
|
)
|
||||||
|
|
||||||
|
if (debugNodes.length === 0) {
|
||||||
|
return (
|
||||||
|
<div style={{
|
||||||
|
padding: '12px',
|
||||||
|
background: 'var(--surface)',
|
||||||
|
borderRadius: '8px',
|
||||||
|
border: '1px solid var(--border)',
|
||||||
|
marginTop: '16px'
|
||||||
|
}}>
|
||||||
|
<p style={{ margin: 0, color: 'var(--text2)', fontSize: '13px' }}>
|
||||||
|
Keine Debug-Informationen verfügbar. Führe den Workflow mit debug=true aus.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const toggleNode = (nodeId) => {
|
||||||
|
const newExpanded = new Set(expandedNodes)
|
||||||
|
if (newExpanded.has(nodeId)) {
|
||||||
|
newExpanded.delete(nodeId)
|
||||||
|
} else {
|
||||||
|
newExpanded.add(nodeId)
|
||||||
|
}
|
||||||
|
setExpandedNodes(newExpanded)
|
||||||
|
}
|
||||||
|
|
||||||
|
const toggleAll = () => {
|
||||||
|
if (showAll) {
|
||||||
|
setExpandedNodes(new Set())
|
||||||
|
} else {
|
||||||
|
setExpandedNodes(new Set(debugNodes.map(n => n.node_id)))
|
||||||
|
}
|
||||||
|
setShowAll(!showAll)
|
||||||
|
}
|
||||||
|
|
||||||
|
const getStatusColor = (status) => {
|
||||||
|
switch (status) {
|
||||||
|
case 'executed': return '#1D9E75'
|
||||||
|
case 'failed': return '#D85A30'
|
||||||
|
case 'skipped': return '#888'
|
||||||
|
default: return 'var(--text2)'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const getNodeLabel = (node) => {
|
||||||
|
if (node.debug_prompt_slug) return node.debug_prompt_slug
|
||||||
|
if (node.debug_node_type) return `${node.debug_node_type}-${node.node_id.substring(0, 8)}`
|
||||||
|
return node.node_id
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div style={{
|
||||||
|
marginTop: '24px',
|
||||||
|
padding: '16px',
|
||||||
|
background: 'var(--surface)',
|
||||||
|
borderRadius: '12px',
|
||||||
|
border: '1px solid var(--border)'
|
||||||
|
}}>
|
||||||
|
<div style={{
|
||||||
|
display: 'flex',
|
||||||
|
justifyContent: 'space-between',
|
||||||
|
alignItems: 'center',
|
||||||
|
marginBottom: '16px'
|
||||||
|
}}>
|
||||||
|
<h3 style={{
|
||||||
|
margin: 0,
|
||||||
|
fontSize: '16px',
|
||||||
|
fontWeight: 600,
|
||||||
|
color: 'var(--text1)'
|
||||||
|
}}>
|
||||||
|
🔍 Debug-Informationen ({debugNodes.length} Nodes)
|
||||||
|
</h3>
|
||||||
|
<button
|
||||||
|
onClick={toggleAll}
|
||||||
|
className="btn"
|
||||||
|
style={{
|
||||||
|
padding: '6px 12px',
|
||||||
|
fontSize: '13px',
|
||||||
|
minWidth: 'auto'
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{showAll ? 'Alle zuklappen' : 'Alle aufklappen'}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div style={{ display: 'flex', flexDirection: 'column', gap: '8px' }}>
|
||||||
|
{debugNodes.map((node, idx) => {
|
||||||
|
const isExpanded = expandedNodes.has(node.node_id)
|
||||||
|
const label = getNodeLabel(node)
|
||||||
|
const statusColor = getStatusColor(node.status)
|
||||||
|
|
||||||
|
const hasFailed = node.status === 'failed'
|
||||||
|
const hasError = node.error != null
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
key={node.node_id}
|
||||||
|
style={{
|
||||||
|
border: hasFailed ? '2px solid #D85A30' : '1px solid var(--border)',
|
||||||
|
borderRadius: '8px',
|
||||||
|
background: hasFailed ? '#D85A3010' : 'var(--bg)',
|
||||||
|
overflow: 'hidden',
|
||||||
|
boxShadow: hasFailed ? '0 0 0 2px #D85A3020' : 'none'
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{/* Node Header */}
|
||||||
|
<div
|
||||||
|
onClick={() => toggleNode(node.node_id)}
|
||||||
|
style={{
|
||||||
|
padding: '12px',
|
||||||
|
display: 'flex',
|
||||||
|
justifyContent: 'space-between',
|
||||||
|
alignItems: 'center',
|
||||||
|
cursor: 'pointer',
|
||||||
|
background: isExpanded ? 'var(--surface)' : 'transparent',
|
||||||
|
transition: 'background 0.2s'
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div style={{ display: 'flex', alignItems: 'center', gap: '12px' }}>
|
||||||
|
<span style={{
|
||||||
|
fontSize: '13px',
|
||||||
|
color: 'var(--text3)',
|
||||||
|
fontFamily: 'monospace'
|
||||||
|
}}>
|
||||||
|
#{idx + 1}
|
||||||
|
</span>
|
||||||
|
<span style={{
|
||||||
|
fontWeight: 500,
|
||||||
|
color: 'var(--text1)',
|
||||||
|
fontSize: '14px'
|
||||||
|
}}>
|
||||||
|
{label}
|
||||||
|
</span>
|
||||||
|
<span style={{
|
||||||
|
fontSize: '12px',
|
||||||
|
padding: '2px 8px',
|
||||||
|
borderRadius: '4px',
|
||||||
|
background: statusColor,
|
||||||
|
color: 'white',
|
||||||
|
fontWeight: 500
|
||||||
|
}}>
|
||||||
|
{node.status}
|
||||||
|
</span>
|
||||||
|
{node.debug_node_type && (
|
||||||
|
<span style={{
|
||||||
|
fontSize: '11px',
|
||||||
|
padding: '2px 6px',
|
||||||
|
borderRadius: '4px',
|
||||||
|
background: 'var(--surface2)',
|
||||||
|
color: 'var(--text2)',
|
||||||
|
fontFamily: 'monospace'
|
||||||
|
}}>
|
||||||
|
{node.debug_node_type}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
{isExpanded ? <ChevronUp size={16} /> : <ChevronDown size={16} />}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Node Debug Content */}
|
||||||
|
{isExpanded && (
|
||||||
|
<div style={{ padding: '0 12px 12px 12px' }}>
|
||||||
|
{node.error && (
|
||||||
|
<div style={{
|
||||||
|
padding: '12px',
|
||||||
|
background: '#D85A3015',
|
||||||
|
border: '1px solid #D85A30',
|
||||||
|
borderRadius: '6px',
|
||||||
|
marginBottom: '12px'
|
||||||
|
}}>
|
||||||
|
<div style={{
|
||||||
|
fontSize: '12px',
|
||||||
|
fontWeight: 600,
|
||||||
|
color: '#D85A30',
|
||||||
|
marginBottom: '6px'
|
||||||
|
}}>
|
||||||
|
Error:
|
||||||
|
</div>
|
||||||
|
<pre style={{
|
||||||
|
margin: 0,
|
||||||
|
fontSize: '12px',
|
||||||
|
color: '#D85A30',
|
||||||
|
whiteSpace: 'pre-wrap',
|
||||||
|
wordBreak: 'break-word'
|
||||||
|
}}>
|
||||||
|
{node.error}
|
||||||
|
</pre>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{node.debug_prompt && (
|
||||||
|
<div style={{ marginBottom: '12px' }}>
|
||||||
|
<div style={{
|
||||||
|
fontSize: '12px',
|
||||||
|
fontWeight: 600,
|
||||||
|
color: 'var(--text2)',
|
||||||
|
marginBottom: '6px',
|
||||||
|
textTransform: 'uppercase',
|
||||||
|
letterSpacing: '0.5px'
|
||||||
|
}}>
|
||||||
|
Prompt:
|
||||||
|
</div>
|
||||||
|
<pre style={{
|
||||||
|
margin: 0,
|
||||||
|
padding: '12px',
|
||||||
|
background: 'var(--surface2)',
|
||||||
|
borderRadius: '6px',
|
||||||
|
fontSize: '12px',
|
||||||
|
lineHeight: '1.6',
|
||||||
|
color: 'var(--text1)',
|
||||||
|
whiteSpace: 'pre-wrap',
|
||||||
|
wordBreak: 'break-word',
|
||||||
|
maxHeight: '400px',
|
||||||
|
overflowY: 'auto',
|
||||||
|
border: '1px solid var(--border)'
|
||||||
|
}}>
|
||||||
|
{node.debug_prompt}
|
||||||
|
</pre>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{node.debug_raw_response && (
|
||||||
|
<div style={{ marginBottom: '12px' }}>
|
||||||
|
<div style={{
|
||||||
|
fontSize: '12px',
|
||||||
|
fontWeight: 600,
|
||||||
|
color: 'var(--text2)',
|
||||||
|
marginBottom: '6px',
|
||||||
|
textTransform: 'uppercase',
|
||||||
|
letterSpacing: '0.5px'
|
||||||
|
}}>
|
||||||
|
Rohe Antwort:
|
||||||
|
</div>
|
||||||
|
<pre style={{
|
||||||
|
margin: 0,
|
||||||
|
padding: '12px',
|
||||||
|
background: 'var(--surface2)',
|
||||||
|
borderRadius: '6px',
|
||||||
|
fontSize: '12px',
|
||||||
|
lineHeight: '1.6',
|
||||||
|
color: 'var(--text1)',
|
||||||
|
whiteSpace: 'pre-wrap',
|
||||||
|
wordBreak: 'break-word',
|
||||||
|
maxHeight: '400px',
|
||||||
|
overflowY: 'auto',
|
||||||
|
border: '1px solid var(--border)'
|
||||||
|
}}>
|
||||||
|
{node.debug_raw_response}
|
||||||
|
</pre>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{node.analysis_core && (
|
||||||
|
<div>
|
||||||
|
<div style={{
|
||||||
|
fontSize: '12px',
|
||||||
|
fontWeight: 600,
|
||||||
|
color: 'var(--text2)',
|
||||||
|
marginBottom: '6px',
|
||||||
|
textTransform: 'uppercase',
|
||||||
|
letterSpacing: '0.5px'
|
||||||
|
}}>
|
||||||
|
Geparste Ergebnisse:
|
||||||
|
</div>
|
||||||
|
<pre style={{
|
||||||
|
margin: 0,
|
||||||
|
padding: '12px',
|
||||||
|
background: 'var(--surface2)',
|
||||||
|
borderRadius: '6px',
|
||||||
|
fontSize: '12px',
|
||||||
|
lineHeight: '1.6',
|
||||||
|
color: 'var(--text1)',
|
||||||
|
whiteSpace: 'pre-wrap',
|
||||||
|
wordBreak: 'break-word',
|
||||||
|
maxHeight: '300px',
|
||||||
|
overflowY: 'auto',
|
||||||
|
border: '1px solid var(--border)'
|
||||||
|
}}>
|
||||||
|
{node.analysis_core}
|
||||||
|
</pre>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{node.normalized_signals && node.normalized_signals.length > 0 && (
|
||||||
|
<div style={{ marginTop: '12px' }}>
|
||||||
|
<div style={{
|
||||||
|
fontSize: '12px',
|
||||||
|
fontWeight: 600,
|
||||||
|
color: 'var(--text2)',
|
||||||
|
marginBottom: '6px',
|
||||||
|
textTransform: 'uppercase',
|
||||||
|
letterSpacing: '0.5px'
|
||||||
|
}}>
|
||||||
|
Signale ({node.normalized_signals.length}):
|
||||||
|
</div>
|
||||||
|
<div style={{ display: 'flex', flexDirection: 'column', gap: '6px' }}>
|
||||||
|
{node.normalized_signals.map((sig, i) => (
|
||||||
|
<div
|
||||||
|
key={i}
|
||||||
|
style={{
|
||||||
|
padding: '8px',
|
||||||
|
background: 'var(--surface2)',
|
||||||
|
borderRadius: '4px',
|
||||||
|
fontSize: '12px',
|
||||||
|
border: '1px solid var(--border)'
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<span style={{ color: 'var(--text2)' }}>{sig.question_type}:</span>{' '}
|
||||||
|
<span style={{ fontWeight: 500, color: 'var(--text1)' }}>
|
||||||
|
"{sig.raw_value}" → "{sig.normalized_value}"
|
||||||
|
</span>{' '}
|
||||||
|
<span style={{
|
||||||
|
fontSize: '11px',
|
||||||
|
padding: '2px 6px',
|
||||||
|
borderRadius: '3px',
|
||||||
|
background: sig.status === 'valid' ? '#1D9E7520' : '#D85A3020',
|
||||||
|
color: sig.status === 'valid' ? '#1D9E75' : '#D85A30'
|
||||||
|
}}>
|
||||||
|
{sig.status}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -47,7 +47,7 @@ export function QuestionAugmentationPanel({ node, onChange }) {
|
|||||||
|
|
||||||
{questions.map((q, idx) => (
|
{questions.map((q, idx) => (
|
||||||
<QuestionEditor
|
<QuestionEditor
|
||||||
key={q.id}
|
key={idx}
|
||||||
question={q}
|
question={q}
|
||||||
index={idx}
|
index={idx}
|
||||||
onChange={(field, value) => handleQuestionChange(idx, field, value)}
|
onChange={(field, value) => handleQuestionChange(idx, field, value)}
|
||||||
|
|||||||
@@ -192,13 +192,17 @@ export function WorkflowResultViewer({ result, onClose }) {
|
|||||||
Node States (Debug)
|
Node States (Debug)
|
||||||
</h3>
|
</h3>
|
||||||
<div style={{ display: 'flex', flexDirection: 'column', gap: '8px' }}>
|
<div style={{ display: 'flex', flexDirection: 'column', gap: '8px' }}>
|
||||||
{nodeStates.map((node) => (
|
{nodeStates.map((node) => {
|
||||||
|
const hasFailed = node.status === 'failed'
|
||||||
|
return (
|
||||||
<div
|
<div
|
||||||
key={node.node_id}
|
key={node.node_id}
|
||||||
style={{
|
style={{
|
||||||
border: '1px solid var(--border)',
|
border: hasFailed ? '2px solid #D85A30' : '1px solid var(--border)',
|
||||||
borderRadius: '8px',
|
borderRadius: '8px',
|
||||||
overflow: 'hidden'
|
background: hasFailed ? '#D85A3010' : 'transparent',
|
||||||
|
overflow: 'hidden',
|
||||||
|
boxShadow: hasFailed ? '0 0 0 2px #D85A3020' : 'none'
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{/* Node Header */}
|
{/* Node Header */}
|
||||||
@@ -216,13 +220,13 @@ export function WorkflowResultViewer({ result, onClose }) {
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<div>
|
<div>
|
||||||
{node.node_type === 'start' && '🚀'}
|
{(node.debug_node_type || node.node_type) === 'start' && '🚀'}
|
||||||
{node.node_type === 'analysis' && '🤖'}
|
{(node.debug_node_type || node.node_type) === 'analysis' && '🤖'}
|
||||||
{node.node_type === 'logic' && '⚡'}
|
{(node.debug_node_type || node.node_type) === 'logic' && '⚡'}
|
||||||
{node.node_type === 'join' && '🔀'}
|
{(node.debug_node_type || node.node_type) === 'join' && '🔀'}
|
||||||
{node.node_type === 'end' && '🏁'}
|
{(node.debug_node_type || node.node_type) === 'end' && '🏁'}
|
||||||
{' '}
|
{' '}
|
||||||
{node.node_label || node.node_id}
|
{node.debug_prompt_slug || node.node_label || ((node.debug_node_type || node.node_type) ? `${node.debug_node_type || node.node_type}-${node.node_id.substring(0, 8)}` : node.node_id)}
|
||||||
{node.status === 'skipped' && (
|
{node.status === 'skipped' && (
|
||||||
<span style={{ color: 'var(--text3)', marginLeft: '8px' }}>
|
<span style={{ color: 'var(--text3)', marginLeft: '8px' }}>
|
||||||
(skipped)
|
(skipped)
|
||||||
@@ -237,45 +241,65 @@ export function WorkflowResultViewer({ result, onClose }) {
|
|||||||
{/* Node Details */}
|
{/* Node Details */}
|
||||||
{expandedNodes[node.node_id] && (
|
{expandedNodes[node.node_id] && (
|
||||||
<div style={{ padding: '12px', fontSize: '12px' }}>
|
<div style={{ padding: '12px', fontSize: '12px' }}>
|
||||||
{node.output && (
|
{/* Error (show first) */}
|
||||||
|
{node.error && (
|
||||||
|
<div style={{ marginBottom: '12px', padding: '12px', background: '#D85A3015', border: '1px solid #D85A30', borderRadius: '6px' }}>
|
||||||
|
<div style={{ fontSize: '12px', fontWeight: 600, color: '#D85A30', marginBottom: '6px' }}>Error:</div>
|
||||||
|
<pre style={{ margin: 0, fontSize: '12px', color: '#D85A30', whiteSpace: 'pre-wrap', wordBreak: 'break-word' }}>{node.error}</pre>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Debug Prompt */}
|
||||||
|
{node.debug_prompt && (
|
||||||
|
<div style={{ marginBottom: '12px' }}>
|
||||||
|
<div style={{ fontSize: '12px', fontWeight: 600, color: 'var(--text2)', marginBottom: '6px', textTransform: 'uppercase' }}>Prompt:</div>
|
||||||
|
<pre style={{ margin: 0, padding: '12px', background: 'var(--surface2)', borderRadius: '6px', fontSize: '11px', color: 'var(--text1)', whiteSpace: 'pre-wrap', maxHeight: '300px', overflowY: 'auto', border: '1px solid var(--border)' }}>{node.debug_prompt}</pre>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Debug Raw Response */}
|
||||||
|
{node.debug_raw_response && (
|
||||||
|
<div style={{ marginBottom: '12px' }}>
|
||||||
|
<div style={{ fontSize: '12px', fontWeight: 600, color: 'var(--text2)', marginBottom: '6px', textTransform: 'uppercase' }}>Rohe Antwort:</div>
|
||||||
|
<pre style={{ margin: 0, padding: '12px', background: 'var(--surface2)', borderRadius: '6px', fontSize: '11px', color: 'var(--text1)', whiteSpace: 'pre-wrap', maxHeight: '300px', overflowY: 'auto', border: '1px solid var(--border)' }}>{node.debug_raw_response}</pre>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Analysis Core */}
|
||||||
|
{node.analysis_core && (
|
||||||
|
<div style={{ marginBottom: '12px' }}>
|
||||||
|
<div style={{ fontSize: '12px', fontWeight: 600, color: 'var(--text2)', marginBottom: '6px', textTransform: 'uppercase' }}>Ergebnis:</div>
|
||||||
|
<pre style={{ margin: 0, padding: '12px', background: 'var(--surface2)', borderRadius: '6px', fontSize: '11px', color: 'var(--text1)', whiteSpace: 'pre-wrap', maxHeight: '300px', overflowY: 'auto', border: '1px solid var(--border)' }}>{node.analysis_core}</pre>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Normalized Signals */}
|
||||||
|
{node.normalized_signals && node.normalized_signals.length > 0 && (
|
||||||
|
<div style={{ marginBottom: '12px' }}>
|
||||||
|
<div style={{ fontSize: '12px', fontWeight: 600, color: 'var(--text2)', marginBottom: '6px', textTransform: 'uppercase' }}>Signale ({node.normalized_signals.length}):</div>
|
||||||
|
{node.normalized_signals.map((sig, i) => (
|
||||||
|
<div key={i} style={{ padding: '6px', background: 'var(--surface2)', borderRadius: '4px', fontSize: '11px', marginBottom: '4px', border: '1px solid var(--border)' }}>
|
||||||
|
<span style={{ color: 'var(--text2)' }}>{sig.question_type}:</span> <span style={{ fontWeight: 500 }}>"{sig.raw_value}" → "{sig.normalized_value}"</span> <span style={{ fontSize: '10px', padding: '2px 4px', borderRadius: '2px', background: sig.status === 'valid' ? '#1D9E7520' : '#D85A3020', color: sig.status === 'valid' ? '#1D9E75' : '#D85A30' }}>{sig.status}</span>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Output (fallback) */}
|
||||||
|
{node.output && !node.analysis_core && (
|
||||||
<div style={{ marginBottom: '8px' }}>
|
<div style={{ marginBottom: '8px' }}>
|
||||||
<strong>Output:</strong>
|
<strong>Output:</strong>
|
||||||
<pre
|
<pre style={{ marginTop: '4px', padding: '8px', background: 'var(--bg)', borderRadius: '4px', fontSize: '11px', overflowX: 'auto', maxHeight: '200px', overflowY: 'auto' }}>
|
||||||
style={{
|
{typeof node.output === 'string' ? node.output : JSON.stringify(node.output, null, 2)}
|
||||||
marginTop: '4px',
|
|
||||||
padding: '8px',
|
|
||||||
background: 'var(--bg)',
|
|
||||||
borderRadius: '4px',
|
|
||||||
fontSize: '11px',
|
|
||||||
overflowX: 'auto',
|
|
||||||
maxHeight: '200px',
|
|
||||||
overflowY: 'auto'
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{typeof node.output === 'string'
|
|
||||||
? node.output
|
|
||||||
: JSON.stringify(node.output, null, 2)}
|
|
||||||
</pre>
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
{node.error && (
|
|
||||||
<div style={{ color: 'var(--danger)', marginBottom: '8px' }}>
|
{/* Metadata */}
|
||||||
<strong>Error:</strong> {node.error}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
{node.metadata && (
|
{node.metadata && (
|
||||||
<div style={{ marginTop: '8px' }}>
|
<div style={{ marginTop: '8px' }}>
|
||||||
<strong>Metadata:</strong>
|
<strong>Metadata:</strong>
|
||||||
<pre
|
<pre style={{ marginTop: '4px', padding: '8px', background: 'var(--bg)', borderRadius: '4px', fontSize: '11px', overflowX: 'auto' }}>
|
||||||
style={{
|
|
||||||
marginTop: '4px',
|
|
||||||
padding: '8px',
|
|
||||||
background: 'var(--bg)',
|
|
||||||
borderRadius: '4px',
|
|
||||||
fontSize: '11px',
|
|
||||||
overflowX: 'auto'
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{JSON.stringify(node.metadata, null, 2)}
|
{JSON.stringify(node.metadata, null, 2)}
|
||||||
</pre>
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
@@ -283,7 +307,7 @@ export function WorkflowResultViewer({ result, onClose }) {
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
))}
|
)})}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -74,6 +74,11 @@ export const ADMIN_GROUPS = [
|
|||||||
label: 'Trainings-Profile',
|
label: 'Trainings-Profile',
|
||||||
description: 'Training-Type-Profile (#15).',
|
description: 'Training-Type-Profile (#15).',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
to: '/admin/activity-attribute-profiles',
|
||||||
|
label: 'Session-Metriken (EAV)',
|
||||||
|
description: 'Messgrößen-Katalog und Zuordnung zu Kategorie / Trainingstyp.',
|
||||||
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useState, useEffect, useRef } from 'react'
|
import { useState, useEffect, useRef, useCallback, startTransition } from 'react'
|
||||||
import { Upload, Pencil, Trash2, Check, X, CheckCircle } from 'lucide-react'
|
import { Upload, Pencil, Trash2, Check, X, CheckCircle } from 'lucide-react'
|
||||||
import { BarChart, Bar, XAxis, YAxis, Tooltip, ResponsiveContainer, CartesianGrid } from 'recharts'
|
import { BarChart, Bar, XAxis, YAxis, Tooltip, ResponsiveContainer, CartesianGrid } from 'recharts'
|
||||||
import { api } from '../utils/api'
|
import { api } from '../utils/api'
|
||||||
@@ -9,15 +9,169 @@ import dayjs from 'dayjs'
|
|||||||
import 'dayjs/locale/de'
|
import 'dayjs/locale/de'
|
||||||
dayjs.locale('de')
|
dayjs.locale('de')
|
||||||
|
|
||||||
|
/** Erfassungsseite /activity: pro Kalendermonat laden (ohne Qualitätsfilter, siehe Backend). */
|
||||||
|
const ACTIVITY_MONTH_FETCH_LIMIT = 25_000
|
||||||
|
|
||||||
|
function ymdMonth(d = dayjs()) {
|
||||||
|
return d.format('YYYY-MM')
|
||||||
|
}
|
||||||
|
|
||||||
|
function prevMonthYm(ym) {
|
||||||
|
return dayjs(`${ym}-01`).subtract(1, 'month').format('YYYY-MM')
|
||||||
|
}
|
||||||
|
|
||||||
|
function compareActivities(a, b) {
|
||||||
|
const da = a.date || ''
|
||||||
|
const db = b.date || ''
|
||||||
|
if (da !== db) return db.localeCompare(da)
|
||||||
|
const sa = String(a.start_time || '')
|
||||||
|
const sb = String(b.start_time || '')
|
||||||
|
if (sa !== sb) return sb.localeCompare(sa)
|
||||||
|
return String(b.id || '').localeCompare(String(a.id || ''))
|
||||||
|
}
|
||||||
|
|
||||||
|
function dedupeActivitiesById(rows) {
|
||||||
|
const m = new Map()
|
||||||
|
for (const r of rows) {
|
||||||
|
if (r?.id) m.set(r.id, r)
|
||||||
|
}
|
||||||
|
return [...m.values()].sort(compareActivities)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** activity_log: Spalten start_time / end_time sind TIME (Uhrzeit zum Kalendertag date), nicht volles Timestamp. */
|
||||||
|
function timeInputValueFromApi(t) {
|
||||||
|
if (t == null || t === '') return ''
|
||||||
|
const s = String(t)
|
||||||
|
if (s.includes('T') && s.length >= 16) return s.slice(11, 16)
|
||||||
|
const m = s.match(/^(\d{1,2}):(\d{2})/)
|
||||||
|
if (!m) return ''
|
||||||
|
return `${m[1].padStart(2, '0')}:${m[2]}`
|
||||||
|
}
|
||||||
|
|
||||||
|
function timePayloadFromInput(v) {
|
||||||
|
const s = v == null ? '' : String(v).trim()
|
||||||
|
if (!s) return null
|
||||||
|
if (/^\d{2}:\d{2}$/.test(s)) return `${s}:00`
|
||||||
|
if (/^\d{2}:\d{2}:\d{2}$/.test(s)) return s
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatTimeForList(t) {
|
||||||
|
const v = timeInputValueFromApi(t)
|
||||||
|
return v || ''
|
||||||
|
}
|
||||||
|
|
||||||
const ACTIVITY_TYPES = [
|
const ACTIVITY_TYPES = [
|
||||||
'Traditionelles Krafttraining','Matrial Arts','Outdoor Spaziergang',
|
'Traditionelles Krafttraining','Matrial Arts','Outdoor Spaziergang',
|
||||||
'Innenräume Spaziergang','Laufen','Radfahren','Schwimmen',
|
'Innenräume Spaziergang','Laufen','Radfahren','Schwimmen',
|
||||||
'Cardio Dance','Geist & Körper','Sonstiges'
|
'Cardio Dance','Geist & Körper','Sonstiges'
|
||||||
]
|
]
|
||||||
|
|
||||||
|
/** Spalten, die mit ActivityEntry / UPDATE activity_log geschrieben werden dürfen (Übergang: Profilfelder → Kopfzeile). */
|
||||||
|
const ACTIVITY_LOG_PAYLOAD_KEYS = new Set([
|
||||||
|
'date',
|
||||||
|
'start_time',
|
||||||
|
'end_time',
|
||||||
|
'activity_type',
|
||||||
|
'duration_min',
|
||||||
|
'kcal_active',
|
||||||
|
'kcal_resting',
|
||||||
|
'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',
|
||||||
|
'training_type_id',
|
||||||
|
'training_category',
|
||||||
|
'training_subcategory',
|
||||||
|
])
|
||||||
|
|
||||||
|
/** activity_log-Spalten, die bereits in EntryForm (Kopfzeile) bearbeitet werden — Profilfeld mit gleichem source_field nicht doppelt anzeigen. */
|
||||||
|
const ENTRY_FORM_ACTIVITY_LOG_COLUMNS = new Set([
|
||||||
|
'duration_min',
|
||||||
|
'kcal_active',
|
||||||
|
'hr_avg',
|
||||||
|
'hr_max',
|
||||||
|
'rpe',
|
||||||
|
'notes',
|
||||||
|
])
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bindung Profilparameter ↔ Kopfzeile: Entweder source_field zeigt auf eine Kopfspalte,
|
||||||
|
* oder der Parameter-key ist selbst eine Kopfspalte (häufig nach Migration / ohne source_field).
|
||||||
|
* @returns {{ headlineCol: string, parameterKey: string } | null}
|
||||||
|
*/
|
||||||
|
function activitySchemaHeadlineBinding(s) {
|
||||||
|
if (!s || !s.key) return null
|
||||||
|
const sf = s.source_field != null ? String(s.source_field).trim() : ''
|
||||||
|
if (sf && ENTRY_FORM_ACTIVITY_LOG_COLUMNS.has(sf)) {
|
||||||
|
return { headlineCol: sf, parameterKey: s.key }
|
||||||
|
}
|
||||||
|
if (ENTRY_FORM_ACTIVITY_LOG_COLUMNS.has(s.key)) {
|
||||||
|
return { headlineCol: s.key, parameterKey: s.key }
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
/** training_parameters.category (siehe Migration 013); feste Reihenfolge der Wertegruppen */
|
||||||
|
const TRAINING_PARAM_CATEGORY_ORDER = [
|
||||||
|
'physical',
|
||||||
|
'physiological',
|
||||||
|
'performance',
|
||||||
|
'subjective',
|
||||||
|
'environmental',
|
||||||
|
]
|
||||||
|
|
||||||
|
const TRAINING_PARAM_CATEGORY_LABEL_DE = {
|
||||||
|
physical: 'Physisch / Bewegung',
|
||||||
|
physiological: 'Physiologie',
|
||||||
|
performance: 'Leistung',
|
||||||
|
subjective: 'Subjektiv und Wahrnehmung',
|
||||||
|
environmental: 'Umwelt',
|
||||||
|
}
|
||||||
|
|
||||||
|
function compareActivityProfileSchemaRows(a, b) {
|
||||||
|
const ca = (a.param_category && String(a.param_category).trim().toLowerCase()) || ''
|
||||||
|
const cb = (b.param_category && String(b.param_category).trim().toLowerCase()) || ''
|
||||||
|
const ia = TRAINING_PARAM_CATEGORY_ORDER.indexOf(ca)
|
||||||
|
const ib = TRAINING_PARAM_CATEGORY_ORDER.indexOf(cb)
|
||||||
|
const ra = ia === -1 ? 1000 : ia
|
||||||
|
const rb = ib === -1 ? 1000 : ib
|
||||||
|
if (ra !== rb) return ra - rb
|
||||||
|
if (ca !== cb) return ca.localeCompare(cb, 'de')
|
||||||
|
|
||||||
|
const ga = (a.ui_group && String(a.ui_group).trim()) || ''
|
||||||
|
const gb = (b.ui_group && String(b.ui_group).trim()) || ''
|
||||||
|
if (ga !== gb) {
|
||||||
|
if (!ga) return -1
|
||||||
|
if (!gb) return 1
|
||||||
|
return ga.localeCompare(gb, 'de')
|
||||||
|
}
|
||||||
|
const sa = Number(a.sort_order) || 0
|
||||||
|
const sb = Number(b.sort_order) || 0
|
||||||
|
if (sa !== sb) return sa - sb
|
||||||
|
return String(a.key).localeCompare(String(b.key), 'de')
|
||||||
|
}
|
||||||
|
|
||||||
|
function sortActivityProfileSchemaRows(rows) {
|
||||||
|
return [...rows].sort(compareActivityProfileSchemaRows)
|
||||||
|
}
|
||||||
|
|
||||||
function empty() {
|
function empty() {
|
||||||
return {
|
return {
|
||||||
date: dayjs().format('YYYY-MM-DD'),
|
date: dayjs().format('YYYY-MM-DD'),
|
||||||
|
start_time: '',
|
||||||
|
end_time: '',
|
||||||
activity_type: 'Traditionelles Krafttraining',
|
activity_type: 'Traditionelles Krafttraining',
|
||||||
duration_min: '', kcal_active: '',
|
duration_min: '', kcal_active: '',
|
||||||
hr_avg: '', hr_max: '', rpe: '', notes: '',
|
hr_avg: '', hr_max: '', rpe: '', notes: '',
|
||||||
@@ -27,6 +181,188 @@ function empty() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function buildMetricsPayload(schema, draft) {
|
||||||
|
const out = []
|
||||||
|
for (const s of schema) {
|
||||||
|
const raw = draft[s.key]
|
||||||
|
if (s.data_type === 'boolean') {
|
||||||
|
if (raw === '' || raw === null || raw === undefined) {
|
||||||
|
if (s.required) throw new Error(`Pflichtfeld: ${s.name_de}`)
|
||||||
|
out.push({ parameter_key: s.key, value: null })
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
out.push({ parameter_key: s.key, value: !!raw })
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
const rawStr = raw === null || raw === undefined ? '' : String(raw).trim()
|
||||||
|
if (rawStr === '') {
|
||||||
|
if (s.required) throw new Error(`Pflichtfeld: ${s.name_de}`)
|
||||||
|
out.push({ parameter_key: s.key, value: null })
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
let v
|
||||||
|
if (s.data_type === 'integer') {
|
||||||
|
v = parseInt(rawStr, 10)
|
||||||
|
if (Number.isNaN(v)) throw new Error(`Ungültige Zahl: ${s.name_de}`)
|
||||||
|
} else if (s.data_type === 'float') {
|
||||||
|
v = parseFloat(rawStr)
|
||||||
|
if (Number.isNaN(v)) throw new Error(`Ungültige Zahl: ${s.name_de}`)
|
||||||
|
} else {
|
||||||
|
v = rawStr
|
||||||
|
}
|
||||||
|
out.push({ parameter_key: s.key, value: v })
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
function SessionMetricsFields({ schema, values, setValues, metrics }) {
|
||||||
|
const schemaList = Array.isArray(schema) ? schema : []
|
||||||
|
const headlineDuplicateKeys = new Set(
|
||||||
|
schemaList.filter((s) => activitySchemaHeadlineBinding(s) != null).map((s) => s.key),
|
||||||
|
)
|
||||||
|
const schemaForDisplay = schemaList.filter((s) => activitySchemaHeadlineBinding(s) == null)
|
||||||
|
const metricRows = Array.isArray(metrics) ? metrics : []
|
||||||
|
const schemaKeys = new Set(schemaForDisplay.map((s) => s.key))
|
||||||
|
const orphanMetrics = metricRows.filter(
|
||||||
|
(row) =>
|
||||||
|
row &&
|
||||||
|
row.key &&
|
||||||
|
!schemaKeys.has(row.key) &&
|
||||||
|
!headlineDuplicateKeys.has(row.key),
|
||||||
|
)
|
||||||
|
|
||||||
|
if (schemaForDisplay.length === 0 && orphanMetrics.length === 0) return null
|
||||||
|
const set = (k, v) => setValues((prev) => ({ ...prev, [k]: v }))
|
||||||
|
|
||||||
|
const sortedForDisplay = sortActivityProfileSchemaRows(schemaForDisplay)
|
||||||
|
const profileFieldNodes = []
|
||||||
|
let lastCategoryKey = null
|
||||||
|
let lastUiGroup = null
|
||||||
|
for (const s of sortedForDisplay) {
|
||||||
|
const catRaw = (s.param_category && String(s.param_category).trim().toLowerCase()) || ''
|
||||||
|
const catKey = catRaw || '_other'
|
||||||
|
if (catKey !== lastCategoryKey) {
|
||||||
|
lastCategoryKey = catKey
|
||||||
|
lastUiGroup = null
|
||||||
|
const catTitle =
|
||||||
|
(catRaw && TRAINING_PARAM_CATEGORY_LABEL_DE[catRaw]) || s.param_category || 'Sonstige'
|
||||||
|
profileFieldNodes.push(
|
||||||
|
<div
|
||||||
|
key={`prof-cat-${catKey}-${profileFieldNodes.length}`}
|
||||||
|
style={{
|
||||||
|
fontSize: 12,
|
||||||
|
fontWeight: 700,
|
||||||
|
color: 'var(--text2)',
|
||||||
|
marginTop: profileFieldNodes.length ? 14 : 6,
|
||||||
|
marginBottom: 6,
|
||||||
|
letterSpacing: '0.02em',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{catTitle}
|
||||||
|
</div>,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
const ug = (s.ui_group && String(s.ui_group).trim()) || ''
|
||||||
|
if (ug) {
|
||||||
|
if (ug !== lastUiGroup) {
|
||||||
|
lastUiGroup = ug
|
||||||
|
profileFieldNodes.push(
|
||||||
|
<div
|
||||||
|
key={`prof-ug-${catKey}-${ug}-${profileFieldNodes.length}`}
|
||||||
|
style={{ fontSize: 12, color: 'var(--text3)', marginTop: 8, marginBottom: 4 }}
|
||||||
|
>
|
||||||
|
{ug}
|
||||||
|
</div>,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
lastUiGroup = null
|
||||||
|
}
|
||||||
|
profileFieldNodes.push(
|
||||||
|
<div key={s.key} className="form-row">
|
||||||
|
<label className="form-label">
|
||||||
|
{s.name_de}
|
||||||
|
{s.required ? ' *' : ''}
|
||||||
|
{s.unit ? ` (${s.unit})` : ''}
|
||||||
|
</label>
|
||||||
|
{s.data_type === 'boolean' ? (
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
style={{ width: 'auto', marginRight: 'auto' }}
|
||||||
|
checked={!!values[s.key]}
|
||||||
|
onChange={(e) => set(s.key, e.target.checked)}
|
||||||
|
/>
|
||||||
|
) : s.data_type === 'integer' || s.data_type === 'float' ? (
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
className="form-input"
|
||||||
|
step={s.data_type === 'integer' ? 1 : 'any'}
|
||||||
|
value={values[s.key] ?? ''}
|
||||||
|
onChange={(e) => set(s.key, e.target.value)}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
className="form-input"
|
||||||
|
value={values[s.key] ?? ''}
|
||||||
|
onChange={(e) => set(s.key, e.target.value)}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
<span className="form-unit" />
|
||||||
|
</div>,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const orphansSorted = [...orphanMetrics].sort((a, b) =>
|
||||||
|
String(a.key).localeCompare(String(b.key), 'de'),
|
||||||
|
)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div style={{ marginTop: 12, paddingTop: 12, borderTop: '1px solid var(--border)' }}>
|
||||||
|
<div style={{ fontSize: 13, fontWeight: 600, marginBottom: 8 }}>Weitere Kennwerte (Profil)</div>
|
||||||
|
{profileFieldNodes}
|
||||||
|
{orphanMetrics.length > 0 && (
|
||||||
|
<div style={{ marginTop: 14 }}>
|
||||||
|
<div style={{ fontSize: 12, color: 'var(--text3)', marginBottom: 8, lineHeight: 1.45 }}>
|
||||||
|
Werte aus Import/älteren Daten, die zum <strong>aktuellen</strong> Trainingsprofil dieser Session (Kategorie/Typ
|
||||||
|
in activity_log) nicht ins Schema passen — nur Anzeige. Sichtbar nach erneutem Laden, wenn die Daten in der
|
||||||
|
Datenbank stehen.
|
||||||
|
</div>
|
||||||
|
{orphansSorted.map((row) => {
|
||||||
|
const disp =
|
||||||
|
values[row.key] === null || values[row.key] === undefined || values[row.key] === ''
|
||||||
|
? '—'
|
||||||
|
: String(values[row.key])
|
||||||
|
return (
|
||||||
|
<div key={row.key} className="form-row">
|
||||||
|
<label className="form-label">
|
||||||
|
{row.key}
|
||||||
|
{row.unit ? ` (${row.unit})` : ''}
|
||||||
|
</label>
|
||||||
|
{row.data_type === 'boolean' ? (
|
||||||
|
<input type="checkbox" style={{ width: 'auto', marginRight: 'auto' }} checked={!!values[row.key]} readOnly disabled />
|
||||||
|
) : (
|
||||||
|
<div
|
||||||
|
className="form-input"
|
||||||
|
style={{
|
||||||
|
background: 'var(--surface2)',
|
||||||
|
cursor: 'default',
|
||||||
|
color: 'var(--text1)',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{disp}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<span className="form-unit" />
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// ── Import Panel ──────────────────────────────────────────────────────────────
|
// ── Import Panel ──────────────────────────────────────────────────────────────
|
||||||
function ImportPanel({ onImported }) {
|
function ImportPanel({ onImported }) {
|
||||||
const fileRef = useRef()
|
const fileRef = useRef()
|
||||||
@@ -85,7 +421,17 @@ function ImportPanel({ onImported }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ── Manual Entry ──────────────────────────────────────────────────────────────
|
// ── Manual Entry ──────────────────────────────────────────────────────────────
|
||||||
function EntryForm({ form, setForm, onSave, onCancel, saveLabel='Speichern', saving=false, error=null, usage=null }) {
|
function EntryForm({
|
||||||
|
form,
|
||||||
|
setForm,
|
||||||
|
onSave,
|
||||||
|
onCancel,
|
||||||
|
saveLabel = 'Speichern',
|
||||||
|
saving = false,
|
||||||
|
error = null,
|
||||||
|
usage = null,
|
||||||
|
formExtras = null,
|
||||||
|
}) {
|
||||||
const set = (k,v) => setForm(f=>({...f,[k]:v}))
|
const set = (k,v) => setForm(f=>({...f,[k]:v}))
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
@@ -94,6 +440,30 @@ function EntryForm({ form, setForm, onSave, onCancel, saveLabel='Speichern', sav
|
|||||||
<input type="date" className="form-input" style={{width:140}} value={form.date} onChange={e=>set('date',e.target.value)}/>
|
<input type="date" className="form-input" style={{width:140}} value={form.date} onChange={e=>set('date',e.target.value)}/>
|
||||||
<span className="form-unit"/>
|
<span className="form-unit"/>
|
||||||
</div>
|
</div>
|
||||||
|
<div className="form-row">
|
||||||
|
<label className="form-label">Start (Uhrzeit)</label>
|
||||||
|
<input
|
||||||
|
type="time"
|
||||||
|
step={1}
|
||||||
|
className="form-input"
|
||||||
|
style={{ width: 'auto', minWidth: 140 }}
|
||||||
|
value={timeInputValueFromApi(form.start_time)}
|
||||||
|
onChange={(e) => set('start_time', e.target.value ? timePayloadFromInput(e.target.value) || '' : '')}
|
||||||
|
/>
|
||||||
|
<span className="form-unit">zum Datum oben</span>
|
||||||
|
</div>
|
||||||
|
<div className="form-row">
|
||||||
|
<label className="form-label">Ende (Uhrzeit)</label>
|
||||||
|
<input
|
||||||
|
type="time"
|
||||||
|
step={1}
|
||||||
|
className="form-input"
|
||||||
|
style={{ width: 'auto', minWidth: 140 }}
|
||||||
|
value={timeInputValueFromApi(form.end_time)}
|
||||||
|
onChange={(e) => set('end_time', e.target.value ? timePayloadFromInput(e.target.value) || '' : '')}
|
||||||
|
/>
|
||||||
|
<span className="form-unit">optional</span>
|
||||||
|
</div>
|
||||||
<div style={{marginBottom:12}}>
|
<div style={{marginBottom:12}}>
|
||||||
<TrainingTypeSelect
|
<TrainingTypeSelect
|
||||||
value={form.training_type_id}
|
value={form.training_type_id}
|
||||||
@@ -144,6 +514,7 @@ function EntryForm({ form, setForm, onSave, onCancel, saveLabel='Speichern', sav
|
|||||||
value={form.notes||''} onChange={e=>set('notes',e.target.value)}/>
|
value={form.notes||''} onChange={e=>set('notes',e.target.value)}/>
|
||||||
<span className="form-unit"/>
|
<span className="form-unit"/>
|
||||||
</div>
|
</div>
|
||||||
|
{formExtras}
|
||||||
{error && (
|
{error && (
|
||||||
<div style={{padding:'10px',background:'var(--danger-bg)',border:'1px solid var(--danger)',borderRadius:8,fontSize:13,color:'var(--danger)',marginBottom:8}}>
|
<div style={{padding:'10px',background:'var(--danger-bg)',border:'1px solid var(--danger)',borderRadius:8,fontSize:13,color:'var(--danger)',marginBottom:8}}>
|
||||||
{error}
|
{error}
|
||||||
@@ -181,12 +552,82 @@ export default function ActivityPage() {
|
|||||||
const [error, setError] = useState(null)
|
const [error, setError] = useState(null)
|
||||||
const [activityUsage, setActivityUsage] = useState(null) // Phase 4: Usage badge
|
const [activityUsage, setActivityUsage] = useState(null) // Phase 4: Usage badge
|
||||||
const [categories, setCategories] = useState({}) // v9d: Training categories
|
const [categories, setCategories] = useState({}) // v9d: Training categories
|
||||||
|
const [sessionDetail, setSessionDetail] = useState(null)
|
||||||
|
const [metricDraft, setMetricDraft] = useState({})
|
||||||
|
/** Beim Wechsel Kategorie/Typ: Nutzerwerte für weiterhin vorhandene Schema-Keys nicht mit Server überschreiben */
|
||||||
|
const editSchemaKeysPrevRef = useRef(new Set())
|
||||||
|
const prevEditingIdRef = useRef(null)
|
||||||
|
const [manualSchema, setManualSchema] = useState(null)
|
||||||
|
const [manualMetricDraft, setManualMetricDraft] = useState({})
|
||||||
|
const manualSchemaKeysPrevRef = useRef(new Set())
|
||||||
|
const [sessionLoadError, setSessionLoadError] = useState(null)
|
||||||
|
const [savingEdit, setSavingEdit] = useState(false)
|
||||||
|
const [listLoadingMore, setListLoadingMore] = useState(false)
|
||||||
|
const [selectedMonth, setSelectedMonth] = useState(() => ymdMonth())
|
||||||
|
const [monthsIncluded, setMonthsIncluded] = useState(() => [ymdMonth()])
|
||||||
|
const monthsIncludedRef = useRef(monthsIncluded)
|
||||||
|
|
||||||
const load = async () => {
|
useEffect(() => {
|
||||||
const [e, s] = await Promise.all([api.listActivity(), api.activityStats()])
|
monthsIncludedRef.current = monthsIncluded
|
||||||
setEntries(e); setStats(s)
|
}, [monthsIncluded])
|
||||||
|
|
||||||
|
const fetchMonthsChain = useCallback(async (chain) => {
|
||||||
|
const lists = await Promise.all(
|
||||||
|
chain.map((ym) =>
|
||||||
|
api.listActivity(ACTIVITY_MONTH_FETCH_LIMIT, undefined, {
|
||||||
|
skipQualityFilter: true,
|
||||||
|
collapseDuplicateSessions: true,
|
||||||
|
month: ym,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
)
|
||||||
|
const merged = dedupeActivitiesById(lists.flat())
|
||||||
|
const s = await api.activityStats({ skipQualityFilter: true })
|
||||||
|
setEntries(merged)
|
||||||
|
setStats(s)
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
const load = useCallback(async () => {
|
||||||
|
await fetchMonthsChain(monthsIncludedRef.current)
|
||||||
|
}, [fetchMonthsChain])
|
||||||
|
|
||||||
|
const onMonthPickerChange = (e) => {
|
||||||
|
const ym = e.target.value
|
||||||
|
if (!ym) return
|
||||||
|
setSelectedMonth(ym)
|
||||||
|
const chain = [ym]
|
||||||
|
monthsIncludedRef.current = chain
|
||||||
|
setMonthsIncluded(chain)
|
||||||
|
void fetchMonthsChain(chain)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const loadPreviousMonth = async () => {
|
||||||
|
const chain = monthsIncludedRef.current
|
||||||
|
if (chain.length === 0) return
|
||||||
|
const oldest = chain[chain.length - 1]
|
||||||
|
const prev = prevMonthYm(oldest)
|
||||||
|
if (chain.includes(prev)) return
|
||||||
|
if (prev < '2000-01') return
|
||||||
|
setListLoadingMore(true)
|
||||||
|
try {
|
||||||
|
const more = await api.listActivity(ACTIVITY_MONTH_FETCH_LIMIT, undefined, {
|
||||||
|
skipQualityFilter: true,
|
||||||
|
collapseDuplicateSessions: true,
|
||||||
|
month: prev,
|
||||||
|
})
|
||||||
|
const newChain = [...chain, prev]
|
||||||
|
monthsIncludedRef.current = newChain
|
||||||
|
setMonthsIncluded(newChain)
|
||||||
|
setEntries((cur) => dedupeActivitiesById([...cur, ...more]))
|
||||||
|
} finally {
|
||||||
|
setListLoadingMore(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const oldestLoadedYm = monthsIncluded.length ? monthsIncluded[monthsIncluded.length - 1] : selectedMonth
|
||||||
|
const nextOlderYm = prevMonthYm(oldestLoadedYm)
|
||||||
|
const canLoadOlder = nextOlderYm >= '2000-01' && !monthsIncluded.includes(nextOlderYm)
|
||||||
|
|
||||||
const loadUsage = () => {
|
const loadUsage = () => {
|
||||||
api.getFeatureUsage().then(features => {
|
api.getFeatureUsage().then(features => {
|
||||||
const activityFeature = features.find(f => f.feature_id === 'activity_entries')
|
const activityFeature = features.find(f => f.feature_id === 'activity_entries')
|
||||||
@@ -194,24 +635,166 @@ export default function ActivityPage() {
|
|||||||
}).catch(err => console.error('Failed to load usage:', err))
|
}).catch(err => console.error('Failed to load usage:', err))
|
||||||
}
|
}
|
||||||
|
|
||||||
useEffect(()=>{
|
useEffect(() => {
|
||||||
load()
|
const ym = ymdMonth()
|
||||||
|
monthsIncludedRef.current = [ym]
|
||||||
|
setMonthsIncluded([ym])
|
||||||
|
setSelectedMonth(ym)
|
||||||
|
void fetchMonthsChain([ym])
|
||||||
loadUsage()
|
loadUsage()
|
||||||
api.getTrainingCategories().then(setCategories).catch(err => console.error('Failed to load categories:', err))
|
api.getTrainingCategories().then(setCategories).catch(err => console.error('Failed to load categories:', err))
|
||||||
},[])
|
}, [fetchMonthsChain])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
editSchemaKeysPrevRef.current = new Set()
|
||||||
|
}, [editing?.id])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!editing?.id) {
|
||||||
|
setSessionDetail(null)
|
||||||
|
setMetricDraft({})
|
||||||
|
setSessionLoadError(null)
|
||||||
|
prevEditingIdRef.current = null
|
||||||
|
return
|
||||||
|
}
|
||||||
|
let cancelled = false
|
||||||
|
setSessionLoadError(null)
|
||||||
|
if (prevEditingIdRef.current !== editing.id) {
|
||||||
|
setSessionDetail(null)
|
||||||
|
prevEditingIdRef.current = editing.id
|
||||||
|
}
|
||||||
|
;(async () => {
|
||||||
|
try {
|
||||||
|
const d = await api.getActivitySession(editing.id, {
|
||||||
|
useFormSchema: true,
|
||||||
|
training_category: editing.training_category,
|
||||||
|
training_type_id: editing.training_type_id,
|
||||||
|
})
|
||||||
|
if (!cancelled) setSessionDetail(d)
|
||||||
|
} catch (err) {
|
||||||
|
if (!cancelled) {
|
||||||
|
setSessionDetail(null)
|
||||||
|
setSessionLoadError(err.message || 'Zusatzfelder konnten nicht geladen werden')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
return () => { cancelled = true }
|
||||||
|
}, [editing?.id, editing?.training_category, editing?.training_type_id])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!sessionDetail) {
|
||||||
|
setMetricDraft({})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const newKeys = new Set((sessionDetail.schema || []).map((s) => s.key))
|
||||||
|
const oldKeys = editSchemaKeysPrevRef.current
|
||||||
|
|
||||||
|
setMetricDraft((prev) => {
|
||||||
|
const next = { ...prev }
|
||||||
|
for (const row of sessionDetail.metrics || []) {
|
||||||
|
const k = row.key
|
||||||
|
if (oldKeys.size > 0 && oldKeys.has(k) && newKeys.has(k) && k in prev) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
next[k] = row.value
|
||||||
|
}
|
||||||
|
for (const s of sessionDetail.schema || []) {
|
||||||
|
if (!(s.key in next)) {
|
||||||
|
next[s.key] = s.data_type === 'boolean' ? false : ''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return next
|
||||||
|
})
|
||||||
|
editSchemaKeysPrevRef.current = newKeys
|
||||||
|
}, [sessionDetail])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (tab !== 'add') {
|
||||||
|
setManualSchema(null)
|
||||||
|
setManualMetricDraft({})
|
||||||
|
manualSchemaKeysPrevRef.current = new Set()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const tid = form.training_type_id
|
||||||
|
const cat = form.training_category
|
||||||
|
if (tid == null && (cat == null || cat === '')) {
|
||||||
|
setManualSchema(null)
|
||||||
|
setManualMetricDraft({})
|
||||||
|
manualSchemaKeysPrevRef.current = new Set()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
let cancelled = false
|
||||||
|
;(async () => {
|
||||||
|
try {
|
||||||
|
const r = await api.getActivityAttributeSchema({
|
||||||
|
training_category: cat || undefined,
|
||||||
|
training_type_id: tid ?? undefined,
|
||||||
|
})
|
||||||
|
if (!cancelled) setManualSchema(Array.isArray(r.schema) ? r.schema : [])
|
||||||
|
} catch (err) {
|
||||||
|
console.error('attribute-schema:', err)
|
||||||
|
if (!cancelled) setManualSchema([])
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
return () => { cancelled = true }
|
||||||
|
}, [tab, form.training_category, form.training_type_id])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (tab !== 'add' || !manualSchema) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const newKeys = new Set(manualSchema.map((s) => s.key))
|
||||||
|
const oldKeys = manualSchemaKeysPrevRef.current
|
||||||
|
|
||||||
|
setManualMetricDraft((prev) => {
|
||||||
|
const next = { ...prev }
|
||||||
|
for (const s of manualSchema) {
|
||||||
|
const k = s.key
|
||||||
|
if (oldKeys.size > 0 && oldKeys.has(k) && newKeys.has(k) && k in prev) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if (!(k in next)) {
|
||||||
|
next[k] = s.data_type === 'boolean' ? false : ''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return next
|
||||||
|
})
|
||||||
|
manualSchemaKeysPrevRef.current = newKeys
|
||||||
|
}, [tab, manualSchema])
|
||||||
|
|
||||||
const handleSave = async () => {
|
const handleSave = async () => {
|
||||||
setSaving(true)
|
setSaving(true)
|
||||||
setError(null)
|
setError(null)
|
||||||
try {
|
try {
|
||||||
const payload = {...form}
|
const payload = {...form}
|
||||||
|
payload.start_time =
|
||||||
|
payload.start_time === '' || payload.start_time == null
|
||||||
|
? null
|
||||||
|
: timePayloadFromInput(payload.start_time)
|
||||||
|
payload.end_time =
|
||||||
|
payload.end_time === '' || payload.end_time == null
|
||||||
|
? null
|
||||||
|
: timePayloadFromInput(payload.end_time)
|
||||||
if(payload.duration_min) payload.duration_min = parseFloat(payload.duration_min)
|
if(payload.duration_min) payload.duration_min = parseFloat(payload.duration_min)
|
||||||
if(payload.kcal_active) payload.kcal_active = parseFloat(payload.kcal_active)
|
if(payload.kcal_active) payload.kcal_active = parseFloat(payload.kcal_active)
|
||||||
if(payload.hr_avg) payload.hr_avg = parseFloat(payload.hr_avg)
|
if(payload.hr_avg) payload.hr_avg = parseFloat(payload.hr_avg)
|
||||||
if(payload.hr_max) payload.hr_max = parseFloat(payload.hr_max)
|
if(payload.hr_max) payload.hr_max = parseFloat(payload.hr_max)
|
||||||
if(payload.rpe) payload.rpe = parseInt(payload.rpe)
|
if(payload.rpe) payload.rpe = parseInt(payload.rpe)
|
||||||
payload.source = 'manual'
|
payload.source = 'manual'
|
||||||
await api.createActivity(payload)
|
const created = await api.createActivity(payload)
|
||||||
|
if (manualSchema && manualSchema.length > 0 && created?.id) {
|
||||||
|
try {
|
||||||
|
const metrics = buildMetricsPayload(manualSchema, manualMetricDraft)
|
||||||
|
await api.putActivityMetrics(created.id, { metrics })
|
||||||
|
} catch (metErr) {
|
||||||
|
console.error(metErr)
|
||||||
|
setError(
|
||||||
|
metErr.message ||
|
||||||
|
'Eintrag gespeichert, aber Zusatzfelder konnten nicht gespeichert werden.',
|
||||||
|
)
|
||||||
|
setTimeout(() => setError(null), 8000)
|
||||||
|
}
|
||||||
|
}
|
||||||
setSaved(true)
|
setSaved(true)
|
||||||
await load()
|
await load()
|
||||||
await loadUsage() // Reload usage after save
|
await loadUsage() // Reload usage after save
|
||||||
@@ -226,9 +809,83 @@ export default function ActivityPage() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const handleUpdate = async () => {
|
const handleUpdate = async () => {
|
||||||
const payload = {...editing}
|
setSavingEdit(true)
|
||||||
await api.updateActivity(editing.id, payload)
|
setError(null)
|
||||||
setEditing(null); await load()
|
try {
|
||||||
|
const payload = { ...editing }
|
||||||
|
delete payload.id
|
||||||
|
for (const s of sessionDetail?.schema || []) {
|
||||||
|
const col = s.source_field
|
||||||
|
if (!col || !ACTIVITY_LOG_PAYLOAD_KEYS.has(col)) continue
|
||||||
|
if (!(s.key in metricDraft)) continue
|
||||||
|
const raw = metricDraft[s.key]
|
||||||
|
const rawStr = raw === null || raw === undefined ? '' : String(raw).trim()
|
||||||
|
if (rawStr === '') {
|
||||||
|
payload[col] = null
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
let v = rawStr
|
||||||
|
if (s.data_type === 'integer') {
|
||||||
|
v = parseInt(rawStr, 10)
|
||||||
|
if (Number.isNaN(v)) throw new Error(`Ungültige Zahl: ${s.name_de}`)
|
||||||
|
} else if (s.data_type === 'float') {
|
||||||
|
v = parseFloat(rawStr)
|
||||||
|
if (Number.isNaN(v)) throw new Error(`Ungültige Zahl: ${s.name_de}`)
|
||||||
|
} else if (s.data_type === 'boolean') {
|
||||||
|
v = !!raw
|
||||||
|
} else {
|
||||||
|
v = rawStr
|
||||||
|
}
|
||||||
|
payload[col] = v
|
||||||
|
}
|
||||||
|
if (payload.duration_min !== '' && payload.duration_min != null) payload.duration_min = parseFloat(payload.duration_min)
|
||||||
|
if (payload.kcal_active !== '' && payload.kcal_active != null) payload.kcal_active = parseFloat(payload.kcal_active)
|
||||||
|
if (payload.hr_avg !== '' && payload.hr_avg != null) payload.hr_avg = parseFloat(payload.hr_avg)
|
||||||
|
if (payload.hr_max !== '' && payload.hr_max != null) payload.hr_max = parseFloat(payload.hr_max)
|
||||||
|
if (payload.rpe !== '' && payload.rpe != null) payload.rpe = parseInt(payload.rpe, 10)
|
||||||
|
payload.start_time =
|
||||||
|
payload.start_time === '' || payload.start_time == null
|
||||||
|
? null
|
||||||
|
: timePayloadFromInput(payload.start_time)
|
||||||
|
payload.end_time =
|
||||||
|
payload.end_time === '' || payload.end_time == null
|
||||||
|
? null
|
||||||
|
: timePayloadFromInput(payload.end_time)
|
||||||
|
await api.updateActivity(editing.id, payload)
|
||||||
|
if (sessionDetail?.schema?.length > 0) {
|
||||||
|
const draftForMetrics = { ...metricDraft }
|
||||||
|
for (const s of sessionDetail.schema) {
|
||||||
|
const bind = activitySchemaHeadlineBinding(s)
|
||||||
|
if (!bind || !(s.key in draftForMetrics)) continue
|
||||||
|
const rawCol =
|
||||||
|
payload[bind.headlineCol] !== undefined ? payload[bind.headlineCol] : editing?.[bind.headlineCol]
|
||||||
|
if (rawCol === undefined) continue
|
||||||
|
if (s.data_type === 'boolean') {
|
||||||
|
draftForMetrics[s.key] = !!rawCol
|
||||||
|
} else if (s.data_type === 'integer') {
|
||||||
|
const n = parseInt(String(rawCol), 10)
|
||||||
|
draftForMetrics[s.key] = Number.isNaN(n) ? '' : n
|
||||||
|
} else if (s.data_type === 'float') {
|
||||||
|
const n = parseFloat(String(rawCol))
|
||||||
|
draftForMetrics[s.key] = Number.isNaN(n) ? '' : n
|
||||||
|
} else {
|
||||||
|
draftForMetrics[s.key] = rawCol == null ? '' : String(rawCol)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const metrics = buildMetricsPayload(sessionDetail.schema, draftForMetrics)
|
||||||
|
await api.putActivityMetrics(editing.id, { metrics })
|
||||||
|
}
|
||||||
|
setEditing(null)
|
||||||
|
setSessionDetail(null)
|
||||||
|
startTransition(() => {
|
||||||
|
void load()
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
setError(err.message || 'Speichern fehlgeschlagen')
|
||||||
|
setTimeout(() => setError(null), 6000)
|
||||||
|
} finally {
|
||||||
|
setSavingEdit(false)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleDelete = async (id) => {
|
const handleDelete = async (id) => {
|
||||||
@@ -266,12 +923,19 @@ export default function ActivityPage() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Übersicht */}
|
{/* Übersicht */}
|
||||||
{stats && stats.count>0 && (
|
{stats && (stats.total_in_profile > 0 || stats.count > 0) && (
|
||||||
<div className="card section-gap">
|
<div className="card section-gap">
|
||||||
|
<div style={{ fontSize: 11, color: 'var(--text3)', marginBottom: 8, lineHeight: 1.45 }}>
|
||||||
|
<strong>{stats.total_in_profile ?? '–'}</strong> Einträge im Profil (gleicher Filter wie diese Seite). Die Summen
|
||||||
|
Kcal/Stunden beziehen sich auf die <strong>neuesten {stats.sample_size ?? stats.count}</strong> Einträge (max.
|
||||||
|
30).
|
||||||
|
</div>
|
||||||
<div style={{display:'flex',gap:8,flexWrap:'wrap'}}>
|
<div style={{display:'flex',gap:8,flexWrap:'wrap'}}>
|
||||||
{[['Trainings',stats.count,'var(--text1)'],
|
{[
|
||||||
['Kcal gesamt',Math.round(stats.total_kcal),'#EF9F27'],
|
['Neueste (max. 30)', stats.count, 'var(--text1)'],
|
||||||
['Stunden',Math.round(stats.total_min/60*10)/10,'#378ADD']].map(([l,v,c])=>(
|
['Kcal (darin)', Math.round(stats.total_kcal), '#EF9F27'],
|
||||||
|
['Stunden (darin)', Math.round(stats.total_min / 60 * 10) / 10, '#378ADD'],
|
||||||
|
].map(([l, v, c]) => (
|
||||||
<div key={l} style={{flex:1,minWidth:80,background:'var(--surface2)',borderRadius:8,padding:'8px 10px',textAlign:'center'}}>
|
<div key={l} style={{flex:1,minWidth:80,background:'var(--surface2)',borderRadius:8,padding:'8px 10px',textAlign:'center'}}>
|
||||||
<div style={{fontSize:18,fontWeight:700,color:c}}>{v}</div>
|
<div style={{fontSize:18,fontWeight:700,color:c}}>{v}</div>
|
||||||
<div style={{fontSize:10,color:'var(--text3)'}}>{l}</div>
|
<div style={{fontSize:10,color:'var(--text3)'}}>{l}</div>
|
||||||
@@ -296,9 +960,23 @@ export default function ActivityPage() {
|
|||||||
<span>Training eintragen</span>
|
<span>Training eintragen</span>
|
||||||
{activityUsage && <UsageBadge {...activityUsage} />}
|
{activityUsage && <UsageBadge {...activityUsage} />}
|
||||||
</div>
|
</div>
|
||||||
<EntryForm form={form} setForm={setForm}
|
<EntryForm
|
||||||
onSave={handleSave} saveLabel={saved?'✓ Gespeichert!':'Speichern'}
|
form={form}
|
||||||
saving={saving} error={error} usage={activityUsage}/>
|
setForm={setForm}
|
||||||
|
onSave={handleSave}
|
||||||
|
saveLabel={saved ? '✓ Gespeichert!' : 'Speichern'}
|
||||||
|
saving={saving}
|
||||||
|
error={error}
|
||||||
|
usage={activityUsage}
|
||||||
|
formExtras={
|
||||||
|
<SessionMetricsFields
|
||||||
|
schema={manualSchema}
|
||||||
|
metrics={[]}
|
||||||
|
values={manualMetricDraft}
|
||||||
|
setValues={setManualMetricDraft}
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
@@ -335,6 +1013,37 @@ export default function ActivityPage() {
|
|||||||
|
|
||||||
{tab==='list' && (
|
{tab==='list' && (
|
||||||
<div>
|
<div>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
display: 'flex',
|
||||||
|
alignItems: 'center',
|
||||||
|
gap: 12,
|
||||||
|
marginBottom: 12,
|
||||||
|
flexWrap: 'wrap',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<label className="form-label" style={{ margin: 0, display: 'flex', alignItems: 'center', gap: 8 }}>
|
||||||
|
Monat
|
||||||
|
<input
|
||||||
|
type="month"
|
||||||
|
className="form-input"
|
||||||
|
value={selectedMonth}
|
||||||
|
onChange={onMonthPickerChange}
|
||||||
|
style={{ width: 'auto', minWidth: 150, margin: 0 }}
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
{monthsIncluded.length > 1 && (
|
||||||
|
<span style={{ fontSize: 11, color: 'var(--text3)' }}>
|
||||||
|
Zeitraum: {dayjs(`${selectedMonth}-01`).format('MMMM YYYY')} bis{' '}
|
||||||
|
{dayjs(`${oldestLoadedYm}-01`).format('MMMM YYYY')}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<p style={{ fontSize: 12, color: 'var(--text3)', margin: '0 0 12px', lineHeight: 1.5 }}>
|
||||||
|
Hier sind <strong>alle</strong> Trainings sichtbar (Profil-Qualitätsfilter aus — auch ohne Bewertung oder bei
|
||||||
|
abweichender Einordnung). Unter „Verlauf“ / Auswertung bleibt der Filter aktiv. Es wird jeweils ein
|
||||||
|
kompletter Kalendermonat geladen; „Vorheriger Monat“ hängt den nächstälteren Monat an (ohne OFFSET-Pagination).
|
||||||
|
</p>
|
||||||
{entries.length===0 && (
|
{entries.length===0 && (
|
||||||
<div className="empty-state">
|
<div className="empty-state">
|
||||||
<h3>Keine Trainings</h3>
|
<h3>Keine Trainings</h3>
|
||||||
@@ -347,8 +1056,28 @@ export default function ActivityPage() {
|
|||||||
return (
|
return (
|
||||||
<div key={e.id} className="card" style={{marginBottom:8,borderLeft:`3px solid ${color}`}}>
|
<div key={e.id} className="card" style={{marginBottom:8,borderLeft:`3px solid ${color}`}}>
|
||||||
{isEd ? (
|
{isEd ? (
|
||||||
<EntryForm form={editing} setForm={setEditing}
|
<EntryForm
|
||||||
onSave={handleUpdate} onCancel={()=>setEditing(null)} saveLabel="Speichern"/>
|
form={editing}
|
||||||
|
setForm={setEditing}
|
||||||
|
onSave={handleUpdate}
|
||||||
|
onCancel={() => { setEditing(null); setSessionDetail(null); setSessionLoadError(null) }}
|
||||||
|
saveLabel="Speichern"
|
||||||
|
saving={savingEdit}
|
||||||
|
error={error}
|
||||||
|
formExtras={
|
||||||
|
<>
|
||||||
|
{sessionLoadError && (
|
||||||
|
<div style={{ fontSize: 12, color: 'var(--text3)', marginBottom: 8 }}>{sessionLoadError}</div>
|
||||||
|
)}
|
||||||
|
<SessionMetricsFields
|
||||||
|
schema={sessionDetail?.schema}
|
||||||
|
metrics={sessionDetail?.metrics}
|
||||||
|
values={metricDraft}
|
||||||
|
setValues={setMetricDraft}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
}
|
||||||
|
/>
|
||||||
) : (
|
) : (
|
||||||
<div>
|
<div>
|
||||||
<div style={{display:'flex',justifyContent:'space-between',alignItems:'flex-start'}}>
|
<div style={{display:'flex',justifyContent:'space-between',alignItems:'flex-start'}}>
|
||||||
@@ -435,7 +1164,12 @@ export default function ActivityPage() {
|
|||||||
</div>
|
</div>
|
||||||
<div style={{fontSize:11,color:'var(--text3)',marginBottom:4}}>
|
<div style={{fontSize:11,color:'var(--text3)',marginBottom:4}}>
|
||||||
{dayjs(e.date).format('dd, DD. MMMM YYYY')}
|
{dayjs(e.date).format('dd, DD. MMMM YYYY')}
|
||||||
{e.start_time && e.start_time.length>10 && ` · ${e.start_time.slice(11,16)}`}
|
{(formatTimeForList(e.start_time) || formatTimeForList(e.end_time)) && (
|
||||||
|
<span>
|
||||||
|
{formatTimeForList(e.start_time) && ` · Start ${formatTimeForList(e.start_time)}`}
|
||||||
|
{formatTimeForList(e.end_time) && ` · Ende ${formatTimeForList(e.end_time)}`}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
<div style={{display:'flex',gap:10,flexWrap:'wrap'}}>
|
<div style={{display:'flex',gap:10,flexWrap:'wrap'}}>
|
||||||
{e.duration_min && <span style={{fontSize:12,color:'var(--text2)'}}>⏱ {Math.round(e.duration_min)} Min</span>}
|
{e.duration_min && <span style={{fontSize:12,color:'var(--text2)'}}>⏱ {Math.round(e.duration_min)} Min</span>}
|
||||||
@@ -458,6 +1192,21 @@ export default function ActivityPage() {
|
|||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
})}
|
})}
|
||||||
|
{canLoadOlder && (
|
||||||
|
<div style={{ marginTop: 12, marginBottom: 8 }}>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="btn btn-secondary"
|
||||||
|
style={{ width: '100%' }}
|
||||||
|
disabled={listLoadingMore}
|
||||||
|
onClick={() => void loadPreviousMonth()}
|
||||||
|
>
|
||||||
|
{listLoadingMore
|
||||||
|
? 'Lade…'
|
||||||
|
: `Vorherigen Monat laden (${dayjs(`${nextOlderYm}-01`).format('MMMM YYYY')})`}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
File diff suppressed because it is too large.
Load diff
@@ -297,10 +297,19 @@ export default function AdminCsvTemplateEditorPage() {
|
|||||||
const aggregateSleepImport = modMeta?.import_mode === 'apple_sleep_aggregate'
|
const aggregateSleepImport = modMeta?.import_mode === 'apple_sleep_aggregate'
|
||||||
const targetOptions = useMemo(() => {
|
const targetOptions = useMemo(() => {
|
||||||
if (!modMeta?.fields || aggregateSleepImport) return []
|
if (!modMeta?.fields || aggregateSleepImport) return []
|
||||||
return Object.entries(modMeta.fields).map(([key, meta]) => ({
|
const entries = Object.entries(modMeta.fields).map(([key, meta]) => {
|
||||||
value: key,
|
const title = meta.label_de || meta.name_de || key
|
||||||
label: `${key}${meta.required ? ' *' : ''}`,
|
return {
|
||||||
}))
|
value: key,
|
||||||
|
label: `${title}${meta.required ? ' *' : ''}`,
|
||||||
|
group: meta.from_training_parameter ? 'eav' : 'log',
|
||||||
|
}
|
||||||
|
})
|
||||||
|
entries.sort((a, b) => {
|
||||||
|
if (a.group !== b.group) return a.group === 'log' ? -1 : 1
|
||||||
|
return a.label.localeCompare(b.label, 'de')
|
||||||
|
})
|
||||||
|
return entries
|
||||||
}, [modMeta, aggregateSleepImport])
|
}, [modMeta, aggregateSleepImport])
|
||||||
|
|
||||||
const requiredTargets = useMemo(() => {
|
const requiredTargets = useMemo(() => {
|
||||||
@@ -1025,11 +1034,23 @@ export default function AdminCsvTemplateEditorPage() {
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<option value="-">— ignorieren</option>
|
<option value="-">— ignorieren</option>
|
||||||
{targetOptions.map((o) => (
|
{['log', 'eav'].map((g) => {
|
||||||
<option key={o.value} value={o.value}>
|
const opts = targetOptions.filter((o) => o.group === g)
|
||||||
{o.label}
|
if (!opts.length) return null
|
||||||
</option>
|
const ogLabel =
|
||||||
))}
|
g === 'log'
|
||||||
|
? 'Activity — Kernfelder (activity_log)'
|
||||||
|
: 'Trainingsparameter (EAV)'
|
||||||
|
return (
|
||||||
|
<optgroup key={g} label={ogLabel}>
|
||||||
|
{opts.map((o) => (
|
||||||
|
<option key={o.value} value={o.value}>
|
||||||
|
{o.label}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</optgroup>
|
||||||
|
)
|
||||||
|
})}
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
import { useAuth } from '../context/AuthContext'
|
import { useAuth } from '../context/AuthContext'
|
||||||
import Markdown from '../utils/Markdown'
|
import Markdown from '../utils/Markdown'
|
||||||
import UsageBadge from '../components/UsageBadge'
|
import UsageBadge from '../components/UsageBadge'
|
||||||
|
import WorkflowDebugPanel from '../components/WorkflowDebugPanel'
|
||||||
import dayjs from 'dayjs'
|
import dayjs from 'dayjs'
|
||||||
import 'dayjs/locale/de'
|
import 'dayjs/locale/de'
|
||||||
dayjs.locale('de')
|
dayjs.locale('de')
|
||||||
@@ -338,6 +339,8 @@ export default function Analysis() {
|
|||||||
/** Kategorie-Schlüssel aus `buildPipelineGroups` (Navigation); Detail = alle Pipelines dieser Kategorie */
|
/** Kategorie-Schlüssel aus `buildPipelineGroups` (Navigation); Detail = alle Pipelines dieser Kategorie */
|
||||||
const [activeCategoryKey, setActiveCategoryKey] = useState(null)
|
const [activeCategoryKey, setActiveCategoryKey] = useState(null)
|
||||||
const [historyScopePick, setHistoryScopePick] = useState(null)
|
const [historyScopePick, setHistoryScopePick] = useState(null)
|
||||||
|
// NEW: Progress tracking for SSE workflows
|
||||||
|
const [progress, setProgress] = useState(null) // { total_nodes, completed_nodes, current_node_label }
|
||||||
|
|
||||||
const loadAll = async () => {
|
const loadAll = async () => {
|
||||||
const [p, i] = await Promise.all([
|
const [p, i] = await Promise.all([
|
||||||
@@ -377,10 +380,21 @@ export default function Analysis() {
|
|||||||
}, [newResult?.scope, prompts])
|
}, [newResult?.scope, prompts])
|
||||||
|
|
||||||
const runPrompt = async (slug) => {
|
const runPrompt = async (slug) => {
|
||||||
setLoading(slug); setError(null); setNewResult(null)
|
setLoading(slug); setError(null); setNewResult(null); setProgress(null)
|
||||||
try {
|
try {
|
||||||
// Use new unified executor with save=true
|
// Use SSE-based executor for long-running workflows
|
||||||
const result = await api.executeUnifiedPrompt(slug, null, null, false, true)
|
const result = await api.executeUnifiedPromptStream(slug, null, null, true, true, (event) => {
|
||||||
|
// Progress callback: update UI in real-time
|
||||||
|
if (event.type === 'execution_started') {
|
||||||
|
setProgress({ total_nodes: 0, completed_nodes: 0, current_node_label: 'Starte...' })
|
||||||
|
} else if (event.type === 'node_complete') {
|
||||||
|
setProgress({
|
||||||
|
total_nodes: event.total_nodes || 0,
|
||||||
|
completed_nodes: event.completed_nodes || 0,
|
||||||
|
current_node_label: event.node_label || `Node ${event.node_id}`
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// Transform result to match old format for InsightCard
|
// Transform result to match old format for InsightCard
|
||||||
let content = ''
|
let content = ''
|
||||||
@@ -429,12 +443,22 @@ export default function Analysis() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setNewResult({ scope: slug, content, metadata })
|
setNewResult({
|
||||||
|
scope: slug,
|
||||||
|
content,
|
||||||
|
metadata,
|
||||||
|
node_states: result.node_states, // For workflow debug panel
|
||||||
|
result_type: result.type,
|
||||||
|
aggregated_result: result.aggregated_result
|
||||||
|
})
|
||||||
await loadAll()
|
await loadAll()
|
||||||
setTab('run')
|
setTab('run')
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
setError('Fehler: ' + e.message)
|
setError('Fehler: ' + e.message)
|
||||||
} finally { setLoading(null) }
|
} finally {
|
||||||
|
setLoading(null)
|
||||||
|
setProgress(null) // Clear progress
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const deleteInsight = async (id) => {
|
const deleteInsight = async (id) => {
|
||||||
@@ -518,6 +542,9 @@ export default function Analysis() {
|
|||||||
defaultOpen={true}
|
defaultOpen={true}
|
||||||
prompts={prompts}
|
prompts={prompts}
|
||||||
/>
|
/>
|
||||||
|
{newResult.node_states && (
|
||||||
|
<WorkflowDebugPanel nodeStates={newResult.node_states} />
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
@@ -575,6 +602,9 @@ export default function Analysis() {
|
|||||||
defaultOpen={true}
|
defaultOpen={true}
|
||||||
prompts={prompts}
|
prompts={prompts}
|
||||||
/>
|
/>
|
||||||
|
{newResult.node_states && (
|
||||||
|
<WorkflowDebugPanel nodeStates={newResult.node_states} />
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
{activeCategoryKey && (() => {
|
{activeCategoryKey && (() => {
|
||||||
@@ -618,7 +648,9 @@ export default function Analysis() {
|
|||||||
disabled={!!loading||!canUseAI||(aiUsage && !aiUsage.allowed)}
|
disabled={!!loading||!canUseAI||(aiUsage && !aiUsage.allowed)}
|
||||||
>
|
>
|
||||||
{loading===p.slug
|
{loading===p.slug
|
||||||
? <><div className="spinner" style={{width:13,height:13}}/> Läuft…</>
|
? (progress
|
||||||
|
? <><div className="spinner" style={{width:13,height:13}}/> {progress.completed_nodes}/{progress.total_nodes} Nodes</>
|
||||||
|
: <><div className="spinner" style={{width:13,height:13}}/> Läuft…</>)
|
||||||
: (aiUsage && !aiUsage.allowed) ? '🔒 Limit'
|
: (aiUsage && !aiUsage.allowed) ? '🔒 Limit'
|
||||||
: <><Brain size={13}/> Starten</>}
|
: <><Brain size={13}/> Starten</>}
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
@@ -55,6 +55,28 @@ const getCategoryForGoalType = (goalType) => {
|
|||||||
return mapping[goalType] || 'other'
|
return mapping[goalType] || 'other'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Fortschritt in % von Start zum Ziel; bevorzugt `progress_pct` vom Server, sonst aus Ist-Werten. */
|
||||||
|
function getGoalProgressPercent(goal) {
|
||||||
|
const apiVal = goal?.progress_pct
|
||||||
|
if (apiVal !== null && apiVal !== undefined && Number.isFinite(Number(apiVal))) {
|
||||||
|
return Number(apiVal)
|
||||||
|
}
|
||||||
|
const s = goal?.start_value != null ? Number(goal.start_value) : NaN
|
||||||
|
const t = goal?.target_value != null ? Number(goal.target_value) : NaN
|
||||||
|
const c =
|
||||||
|
goal?.current_value != null && goal.current_value !== ''
|
||||||
|
? Number(goal.current_value)
|
||||||
|
: NaN
|
||||||
|
if (!Number.isFinite(s) || !Number.isFinite(t) || !Number.isFinite(c)) return null
|
||||||
|
const span = Math.abs(t - s)
|
||||||
|
if (span < 1e-9) {
|
||||||
|
if (Math.abs(c - s) < 1e-9) return 100
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
if (t > s) return ((c - s) / (t - s)) * 100
|
||||||
|
return ((s - c) / (s - t)) * 100
|
||||||
|
}
|
||||||
|
|
||||||
export default function GoalsPage() {
|
export default function GoalsPage() {
|
||||||
const [goalMode, setGoalMode] = useState(null)
|
const [goalMode, setGoalMode] = useState(null)
|
||||||
const [userFocusWeights, setUserFocusWeights] = useState([]) // v2.0: User's focus area weights
|
const [userFocusWeights, setUserFocusWeights] = useState([]) // v2.0: User's focus area weights
|
||||||
@@ -613,6 +635,17 @@ export default function GoalsPage() {
|
|||||||
{categoryGoals.map(goal => {
|
{categoryGoals.map(goal => {
|
||||||
const typeInfo = goalTypesMap[goal.goal_type] || { label_de: goal.goal_type, unit: '', icon: '📊' }
|
const typeInfo = goalTypesMap[goal.goal_type] || { label_de: goal.goal_type, unit: '', icon: '📊' }
|
||||||
const priorityInfo = PRIORITY_LEVELS[goal.priority] || PRIORITY_LEVELS[2]
|
const priorityInfo = PRIORITY_LEVELS[goal.priority] || PRIORITY_LEVELS[2]
|
||||||
|
const progressPct = getGoalProgressPercent(goal)
|
||||||
|
const progressLabel =
|
||||||
|
progressPct != null
|
||||||
|
? (Math.abs(progressPct - Math.round(progressPct)) < 0.05
|
||||||
|
? `${Math.round(progressPct)}`
|
||||||
|
: `${Math.round(progressPct * 10) / 10}`.replace('.', ','))
|
||||||
|
: null
|
||||||
|
const progressBarWidth =
|
||||||
|
progressPct != null
|
||||||
|
? Math.min(100, Math.max(0, progressPct))
|
||||||
|
: 0
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
@@ -719,30 +752,40 @@ export default function GoalsPage() {
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{goal.progress_pct !== null && (
|
{progressPct != null && (
|
||||||
<div>
|
<div style={{ marginTop: 4 }}>
|
||||||
<div style={{
|
<div style={{
|
||||||
display: 'flex',
|
display: 'flex',
|
||||||
justifyContent: 'space-between',
|
justifyContent: 'space-between',
|
||||||
fontSize: 12,
|
fontSize: 12,
|
||||||
marginBottom: 4,
|
marginBottom: 6,
|
||||||
color: 'var(--text2)'
|
color: 'var(--text2)',
|
||||||
|
alignItems: 'baseline',
|
||||||
|
gap: 8
|
||||||
}}>
|
}}>
|
||||||
<span>Fortschritt</span>
|
<span>Fortschritt (Start → Ziel)</span>
|
||||||
<span style={{ fontWeight: 600, color: 'var(--text1)' }}>{goal.progress_pct}%</span>
|
<span style={{ fontWeight: 600, color: 'var(--text1)' }}>
|
||||||
|
{progressLabel}%
|
||||||
|
{progressPct > 100 && (
|
||||||
|
<span style={{ fontWeight: 500, color: 'var(--text3)', marginLeft: 4 }}>
|
||||||
|
(über Ziel)
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div style={{
|
<div style={{
|
||||||
width: '100%',
|
width: '100%',
|
||||||
height: 8,
|
height: 10,
|
||||||
background: 'var(--surface)',
|
background: 'var(--surface)',
|
||||||
borderRadius: 4,
|
borderRadius: 6,
|
||||||
overflow: 'hidden'
|
overflow: 'hidden',
|
||||||
|
border: '1px solid var(--border)'
|
||||||
}}>
|
}}>
|
||||||
<div style={{
|
<div style={{
|
||||||
width: `${Math.min(100, Math.max(0, goal.progress_pct))}%`,
|
width: `${progressBarWidth}%`,
|
||||||
height: '100%',
|
height: '100%',
|
||||||
background: catInfo.color,
|
background: getProgressColor(Math.min(100, Math.max(0, progressPct))),
|
||||||
transition: 'width 0.3s ease'
|
transition: 'width 0.35s ease'
|
||||||
}} />
|
}} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
// Lightweight Markdown renderer – handles the subset used by the AI:
|
// Lightweight Markdown renderer – handles the subset used by the AI:
|
||||||
// ## Headings, **bold**, bullet lists, numbered lists, line breaks
|
// ## Headings, **bold**, bullet lists, numbered lists, fenced ``` code ```, line breaks
|
||||||
|
|
||||||
export default function Markdown({ text }) {
|
export default function Markdown({ text }) {
|
||||||
if (!text) return null
|
if (!text) return null
|
||||||
@@ -7,6 +7,19 @@ export default function Markdown({ text }) {
|
|||||||
const lines = text.split('\n')
|
const lines = text.split('\n')
|
||||||
const elements = []
|
const elements = []
|
||||||
let i = 0
|
let i = 0
|
||||||
|
let blockId = 0
|
||||||
|
|
||||||
|
const codeBlockStyle = {
|
||||||
|
margin: '10px 0',
|
||||||
|
padding: 12,
|
||||||
|
background: 'var(--surface2)',
|
||||||
|
borderRadius: 8,
|
||||||
|
border: '1px solid var(--border)',
|
||||||
|
overflow: 'auto',
|
||||||
|
fontSize: 12,
|
||||||
|
lineHeight: 1.5,
|
||||||
|
fontFamily: 'ui-monospace, Consolas, monospace'
|
||||||
|
}
|
||||||
|
|
||||||
const parseLine = (line) => {
|
const parseLine = (line) => {
|
||||||
// Parse inline **bold** and *italic*
|
// Parse inline **bold** and *italic*
|
||||||
@@ -39,6 +52,44 @@ export default function Markdown({ text }) {
|
|||||||
while (i < lines.length) {
|
while (i < lines.length) {
|
||||||
const line = lines[i]
|
const line = lines[i]
|
||||||
|
|
||||||
|
// Fenced code block: ``` or ```lang
|
||||||
|
const trimmedStart = line.trimStart()
|
||||||
|
if (trimmedStart.startsWith('```')) {
|
||||||
|
const lang = trimmedStart.slice(3).trim() || null
|
||||||
|
i++
|
||||||
|
const codeLines = []
|
||||||
|
while (i < lines.length) {
|
||||||
|
if (lines[i].trim().startsWith('```')) {
|
||||||
|
i++
|
||||||
|
break
|
||||||
|
}
|
||||||
|
codeLines.push(lines[i])
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
const code = codeLines.join('\n')
|
||||||
|
elements.push(
|
||||||
|
<div key={`code-${blockId++}`} style={{ margin: '10px 0' }}>
|
||||||
|
{lang && (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
fontSize: 10,
|
||||||
|
color: 'var(--text3)',
|
||||||
|
marginBottom: 6,
|
||||||
|
fontFamily: 'ui-monospace, monospace',
|
||||||
|
letterSpacing: 0.02
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{lang}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<pre style={codeBlockStyle}>
|
||||||
|
<code style={{ color: 'var(--text1)', whiteSpace: 'pre', display: 'block' }}>{code}</code>
|
||||||
|
</pre>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
// Skip empty lines (add spacing)
|
// Skip empty lines (add spacing)
|
||||||
if (line.trim() === '') {
|
if (line.trim() === '') {
|
||||||
elements.push(<div key={i} style={{ height: 8 }} />)
|
elements.push(<div key={i} style={{ height: 8 }} />)
|
||||||
|
|||||||
+144
-3
@@ -138,16 +138,30 @@ export const api = {
|
|||||||
deleteCaliper: (id) => req(`/caliper/${id}`,{method:'DELETE'}),
|
deleteCaliper: (id) => req(`/caliper/${id}`,{method:'DELETE'}),
|
||||||
|
|
||||||
// Activity
|
// Activity
|
||||||
/** @param {number} [limit=200] @param {number} [days] nur Einträge ab HEUTE−days (Kalendertage), backend-filtert */
|
/**
|
||||||
listActivity: (limit=200, days)=> {
|
* @param {number} [limit=200]
|
||||||
|
* @param {number} [days] nur Einträge ab HEUTE−days (Kalendertage), backend-filtert
|
||||||
|
* @param {{ offset?: number, skipQualityFilter?: boolean, month?: string, collapseDuplicateSessions?: boolean }} [opts] month = YYYY-MM (schließt days/offset aus)
|
||||||
|
*/
|
||||||
|
listActivity: (limit=200, days, opts={})=> {
|
||||||
const q = new URLSearchParams({ limit: String(limit) })
|
const q = new URLSearchParams({ limit: String(limit) })
|
||||||
if (days != null && days !== '') q.set('days', String(days))
|
if (days != null && days !== '') q.set('days', String(days))
|
||||||
|
if (opts.month) q.set('month', String(opts.month))
|
||||||
|
if (opts.offset != null && opts.offset > 0) q.set('offset', String(opts.offset))
|
||||||
|
if (opts.collapseDuplicateSessions) q.set('collapse_duplicate_sessions', 'true')
|
||||||
|
if (opts.skipQualityFilter) q.set('skip_quality_filter', 'true')
|
||||||
return req(`/activity?${q}`)
|
return req(`/activity?${q}`)
|
||||||
},
|
},
|
||||||
createActivity: (d) => req('/activity',json(d)),
|
createActivity: (d) => req('/activity',json(d)),
|
||||||
updateActivity: (id,d) => req(`/activity/${id}`,jput(d)),
|
updateActivity: (id,d) => req(`/activity/${id}`,jput(d)),
|
||||||
deleteActivity: (id) => req(`/activity/${id}`,{method:'DELETE'}),
|
deleteActivity: (id) => req(`/activity/${id}`,{method:'DELETE'}),
|
||||||
activityStats: () => req('/activity/stats'),
|
/** @param {{ skipQualityFilter?: boolean }} [opts] */
|
||||||
|
activityStats: (opts={}) => {
|
||||||
|
const q = new URLSearchParams()
|
||||||
|
if (opts.skipQualityFilter) q.set('skip_quality_filter', 'true')
|
||||||
|
const qs = q.toString()
|
||||||
|
return req(`/activity/stats${qs ? `?${qs}` : ''}`)
|
||||||
|
},
|
||||||
listUncategorizedActivities: () => req('/activity/uncategorized'),
|
listUncategorizedActivities: () => req('/activity/uncategorized'),
|
||||||
bulkCategorizeActivities: (d) => req('/activity/bulk-categorize', json(d)),
|
bulkCategorizeActivities: (d) => req('/activity/bulk-categorize', json(d)),
|
||||||
importActivityCsv: async(file)=>{
|
importActivityCsv: async(file)=>{
|
||||||
@@ -314,6 +328,66 @@ export const api = {
|
|||||||
adminDeleteActivityMapping: (id) => req(`/admin/activity-mappings/${id}`, {method:'DELETE'}),
|
adminDeleteActivityMapping: (id) => req(`/admin/activity-mappings/${id}`, {method:'DELETE'}),
|
||||||
adminGetMappingCoverage: () => req('/admin/activity-mappings/stats/coverage'),
|
adminGetMappingCoverage: () => req('/admin/activity-mappings/stats/coverage'),
|
||||||
|
|
||||||
|
// Admin: Training session metrics (EAV) & attribute profiles (Migration 054)
|
||||||
|
adminListTrainingParameters: (includeInactive = false) =>
|
||||||
|
req(`/admin/training-parameters${includeInactive ? '?include_inactive=true' : ''}`),
|
||||||
|
adminCreateTrainingParameter: (d) => req('/admin/training-parameters', json(d)),
|
||||||
|
adminUpdateTrainingParameter: (id, d) => req(`/admin/training-parameters/${id}`, jput(d)),
|
||||||
|
adminDeleteTrainingParameter: (id) =>
|
||||||
|
req(`/admin/training-parameters/${id}`, { method: 'DELETE' }),
|
||||||
|
adminListTrainingCategoryParameters: (category = '') =>
|
||||||
|
req(
|
||||||
|
`/admin/training-category-parameters${category ? `?category=${encodeURIComponent(category)}` : ''}`,
|
||||||
|
),
|
||||||
|
adminAddTrainingCategoryParameter: (d) => req('/admin/training-category-parameters', json(d)),
|
||||||
|
adminUpdateTrainingCategoryParameter: (id, d) =>
|
||||||
|
req(`/admin/training-category-parameters/${id}`, jput(d)),
|
||||||
|
adminDeleteTrainingCategoryParameter: (id) =>
|
||||||
|
req(`/admin/training-category-parameters/${id}`, { method: 'DELETE' }),
|
||||||
|
adminListTrainingTypeParameters: (trainingTypeId) =>
|
||||||
|
req(`/admin/training-type-parameters?training_type_id=${encodeURIComponent(trainingTypeId)}`),
|
||||||
|
adminAddTrainingTypeParameter: (d) => req('/admin/training-type-parameters', json(d)),
|
||||||
|
adminUpdateTrainingTypeParameter: (id, d) =>
|
||||||
|
req(`/admin/training-type-parameters/${id}`, jput(d)),
|
||||||
|
adminDeleteTrainingTypeParameter: (id) =>
|
||||||
|
req(`/admin/training-type-parameters/${id}`, { method: 'DELETE' }),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} id
|
||||||
|
* @param {{ useFormSchema?: boolean, training_category?: string | null, training_type_id?: number | null }} [opts]
|
||||||
|
*/
|
||||||
|
getActivitySession: (id, opts = {}) => {
|
||||||
|
const q = new URLSearchParams()
|
||||||
|
if (opts.useFormSchema) {
|
||||||
|
q.set('use_form_schema', 'true')
|
||||||
|
if (opts.training_category != null && opts.training_category !== '') {
|
||||||
|
q.set('training_category', String(opts.training_category))
|
||||||
|
}
|
||||||
|
if (opts.training_type_id != null && opts.training_type_id !== '') {
|
||||||
|
q.set('training_type_id', String(opts.training_type_id))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const qs = q.toString()
|
||||||
|
return req(`/activity/${encodeURIComponent(id)}${qs ? `?${qs}` : ''}`)
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* Attributprofil ohne Session (manuelle Erfassung / Vorschau).
|
||||||
|
* @param {{ training_category?: string | null, training_type_id?: number | null }} [params]
|
||||||
|
*/
|
||||||
|
getActivityAttributeSchema: (params = {}) => {
|
||||||
|
const q = new URLSearchParams()
|
||||||
|
if (params.training_category != null && params.training_category !== '') {
|
||||||
|
q.set('training_category', String(params.training_category))
|
||||||
|
}
|
||||||
|
if (params.training_type_id != null && params.training_type_id !== '') {
|
||||||
|
q.set('training_type_id', String(params.training_type_id))
|
||||||
|
}
|
||||||
|
const qs = q.toString()
|
||||||
|
return req(`/activity/attribute-schema${qs ? `?${qs}` : ''}`)
|
||||||
|
},
|
||||||
|
putActivityMetrics: (id, body) =>
|
||||||
|
req(`/activity/${encodeURIComponent(id)}/metrics`, jput(body)),
|
||||||
|
|
||||||
// Sleep Module (v9d Phase 2b)
|
// Sleep Module (v9d Phase 2b)
|
||||||
listSleep: (l=90) => req(`/sleep?limit=${l}`),
|
listSleep: (l=90) => req(`/sleep?limit=${l}`),
|
||||||
getSleepByDate: (date) => req(`/sleep/by-date/${date}`),
|
getSleepByDate: (date) => req(`/sleep/by-date/${date}`),
|
||||||
@@ -402,6 +476,73 @@ export const api = {
|
|||||||
return req('/prompts/execute?' + params, json(body))
|
return req('/prompts/execute?' + params, json(body))
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// NEW: SSE-based execution for long-running workflows
|
||||||
|
executeUnifiedPromptStream: (slug, modules=null, timeframes=null, debug=false, save=false, onProgress=null) => {
|
||||||
|
const params = new URLSearchParams({ prompt_slug: slug })
|
||||||
|
if (debug) params.append('debug', 'true')
|
||||||
|
if (save) params.append('save', 'true')
|
||||||
|
|
||||||
|
// TODO: Security improvement - use session cookie instead of token in URL
|
||||||
|
// For now, send token as query param since EventSource doesn't support custom headers
|
||||||
|
// Using 'ssetoken' to avoid conflicts with endpoint 'token' parameters
|
||||||
|
const token = getToken()
|
||||||
|
if (token) params.append('ssetoken', token)
|
||||||
|
|
||||||
|
if (modules) {
|
||||||
|
Object.entries(modules).forEach(([k, v]) => params.append(`modules[${k}]`, v))
|
||||||
|
}
|
||||||
|
if (timeframes) {
|
||||||
|
Object.entries(timeframes).forEach(([k, v]) => params.append(`timeframes[${k}]`, v))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return a Promise that resolves with final result
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const url = `${BASE}/prompts/execute-stream?${params}`
|
||||||
|
|
||||||
|
const eventSource = new EventSource(url)
|
||||||
|
|
||||||
|
let finalResult = null
|
||||||
|
|
||||||
|
eventSource.onmessage = (event) => {
|
||||||
|
try {
|
||||||
|
const data = JSON.parse(event.data)
|
||||||
|
|
||||||
|
// Call progress callback if provided
|
||||||
|
if (onProgress) {
|
||||||
|
onProgress(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for final result
|
||||||
|
if (data.type === 'execution_complete') {
|
||||||
|
// Transform SSE result to match regular execute format
|
||||||
|
finalResult = {
|
||||||
|
type: 'workflow',
|
||||||
|
execution_id: data.execution_id,
|
||||||
|
status: data.status,
|
||||||
|
aggregated_result: data.aggregated_result,
|
||||||
|
debug: {
|
||||||
|
node_states: [] // TODO: collect from progress events if needed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
eventSource.close()
|
||||||
|
resolve(finalResult)
|
||||||
|
} else if (data.type === 'execution_failed') {
|
||||||
|
eventSource.close()
|
||||||
|
reject(new Error(data.error || 'Workflow execution failed'))
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Error parsing SSE event:', e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
eventSource.onerror = (error) => {
|
||||||
|
console.error('SSE connection error:', error)
|
||||||
|
eventSource.close()
|
||||||
|
reject(new Error('Connection to server lost'))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|
||||||
// Workflow Execution (Part 2: Frontend Execute Integration)
|
// Workflow Execution (Part 2: Frontend Execute Integration)
|
||||||
executeWorkflow: (slug, variables=null, debug=true, save=false) => {
|
executeWorkflow: (slug, variables=null, debug=true, save=false) => {
|
||||||
const params = new URLSearchParams({ prompt_slug: slug })
|
const params = new URLSearchParams({ prompt_slug: slug })
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Volles PostgreSQL-Backup im Custom-Format (pg_restore-kompatibel).
|
||||||
|
# Auf dem Host ausführen, wo der Postgres-Container läuft (z. B. Raspberry Pi mit docker compose).
|
||||||
|
#
|
||||||
|
# BACKUP_DIR=/path/to/safe/storage ./scripts/backup/mitai_pg_dump.sh
|
||||||
|
#
|
||||||
|
# Variablen (optional): POSTGRES_CONTAINER, POSTGRES_DB, POSTGRES_USER, BACKUP_DIR
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
CONTAINER="${POSTGRES_CONTAINER:-mitai-db-prod}"
|
||||||
|
DB="${POSTGRES_DB:-mitai_prod}"
|
||||||
|
USER="${POSTGRES_USER:-mitai_prod}"
|
||||||
|
OUT_DIR="${BACKUP_DIR:-.}"
|
||||||
|
STAMP="$(date +%Y%m%d_%H%M%S)"
|
||||||
|
mkdir -p "${OUT_DIR}"
|
||||||
|
OUT="${OUT_DIR}/${DB}_${STAMP}.dump"
|
||||||
|
|
||||||
|
if ! docker inspect "$CONTAINER" &>/dev/null; then
|
||||||
|
echo "Container nicht gefunden: $CONTAINER" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
docker exec "$CONTAINER" pg_dump -U "$USER" -Fc --no-owner --no-acl "$DB" > "$OUT"
|
||||||
|
ls -la "$OUT"
|
||||||
|
echo "OK: $OUT (zum Zurückspielen: siehe Kommentar unten in diesem Skript)"
|
||||||
|
|
||||||
|
# ── Restore (nur bei Notfall; Backend vorher stoppen, sonst offene Verbindungen) ──
|
||||||
|
# docker compose stop backend
|
||||||
|
# docker cp "$OUT" "$CONTAINER:/tmp/restore.dump"
|
||||||
|
# docker exec "$CONTAINER" pg_restore -U "$USER" -d "$DB" --clean --if-exists --no-owner --no-acl /tmp/restore.dump
|
||||||
|
# docker compose start backend
|
||||||
|
#
|
||||||
|
# Hinweis: --clean entfernt Objekte vor dem Wiederherstellen; kurze Unterbrechung der DB.
|
||||||
|
# Für „nur lesen“ Backup reicht die .dump-Datei auf externem Medium zu kopieren.
|
||||||
@@ -0,0 +1,246 @@
|
|||||||
|
"""
|
||||||
|
Test Condition Parsing - Alle Formate und Verschachtelungen
|
||||||
|
|
||||||
|
Testet ob Pydantic die verschiedenen Condition-Formate korrekt deserialisiert.
|
||||||
|
"""
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
# Force UTF-8 encoding on Windows
|
||||||
|
if sys.platform == 'win32':
|
||||||
|
import io
|
||||||
|
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
|
||||||
|
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8', errors='replace')
|
||||||
|
|
||||||
|
sys.path.insert(0, 'backend')
|
||||||
|
|
||||||
|
from workflow_models import LogicExpression, Condition, WorkflowNode, LogicOperator
|
||||||
|
from pydantic import ValidationError
|
||||||
|
import json
|
||||||
|
|
||||||
|
def test_case(name, data, expected_type, should_fail=False):
|
||||||
|
"""Test a single condition format"""
|
||||||
|
print(f"\n{'='*60}")
|
||||||
|
print(f"TEST: {name}")
|
||||||
|
print(f"{'='*60}")
|
||||||
|
print(f"Input: {json.dumps(data, indent=2)}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Test 1: Direct deserialization
|
||||||
|
if expected_type == LogicExpression:
|
||||||
|
result = LogicExpression(**data)
|
||||||
|
elif expected_type == Condition:
|
||||||
|
result = Condition(**data)
|
||||||
|
|
||||||
|
if should_fail:
|
||||||
|
print("❌ FAILED: Should have raised ValidationError but didn't")
|
||||||
|
return False
|
||||||
|
|
||||||
|
print(f"✅ PASSED: Deserialized as {type(result).__name__}")
|
||||||
|
print(f"Result: {result.model_dump()}")
|
||||||
|
|
||||||
|
# Test 2: As part of WorkflowNode
|
||||||
|
node_data = {
|
||||||
|
"id": "test_node",
|
||||||
|
"type": "logic",
|
||||||
|
"condition": data
|
||||||
|
}
|
||||||
|
node = WorkflowNode(**node_data)
|
||||||
|
print(f"✅ PASSED: WorkflowNode.condition type: {type(node.condition).__name__}")
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
except ValidationError as e:
|
||||||
|
if should_fail:
|
||||||
|
print(f"✅ PASSED: Correctly raised ValidationError")
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
print(f"❌ FAILED: {e}")
|
||||||
|
return False
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ FAILED: Unexpected error: {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
# ============================================================================
|
||||||
|
# Test Cases
|
||||||
|
# ============================================================================
|
||||||
|
|
||||||
|
test_results = []
|
||||||
|
|
||||||
|
# Test 1: Simple comparison (UI format - einfachster Fall)
|
||||||
|
test_results.append(test_case(
|
||||||
|
"Simple comparison (eq)",
|
||||||
|
{
|
||||||
|
"operator": "eq",
|
||||||
|
"ref": "node_1.q1",
|
||||||
|
"value": "ja"
|
||||||
|
},
|
||||||
|
LogicExpression
|
||||||
|
))
|
||||||
|
|
||||||
|
# Test 2: Simple AND (UI format - wie im Workflow)
|
||||||
|
test_results.append(test_case(
|
||||||
|
"Simple AND with 2 operands",
|
||||||
|
{
|
||||||
|
"operator": "and",
|
||||||
|
"operands": [
|
||||||
|
{"operator": "eq", "ref": "node_5.qTiefananalyseRecovery", "value": "ja"},
|
||||||
|
{"operator": "neq", "ref": "node_6.qKonsistenz", "value": "nein"}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
LogicExpression
|
||||||
|
))
|
||||||
|
|
||||||
|
# Test 3: Simple OR
|
||||||
|
test_results.append(test_case(
|
||||||
|
"Simple OR with 3 operands",
|
||||||
|
{
|
||||||
|
"operator": "or",
|
||||||
|
"operands": [
|
||||||
|
{"operator": "eq", "ref": "node_1.q1", "value": "ja"},
|
||||||
|
{"operator": "eq", "ref": "node_1.q2", "value": "nein"},
|
||||||
|
{"operator": "eq", "ref": "node_1.q3", "value": "unklar"}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
LogicExpression
|
||||||
|
))
|
||||||
|
|
||||||
|
# Test 4: Nested AND/OR
|
||||||
|
test_results.append(test_case(
|
||||||
|
"Nested: OR with nested AND",
|
||||||
|
{
|
||||||
|
"operator": "or",
|
||||||
|
"operands": [
|
||||||
|
{
|
||||||
|
"operator": "and",
|
||||||
|
"operands": [
|
||||||
|
{"operator": "eq", "ref": "node_1.q1", "value": "ja"},
|
||||||
|
{"operator": "neq", "ref": "node_1.q2", "value": "nein"}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{"operator": "eq", "ref": "node_2.q1", "value": "hoch"}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
LogicExpression
|
||||||
|
))
|
||||||
|
|
||||||
|
# Test 5: Deep nesting (3 levels)
|
||||||
|
test_results.append(test_case(
|
||||||
|
"Deep nesting (3 levels)",
|
||||||
|
{
|
||||||
|
"operator": "and",
|
||||||
|
"operands": [
|
||||||
|
{
|
||||||
|
"operator": "or",
|
||||||
|
"operands": [
|
||||||
|
{"operator": "eq", "ref": "node_1.q1", "value": "ja"},
|
||||||
|
{
|
||||||
|
"operator": "and",
|
||||||
|
"operands": [
|
||||||
|
{"operator": "eq", "ref": "node_2.q1", "value": "hoch"},
|
||||||
|
{"operator": "neq", "ref": "node_2.q2", "value": "niedrig"}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{"operator": "eq", "ref": "node_3.q1", "value": "aktiv"}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
LogicExpression
|
||||||
|
))
|
||||||
|
|
||||||
|
# Test 6: Different operators
|
||||||
|
test_results.append(test_case(
|
||||||
|
"Different comparison operators (gt, lt, in)",
|
||||||
|
{
|
||||||
|
"operator": "and",
|
||||||
|
"operands": [
|
||||||
|
{"operator": "gt", "ref": "node_1.score", "value": 50},
|
||||||
|
{"operator": "lt", "ref": "node_1.score", "value": 100},
|
||||||
|
{"operator": "in", "ref": "node_1.category", "value": ["high", "medium"]}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
LogicExpression
|
||||||
|
))
|
||||||
|
|
||||||
|
# Test 7: Legacy format (wrapped in Condition)
|
||||||
|
test_results.append(test_case(
|
||||||
|
"Legacy format: Condition with expression",
|
||||||
|
{
|
||||||
|
"type": "if",
|
||||||
|
"expression": {
|
||||||
|
"operator": "eq",
|
||||||
|
"ref": "node_1.q1",
|
||||||
|
"value": "ja"
|
||||||
|
},
|
||||||
|
"then_path": "edge_1",
|
||||||
|
"else_path": "edge_2"
|
||||||
|
},
|
||||||
|
Condition
|
||||||
|
))
|
||||||
|
|
||||||
|
# Test 8: NOT operator
|
||||||
|
test_results.append(test_case(
|
||||||
|
"NOT operator",
|
||||||
|
{
|
||||||
|
"operator": "not",
|
||||||
|
"operands": [
|
||||||
|
{"operator": "eq", "ref": "node_1.q1", "value": "nein"}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
LogicExpression
|
||||||
|
))
|
||||||
|
|
||||||
|
# Test 9: Complex real-world scenario
|
||||||
|
test_results.append(test_case(
|
||||||
|
"Complex real-world: Multiple nested conditions",
|
||||||
|
{
|
||||||
|
"operator": "and",
|
||||||
|
"operands": [
|
||||||
|
{
|
||||||
|
"operator": "or",
|
||||||
|
"operands": [
|
||||||
|
{"operator": "eq", "ref": "node_1.relevance", "value": "high"},
|
||||||
|
{
|
||||||
|
"operator": "and",
|
||||||
|
"operands": [
|
||||||
|
{"operator": "eq", "ref": "node_1.relevance", "value": "medium"},
|
||||||
|
{"operator": "gt", "ref": "node_1.priority", "value": 5}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{"operator": "neq", "ref": "node_2.status", "value": "blocked"},
|
||||||
|
{
|
||||||
|
"operator": "in",
|
||||||
|
"ref": "node_3.category",
|
||||||
|
"value": ["training", "nutrition", "recovery"]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
LogicExpression
|
||||||
|
))
|
||||||
|
|
||||||
|
# ============================================================================
|
||||||
|
# Results Summary
|
||||||
|
# ============================================================================
|
||||||
|
|
||||||
|
print("\n" + "="*60)
|
||||||
|
print("TEST RESULTS SUMMARY")
|
||||||
|
print("="*60)
|
||||||
|
|
||||||
|
passed = sum(test_results)
|
||||||
|
total = len(test_results)
|
||||||
|
failed = total - passed
|
||||||
|
|
||||||
|
print(f"\n✅ Passed: {passed}/{total}")
|
||||||
|
if failed > 0:
|
||||||
|
print(f"❌ Failed: {failed}/{total}")
|
||||||
|
print(f"\n⚠️ CRITICAL: Some tests failed! Do NOT deploy until fixed.")
|
||||||
|
sys.exit(1)
|
||||||
|
else:
|
||||||
|
print(f"\n🎉 All tests passed! Safe to deploy.")
|
||||||
|
sys.exit(0)
|
||||||
Loaded 100 of 101 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user