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
@@ -108,6 +108,7 @@ function SampleTable({ sampleRows, columns }) {
|
||||
export default function UniversalCsvImportPage() {
|
||||
const navigate = useNavigate()
|
||||
const fileInputRef = useRef(null)
|
||||
const analyzeGenRef = useRef(0)
|
||||
const [file, setFile] = useState(null)
|
||||
const [dragActive, setDragActive] = useState(false)
|
||||
const [analyzeResult, setAnalyzeResult] = useState(null)
|
||||
@@ -124,34 +125,19 @@ export default function UniversalCsvImportPage() {
|
||||
)
|
||||
const importAllowed = selectedChoice && EXECUTOR_READY.has(selectedChoice.module)
|
||||
|
||||
const assignCsvFile = (f) => {
|
||||
if (!f) return
|
||||
const name = (f.name || '').toLowerCase()
|
||||
if (!name.endsWith('.csv')) {
|
||||
setError('Bitte eine .csv-Datei wählen.')
|
||||
return
|
||||
}
|
||||
setFile(f)
|
||||
setAnalyzeResult(null)
|
||||
setMappingChoices([])
|
||||
setMappingId('')
|
||||
setSuccess(null)
|
||||
setError(null)
|
||||
}
|
||||
|
||||
const handleAnalyze = async () => {
|
||||
if (!file) {
|
||||
setError('Bitte eine CSV-Datei wählen')
|
||||
return
|
||||
}
|
||||
const runAnalyze = async (fileToAnalyze) => {
|
||||
if (!fileToAnalyze) return
|
||||
const gen = ++analyzeGenRef.current
|
||||
setLoadingAnalyze(true)
|
||||
setError(null)
|
||||
setSuccess(null)
|
||||
setMappingId('')
|
||||
try {
|
||||
const res = await api.analyzeCsv(file)
|
||||
const res = await api.analyzeCsv(fileToAnalyze)
|
||||
if (gen !== analyzeGenRef.current) return
|
||||
setAnalyzeResult(res)
|
||||
const mapData = await api.getCsvMappings()
|
||||
if (gen !== analyzeGenRef.current) return
|
||||
const merged = mergeMappingChoices(res.detected_mappings || [], mapData)
|
||||
setMappingChoices(merged)
|
||||
|
||||
@@ -165,14 +151,33 @@ export default function UniversalCsvImportPage() {
|
||||
}
|
||||
setMappingId(pick)
|
||||
} catch (e) {
|
||||
if (gen !== analyzeGenRef.current) return
|
||||
setError(e.message || 'Analyse fehlgeschlagen')
|
||||
setAnalyzeResult(null)
|
||||
setMappingChoices([])
|
||||
} finally {
|
||||
setLoadingAnalyze(false)
|
||||
if (gen === analyzeGenRef.current) {
|
||||
setLoadingAnalyze(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const assignCsvFile = (f) => {
|
||||
if (!f) return
|
||||
const name = (f.name || '').toLowerCase()
|
||||
if (!name.endsWith('.csv')) {
|
||||
setError('Bitte eine .csv-Datei wählen.')
|
||||
return
|
||||
}
|
||||
setFile(f)
|
||||
setAnalyzeResult(null)
|
||||
setMappingChoices([])
|
||||
setMappingId('')
|
||||
setSuccess(null)
|
||||
setError(null)
|
||||
void runAnalyze(f)
|
||||
}
|
||||
|
||||
const handleImport = async () => {
|
||||
if (!file || !mappingId) {
|
||||
setError('Bitte Datei und Vorlage wählen')
|
||||
@@ -223,10 +228,10 @@ export default function UniversalCsvImportPage() {
|
||||
CSV-Import
|
||||
</h1>
|
||||
<p style={{ fontSize: 14, color: 'var(--text2)', marginBottom: 20, lineHeight: 1.55 }}>
|
||||
CSV hier ablegen oder die Fläche antippen: Die App vergleicht die Spalten mit gespeicherten Vorlagen
|
||||
(Ernährung, Gewicht, Blutdruck, Aktivität, Schlaf, Vitalwerte) und schlägt passende Ziele vor. Du
|
||||
bestätigst die Vorlage — ohne zuerst ein Modell raten zu müssen. Schlaf-Import erwartet das
|
||||
Apple-Health-Schlaf-CSV (Segment- oder Zusammenfassungs-Export).{' '}
|
||||
CSV hier ablegen oder die Fläche antippen — die Analyse startet sofort. Die App vergleicht die Spalten
|
||||
mit gespeicherten Vorlagen (Ernährung, Gewicht, Blutdruck, Aktivität, Schlaf, Vitalwerte) und schlägt
|
||||
passende Ziele vor. Du bestätigst die Vorlage — ohne zuerst ein Modell raten zu müssen. Schlaf-Import
|
||||
erwartet das Apple-Health-Schlaf-CSV (Segment- oder Zusammenfassungs-Export).{' '}
|
||||
<strong>Ausblick:</strong> Eine Datei → mehrere Zieltabellen.
|
||||
</p>
|
||||
|
||||
@@ -310,33 +315,60 @@ export default function UniversalCsvImportPage() {
|
||||
Datei ablegen oder tippen zum Auswählen
|
||||
</span>
|
||||
<span style={{ fontSize: 13, color: 'var(--text3)' }}>
|
||||
{file ? file.name : 'Noch keine Datei gewählt'}
|
||||
{loadingAnalyze ? (
|
||||
<span style={{ display: 'inline-flex', alignItems: 'center', gap: 8 }}>
|
||||
<Loader2 size={16} style={{ animation: 'spin 0.7s linear infinite' }} />
|
||||
Datei wird analysiert …
|
||||
</span>
|
||||
) : file ? (
|
||||
file.name
|
||||
) : (
|
||||
'Noch keine Datei gewählt'
|
||||
)}
|
||||
</span>
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-primary"
|
||||
style={{ marginTop: 12, width: '100%' }}
|
||||
disabled={!file || loadingAnalyze}
|
||||
onClick={handleAnalyze}
|
||||
>
|
||||
{loadingAnalyze ? (
|
||||
<>
|
||||
<Loader2 size={18} style={{ marginRight: 8, animation: 'spin 0.7s linear infinite' }} /> Datei
|
||||
wird erkannt …
|
||||
</>
|
||||
) : (
|
||||
'Datei analysieren'
|
||||
)}
|
||||
</button>
|
||||
{file && !loadingAnalyze && (
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-secondary"
|
||||
style={{ marginTop: 12, width: '100%' }}
|
||||
onClick={() => void runAnalyze(file)}
|
||||
>
|
||||
Dieselbe Datei erneut analysieren
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{analyzeResult && (
|
||||
<div className="card" style={{ marginBottom: 16, padding: 16 }}>
|
||||
<div className="form-label">2. Erkennung & Vorschau</div>
|
||||
{(analyzeResult.warnings || []).length > 0 && (
|
||||
<div
|
||||
style={{
|
||||
marginTop: 12,
|
||||
padding: 12,
|
||||
borderRadius: 12,
|
||||
background: 'rgba(216, 90, 48, 0.12)',
|
||||
border: '1px solid var(--danger)',
|
||||
color: 'var(--danger-dark, var(--danger))',
|
||||
fontSize: 14,
|
||||
lineHeight: 1.5,
|
||||
}}
|
||||
>
|
||||
{(analyzeResult.warnings || []).map((w, i) => (
|
||||
<div key={i}>{w}</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
<div style={{ fontSize: 13, color: 'var(--text2)', marginTop: 8 }}>
|
||||
Trennzeichen: <strong>{analyzeResult.delimiter}</strong> · Spalten:{' '}
|
||||
{analyzeResult.columns?.length ?? 0}
|
||||
{analyzeResult.format_detection?.apple_sleep ? (
|
||||
<>
|
||||
{' '}
|
||||
· <strong>Format:</strong> Apple-Schlaf (Schlafanalyse oder Segment-Export)
|
||||
</>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
{analyzeResult.recommended && (
|
||||
|
||||
Reference in new issue
Block a user