From df42a684c3ed4ea703ea519c2c741adc6f6e10bb Mon Sep 17 00:00:00 2001 From: evlist Date: Mon, 13 Feb 2023 11:23:17 +0100 Subject: [PATCH] Updating GPX import to use parent's shared. --- src/db-admin/health-legacy.ts | 9 ++- src/db/gpx.ts | 103 +++++++--------------------------- 2 files changed, 27 insertions(+), 85 deletions(-) diff --git a/src/db-admin/health-legacy.ts b/src/db-admin/health-legacy.ts index e8ef099..75e63f7 100644 --- a/src/db-admin/health-legacy.ts +++ b/src/db-admin/health-legacy.ts @@ -234,7 +234,7 @@ export const watchDbLegacy = async () => { localPurges._deleted = true; await db.put(localPurges); } catch (error) { - console.warn({ caller: 'watchDbLegacy / _local/purges', error }); + // console.warn({ caller: 'watchDbLegacy / _local/purges', error }); } const deletedDocs = await getDeletedDocs(); @@ -274,7 +274,12 @@ export const watchDbLegacy = async () => { } } } - if (i !== deletedDocs.length) { + console.log({ + caller: 'watchDbLegacy / purge', + nbDeletedDocs: i, + nbDocsToDelete: deletedDocs.length, + }); + if (i === deletedDocs.length) { await sleep(60000); } } diff --git a/src/db/gpx.ts b/src/db/gpx.ts index 36ac110..288cdaf 100644 --- a/src/db/gpx.ts +++ b/src/db/gpx.ts @@ -3,6 +3,7 @@ import { cloneDeep, isEmpty } from 'lodash'; import { PureComponent } from 'react'; import { $DEVCOMP } from 'solid-js'; import { Point, Rectangle } from '../components/map/types'; +import { state } from '../db-admin/health-legacy'; import { lat2tile, lon2tile, rectanglesIntersect } from '../lib/geo'; import { findStartTime } from '../lib/gpx'; import getUri, { intToGpxId, intToTrkptId } from '../lib/ids'; @@ -78,7 +79,8 @@ const prune = ( trkpt: number; }, extensions: any, - docs: any[] + docs: any[], + shared: string[] ) => { if (typeof object === 'object') { for (const key in object) { @@ -124,94 +126,18 @@ const prune = ( docs.push({ _id: getUri(key, subId), type: key, + shared, + origin: state().remoteUrl, doc: subObjects[index], }); - prune(subId, subObjects[index], previousIds, {}, docs); + prune(subId, subObjects[index], previousIds, {}, docs, shared); } object[key] = undefined; - } else prune(id, object[key], previousIds, extensions, docs); + } else prune(id, object[key], previousIds, extensions, docs, shared); } } }; -// const extensionsFromObject = ( -// object: any, -// extensions = { -// viewport: { topLeft: {}, bottomRight: {} }, -// bbox: { -// minLon: undefined, -// minLat: undefined, -// maxLon: undefined, -// maxLat: undefined, -// }, -// } -// ) => { -// if (typeof object === 'object') { -// if ('$' in object) { -// const attributes = object.$; -// if ('lat' in attributes) { -// const lat = +attributes.lat; -// if ( -// extensions.bbox.minLat === undefined || -// lat < extensions.bbox.minLat -// ) { -// extensions.bbox.minLat = lat; -// } -// if ( -// extensions.bbox.maxLat === undefined || -// lat > extensions.bbox.maxLat -// ) { -// extensions.bbox.maxLat = lat; -// } -// } -// if ('lon' in attributes) { -// const lon = +attributes.lon; -// if ( -// extensions.bbox.minLon === undefined || -// lon < extensions.bbox.minLon -// ) { -// extensions.bbox.minLon = lon; -// } -// if ( -// extensions.bbox.maxLon === undefined || -// lon > extensions.bbox.maxLon -// ) { -// extensions.bbox.maxLon = lon; -// } -// } -// } -// for (const key in object) { -// extensionsFromObject(object[key], extensions); -// } -// } -// return extensions; -// }; - -// const extensionsFromGpx = (gpx: Gpx) => { -// const extensions = { ...gpx.extensions, ...extensionsFromObject(gpx) }; -// gpx.extensions = undefined; -// if ( -// extensions.bbox.maxLat !== undefined && -// extensions.bbox.minLon !== undefined -// ) { -// extensions.viewport.topLeft = { -// x: lon2tile(extensions.bbox.minLon, 0), -// y: lat2tile(extensions.bbox.maxLat, 0), -// }; -// } -// if ( -// extensions.bbox.minLat !== undefined && -// extensions.bbox.maxLon !== undefined -// ) { -// extensions.viewport.bottomRight = { -// x: lon2tile(extensions.bbox.maxLon, 0), -// y: lat2tile(extensions.bbox.minLat, 0), -// }; -// } - -// return extensions; -// }; - const hasMissingTimestamps = (trk: Trk) => { for (const trkseg of trk.trkseg!) { for (const trkpt of trkseg.trkpt!) { @@ -261,6 +187,7 @@ export const pruneAndSaveImportedGpx = async (params: any) => { }; convertTrkToRteWhenNeeded(gpx); let previousGpx: Gpx | null = null; + let shared: string[]; if (id === 'new') { const currentDateTime = new Date().toISOString(); const startTime = new Date(findStartTime(gpx, currentDateTime)); @@ -272,16 +199,26 @@ export const pruneAndSaveImportedGpx = async (params: any) => { } gpxId = { gpx: intToGpxId(startTime.valueOf()) }; docs = [ - { _id: getUri('gpx', gpxId), type: 'gpx', doc: gpx }, + { + _id: getUri('gpx', gpxId), + type: 'gpx', + origin: state().remoteUrl, + doc: gpx, + // a new GPX isn't shared... + }, { _id: getUri('extensions', gpxId), type: 'extensions', + origin: state().remoteUrl, doc: {}, }, ]; } else { gpxId = getUri('gpx', id); previousGpx = (await getGpx({ id })) ?? null; + if (previousGpx?.extensions?.shared) { + shared = previousGpx?.extensions?.shared.split(/\s*,\s*/); + } console.log({ caller: 'pruneAndSaveImportedGpx / previousGpx', id, @@ -312,7 +249,7 @@ export const pruneAndSaveImportedGpx = async (params: any) => { const trkId = getUri('trk', trkUri); previousIds.trk = trkId.trk; } - prune(gpxId, gpx, previousIds, extensions, docs); + prune(gpxId, gpx, previousIds, extensions, docs, shared); console.log({ caller: 'pruneAndSaveImportedGpx / pruned', docs }); try { const result = await putAll(docs);