From 9a749e63375953b0440e86d8f7b5785bd5cd140f Mon Sep 17 00:00:00 2001 From: Paul Woelfel Date: Mon, 10 Jun 2024 22:11:04 +0200 Subject: [PATCH] Improved parsing of unwetter data --- src/components/Map/layers/UnwetterAction.ts | 105 ++++++++++---------- src/components/Map/layers/UnwetterLayer.tsx | 14 +-- 2 files changed, 63 insertions(+), 56 deletions(-) diff --git a/src/components/Map/layers/UnwetterAction.ts b/src/components/Map/layers/UnwetterAction.ts index f2be0d3..19cf21b 100644 --- a/src/components/Map/layers/UnwetterAction.ts +++ b/src/components/Map/layers/UnwetterAction.ts @@ -45,63 +45,68 @@ function parseTime(time: string | number) { const fetchUWD = async (sheetId: string, range: string) => { console.info(`fetching unwetter data of ${sheetId} ${range}`); - const values = await getSpreadsheetData( + const [headers, ...values] = await getSpreadsheetData( sheetId || process.env.EINSATZMAPPE_SHEET_ID || '', range || process.env.EINSATZMAPPE_SHEET_RANGE || '' ); const markers = ( await Promise.all( - values.map( - async ([ - name = '', - street = '', - number = '', - city = 'Neusiedl am See', - status = '', - fzg = '', - description = '', - alarmTime = '', - start = '', - done = '', - info = '', - latLng = '', - title = '', - ]) => { - let [lat, lng] = parseLatLng(latLng || ''); - const searchString = `${city} ${street} ${number}`; - if ((!lat || !lng) && street) { - const [place] = await searchPlace(searchString, { - position: defaultGeoPosition, - maxResults: 3, - }); - + values + .map((row) => Object.fromEntries(row.map((v, i) => [headers[i], v]))) + .map( + async ( + { + Name: name = '', + Straße: street = '', + Nummer: number = '', + Ort: city = 'Neusiedl am See', + 'Status kurz': status = '', + Fzg: fzg = '', + 'Status lang': description = '', + Alarmzeit: alarmTime = '', + Start: start = '', + 'Erledigt um': done = '', + 'Info/Alarm': info = '', + GPS: latLng = '', + Bezeichnung: title = '', + }, + idx + ) => { + let [lat, lng] = parseLatLng(latLng || ''); + const searchString = `${city} ${street} ${number}`; + // if ((!lat || !lng) && street) { + // const [place] = await searchPlace(searchString, { + // position: defaultGeoPosition, + // maxResults: 3, + // }); // console.info(`place: ${searchString}: ${JSON.stringify(place)}`); - if (place) { - lat = Number.parseFloat(place.lat); - lng = Number.parseFloat(place.lon); - } + // if (place) { + // lat = Number.parseFloat(place.lat); + // lng = Number.parseFloat(place.lon); + // } + // } + const desc = + `${searchString}\nStatus: ${status} ${fzg}\n${description}\n${ + alarmTime && '\nalarmiert: ' + parseTime(alarmTime) + }\n${start && 'begonnen: ' + parseTime(start)}${ + done && '\nabgeschlossen: ' + parseTime(done) + }\n${info}` + .replace(/\n{2,}/g, '\n') + .trim(); + return { + id: `${idx} ${name} ${street} ${number}`.trim(), + street, + number, + city: city, + name: title, + description: desc, + lat, + lng, + status, + idx, + } as UnwetterData; } - const desc = - `${searchString}\nStatus: ${status} ${fzg}\n${description}\n${ - alarmTime && '\nalarmiert: ' + parseTime(alarmTime) - }\n${start && 'begonnen: ' + parseTime(start)}${ - done && '\nabgeschlossen: ' + parseTime(done) - }\n${info}` - .replace(/\n{2,}/g, '\n') - .trim(); - return { - id: `${name} ${lat} ${lng}`.trim(), - street, - number, - city: city, - name: title, - description: desc, - lat, - lng, - status, - } as UnwetterData; - } - ) + ) ) ).filter(({ lat, lng }) => lat && lng); diff --git a/src/components/Map/layers/UnwetterLayer.tsx b/src/components/Map/layers/UnwetterLayer.tsx index a691ce4..80d303e 100644 --- a/src/components/Map/layers/UnwetterLayer.tsx +++ b/src/components/Map/layers/UnwetterLayer.tsx @@ -11,12 +11,14 @@ function useUnwetterSheetData() { const firecall = useFirecall(); const refreshData = useCallback(async () => { - const unwetterData = await fetchUnwetterData( - firecall.sheetId, - firecall.range - ); - console.info(`unwetter data`, unwetterData); - setUnwetterData(unwetterData); + if (firecall.id && firecall.id !== 'unknown') { + const unwetterData = await fetchUnwetterData( + firecall.sheetId, + firecall.range + ); + console.info(`unwetter data`, unwetterData); + setUnwetterData(unwetterData); + } }, [firecall]); useEffect(() => {