revert: Wiederherstellung Codezustand von ca8cee9 (ohne Branch-Historie zu überschreiben)
Reverts cd29c7d..026c51b per git revert. Alle zwischenliegenden Commits bleiben in Gitea sichtbar; der Arbeitsbaum entspricht wieder dem Stand von ca8cee9.
Made-with: Cursor
This commit is contained in:
1 parent
026c51b6b5
commit
06f83e2ffc
10 files changed
+117
-503
No files matched your search
@@ -5,31 +5,30 @@ Single Source für: welche Felder das CSV-/Registry-Modul „activity“ direkt
|
||||
und welche training_parameters primär über EAV laufen (mit optionalem Lesefallback auf Legacy-Spalten).
|
||||
|
||||
Normative Doku: .claude/docs/technical/ACTIVITY_PRODUCTION_ARCHITECTURE_AND_PHASES.md
|
||||
|
||||
Phase A: Keine zweite hartcodierte Key-Liste — Registry-Felder kommen ausschließlich aus
|
||||
``csv_parser.module_registry.MODULE_DEFINITIONS["activity"].fields``.
|
||||
"""
|
||||
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`` (= Spine-Spalten-Namen in activity_log)."""
|
||||
mod = get_module_definition("activity")
|
||||
if not mod:
|
||||
return frozenset()
|
||||
return frozenset((mod.get("fields") or {}).keys())
|
||||
|
||||
|
||||
# ── activity_log: Modul „activity“ (Universal-CSV-Kern) ───────────────────────
|
||||
# Ableitung aus module_registry — bei neuen Registry-Feldern hier kein manuelles Update nötig.
|
||||
ACTIVITY_MODULE_REGISTRY_FIELD_KEYS: Final[frozenset[str]] = get_activity_module_registry_field_keys()
|
||||
|
||||
# Teil-UPDATEs (Import): alle Registry-Kernfelder außer ``date`` (Identität/Duplikat-Key).
|
||||
ACTIVITY_LOG_PATCHABLE_COLUMNS: Final[frozenset[str]] = ACTIVITY_MODULE_REGISTRY_FIELD_KEYS - {"date"}
|
||||
# Nur diese Keys erscheinen in csv_parser.module_registry MODULE_DEFINITIONS["activity"].fields.
|
||||
# Alles Weitere: training_parameters + EAV (Import über upsert_session_metrics_from_csv_mapped).
|
||||
ACTIVITY_MODULE_REGISTRY_FIELD_KEYS: Final[frozenset[str]] = frozenset(
|
||||
{
|
||||
"date",
|
||||
"start_time",
|
||||
"end_time",
|
||||
"activity_type",
|
||||
"duration_min",
|
||||
"kcal_active",
|
||||
"kcal_resting",
|
||||
"distance_km",
|
||||
"hr_avg",
|
||||
"hr_max",
|
||||
"rpe",
|
||||
"notes",
|
||||
}
|
||||
)
|
||||
|
||||
# Parameter-Keys (training_parameters.key), die primär in EAV geführt werden; source_field nach Migration 057 NULL.
|
||||
# Lesefallback: activity_log-Spalte unter ACTIVITY_LOG_LEGACY_COLUMN_FOR_EAV_PRIMARY_PARAM, falls EAV leer.
|
||||
@@ -59,3 +58,21 @@ ACTIVITY_LOG_LEGACY_COLUMN_FOR_EAV_PRIMARY_PARAM: Final[Dict[str, str]] = {
|
||||
"avg_hr_percent": "avg_hr_percent",
|
||||
"kcal_per_km": "kcal_per_km",
|
||||
}
|
||||
|
||||
# Spalten, die mit training_parameters.source_field (nach Migration 057) noch activity_log abbilden.
|
||||
# Erweiterte Metriken sind EAV-primär — nicht hier auflisten.
|
||||
ACTIVITY_LOG_PATCHABLE_COLUMNS: Final[frozenset[str]] = frozenset(
|
||||
{
|
||||
"start_time",
|
||||
"end_time",
|
||||
"activity_type",
|
||||
"duration_min",
|
||||
"kcal_active",
|
||||
"kcal_resting",
|
||||
"hr_avg",
|
||||
"hr_max",
|
||||
"distance_km",
|
||||
"rpe",
|
||||
"notes",
|
||||
}
|
||||
)
|
||||
@@ -15,7 +15,6 @@ 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__)
|
||||
|
||||
@@ -51,8 +50,10 @@ _ACTIVITY_CSV_REGISTRY_EXCLUDE = frozenset({"date", "start_time", "end_time", "a
|
||||
|
||||
|
||||
def activity_registry_field_keys() -> frozenset[str]:
|
||||
"""Gleiche Menge wie ``ACTIVITY_MODULE_REGISTRY_FIELD_KEYS`` (Phase A: eine Quelle)."""
|
||||
return get_activity_module_registry_field_keys()
|
||||
mod = get_module_definition("activity")
|
||||
if not mod:
|
||||
return frozenset()
|
||||
return frozenset((mod.get("fields") or {}).keys())
|
||||
|
||||
|
||||
def activity_csv_registry_updates_from_mapped(mapped: Mapping[str, Any]) -> Dict[str, Any]:
|
||||
@@ -82,20 +83,11 @@ def activity_csv_registry_updates_from_mapped(mapped: Mapping[str, Any]) -> Dict
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
def _within_num_bounds(v: float | int, spec: dict, *, as_float: bool) -> bool:
|
||||
mn = spec.get("min")
|
||||
mx = spec.get("max")
|
||||
if mn is not None:
|
||||
if as_float and v < float(mn):
|
||||
return False
|
||||
if not as_float and v < int(mn):
|
||||
return False
|
||||
if mx is not None:
|
||||
if as_float and v > float(mx):
|
||||
return False
|
||||
if not as_float and v > int(mx):
|
||||
return False
|
||||
return True
|
||||
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:
|
||||
@@ -109,15 +101,11 @@ def activity_csv_registry_updates_from_mapped(mapped: Mapping[str, Any]) -> Dict
|
||||
continue
|
||||
typ = spec.get("type", "string")
|
||||
if typ == "float":
|
||||
v = _sf(raw)
|
||||
if v is not None and not _within_num_bounds(v, spec, as_float=True):
|
||||
v = None
|
||||
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 and not _within_num_bounds(v, spec, as_float=False):
|
||||
v = None
|
||||
if v is not None:
|
||||
out[key] = v
|
||||
elif typ == "datetime":
|
||||
@@ -227,7 +215,7 @@ def insert_activity_csv_minimal(
|
||||
training_subcategory: Any,
|
||||
source: str,
|
||||
) -> None:
|
||||
"""INSERT activity_log-Zeile (Universal-CSV): Kernspalten im INSERT; optional zusätzliches PATCH."""
|
||||
"""INSERT minimale activity_log-Zeile (Universal-CSV)."""
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO activity_log (
|
||||
@@ -296,7 +284,7 @@ def run_activity_post_write_hooks_import(
|
||||
kcal_active: Any,
|
||||
kcal_resting: Any,
|
||||
) -> None:
|
||||
"""Auto-Eval nach Import (gleiche Transaktion wie Schreibpfad — keine Abhängigkeit vom DB-Read-Timing)."""
|
||||
"""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 = {
|
||||
|
||||
@@ -9,10 +9,8 @@ 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,
|
||||
)
|
||||
from csv_parser.module_registry import get_module_definition
|
||||
from data_layer.activity_data_canon import ACTIVITY_LOG_LEGACY_COLUMN_FOR_EAV_PRIMARY_PARAM
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -31,16 +29,6 @@ ACTIVITY_LOG_PATCH_FORBIDDEN = frozenset(
|
||||
)
|
||||
|
||||
|
||||
def _parameter_value_stored_in_eav_only(spec: Mapping[str, Any], parameter_key: str) -> bool:
|
||||
"""False = kanonisch activity_log (Modul-Registry oder training_parameters.source_field)."""
|
||||
if parameter_key in ACTIVITY_MODULE_REGISTRY_FIELD_KEYS:
|
||||
return False
|
||||
sf = spec.get("source_field")
|
||||
if sf is not None and str(sf).strip():
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
class ActivitySessionMetricsError(Exception):
|
||||
"""Raised by Layer 1; routers map to HTTP (404/400)."""
|
||||
|
||||
@@ -174,74 +162,6 @@ def resolve_activity_attribute_schema(
|
||||
return merge_parameter_schema_rows(category_rows, type_rows)
|
||||
|
||||
|
||||
def resolve_activity_attribute_schema_for_csv_import(
|
||||
cur,
|
||||
training_category: Optional[str],
|
||||
training_type_id: Optional[int],
|
||||
mapped: Mapping[str, Any],
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
Wie resolve_activity_attribute_schema, plus alle aktiven training_parameters, deren key in
|
||||
``mapped`` vorkommt (nicht Modul-Registry), aber nicht in Kategorie/Typ-Profil — z. B. wenn
|
||||
``activity_type_mappings`` fehlt oder der Parameter nur für andere Typen gebucht ist.
|
||||
|
||||
Damit schreibt der Universal-CSV-Import EAV für gültig gemappte Zielfelder wie bei cd29c7d,
|
||||
sobald der Parameter in ``training_parameters`` existiert.
|
||||
"""
|
||||
base = resolve_activity_attribute_schema(cur, training_category, training_type_id)
|
||||
by_key: Dict[str, Dict[str, Any]] = {s["key"]: s for s in base}
|
||||
|
||||
for k, raw in mapped.items():
|
||||
if raw is None or raw == "":
|
||||
continue
|
||||
if k in by_key:
|
||||
continue
|
||||
if k in ACTIVITY_MODULE_REGISTRY_FIELD_KEYS:
|
||||
continue
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
id,
|
||||
key,
|
||||
name_de,
|
||||
name_en,
|
||||
category AS param_category,
|
||||
data_type,
|
||||
unit,
|
||||
validation_rules,
|
||||
source_field
|
||||
FROM training_parameters
|
||||
WHERE key = %s AND is_active = true
|
||||
LIMIT 1
|
||||
""",
|
||||
(k,),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
if not row:
|
||||
continue
|
||||
pid = int(row["id"])
|
||||
if any(s["training_parameter_id"] == pid for s in by_key.values()):
|
||||
continue
|
||||
by_key[k] = {
|
||||
"training_parameter_id": pid,
|
||||
"key": row["key"],
|
||||
"name_de": row["name_de"],
|
||||
"name_en": row["name_en"],
|
||||
"param_category": row["param_category"],
|
||||
"data_type": row["data_type"],
|
||||
"unit": row["unit"],
|
||||
"validation_rules": row["validation_rules"] or {},
|
||||
"source_field": row["source_field"],
|
||||
"sort_order": 100_000,
|
||||
"required": False,
|
||||
"ui_group": None,
|
||||
}
|
||||
|
||||
out = list(by_key.values())
|
||||
out.sort(key=lambda x: (x.get("sort_order", 0), x["key"]))
|
||||
return out
|
||||
|
||||
|
||||
def _validation_rules_dict(raw: Any) -> Dict[str, Any]:
|
||||
if isinstance(raw, dict):
|
||||
return raw
|
||||
@@ -321,45 +241,6 @@ def _coerce_raw_value_for_parameter(data_type: str, raw: Any) -> Any:
|
||||
raise ValueError(data_type)
|
||||
|
||||
|
||||
def apply_activity_mapped_column_aliases_from_schema(
|
||||
mapped: Mapping[str, Any],
|
||||
schema: Sequence[Dict[str, Any]],
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
training_parameters.key weicht oft von activity_log-Spalte ab (z. B. avg_hr → hr_avg).
|
||||
Kopiert Werte auf die Spalte, wenn die Spalte leer ist, damit CSV/Registry activity_log befüllt.
|
||||
"""
|
||||
m = dict(mapped)
|
||||
for s in schema:
|
||||
sf = s.get("source_field")
|
||||
if not sf or not str(sf).strip():
|
||||
continue
|
||||
col = str(sf).strip()
|
||||
pkey = s["key"]
|
||||
if pkey == col:
|
||||
continue
|
||||
col_v = m.get(col)
|
||||
if col_v is not None and col_v != "":
|
||||
continue
|
||||
pk_v = m.get(pkey)
|
||||
if pk_v is None or pk_v == "":
|
||||
continue
|
||||
m[col] = pk_v
|
||||
return m
|
||||
|
||||
|
||||
def apply_activity_mapped_column_aliases(
|
||||
cur,
|
||||
mapped: Mapping[str, Any],
|
||||
training_category: Optional[str],
|
||||
training_type_id: Optional[int],
|
||||
) -> Dict[str, Any]:
|
||||
schema = resolve_activity_attribute_schema_for_csv_import(
|
||||
cur, training_category, training_type_id, mapped
|
||||
)
|
||||
return apply_activity_mapped_column_aliases_from_schema(mapped, schema)
|
||||
|
||||
|
||||
def upsert_session_metrics_from_csv_mapped(
|
||||
cur,
|
||||
profile_id: str,
|
||||
@@ -369,10 +250,10 @@ def upsert_session_metrics_from_csv_mapped(
|
||||
training_type_id: Optional[int],
|
||||
) -> None:
|
||||
"""
|
||||
EAV für Trainingsparameter aus CSV (nur Keys ohne activity_log-Spalte / ohne source_field).
|
||||
EAV für Trainingsparameter aus CSV (nur Keys, die nicht im activity-Modul-Registry liegen).
|
||||
|
||||
Parameter mit gesetztem source_field sind kanonisch in activity_log — kein EAV-Schreiben (vermeidet
|
||||
Doppelung zu avg_hr vs. hr_avg o. Ä.). Keys im activity-CSV-Modul werden ebenfalls übersprungen.
|
||||
Kernfelder (Datum, Start, Distanz, HF, …) schreibt der Executor nach activity_log;
|
||||
hier keine doppelten EAV-Zeilen für dieselben Registry-Keys.
|
||||
"""
|
||||
cur.execute(
|
||||
"SELECT profile_id FROM activity_log WHERE id = %s",
|
||||
@@ -381,9 +262,9 @@ def upsert_session_metrics_from_csv_mapped(
|
||||
row = cur.fetchone()
|
||||
if not row or str(row["profile_id"]) != str(profile_id):
|
||||
return
|
||||
schema = resolve_activity_attribute_schema_for_csv_import(
|
||||
cur, training_category, training_type_id, mapped
|
||||
)
|
||||
mod = get_module_definition("activity") or {}
|
||||
activity_registry_keys = frozenset((mod.get("fields") or {}).keys())
|
||||
schema = resolve_activity_attribute_schema(cur, training_category, training_type_id)
|
||||
for spec in schema:
|
||||
pkey = spec["key"]
|
||||
if pkey not in mapped:
|
||||
@@ -391,7 +272,7 @@ def upsert_session_metrics_from_csv_mapped(
|
||||
raw = mapped[pkey]
|
||||
if raw is None or raw == "":
|
||||
continue
|
||||
if not _parameter_value_stored_in_eav_only(spec, pkey):
|
||||
if pkey in activity_registry_keys:
|
||||
continue
|
||||
tid = spec["training_parameter_id"]
|
||||
dt = spec["data_type"]
|
||||
@@ -647,8 +528,6 @@ def replace_activity_session_metrics(
|
||||
if not s["required"]:
|
||||
continue
|
||||
itk = s["key"]
|
||||
if not _parameter_value_stored_in_eav_only(s, itk):
|
||||
continue
|
||||
hit = payload_by_key.get(itk)
|
||||
if hit is None or hit.get("value") is None:
|
||||
raise ActivitySessionMetricsError(400, f"Pflichtfeld fehlt: {itk}")
|
||||
@@ -661,11 +540,9 @@ def replace_activity_session_metrics(
|
||||
for item in metrics:
|
||||
k = str(item["parameter_key"]).strip()
|
||||
spec = by_key[k]
|
||||
if not _parameter_value_stored_in_eav_only(spec, k):
|
||||
continue
|
||||
val = item.get("value")
|
||||
if val is None:
|
||||
if spec["required"] and _parameter_value_stored_in_eav_only(spec, k):
|
||||
if spec["required"]:
|
||||
raise ActivitySessionMetricsError(400, f"Pflichtfeld fehlt: {k}")
|
||||
continue
|
||||
rules = _validation_rules_dict(spec["validation_rules"])
|
||||
|
||||
Reference in new issue
Block a user