feat: Enhance activity metrics handling and documentation
- Updated the README to include new activity production architecture and phases, improving clarity on the development roadmap. - Enhanced the `ACTIVITY_SESSION_METRICS_EAV_AGENT_GUIDE` with details on the target architecture and phase plan for production readiness. - Introduced a new function `merge_column_backed_and_eav_metrics` to streamline the merging of metrics from column-backed and EAV sources, ensuring data integrity and reducing duplication. - Refactored session metrics handling to eliminate deprecated synchronization methods, improving the overall efficiency of data processing. - Added unit tests for the new merging logic, ensuring robust validation of metrics handling.
This commit is contained in:
1 parent
58ddde6b1e
commit
ca8cee990b
9 files changed
+619
-82
No files matched your search
@@ -34,6 +34,8 @@ 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": {
|
||||
"table": "activity_log",
|
||||
"fields": {
|
||||
@@ -63,16 +65,7 @@ MODULE_DEFINITIONS: Dict[str, Dict[str, Any]] = {
|
||||
"max": 220,
|
||||
"label_de": "Herzfrequenz max (bpm)",
|
||||
},
|
||||
"hr_min": {"type": "int", "required": False, "label_de": "Herzfrequenz min (bpm)"},
|
||||
"rpe": {"type": "int", "required": False, "label_de": "RPE (1–10)"},
|
||||
"pace_min_per_km": {"type": "float", "required": False, "label_de": "Tempo (min/km)"},
|
||||
"cadence": {"type": "int", "required": False, "label_de": "Kadenz"},
|
||||
"avg_power": {"type": "int", "required": False, "label_de": "Leistung Ø (W)"},
|
||||
"elevation_gain": {"type": "int", "required": False, "label_de": "Höhenmeter / Aufstieg"},
|
||||
"temperature_celsius": {"type": "float", "required": False, "label_de": "Temperatur (°C)"},
|
||||
"humidity_percent": {"type": "int", "required": False, "label_de": "Luftfeuchtigkeit (%)"},
|
||||
"avg_hr_percent": {"type": "float", "required": False, "label_de": "HF Ø (% von max)"},
|
||||
"kcal_per_km": {"type": "float", "required": False, "label_de": "Kalorien pro km"},
|
||||
"notes": {"type": "string", "required": False, "label_de": "Notiz"},
|
||||
},
|
||||
"derive_date_from_datetime_field": "start_time",
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
"""
|
||||
Kanonische Aufteilung activity_log vs. EAV für Aktivitätssessions.
|
||||
|
||||
Single Source für: welche Felder das CSV-/Registry-Modul „activity“ direkt in activity_log schreibt,
|
||||
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
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Dict, Final
|
||||
|
||||
# ── activity_log: Modul „activity“ (Universal-CSV-Kern) ───────────────────────
|
||||
# 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.
|
||||
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-Lesefallback (Merge), wenn EAV für den Parameter fehlt.
|
||||
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",
|
||||
}
|
||||
|
||||
# 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_session_metrics import sync_column_backed_session_metrics
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -248,7 +247,7 @@ def insert_activity_csv_minimal(
|
||||
|
||||
|
||||
def run_activity_post_write_hooks(cur, profile_id: str, eid: str) -> None:
|
||||
"""Auto-Eval (falls aktiv) + EAV-Spiegel aus activity_log-Spalten."""
|
||||
"""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(
|
||||
"""
|
||||
@@ -269,7 +268,6 @@ def run_activity_post_write_hooks(cur, profile_id: str, eid: str) -> None:
|
||||
_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)
|
||||
sync_column_backed_session_metrics(cur, str(profile_id), str(eid))
|
||||
|
||||
|
||||
def run_activity_post_write_hooks_import(
|
||||
@@ -286,7 +284,7 @@ def run_activity_post_write_hooks_import(
|
||||
kcal_active: Any,
|
||||
kcal_resting: Any,
|
||||
) -> None:
|
||||
"""Eval + EAV nach Legacy-Import mit vorgebautem Kontext-Dict."""
|
||||
"""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 = {
|
||||
@@ -308,7 +306,6 @@ def run_activity_post_write_hooks_import(
|
||||
_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)
|
||||
sync_column_backed_session_metrics(cur, str(profile_id), str(eid))
|
||||
|
||||
|
||||
def merge_activity_csv_module_fields(
|
||||
|
||||
@@ -10,36 +10,10 @@ from decimal import Decimal
|
||||
from typing import Any, Dict, List, Mapping, Optional, Sequence
|
||||
|
||||
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__)
|
||||
|
||||
# activity_log-Spalten, die per training_parameters.source_field aus CSV (Parameter-Key) befüllt werden dürfen.
|
||||
# Muss mit sync_column_backed_session_metrics übereinstimmen (inkl. Kernmetriken wie hr_avg).
|
||||
ACTIVITY_LOG_PATCHABLE_COLUMNS = frozenset(
|
||||
{
|
||||
"start_time",
|
||||
"end_time",
|
||||
"activity_type",
|
||||
"duration_min",
|
||||
"kcal_active",
|
||||
"kcal_resting",
|
||||
"hr_avg",
|
||||
"hr_max",
|
||||
"hr_min",
|
||||
"distance_km",
|
||||
"rpe",
|
||||
"pace_min_per_km",
|
||||
"cadence",
|
||||
"avg_power",
|
||||
"elevation_gain",
|
||||
"temperature_celsius",
|
||||
"humidity_percent",
|
||||
"avg_hr_percent",
|
||||
"kcal_per_km",
|
||||
"notes",
|
||||
}
|
||||
)
|
||||
|
||||
# Diese Spalten nicht aus CSV-Parameter-Zuordnung überschreiben (kommen aus Typ-Mapping / System).
|
||||
ACTIVITY_LOG_PATCH_FORBIDDEN = frozenset(
|
||||
{
|
||||
@@ -328,13 +302,87 @@ def upsert_session_metrics_from_csv_mapped(
|
||||
)
|
||||
|
||||
|
||||
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: Pro Schema-Parameter mit source_field gilt activity_log als kanonisch, wenn
|
||||
die Spalte befüllt und koerzierbar ist; sonst Fallback EAV. Reine EAV-Parameter (ohne Spalte oder
|
||||
leere Spalte) kommen aus EAV. Verhindert doppelte Semantik ohne Schreib-Sync.
|
||||
"""
|
||||
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,
|
||||
}
|
||||
)
|
||||
used_column = True
|
||||
keys_handled.add(k)
|
||||
except (TypeError, ValueError):
|
||||
pass
|
||||
|
||||
if used_column:
|
||||
continue
|
||||
if k in eav_by_key:
|
||||
merged.append(dict(eav_by_key[k]))
|
||||
keys_handled.add(k)
|
||||
continue
|
||||
|
||||
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,
|
||||
}
|
||||
)
|
||||
keys_handled.add(k)
|
||||
except (TypeError, ValueError):
|
||||
pass
|
||||
|
||||
for m in eav_metrics:
|
||||
if m["key"] in keys_handled:
|
||||
continue
|
||||
merged.append(dict(m))
|
||||
|
||||
merged.sort(key=lambda x: x["key"])
|
||||
return merged
|
||||
|
||||
|
||||
def sync_column_backed_session_metrics(cur, profile_id: str, activity_log_id: str) -> None:
|
||||
"""
|
||||
EAV-Zeilen für alle Schema-Parameter mit gesetztem source_field aus der activity_log-Zeile
|
||||
schreiben (Upsert) bzw. bei NULL in der Quellspalte löschen. Reine Layer-1-Logik; keine Router-Abhängigkeit.
|
||||
[Veraltet / nicht mehr in Schreibpfaden aufgerufen]
|
||||
|
||||
Synchron mit Übergangsphase: activity_log bleibt kanonisch für klassische Spalten; EAV spiegelt dieselben
|
||||
Werte für Profil/Platzhalter/Detail-API, ohne replace_activity_session_metrics aufzurufen.
|
||||
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()
|
||||
@@ -527,36 +575,7 @@ def get_activity_session_logical_unit(cur, profile_id: str, activity_log_id: str
|
||||
cur, header.get("training_category"), header.get("training_type_id")
|
||||
)
|
||||
metrics = fetch_activity_session_metrics(cur, activity_log_id)
|
||||
by_key = {m["key"]: m for m in metrics}
|
||||
merged_metrics: List[Dict[str, Any]] = list(metrics)
|
||||
for s in schema:
|
||||
k = s["key"]
|
||||
if k in by_key:
|
||||
continue
|
||||
sf = s.get("source_field")
|
||||
if not sf or (isinstance(sf, str) and not str(sf).strip()):
|
||||
continue
|
||||
col = str(sf).strip()
|
||||
if col not in header:
|
||||
continue
|
||||
raw = header.get(col)
|
||||
if raw is None:
|
||||
continue
|
||||
dt = s["data_type"]
|
||||
try:
|
||||
val = _coerce_raw_value_for_parameter(dt, raw)
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
merged_metrics.append(
|
||||
{
|
||||
"training_parameter_id": s["training_parameter_id"],
|
||||
"key": k,
|
||||
"data_type": dt,
|
||||
"unit": s.get("unit"),
|
||||
"value": val,
|
||||
}
|
||||
)
|
||||
merged_metrics.sort(key=lambda x: x["key"])
|
||||
merged_metrics = merge_column_backed_and_eav_metrics(header, schema, metrics)
|
||||
return {
|
||||
"header": header,
|
||||
"schema": schema,
|
||||
@@ -565,17 +584,33 @@ def get_activity_session_logical_unit(cur, profile_id: str, activity_log_id: str
|
||||
|
||||
|
||||
def enrich_sessions_with_metrics(cur, sessions: List[Dict[str, Any]]) -> None:
|
||||
"""Mutates each session dict: adds key 'session_metrics' (list) when sessions non-empty."""
|
||||
"""
|
||||
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,
|
||||
@@ -603,8 +638,33 @@ def enrich_sessions_with_metrics(cur, sessions: List[Dict[str, Any]]) -> None:
|
||||
else:
|
||||
val = r["value_bool"]
|
||||
by_act.setdefault(aid, []).append(
|
||||
{"key": r["key"], "data_type": dt, "unit": r["unit"], "value": val}
|
||||
{
|
||||
"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"))
|
||||
s["session_metrics"] = by_act.get(aid, [])
|
||||
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"]}
|
||||
for m in merged
|
||||
]
|
||||
@@ -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 $$;
|
||||
@@ -1,12 +1,14 @@
|
||||
"""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,
|
||||
_row_value_tuple,
|
||||
@@ -171,22 +173,39 @@ def test_resolve_loads_category_from_training_type_id():
|
||||
assert cur.executes[0][1] == (42,)
|
||||
|
||||
|
||||
def test_enrich_sessions_batch():
|
||||
@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,
|
||||
@@ -202,3 +221,87 @@ def test_enrich_sessions_batch():
|
||||
assert sessions[0]["session_metrics"][0]["value"] == 7
|
||||
assert sessions[0]["session_metrics"][0]["key"] == "rpe"
|
||||
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_keeps_eav_only_keys():
|
||||
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 len(out) == 1
|
||||
assert out[0]["key"] == "custom_param"
|
||||
|
||||
|
||||
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
|
||||
Reference in new issue
Block a user