mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 01:44:13 +00:00
fix(maps): keep index ids away from Google, and let the area download be big
Four things the index changed and nothing followed: `gers:` was missing from NON_GOOGLE_PLACE_ID, so isGooglePlaceId said true for every id the index hands out. Five call sites gate on that function to keep unresolvable ids away from Google, which answers them with a billable 400 INVALID_ARGUMENT: photo refs, the editorial summary, the place-photo route, the expanded details lookup and the enrichment photo pass. Since the index answers first for search and autocomplete, a GERS id is what an ordinary new place now carries, so this was the normal case rather than an edge one — three billed lookups per added place, none of which could ever return anything. The area download shared the search path's megabyte cap and its 3.5 s timeout. Both are right for a keystroke and wrong for the offline prefetch, which asks for 3000 rows at once: a city-sized answer sits on that megabyte, readCapped threw, and the caller could only report "index unavailable" — the trip then went offline with an empty place cache and nothing said so. The cap now follows the row count the caller asked for, with a floor, and the bulk call gets a budget that fits a bulk download. It is still capped, so a broken upstream cannot hand us an endless body. searchPlaces returned before the Google branch as soon as the index or OpenStreetMap had anything, and both set google_place_id to null. That is right for a search and wrong for the one caller that wants an identity rather than an answer: list-import enrichment (#886) exists to attach a google_place_id to a bare imported pin, and pickEnrichmentMatch discards every candidate without one. So the import silently stayed unenriched on an instance that pays for a key. That caller now asks for the Google identity explicitly. Nothing changes for an instance without a key — enrichment could never resolve anything there, before or after. Tests cover the three functions this branch had left untested, which is what put the maps ratchet under its functions threshold: the autocomplete index path (a new file, MAPS-AUTO-001..007), the gers details branch when Overpass is down (MAPS-GERS-008), and the client's abort timeout — including that the signal exists at all, which nothing asserted before.
This commit is contained in:
@@ -580,12 +580,18 @@ export function parseWikipediaTag(tag: string | undefined | null): { lang: strin
|
||||
// Ids that can never resolve against the Google Places API: coordinate pseudo-ids
|
||||
// (right-click places, in both the coords: and the bare "lat,lng" spelling the
|
||||
// collection views send), OSM ids the client sends when a place has no
|
||||
// google_place_id, the raw photo URLs legacy rows keep in image_url, and photo
|
||||
// cache keys of the form "<placeId>~p3" that enrichment mints for the picker.
|
||||
// google_place_id, GERS ids from the TREK index, the raw photo URLs legacy rows
|
||||
// keep in image_url, and photo cache keys of the form "<placeId>~p3" that
|
||||
// enrichment mints for the picker.
|
||||
// Google answers those with a billable 400 INVALID_ARGUMENT, so every lookup
|
||||
// sorts them out before the call and uses the OSM/Wikimedia path instead.
|
||||
//
|
||||
// `gers:` belongs here for the same reason as `node:`, and it matters more: the
|
||||
// index answers first for search and autocomplete, so a GERS id is what an
|
||||
// ordinary new place now carries. Leaving it out billed three invalid lookups
|
||||
// per place — photo refs, editorial summary, and the photo route.
|
||||
const NON_GOOGLE_PLACE_ID =
|
||||
/^(?:coords|node|way|relation):|^https?:\/\/|^-?\d+(?:\.\d+)?,\s*-?\d+(?:\.\d+)?$|~p\d+$/i;
|
||||
/^(?:coords|gers|node|way|relation):|^https?:\/\/|^-?\d+(?:\.\d+)?,\s*-?\d+(?:\.\d+)?$|~p\d+$/i;
|
||||
// The subset that still has a provider behind it — Overpass for details,
|
||||
// Wikimedia for photos.
|
||||
export const OSM_PLACE_ID = /^(?:node|way|relation):/i;
|
||||
|
||||
@@ -1625,6 +1625,7 @@ export class MapsService {
|
||||
query: string,
|
||||
lang?: string,
|
||||
locationBias?: { lat: number; lng: number; radius?: number },
|
||||
opts: { googleIdentityOnly?: boolean } = {},
|
||||
): Promise<{ places: Record<string, unknown>[]; source: string }> {
|
||||
const { key: apiKey, source: keySource } = this.resolveMapsKey(userId);
|
||||
|
||||
@@ -1636,7 +1637,17 @@ export class MapsService {
|
||||
// It never throws upward. A search that used to work must keep working when
|
||||
// the service is slow or down, so a failure drops through to exactly what
|
||||
// this method did before.
|
||||
if (this.trekPlacesEnabled()) {
|
||||
//
|
||||
// `googleIdentityOnly` skips it. One caller does not want the best answer,
|
||||
// it wants a Google id: list-import enrichment (#886) exists to attach a
|
||||
// `google_place_id` to a bare imported pin, and `pickEnrichmentMatch`
|
||||
// discards every candidate that has none. Index and OpenStreetMap records
|
||||
// both carry `google_place_id: null`, so answering that caller from them
|
||||
// returns matches it must throw away, and the import silently stays
|
||||
// unenriched on an instance that pays for a key. It changes nothing for an
|
||||
// instance without one: enrichment could never resolve anything there
|
||||
// either, before this branch existed or after.
|
||||
if (this.trekPlacesEnabled() && !(opts.googleIdentityOnly && apiKey)) {
|
||||
try {
|
||||
// Both at once. The index is a dataset of businesses and is very good
|
||||
// at those; OpenStreetMap is where the temples, bridges, riverside
|
||||
|
||||
@@ -26,6 +26,22 @@ const TIMEOUT_MS = 3500;
|
||||
/** A search answer is a few kB. A megabyte means something is wrong upstream. */
|
||||
const MAX_BYTES = 1_000_000;
|
||||
|
||||
/**
|
||||
* The area download is the one call that is neither short nor interactive.
|
||||
*
|
||||
* It runs from the offline prefetch, asks for thousands of rows at once, and
|
||||
* nobody is watching a spinner while it does. Holding it to the search budget
|
||||
* capped a Rostock-sized city at the megabyte the prefetch was measured to need
|
||||
* and threw, which the caller could only report as "index unavailable" — the
|
||||
* trip then went offline with an empty place cache and nothing said so.
|
||||
*
|
||||
* Budgeted per row rather than as a flat number so the ceiling follows the
|
||||
* limit the caller actually asked for, with a floor for small boxes.
|
||||
*/
|
||||
const AREA_TIMEOUT_MS = 20_000;
|
||||
const AREA_BYTES_PER_ROW = 1_200;
|
||||
const AREA_MIN_BYTES = 2_000_000;
|
||||
|
||||
export interface TrekPlace {
|
||||
gers: string;
|
||||
name: string;
|
||||
@@ -86,14 +102,18 @@ export function trekPlacesBaseUrl(): string {
|
||||
return (configured || DEFAULT_TREK_PLACES_URL).replace(/\/+$/, '');
|
||||
}
|
||||
|
||||
async function getJson<T>(path: string, params: Record<string, string | number | undefined>): Promise<T> {
|
||||
async function getJson<T>(
|
||||
path: string,
|
||||
params: Record<string, string | number | undefined>,
|
||||
opts: { maxBytes?: number; timeoutMs?: number } = {},
|
||||
): Promise<T> {
|
||||
const url = new URL(trekPlacesBaseUrl() + path);
|
||||
for (const [k, v] of Object.entries(params)) {
|
||||
if (v !== undefined && v !== null && v !== '') url.searchParams.set(k, String(v));
|
||||
}
|
||||
|
||||
const controller = new AbortController();
|
||||
const timer = setTimeout(() => controller.abort(), TIMEOUT_MS);
|
||||
const timer = setTimeout(() => controller.abort(), opts.timeoutMs ?? TIMEOUT_MS);
|
||||
try {
|
||||
const res = await fetch(url, {
|
||||
signal: controller.signal,
|
||||
@@ -109,7 +129,7 @@ async function getJson<T>(path: string, params: Record<string, string | number |
|
||||
}
|
||||
// Read with a cap rather than res.json(): a hostile or broken upstream
|
||||
// should not be able to hand us an unbounded body to buffer.
|
||||
const text = await readCapped(res, MAX_BYTES);
|
||||
const text = await readCapped(res, opts.maxBytes ?? MAX_BYTES);
|
||||
return JSON.parse(text) as T;
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
@@ -212,7 +232,14 @@ export async function trekPlacesArea(
|
||||
bbox: { minLat: number; minLng: number; maxLat: number; maxLng: number },
|
||||
limit = 2000,
|
||||
): Promise<TrekPlacesArea> {
|
||||
const body = await getJson<TrekPlacesArea>('/v1/bbox', { ...bbox, limit });
|
||||
const body = await getJson<TrekPlacesArea>(
|
||||
'/v1/bbox',
|
||||
{ ...bbox, limit },
|
||||
{
|
||||
maxBytes: Math.max(AREA_MIN_BYTES, limit * AREA_BYTES_PER_ROW),
|
||||
timeoutMs: AREA_TIMEOUT_MS,
|
||||
},
|
||||
);
|
||||
return {
|
||||
count: Number(body.count) || 0,
|
||||
truncated: !!body.truncated,
|
||||
|
||||
@@ -1226,11 +1226,18 @@ export class PlacesService {
|
||||
if (place.google_place_id) return;
|
||||
if (typeof place.lat !== 'number' || typeof place.lng !== 'number') return;
|
||||
|
||||
const { places: results } = await this.maps.searchPlaces(userId, place.name, lang, {
|
||||
lat: place.lat,
|
||||
lng: place.lng,
|
||||
radius: SEARCH_BIAS_RADIUS_METERS,
|
||||
});
|
||||
// Asked for a Google identity rather than for the best answer: the whole
|
||||
// point here is the `google_place_id` that `pickEnrichmentMatch` selects on,
|
||||
// and the TREK index and OpenStreetMap have none to give. Without this the
|
||||
// search returns index records, every candidate is discarded for a missing
|
||||
// id, and the import quietly stays unenriched on an instance with a key.
|
||||
const { places: results } = await this.maps.searchPlaces(
|
||||
userId,
|
||||
place.name,
|
||||
lang,
|
||||
{ lat: place.lat, lng: place.lng, radius: SEARCH_BIAS_RADIUS_METERS },
|
||||
{ googleIdentityOnly: true },
|
||||
);
|
||||
const match = pickEnrichmentMatch(results, { lat: place.lat, lng: place.lng });
|
||||
if (!match) return;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user