mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Bogotá's and Santiago's PBSC feeds list depot stations in São Paulo and Madrid as installed and renting, so activating either city drew live docks thousands of kilometres away and counted them in the layer total. Station information is now limited to the city's registered load radius; every other registered feed already sits inside its radius (checked live 2026-10-16), so nothing else changes. Adds a Bogotá/Santiago registry test matching the Brazilian and Barcelona ones: proxy allowlist, outermost-station activation and radius reach, and the depot filter through the real ingestion path. Co-Authored-By: Claude Opus 5.5 <[email protected]>
220 lines
7.4 KiB
JavaScript
220 lines
7.4 KiB
JavaScript
import { CITY_BY_ID } from './registry.js';
|
|
|
|
export function createIngestion({
|
|
state: layerState,
|
|
services,
|
|
parts,
|
|
source,
|
|
}) {
|
|
/**
|
|
* Convert an upstream GBFS URL into a local proxy URL.
|
|
* The dev server proxies /api/gbfs/* to avoid CORS issues with third-party feeds.
|
|
* @param {string} upstreamUrl - Full HTTPS GBFS endpoint URL.
|
|
* @returns {string} Relative proxy URL.
|
|
*/
|
|
|
|
function toProxyUrl(upstreamUrl) {
|
|
return `/api/gbfs/${encodeURIComponent(upstreamUrl)}`;
|
|
}
|
|
|
|
/** Increment the loading reference count and mark loading state active. */
|
|
|
|
function beginLoading() {
|
|
layerState._loadingOps++;
|
|
layerState._loading = true;
|
|
}
|
|
|
|
/** Decrement the loading reference count; clear loading flag when zero. */
|
|
|
|
function endLoading() {
|
|
layerState._loadingOps = Math.max(0, layerState._loadingOps - 1);
|
|
layerState._loading = layerState._loadingOps > 0;
|
|
}
|
|
|
|
/**
|
|
* Fetch and parse JSON from a GBFS endpoint via the local proxy.
|
|
* @param {string} upstreamUrl - Full HTTPS GBFS endpoint URL.
|
|
* @param {Object} [options]
|
|
* @param {AbortSignal} [options.signal] - Optional abort signal for cancellation.
|
|
* @returns {Promise<Object>} Parsed JSON payload.
|
|
* @throws {Error} On non-OK HTTP status or malformed JSON.
|
|
*/
|
|
|
|
async function fetchGbfsJson(upstreamUrl, options) {
|
|
return source.getStations(upstreamUrl, options);
|
|
}
|
|
|
|
/**
|
|
* Abort an in-flight GBFS request for a specific city and remove it from the map.
|
|
* @param {Map<string, { controller: AbortController }>} map - In-flight request map.
|
|
* @param {string} cityId - City whose request should be cancelled.
|
|
*/
|
|
|
|
function abortInFlight(map, cityId) {
|
|
const entry = map.get(cityId);
|
|
if (!entry) return;
|
|
try {
|
|
entry.controller?.abort();
|
|
} catch {
|
|
// no-op
|
|
}
|
|
map.delete(cityId);
|
|
}
|
|
|
|
/** Abort all in-flight station info and status requests across all cities. */
|
|
|
|
function abortAllInFlight() {
|
|
for (const cityId of layerState._inFlightInfo.keys())
|
|
abortInFlight(layerState._inFlightInfo, cityId);
|
|
for (const cityId of layerState._inFlightStatus.keys())
|
|
abortInFlight(layerState._inFlightStatus, cityId);
|
|
}
|
|
|
|
/**
|
|
* Load station information for a city. Returns cached data if available,
|
|
* deduplicates concurrent requests, and caches the result on success.
|
|
* Station info is static metadata (location, name, capacity) and is
|
|
* fetched once per city per session.
|
|
* @param {string} cityId - City identifier.
|
|
* @param {number} generation - Proximity generation to detect stale requests.
|
|
* @returns {Promise<Map<string, Object>>} Map of stationId to station info.
|
|
* @throws {Error} On unknown city, fetch failure, or empty station list.
|
|
*/
|
|
|
|
async function loadCityStationInfo(cityId, generation) {
|
|
if (layerState._stationInfoCache.has(cityId))
|
|
return layerState._stationInfoCache.get(cityId);
|
|
const inFlight = layerState._inFlightInfo.get(cityId);
|
|
if (inFlight) return inFlight.promise;
|
|
|
|
const city = CITY_BY_ID.get(cityId);
|
|
if (!city) throw new Error(`Unknown GBFS city "${cityId}"`);
|
|
const controller = new AbortController();
|
|
|
|
const promise = (async () => {
|
|
beginLoading();
|
|
try {
|
|
const payload = await fetchGbfsJson(city.stationInformationUrl, {
|
|
signal: controller.signal,
|
|
});
|
|
const stationMap = parts.model.stationsWithinCity(
|
|
city,
|
|
parts.model.parseStationInformation(payload),
|
|
);
|
|
if (stationMap.size === 0) {
|
|
throw new Error(`No station information for ${city.city}`);
|
|
}
|
|
layerState._stationInfoCache.set(cityId, stationMap);
|
|
return stationMap;
|
|
} finally {
|
|
endLoading();
|
|
}
|
|
})().finally(() => {
|
|
// Clean up in-flight entry only if it is still ours (not replaced by a newer request)
|
|
const current = layerState._inFlightInfo.get(cityId);
|
|
if (current && current.controller === controller)
|
|
layerState._inFlightInfo.delete(cityId);
|
|
});
|
|
|
|
layerState._inFlightInfo.set(cityId, { promise, controller, generation });
|
|
return promise;
|
|
}
|
|
|
|
/**
|
|
* Load real-time station status for a city (bike/dock counts, operational flags).
|
|
* Unlike station info, status is NOT served from cache — it is always re-fetched
|
|
* to keep availability data current. Concurrent requests are deduplicated.
|
|
* @param {string} cityId - City identifier.
|
|
* @param {number} generation - Proximity generation to detect stale requests.
|
|
* @returns {Promise<Map<string, Object>>} Map of stationId to status objects.
|
|
* @throws {Error} On unknown city or fetch failure.
|
|
*/
|
|
|
|
async function loadCityStationStatus(cityId, generation) {
|
|
const inFlight = layerState._inFlightStatus.get(cityId);
|
|
if (inFlight) return inFlight.promise;
|
|
|
|
const city = CITY_BY_ID.get(cityId);
|
|
if (!city) throw new Error(`Unknown GBFS city "${cityId}"`);
|
|
const controller = new AbortController();
|
|
|
|
const promise = (async () => {
|
|
beginLoading();
|
|
try {
|
|
const payload = await fetchGbfsJson(city.stationStatusUrl, {
|
|
signal: controller.signal,
|
|
});
|
|
const statusMap = parts.model.parseStationStatus(payload);
|
|
layerState._statusCache.set(cityId, {
|
|
statusMap,
|
|
timestamp: Date.now(),
|
|
});
|
|
return statusMap;
|
|
} finally {
|
|
endLoading();
|
|
}
|
|
})().finally(() => {
|
|
const current = layerState._inFlightStatus.get(cityId);
|
|
if (current && current.controller === controller)
|
|
layerState._inFlightStatus.delete(cityId);
|
|
});
|
|
|
|
layerState._inFlightStatus.set(cityId, { promise, controller, generation });
|
|
return promise;
|
|
}
|
|
const methods = {
|
|
/**
|
|
* Periodic update tick — re-fetches station status for all active cities
|
|
* and refreshes point colors/sizes. Called by the layer manager at
|
|
* STATUS_POLL_MS intervals.
|
|
* @returns {Promise<void>}
|
|
*/
|
|
async update() {
|
|
if (!layerState._enabled || layerState._activeCityIds.size === 0) return;
|
|
|
|
const generation = layerState._proximityGeneration;
|
|
const cityIds = Array.from(layerState._activeCityIds);
|
|
const isCurrent = (cityId) =>
|
|
layerState._enabled &&
|
|
layerState._activeCityIds.has(cityId) &&
|
|
generation === layerState._proximityGeneration;
|
|
let refreshed = false;
|
|
await Promise.all(
|
|
cityIds.map(async (cityId) => {
|
|
try {
|
|
const statusMap = await loadCityStationStatus(cityId, generation);
|
|
if (!isCurrent(cityId)) return;
|
|
parts.rendering.applyStatusToPoints(cityId, statusMap);
|
|
layerState._failedCityIds.delete(cityId);
|
|
refreshed = true;
|
|
} catch (error) {
|
|
if (error?.name === 'AbortError' || !isCurrent(cityId)) return;
|
|
console.warn(
|
|
`[Data:Bikeshare] ${cityId} status update error:`,
|
|
error,
|
|
);
|
|
layerState._failedCityIds.add(cityId);
|
|
}
|
|
}),
|
|
);
|
|
|
|
layerState._count = layerState._stationRenderMap.size;
|
|
// Freshness advances only on a poll that actually refreshed a city.
|
|
if (refreshed && layerState._count > 0)
|
|
layerState._lastUpdate = Date.now();
|
|
},
|
|
};
|
|
|
|
return {
|
|
toProxyUrl,
|
|
beginLoading,
|
|
endLoading,
|
|
fetchGbfsJson,
|
|
abortInFlight,
|
|
abortAllInFlight,
|
|
loadCityStationInfo,
|
|
loadCityStationStatus,
|
|
methods,
|
|
};
|
|
}
|