fix(phase26): batch geocode endpoint for faster server trip scrubber
This commit is contained in:
+20
-19
@@ -40,7 +40,6 @@
|
||||
}
|
||||
|
||||
function preloadGeocodes(points) {
|
||||
// Collect unique rounded coordinates and batch-fetch all labels upfront.
|
||||
_geoCache = {};
|
||||
const seen = new Set();
|
||||
const queue = [];
|
||||
@@ -50,25 +49,27 @@
|
||||
seen.add(key);
|
||||
queue.push({ key, lat: p.latitude, lon: p.longitude });
|
||||
}
|
||||
if (!queue.length) return;
|
||||
|
||||
// Fetch in parallel with a small concurrency cap
|
||||
const CONCURRENCY = 4;
|
||||
let idx = 0;
|
||||
function next() {
|
||||
if (idx >= queue.length) return;
|
||||
const item = queue[idx++];
|
||||
const ctrl = new AbortController();
|
||||
const t = setTimeout(() => ctrl.abort(), 4000);
|
||||
return fetch(`/api/geocode?lat=${item.lat}&lon=${item.lon}`, { signal: ctrl.signal })
|
||||
.then(r => { clearTimeout(t); return r.ok ? r.json() : null; })
|
||||
.then(data => {
|
||||
_geoCache[item.key] = (data && data.label) || null;
|
||||
})
|
||||
.catch(() => { clearTimeout(t); _geoCache[item.key] = null; })
|
||||
.finally(() => next());
|
||||
}
|
||||
// Start initial batch
|
||||
for (let i = 0; i < CONCURRENCY && i < queue.length; i++) next();
|
||||
// Try batch endpoint first (server has it, Pi falls back to 404)
|
||||
const coords = queue.map(q => ({lat: q.lat, lon: q.lon}));
|
||||
const ctrl = new AbortController();
|
||||
const t = setTimeout(() => ctrl.abort(), 15000);
|
||||
fetch('/api/geocode/batch', {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({coords: coords}),
|
||||
signal: ctrl.signal,
|
||||
})
|
||||
.then(r => { clearTimeout(t); return r.ok ? r.json() : null; })
|
||||
.then(data => {
|
||||
if (data) {
|
||||
for (const q of queue) {
|
||||
if (data[q.key]) _geoCache[q.key] = data[q.key];
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch(() => { clearTimeout(t); });
|
||||
}
|
||||
|
||||
function geocodePoint(lat, lon) {
|
||||
|
||||
Reference in New Issue
Block a user