feat(csv-import): Add CSV import diagnosis endpoint and related functionality
Deploy Development / deploy (push) Successful in 50s
Build Test / pytest-backend (push) Successful in 4s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 17s

- Implemented a new endpoint for diagnosing CSV imports without writing to the database, allowing users to validate mappings and type conversions.
- Introduced the `diagnose_vitals_row` function to analyze vital metrics and provide detailed feedback on data validity.
- Enhanced the CSV import logic to include alias handling for vital fields, improving compatibility with different CSV formats.
- Updated the frontend to support the new diagnosis feature, including UI elements for displaying diagnosis results and error details.
- Added tests to ensure the correctness of the new diagnosis functionality and its integration with existing import processes.
This commit is contained in:
Lars committed 2026-04-10 16:35:31 +02:00
1 parent 1855f6e57a
commit c5b0540b11
6 files changed
+394 -18

No files matched your search

+64 -1
View File
@@ -11,8 +11,9 @@ import uuid
import pytest
from csv_parser.executor import run_universal_csv_import
from csv_parser.executor import diagnose_vitals_row, run_universal_csv_import
from csv_parser.sleep_apple_import import detect_apple_sleep_csv_format
from csv_parser.type_converter import build_row_after_mapping, diagnose_row_mapping
class _SeqCursor:
@@ -130,6 +131,44 @@ def test_run_universal_import_vitals_baseline_upsert_insert_path():
assert any("INSERT INTO vitals_baseline" in q[0] for q in cur.executes)
def test_run_universal_import_wide_german_vitals_with_english_template_slots():
"""Breiter Apple-DE-Export, aber nur englische Vorlagen-Spalten → über Aliase erkennbar."""
text = (
"Datum/Uhrzeit,Aktive Energie (kJ),Ruhepuls (count/min),Atemfrequenz (count/min)"
",Blutsauerstoffsättigung (%),Herzfrequenzvariabilität (ms),VO2 max (ml/(kg·min))\n"
"2026-04-03 00:00:00,,53,15.61,95.22,37.26,\n"
)
mapping = {
"delimiter": ",",
"has_header": True,
"field_mappings": {
"Start": "date",
"Resting Heart Rate": "resting_hr",
"Heart Rate Variability": "hrv",
"VO2 Max": "vo2_max",
"Oxygen Saturation": "spo2",
"Respiratory Rate": "respiratory_rate",
},
"type_conversions": {
"date": {
"type": "datetime",
"format": "yyyy-mm-dd HH:MM:SS",
"extract": "date_only",
"flexible": True,
},
"resting_hr": {"type": "int", "flexible": True},
"hrv": {"type": "int", "flexible": True},
"vo2_max": {"type": "float", "decimal_separator": "auto", "flexible": True},
"spo2": {"type": "int", "flexible": True},
"respiratory_rate": {"type": "float", "decimal_separator": "auto", "flexible": True},
},
}
cur = _SeqCursor([{"inserted": True, "id": "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}])
out = run_universal_csv_import(cur, PID, "vitals_baseline", text, "wide.csv", mapping)
assert out["rows_errors"] == 0
assert out["rows_imported"] == 1
def test_run_universal_import_vitals_baseline_two_rows_same_day_averages():
text = (
"Start,Resting Heart Rate,Heart Rate Variability,VO2 Max\n"
@@ -276,3 +315,27 @@ def test_run_universal_import_weight_two_rows_same_day_last_value():
assert len(insert_sqls) == 1
params = insert_sqls[0][1]
assert params[3] == 83.5
def test_diagnose_vitals_row_and_mapping_smoke():
fm = {
"Datum/Uhrzeit": "date",
"Ruhepuls (count/min)": "resting_hr",
}
tc = {
"date": {
"type": "datetime",
"format": "yyyy-mm-dd HH:MM:SS",
"extract": "date_only",
"flexible": True,
},
"resting_hr": {"type": "int", "flexible": True},
}
row = {"Datum/Uhrzeit": "2026-04-03 00:00:00", "Ruhepuls (count/min)": "53"}
typed = build_row_after_mapping(row, fm, tc, module="vitals_baseline")
d = diagnose_vitals_row(typed)
assert d["date_coerced_iso"] == "2026-04-03"
assert d["would_pass_prefilter"] is True
dm = diagnose_row_mapping(row, fm, tc, module="vitals_baseline", mapped_typed=typed)
assert str(dm["mapped"]["date"]).startswith("2026-04-03")
assert any(c["csv_column"] == "Datum/Uhrzeit" and c["source"] == "template" for c in dm["per_column"])