feat(csv-import): Add CSV import diagnosis endpoint and related functionality
- 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:
1 parent
1855f6e57a
commit
c5b0540b11
6 files changed
+394
-18
No files matched your search
@@ -524,6 +524,36 @@ def _v_safe_float(value: Any) -> float | None:
|
||||
return None
|
||||
|
||||
|
||||
def diagnose_vitals_row(mapped_typed: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Erklärt Vital-Baseline-Zeile nach Typkonvertierung (ohne DB)."""
|
||||
d = coerce_date(mapped_typed.get("date"))
|
||||
rhr = _v_safe_int(mapped_typed.get("resting_hr"))
|
||||
hrv = _v_safe_int(mapped_typed.get("hrv"))
|
||||
vo2 = _v_safe_float(mapped_typed.get("vo2_max"))
|
||||
spo2 = _v_safe_int(mapped_typed.get("spo2"))
|
||||
resp = _v_safe_float(mapped_typed.get("respiratory_rate"))
|
||||
has_metric = any(x is not None for x in (rhr, hrv, vo2, spo2, resp))
|
||||
date_raw = mapped_typed.get("date")
|
||||
return {
|
||||
"date_coerced_iso": d.isoformat() if d else None,
|
||||
"date_after_convert_repr": repr(date_raw),
|
||||
"date_after_convert_type": type(date_raw).__name__,
|
||||
"metrics": {
|
||||
"resting_hr": rhr,
|
||||
"hrv": hrv,
|
||||
"vo2_max": vo2,
|
||||
"spo2": spo2,
|
||||
"respiratory_rate": resp,
|
||||
},
|
||||
"would_pass_prefilter": d is not None and has_metric,
|
||||
"prefilter_fail_reason": (
|
||||
"datum_fehlt"
|
||||
if d is None
|
||||
else ("keine_baseline_metrik" if not has_metric else None)
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
def _import_vitals_baseline(
|
||||
cur,
|
||||
profile_id: str,
|
||||
|
||||
@@ -402,6 +402,29 @@ def _lookup_db_field(csv_col: str, field_mappings: Mapping[str, str]) -> str | N
|
||||
return None
|
||||
|
||||
|
||||
def _vitals_baseline_alias_db_field(csv_col: str) -> str | None:
|
||||
"""
|
||||
Apple Health: deutsch „Vitalwerte.csv“ (Breitexport) vs. schmale Vorlage
|
||||
(Start / Resting Heart Rate …). Ohne Alias wählt die Analyse oft die
|
||||
englische Vorlage → jede Zeile „Datum fehlt“.
|
||||
Abgleich über normalisierten Header (normalize_header_for_signature).
|
||||
"""
|
||||
n = normalize_header_for_signature(str(csv_col))
|
||||
if n in ("datum_uhrzeit", "start", "date_time", "datetime"):
|
||||
return "date"
|
||||
if "ruhepuls" in n or n.startswith("resting_heart_rate"):
|
||||
return "resting_hr"
|
||||
if "herzfrequenzvariabilit" in n or "heart_rate_variability" in n:
|
||||
return "hrv"
|
||||
if "vo2" in n and "max" in n:
|
||||
return "vo2_max"
|
||||
if "blutsauerstoff" in n or "oxygen_saturation" in n:
|
||||
return "spo2"
|
||||
if "atemfrequenz" in n or "respiratory_rate" in n:
|
||||
return "respiratory_rate"
|
||||
return None
|
||||
|
||||
|
||||
def build_row_after_mapping(
|
||||
csv_row: Mapping[str, str],
|
||||
field_mappings: Mapping[str, str],
|
||||
@@ -416,6 +439,8 @@ def build_row_after_mapping(
|
||||
tc = type_conversions or {}
|
||||
for csv_col, raw in csv_row.items():
|
||||
db_field = _lookup_db_field(str(csv_col), field_mappings)
|
||||
if not db_field and module == "vitals_baseline":
|
||||
db_field = _vitals_baseline_alias_db_field(csv_col)
|
||||
if not db_field:
|
||||
continue
|
||||
spec = tc.get(db_field)
|
||||
@@ -426,3 +451,79 @@ def build_row_after_mapping(
|
||||
except Exception:
|
||||
out[db_field] = None
|
||||
return out
|
||||
|
||||
|
||||
def diagnose_row_mapping(
|
||||
csv_row: Mapping[str, str],
|
||||
field_mappings: Mapping[str, str],
|
||||
type_conversions: Mapping[str, Any] | None,
|
||||
module: str | None = None,
|
||||
*,
|
||||
mapped_typed: Mapping[str, Any] | None = None,
|
||||
max_columns: int = 96,
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
Nur für Diagnose-Endpunkt: Quelle (Vorlage vs. Vital-Alias), Konvertierung pro Spalte,
|
||||
Ergebnis wie build_row_after_mapping (json-freundliche Vorschau).
|
||||
"""
|
||||
tc = type_conversions or {}
|
||||
per_column: list[dict[str, Any]] = []
|
||||
n = 0
|
||||
for csv_col, raw in csv_row.items():
|
||||
if n >= max_columns:
|
||||
break
|
||||
n += 1
|
||||
sc = str(csv_col)
|
||||
via_t = _lookup_db_field(sc, field_mappings)
|
||||
via_a = None
|
||||
if not via_t and module == "vitals_baseline":
|
||||
via_a = _vitals_baseline_alias_db_field(sc)
|
||||
target = via_t or via_a
|
||||
src = "template" if via_t else ("alias" if via_a else "none")
|
||||
spec = tc.get(target) if target else None
|
||||
conv_err: str | None = None
|
||||
conv_preview: Any = None
|
||||
if target:
|
||||
try:
|
||||
conv_val = convert_value(
|
||||
(raw or "").strip(),
|
||||
target,
|
||||
spec if isinstance(spec, dict) else None,
|
||||
module=module,
|
||||
)
|
||||
conv_preview = conv_val.isoformat() if hasattr(conv_val, "isoformat") else conv_val
|
||||
except Exception as e:
|
||||
conv_err = str(e)
|
||||
per_column.append(
|
||||
{
|
||||
"csv_column": sc,
|
||||
"raw_preview": ((raw or "")[:120]),
|
||||
"db_field": target,
|
||||
"source": src,
|
||||
"convert_error": conv_err,
|
||||
"converted_preview": conv_preview,
|
||||
}
|
||||
)
|
||||
|
||||
src_map = (
|
||||
build_row_after_mapping(csv_row, field_mappings, type_conversions, module=module)
|
||||
if mapped_typed is None
|
||||
else mapped_typed
|
||||
)
|
||||
mapped_preview: dict[str, Any] = {}
|
||||
for k, v in src_map.items():
|
||||
mapped_preview[k] = v.isoformat() if hasattr(v, "isoformat") else v
|
||||
|
||||
tmpl_keys = [
|
||||
str(k)
|
||||
for k, v in field_mappings.items()
|
||||
if v not in (None, "-", "_skip")
|
||||
]
|
||||
|
||||
return {
|
||||
"per_column": per_column,
|
||||
"columns_truncated": len(csv_row) > max_columns,
|
||||
"template_mapped_keys": tmpl_keys[:40],
|
||||
"template_mapped_keys_truncated": len(tmpl_keys) > 40,
|
||||
"mapped": mapped_preview,
|
||||
}
|
||||
Reference in new issue
Block a user