feat(csv-import): Add blood pressure and activity row diagnosis functionality
- Introduced `diagnose_blood_pressure_row` and `diagnose_activity_row` functions to validate and analyze blood pressure and activity data from CSV imports. - Updated the CSV import logic to handle combined datetime columns for blood pressure and activity, improving data integrity during import. - Enhanced type conversion specifications to include `start_time` for blood pressure and activity, ensuring accurate data mapping. - Added tests to validate the new diagnosis functions and their integration with existing import processes, ensuring robustness and reliability. - Updated frontend messages to provide clearer guidance on blood pressure and activity data handling during CSV imports.
This commit is contained in:
1 parent
c5b0540b11
commit
5b96bd4f75
9 files changed
+295
-9
No files matched your search
@@ -397,6 +397,87 @@ def _import_weight(
|
||||
}
|
||||
|
||||
|
||||
def diagnose_blood_pressure_row(mapped_typed: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Zeigt, ob Datum/Zeit nach Vorlage + Alias + Apple-Start-Spalte erkannt werden."""
|
||||
md = coerce_date(mapped_typed.get("measured_date"))
|
||||
mt = mapped_typed.get("measured_time")
|
||||
st_combined = mapped_typed.get("start_time")
|
||||
if isinstance(st_combined, dt.datetime):
|
||||
if md is None:
|
||||
md = st_combined.date()
|
||||
if mt is None:
|
||||
mt = st_combined.time()
|
||||
elif isinstance(st_combined, str) and st_combined.strip() and (md is None or mt is None):
|
||||
try:
|
||||
from dateutil import parser as du_parser
|
||||
|
||||
dtp = du_parser.parse(st_combined.strip())
|
||||
if md is None:
|
||||
md = dtp.date()
|
||||
if mt is None:
|
||||
mt = dtp.time()
|
||||
except (ValueError, TypeError, OverflowError):
|
||||
pass
|
||||
sys_v = mapped_typed.get("systolic")
|
||||
dia_v = mapped_typed.get("diastolic")
|
||||
try:
|
||||
int(sys_v)
|
||||
int(dia_v)
|
||||
ok_bp = True
|
||||
except (TypeError, ValueError):
|
||||
ok_bp = False
|
||||
return {
|
||||
"measured_date_iso": md.isoformat() if md else None,
|
||||
"has_measured_time": mt is not None,
|
||||
"start_time_raw_type": type(st_combined).__name__ if st_combined is not None else None,
|
||||
"systolic_ok": ok_bp,
|
||||
"would_reach_insert_check": md is not None and mt is not None,
|
||||
}
|
||||
|
||||
|
||||
def diagnose_activity_row(mapped_typed: dict[str, Any]) -> dict[str, Any]:
|
||||
activity_type = mapped_typed.get("activity_type")
|
||||
start_raw = mapped_typed.get("start_time")
|
||||
date_d = coerce_date(mapped_typed.get("date"))
|
||||
start_key: str | None = None
|
||||
fail_hint: str | None = None
|
||||
if isinstance(start_raw, dt.datetime):
|
||||
start_key = start_raw.strftime("%Y-%m-%d %H:%M:%S")
|
||||
if date_d is None:
|
||||
date_d = start_raw.date()
|
||||
elif isinstance(start_raw, dt.time):
|
||||
if date_d is None:
|
||||
fail_hint = "startzeit_ohne_datum"
|
||||
else:
|
||||
start_key = f"{date_d.isoformat()} {start_raw.strftime('%H:%M:%S')}"
|
||||
elif isinstance(start_raw, str) and start_raw.strip():
|
||||
s = start_raw.strip()
|
||||
if date_d is not None and _looks_like_time_only(s):
|
||||
start_key = f"{date_d.isoformat()} {s}"
|
||||
else:
|
||||
start_key = s
|
||||
if date_d is None and len(start_key) >= 10:
|
||||
for fmt in ("%Y-%m-%d", "%d.%m.%Y"):
|
||||
try:
|
||||
date_d = dt.datetime.strptime(start_key[:10], fmt).date()
|
||||
break
|
||||
except ValueError:
|
||||
continue
|
||||
has_type = bool(activity_type and str(activity_type).strip())
|
||||
ok = has_type and date_d is not None and bool(start_key)
|
||||
if fail_hint is None and not has_type:
|
||||
fail_hint = "trainingsart_fehlt"
|
||||
elif fail_hint is None and not ok:
|
||||
fail_hint = "datum_start_fehlt"
|
||||
return {
|
||||
"activity_type_preview": (str(activity_type).strip()[:80] if activity_type else None),
|
||||
"date_iso": date_d.isoformat() if date_d else None,
|
||||
"start_key_preview": (start_key[:80] if start_key else None),
|
||||
"would_pass_row_gate": ok,
|
||||
"fail_hint": fail_hint,
|
||||
}
|
||||
|
||||
|
||||
def _import_blood_pressure(
|
||||
cur,
|
||||
profile_id: str,
|
||||
@@ -417,6 +498,23 @@ def _import_blood_pressure(
|
||||
mapped = build_row_after_mapping(csv_row, fm, tc, module="blood_pressure")
|
||||
md = coerce_date(mapped.get("measured_date"))
|
||||
mt = mapped.get("measured_time")
|
||||
st_combined = mapped.get("start_time")
|
||||
if isinstance(st_combined, dt.datetime):
|
||||
if md is None:
|
||||
md = st_combined.date()
|
||||
if mt is None:
|
||||
mt = st_combined.time()
|
||||
elif isinstance(st_combined, str) and st_combined.strip() and (md is None or mt is None):
|
||||
try:
|
||||
from dateutil import parser as du_parser
|
||||
|
||||
dtp = du_parser.parse(st_combined.strip())
|
||||
if md is None:
|
||||
md = dtp.date()
|
||||
if mt is None:
|
||||
mt = dtp.time()
|
||||
except (ValueError, TypeError, OverflowError):
|
||||
pass
|
||||
if md is None:
|
||||
error_details.append({"row": rows_total, "error": "Datum fehlt"})
|
||||
continue
|
||||
|
||||
Reference in new issue
Block a user