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
@@ -116,8 +116,11 @@ export default function UniversalCsvImportPage() {
|
||||
const [mappingId, setMappingId] = useState('')
|
||||
const [loadingAnalyze, setLoadingAnalyze] = useState(false)
|
||||
const [loadingImport, setLoadingImport] = useState(false)
|
||||
const [loadingDiagnose, setLoadingDiagnose] = useState(false)
|
||||
const [error, setError] = useState(null)
|
||||
const [success, setSuccess] = useState(null)
|
||||
const [lastImport, setLastImport] = useState(null)
|
||||
const [diagnoseResult, setDiagnoseResult] = useState(null)
|
||||
|
||||
const selectedChoice = useMemo(
|
||||
() => mappingChoices.find((c) => String(c.id) === String(mappingId)),
|
||||
@@ -175,6 +178,8 @@ export default function UniversalCsvImportPage() {
|
||||
setMappingId('')
|
||||
setSuccess(null)
|
||||
setError(null)
|
||||
setLastImport(null)
|
||||
setDiagnoseResult(null)
|
||||
void runAnalyze(f)
|
||||
}
|
||||
|
||||
@@ -192,6 +197,7 @@ export default function UniversalCsvImportPage() {
|
||||
setSuccess(null)
|
||||
try {
|
||||
const res = await api.importUniversalCsv(file, Number(mappingId))
|
||||
setLastImport(res)
|
||||
const st = res.stats || {}
|
||||
const modLabel = MODULE_LABEL[res.module] || res.module || ''
|
||||
setSuccess(
|
||||
@@ -206,6 +212,24 @@ export default function UniversalCsvImportPage() {
|
||||
}
|
||||
}
|
||||
|
||||
const runDiagnose = async () => {
|
||||
if (!file || !mappingId) {
|
||||
setError('Bitte Datei und Vorlage wählen')
|
||||
return
|
||||
}
|
||||
setLoadingDiagnose(true)
|
||||
setError(null)
|
||||
setDiagnoseResult(null)
|
||||
try {
|
||||
const res = await api.diagnoseUniversalCsv(file, Number(mappingId))
|
||||
setDiagnoseResult(res)
|
||||
} catch (e) {
|
||||
setError(e.message || 'Diagnose fehlgeschlagen')
|
||||
} finally {
|
||||
setLoadingDiagnose(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="capture-page" style={{ paddingBottom: 88 }}>
|
||||
<button
|
||||
@@ -263,6 +287,32 @@ export default function UniversalCsvImportPage() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{lastImport?.error_details?.length > 0 && (
|
||||
<details
|
||||
className="card"
|
||||
style={{ marginBottom: 16, padding: 16, cursor: 'pointer' }}
|
||||
open
|
||||
>
|
||||
<summary style={{ fontWeight: 600, color: 'var(--text1)' }}>
|
||||
Zeilenfehler vom letzten Import ({lastImport.error_details.length}) — zum Kopieren aufklappen
|
||||
</summary>
|
||||
<pre
|
||||
style={{
|
||||
marginTop: 12,
|
||||
fontSize: 12,
|
||||
overflow: 'auto',
|
||||
maxHeight: 320,
|
||||
background: 'var(--surface2)',
|
||||
padding: 12,
|
||||
borderRadius: 8,
|
||||
color: 'var(--text1)',
|
||||
}}
|
||||
>
|
||||
{JSON.stringify(lastImport.error_details, null, 2)}
|
||||
</pre>
|
||||
</details>
|
||||
)}
|
||||
|
||||
<div className="card" style={{ marginBottom: 16, padding: 16 }}>
|
||||
<div className="form-label">1. CSV-Datei</div>
|
||||
<input
|
||||
@@ -461,22 +511,71 @@ export default function UniversalCsvImportPage() {
|
||||
</p>
|
||||
)}
|
||||
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-primary"
|
||||
style={{ marginTop: 16, width: '100%' }}
|
||||
disabled={!file || !mappingId || !importAllowed || loadingImport}
|
||||
onClick={handleImport}
|
||||
>
|
||||
{loadingImport ? (
|
||||
<>
|
||||
<Loader2 size={18} style={{ marginRight: 8, animation: 'spin 0.7s linear infinite' }} /> Import
|
||||
läuft …
|
||||
</>
|
||||
) : (
|
||||
'Import starten'
|
||||
)}
|
||||
</button>
|
||||
<div style={{ display: 'flex', flexDirection: 'column', gap: 12, marginTop: 16 }}>
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-secondary"
|
||||
style={{ width: '100%' }}
|
||||
disabled={!file || !mappingId || !importAllowed || loadingDiagnose}
|
||||
onClick={() => void runDiagnose()}
|
||||
>
|
||||
{loadingDiagnose ? (
|
||||
<>
|
||||
<Loader2
|
||||
size={18}
|
||||
style={{ marginRight: 8, animation: 'spin 0.7s linear infinite' }}
|
||||
/>{' '}
|
||||
Diagnose …
|
||||
</>
|
||||
) : (
|
||||
'Mapping prüfen (ohne Import)'
|
||||
)}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-primary"
|
||||
style={{ width: '100%' }}
|
||||
disabled={!file || !mappingId || !importAllowed || loadingImport}
|
||||
onClick={handleImport}
|
||||
>
|
||||
{loadingImport ? (
|
||||
<>
|
||||
<Loader2 size={18} style={{ marginRight: 8, animation: 'spin 0.7s linear infinite' }} /> Import
|
||||
läuft …
|
||||
</>
|
||||
) : (
|
||||
'Import starten'
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{diagnoseResult && (
|
||||
<details style={{ marginTop: 20 }} open>
|
||||
<summary style={{ cursor: 'pointer', fontWeight: 600, color: 'var(--text2)' }}>
|
||||
Diagnose-Ergebnis ({diagnoseResult.rows_diagnosed ?? 0} Zeilen)
|
||||
</summary>
|
||||
<p style={{ fontSize: 13, color: 'var(--text3)', marginTop: 8, lineHeight: 1.5 }}>
|
||||
Vorlage #{diagnoseResult.mapping_id} · {diagnoseResult.mapping_name} · Modul{' '}
|
||||
{MODULE_LABEL[diagnoseResult.module] || diagnoseResult.module}. Bei Vitalwerten: pro Zeile{' '}
|
||||
<code>vitals.would_pass_prefilter</code> und{' '}
|
||||
<code>prefilter_fail_reason</code> prüfen (z. B. <code>datum_fehlt</code>).
|
||||
</p>
|
||||
<pre
|
||||
style={{
|
||||
marginTop: 8,
|
||||
fontSize: 11,
|
||||
overflow: 'auto',
|
||||
maxHeight: 480,
|
||||
background: 'var(--surface2)',
|
||||
padding: 12,
|
||||
borderRadius: 8,
|
||||
color: 'var(--text1)',
|
||||
}}
|
||||
>
|
||||
{JSON.stringify(diagnoseResult, null, 2)}
|
||||
</pre>
|
||||
</details>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -490,6 +490,23 @@ export const api = {
|
||||
copyCsvMapping: (mappingId, body = null) =>
|
||||
req(`/csv/mappings/${mappingId}/copy`, body ? json(body) : { method: 'POST' }),
|
||||
/** Universal-CSV (Issue #21): Zielmodul steckt in der Vorlage; nur file + mapping_id */
|
||||
/** Import-Diagnose: keine Datenbank-Schreibung, erste Zeilen + Mapping-Auflösung */
|
||||
diagnoseUniversalCsv: async (file, mappingId, module = null) => {
|
||||
const fd = new FormData()
|
||||
fd.append('file', file)
|
||||
fd.append('mapping_id', String(mappingId))
|
||||
if (module) fd.append('module', module)
|
||||
const res = await fetch(BASE + '/csv/import-diagnose', { method: 'POST', headers: hdrs(), body: fd })
|
||||
if (!res.ok) {
|
||||
const errText = await res.text()
|
||||
let parsed = null
|
||||
try {
|
||||
parsed = JSON.parse(errText)
|
||||
} catch { /* ignore */ }
|
||||
throw new Error(formatFastApiDetail(parsed?.detail, errText.trim() || `HTTP ${res.status}`))
|
||||
}
|
||||
return res.json()
|
||||
},
|
||||
importUniversalCsv: async (file, mappingId) => {
|
||||
const fd = new FormData()
|
||||
fd.append('file', file)
|
||||
|
||||
Reference in new issue
Block a user