fix: BLS-Datenimport als Hintergrundjob gegen HTTP 504
Prüfung und Apply laufen serverseitig, die UI pollt nur noch den Status. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
8e964509d7
commit
a07a668de2
6 files changed
+281
-35
No files matched your search
@@ -1,32 +1,44 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { api } from '../utils/api'
|
||||
|
||||
function summarizeCheck(kind, res) {
|
||||
if (!res) return ''
|
||||
if (kind === 'components') return `${res.attributes ?? 0} Stoffe erkannt`
|
||||
return `${res.foods ?? 0} Lebensmittel, ${res.attribute_columns ?? 0} Stoffspalten`
|
||||
function summarizeCheck(kind, check) {
|
||||
if (!check) return ''
|
||||
if (kind === 'components') return `${check.attributes ?? 0} Stoffe erkannt`
|
||||
return `${check.foods ?? 0} Lebensmittel, ${check.attribute_columns ?? 0} Stoffspalten`
|
||||
}
|
||||
|
||||
function sleep(ms) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms))
|
||||
}
|
||||
|
||||
async function waitForJob(jobId, doneStatuses, onTick) {
|
||||
for (;;) {
|
||||
const job = await api.adminBlsImportJob(jobId)
|
||||
onTick?.(job)
|
||||
if (job.status === 'error') {
|
||||
throw new Error(job.error || 'Import fehlgeschlagen')
|
||||
}
|
||||
if (doneStatuses.includes(job.status)) return job
|
||||
await sleep(1200)
|
||||
}
|
||||
}
|
||||
|
||||
function FileImportBlock({ label, kind, onImported }) {
|
||||
const inputRef = useRef(null)
|
||||
const [file, setFile] = useState(null)
|
||||
const [check, setCheck] = useState(null)
|
||||
const [job, setJob] = useState(null)
|
||||
const [error, setError] = useState(null)
|
||||
const [busy, setBusy] = useState(null)
|
||||
const [imported, setImported] = useState(false)
|
||||
const [applyResult, setApplyResult] = useState(null)
|
||||
|
||||
const apiFn = kind === 'components' ? api.adminBlsImportComponents : api.adminBlsImportFoods
|
||||
|
||||
const checkFile = async (nextFile) => {
|
||||
setError(null)
|
||||
setCheck(null)
|
||||
setApplyResult(null)
|
||||
setImported(false)
|
||||
setJob(null)
|
||||
if (!nextFile) return
|
||||
setBusy('check')
|
||||
try {
|
||||
setCheck(await apiFn(nextFile, true))
|
||||
const started = await api.adminBlsImportStart(kind, nextFile)
|
||||
const done = await waitForJob(started.id, ['checked'], setJob)
|
||||
setJob(done)
|
||||
} catch (e) {
|
||||
setError(e.message)
|
||||
} finally {
|
||||
@@ -35,22 +47,24 @@ function FileImportBlock({ label, kind, onImported }) {
|
||||
}
|
||||
|
||||
const applyImport = async () => {
|
||||
if (!file || !check) return
|
||||
if (!job?.id || job.status !== 'checked') return
|
||||
setBusy('apply')
|
||||
setError(null)
|
||||
try {
|
||||
const res = await apiFn(file, false)
|
||||
setApplyResult(res)
|
||||
setImported(true)
|
||||
await api.adminBlsImportApply(job.id)
|
||||
const done = await waitForJob(job.id, ['done'], setJob)
|
||||
setJob(done)
|
||||
onImported?.()
|
||||
} catch (e) {
|
||||
setError(e.message)
|
||||
setImported(false)
|
||||
} finally {
|
||||
setBusy(null)
|
||||
}
|
||||
}
|
||||
|
||||
const progress = job?.progress
|
||||
const result = job?.result
|
||||
|
||||
return (
|
||||
<div className="settings-page__field">
|
||||
<label className="settings-page__field-label">{label}</label>
|
||||
@@ -74,26 +88,31 @@ function FileImportBlock({ label, kind, onImported }) {
|
||||
>
|
||||
{file ? file.name : 'Datei auswählen'}
|
||||
</button>
|
||||
{busy && <p style={{ fontSize: 13, color: 'var(--text2)', margin: 0 }}>{busy === 'apply' ? 'Importiere… das kann ein paar Minuten dauern.' : 'Prüfe…'}</p>}
|
||||
{error && <p style={{ color: 'var(--danger)', margin: 0 }}>{error}</p>}
|
||||
{check && !error && (
|
||||
{busy === 'check' && <p style={{ fontSize: 13, color: 'var(--text2)', margin: 0 }}>Prüfe…</p>}
|
||||
{busy === 'apply' && (
|
||||
<p style={{ fontSize: 13, color: 'var(--text2)', margin: 0 }}>
|
||||
Prüfung: {summarizeCheck(kind, check)}
|
||||
Importiere…{progress?.total ? ` ${progress.current}/${progress.total}` : ''}
|
||||
</p>
|
||||
)}
|
||||
{applyResult && (
|
||||
{error && <p style={{ color: 'var(--danger)', margin: 0 }}>{error}</p>}
|
||||
{job?.status === 'checked' && !error && (
|
||||
<p style={{ fontSize: 13, color: 'var(--text2)', margin: 0 }}>
|
||||
{(applyResult.inserted ?? applyResult.foods_inserted) ?? 0} neu · {(applyResult.updated ?? applyResult.foods_updated) ?? 0} aktualisiert
|
||||
{applyResult.values_written != null ? ` · ${applyResult.values_written} Werte` : ''}
|
||||
Prüfung: {summarizeCheck(kind, job.check)}
|
||||
</p>
|
||||
)}
|
||||
{job?.status === 'done' && result && (
|
||||
<p style={{ fontSize: 13, color: 'var(--text2)', margin: 0 }}>
|
||||
{(result.inserted ?? result.foods_inserted) ?? 0} neu · {(result.updated ?? result.foods_updated) ?? 0} aktualisiert
|
||||
{result.values_written != null ? ` · ${result.values_written} Werte` : ''}
|
||||
</p>
|
||||
)}
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-primary btn-full"
|
||||
disabled={!check || !!error || !!busy || imported}
|
||||
disabled={job?.status !== 'checked' || !!error || !!busy}
|
||||
onClick={applyImport}
|
||||
>
|
||||
{imported ? 'Importiert' : 'Importieren'}
|
||||
{job?.status === 'done' ? 'Importiert' : 'Importieren'}
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
|
||||
@@ -251,16 +251,13 @@ export const api = {
|
||||
upsertMyFoodMapping: (d) => req('/bls/mappings', json(d)),
|
||||
deleteMyFoodMapping: (id) => req(`/bls/mappings/${id}`, {method:'DELETE'}),
|
||||
adminBlsStatus: () => req('/admin/bls/status'),
|
||||
adminBlsImportComponents: async (file, dryRun=true) => {
|
||||
adminBlsImportStart: async (kind, file) => {
|
||||
const fd=new FormData();fd.append('file',file)
|
||||
const r=await fetch(`${BASE}/admin/bls/import/components?dry_run=${dryRun}`,{method:'POST',body:fd,headers:hdrs()})
|
||||
return readJsonResponse(r)
|
||||
},
|
||||
adminBlsImportFoods: async (file, dryRun=true) => {
|
||||
const fd=new FormData();fd.append('file',file)
|
||||
const r=await fetch(`${BASE}/admin/bls/import/foods?dry_run=${dryRun}`,{method:'POST',body:fd,headers:hdrs()})
|
||||
const r=await fetch(`${BASE}/admin/bls/import/jobs?kind=${encodeURIComponent(kind)}`,{method:'POST',body:fd,headers:hdrs()})
|
||||
return readJsonResponse(r)
|
||||
},
|
||||
adminBlsImportJob: (id) => req(`/admin/bls/import/jobs/${id}`),
|
||||
adminBlsImportApply: (id) => req(`/admin/bls/import/jobs/${id}/apply`, {method:'POST'}),
|
||||
adminBlsFoods: (q, kind) => req(`/admin/bls/foods?${q?('q='+encodeURIComponent(q)+'&'):''}${kind?('kind='+kind):''}`),
|
||||
adminBlsFoodDetail: (id) => req(`/admin/bls/foods/${id}`),
|
||||
adminCreateManualFood: (d) => req('/admin/bls/foods/manual', json(d)),
|
||||
|
||||
Reference in new issue
Block a user