feat(csv-import): Enhance Apple sleep CSV import functionality
- Integrated date parsing improvements using dateutil for better handling of various date formats in sleep data. - Added total sleep hours to the nights dictionary for comprehensive sleep analysis. - Updated the import logic to handle cases where sleep duration is zero, providing appropriate warnings. - Enhanced the CSV import interface to detect Apple sleep CSV format and provide user feedback on template selection. - Improved the admin CSV template editor to accommodate new sleep import requirements and clarify usage instructions.
This commit is contained in:
1 parent
26ab11eb7b
commit
41cc0ed2a8
6 files changed
+223
-54
No files matched your search
@@ -25,6 +25,7 @@ from csv_parser.core import (
|
||||
parse_csv_sample,
|
||||
)
|
||||
from csv_parser.module_registry import get_module_definition, list_modules, validate_field_mappings
|
||||
from csv_parser.sleep_apple_import import detect_apple_sleep_csv_format
|
||||
|
||||
router = APIRouter(prefix="/api/csv", tags=["csv-import"])
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -59,7 +60,14 @@ def csv_modules(session: dict = Depends(require_auth)):
|
||||
for mid in list_modules():
|
||||
d = get_module_definition(mid)
|
||||
if d:
|
||||
out.append({"id": mid, "table": d["table"], "fields": d["fields"]})
|
||||
out.append(
|
||||
{
|
||||
"id": mid,
|
||||
"table": d["table"],
|
||||
"fields": d["fields"],
|
||||
"import_mode": d.get("import_mode"),
|
||||
}
|
||||
)
|
||||
return {"modules": out}
|
||||
|
||||
|
||||
@@ -216,6 +224,13 @@ async def analyze_csv(
|
||||
headers, sample_rows, used_delim = parse_csv_sample(text, delimiter=delim, max_data_rows=5)
|
||||
sig = column_signature(headers)
|
||||
|
||||
apple_sleep_csv = False
|
||||
try:
|
||||
detect_apple_sleep_csv_format(headers)
|
||||
apple_sleep_csv = True
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
mod_def = get_module_definition(module) if module else None
|
||||
available_fields = mod_def["fields"] if mod_def else None
|
||||
|
||||
@@ -248,13 +263,16 @@ async def analyze_csv(
|
||||
for t in templates:
|
||||
t_sig = list(t["column_signature"]) if t["column_signature"] else []
|
||||
metrics = headers_signature_rank_metrics(sig, t_sig)
|
||||
conf = float(metrics["confidence"] or 0)
|
||||
if apple_sleep_csv and t.get("module") == "sleep":
|
||||
conf = max(conf, 1.0)
|
||||
ranked.append(
|
||||
{
|
||||
"mapping_id": t["id"],
|
||||
"module": t["module"],
|
||||
"mapping_name": t["mapping_name"],
|
||||
"is_system": bool(t.get("is_system")),
|
||||
"confidence": metrics["confidence"],
|
||||
"confidence": round(conf, 4),
|
||||
"template_recall": metrics["template_recall"],
|
||||
"jaccard": metrics["jaccard"],
|
||||
"columns_matched": metrics["columns_matched"],
|
||||
@@ -274,6 +292,24 @@ async def analyze_csv(
|
||||
top = ranked[:25]
|
||||
recommended = top[0] if top and (top[0]["confidence"] or 0) > 0 else None
|
||||
|
||||
warnings: list[str] = []
|
||||
if apple_sleep_csv and not any(t.get("module") == "sleep" for t in templates):
|
||||
warnings.append(
|
||||
"Diese Datei ist ein Apple-Schlafexport, aber es fehlt eine Vorlage für das Modul «Schlaf» "
|
||||
"(z. B. Migration 044 / Admin → CSV-Vorlagen)."
|
||||
)
|
||||
if (
|
||||
apple_sleep_csv
|
||||
and recommended
|
||||
and recommended.get("module") != "sleep"
|
||||
and any(t.get("module") == "sleep" for t in templates)
|
||||
):
|
||||
warnings.append(
|
||||
"Apple-Schlaf-CSV erkannt — bitte eine Vorlage unter Modul «Schlaf» wählen, nicht «"
|
||||
+ str(recommended.get("module") or "")
|
||||
+ "».",
|
||||
)
|
||||
|
||||
return {
|
||||
"module_filter": module,
|
||||
"filename": file.filename,
|
||||
@@ -285,6 +321,8 @@ async def analyze_csv(
|
||||
"detected_mappings": top,
|
||||
"recommended": recommended,
|
||||
"available_fields": available_fields,
|
||||
"format_detection": {"apple_sleep": apple_sleep_csv},
|
||||
"warnings": warnings,
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user