feat: Update session metrics handling for CSV-mapped values
All checks were successful
Deploy Development / deploy (push) Successful in 54s
Build Test / pytest-backend (push) Successful in 5s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 16s

- Enhanced the docstring for `upsert_session_metrics_from_csv_mapped` to clarify the handling of schema parameters and EAV logic.
- Modified the condition for skipping updates based on `source_field` to ensure only patchable columns are processed, improving data integrity during session metrics upsert operations.
This commit is contained in:
Lars 2026-04-15 10:28:13 +02:00
parent e4e8c70cd2
commit 9d47c4ef84
2 changed files with 47 additions and 2 deletions

View File

@ -313,7 +313,17 @@ def upsert_session_metrics_from_csv_mapped(
training_category: Optional[str],
training_type_id: Optional[int],
) -> None:
"""EAV nur für Schema-Parameter ohne source_field (reine Session-Metriken)."""
"""
EAV für Schema-Parameter aus CSV-mapped Werten.
Spalten-gestützte Parameter (source_field ACTIVITY_LOG_PATCHABLE_COLUMNS) werden
ausschließlich über resolve_activity_log_column_patch_from_csv activity_log
geschrieben hier kein EAV.
Wenn source_field gesetzt ist, aber **kein** patchbarer Spaltenname (z. B. eigener
Key stola wie der Parametername), wäre früher weder Spalten-Update noch EAV erfolgt;
dann EAV wie bei reinen Metriken.
"""
cur.execute(
"SELECT profile_id FROM activity_log WHERE id = %s",
(activity_log_id,),
@ -330,7 +340,7 @@ def upsert_session_metrics_from_csv_mapped(
if raw is None or raw == "":
continue
src_col = (spec.get("source_field") or "").strip()
if src_col:
if src_col and src_col in ACTIVITY_LOG_PATCHABLE_COLUMNS:
continue
tid = spec["training_parameter_id"]
dt = spec["data_type"]

View File

@ -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 $$;