feat: Refactor activity data handling and improve CSV import logic
Deploy Development / deploy (push) Successful in 53s
Build Test / pytest-backend (push) Successful in 4s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 16s

- Updated `ACTIVITY_PRODUCTION_ARCHITECTURE_AND_PHASES.md` to clarify the derivation of `ACTIVITY_MODULE_REGISTRY_FIELD_KEYS` from `csv_parser.module_registry`.
- Enhanced `activity_data_canon.py` to eliminate hardcoded key lists, ensuring all registry fields are derived dynamically.
- Refactored the `_import_activity` function to remove redundant parameters and streamline the import process.
- Improved the `insert_activity_csv_minimal` function to handle metrics exclusively through `update_activity_columns`, preventing hardcoded values.
- Updated frontend components to manage editable activity log fields more effectively, ensuring proper handling of metrics during CSV imports.
- Added unit tests to validate the new logic and ensure consistency in activity session metrics handling.
This commit is contained in:
Lars committed 2026-04-16 10:35:08 +02:00
1 parent cd29c7d433
commit 5cda485458
9 files changed
+191 -135

No files matched your search

+19 -36
View File
@@ -5,30 +5,31 @@ 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) ───────────────────────
# 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",
}
)
# 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"}
# 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.
@@ -58,21 +59,3 @@ 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",
}
)