feat(places): record which search result was picked, on this instance only

The corpus behind "is our own index good enough to replace Nominatim".
When somebody searches and then picks a result, the client posts the query,
the rank and the pick. Nothing else: no user, no trip, no session, and
coordinates only to three decimals.

Deliberately decoupled from the search endpoint. Correlating a response
with a later pick would mean handing out a ticket on POST /api/maps/search
and threading it through all fifteen call sites; the client already holds
everything the row needs at the moment of the pick.

Evaluation happens offline afterwards: export the corpus, run it against a
candidate index, count how often the pick came back in the top five. That
is why collection can start before such an index exists, which is the whole
point, because the corpus needs weeks of real use and the index needs a
server, and those two waits should not be serial.

Off unless an admin switches it on, and it reads === 'true' rather than
!== 'false'. The three older places switches fail open because a migration
backfilled a row for installs already using them; nothing uses this one, so
an instance that upgrades and never visits the admin panel logs nothing.

Reading is admin only, retention is 180 days, and the switch state is on the
unauthenticated app-config endpoint on purpose: whether an instance records
what its users search for is something a visitor may know before logging in.
This commit is contained in:
Maurice
2026-09-05 23:15:51 +02:00
committed by Maurice
parent 0a2fa32fc1
commit b3d69aa760
49 changed files with 1385 additions and 16 deletions
@@ -25,11 +25,12 @@ interface MAdminSettingsSectionProps {
export default function MAdminSettingsSection({ admin, t }: MAdminSettingsSectionProps) {
const {
toast,
setPlacesPhotosEnabled, setPlacesAutocompleteEnabled, setPlacesDetailsEnabled, setPlacesEnrichEnabled,
setPlacesPhotosEnabled, setPlacesAutocompleteEnabled, setPlacesDetailsEnabled, setPlacesEnrichEnabled, setPlaceShadowEnabled,
placesPhotosEnabled, setPlacesPhotosEnabledState,
placesAutocompleteEnabled, setPlacesAutocompleteEnabledState,
placesDetailsEnabled, setPlacesDetailsEnabledState,
placesEnrichEnabled, setPlacesEnrichEnabledState,
placeShadowEnabled, setPlaceShadowEnabledState,
oidcConfig, setOidcConfig, savingOidc, setSavingOidc,
passwordLogin, setPasswordLogin, passwordRegistration, setPasswordRegistration,
oidcLogin, setOidcLogin, oidcRegistration, setOidcRegistration,
@@ -344,6 +345,26 @@ export default function MAdminSettingsSection({ admin, t }: MAdminSettingsSectio
/>
}
/>
<MAdminRow
title={t('admin.placeShadow.title')}
hint={t('admin.placeShadow.subtitle')}
trailing={
<MToggle
checked={placeShadowEnabled}
ariaLabel={t('admin.placeShadow.title')}
onChange={async (next) => {
setPlaceShadowEnabledState(next)
setPlaceShadowEnabled(next)
try {
await adminApi.updatePlaceShadow(next)
} catch {
setPlaceShadowEnabledState(!next)
setPlaceShadowEnabled(!next)
}
}}
/>
}
/>
</div>
{/* Open-Meteo weather info */}
@@ -1,6 +1,7 @@
import { useCallback, useEffect, useRef, useState } from 'react'
import { Loader2, Search } from 'lucide-react'
import { mapsApi } from '../../../../api/client'
import { recordPlacePick } from '../../../../api/placeShadow'
import { PlacesSession } from '../../../../utils/placesSession'
import { isGoogleMapsUrl } from '../../../../components/Planner/PlaceFormModal.helpers'
import { getApiErrorMessage } from '../../../../utils/apiError'
@@ -70,6 +71,10 @@ export default function PlPlaceSearch({ planner, locationBias, onPick, onResolvi
const abortRef = useRef<AbortController | null>(null)
// One Google billing session per search (see utils/placesSession).
const placesSessionRef = useRef(new PlacesSession())
// What produced the list on screen, for the shadow log. Refs, because a pick
// has to read the values that belonged to that list. Mirrors PlaceFormModal.
const searchMetaRef = useRef<{ query: string; source: string } | null>(null)
const acMetaRef = useRef<{ query: string; source: string } | null>(null)
const setResolving = useCallback(
(v: boolean) => {
@@ -86,6 +91,7 @@ export default function PlPlaceSearch({ planner, locationBias, onPick, onResolvi
abortRef.current = controller
try {
const result = await mapsApi.autocomplete(input, language, locationBias, controller.signal, placesSessionRef.current.current())
acMetaRef.current = { query: input, source: result.source || 'unknown' }
setSuggestions(result.suggestions || [])
} catch (err: unknown) {
// Superseded request — axios rejects an aborted call with CanceledError.
@@ -110,8 +116,32 @@ export default function PlPlaceSearch({ planner, locationBias, onPick, onResolvi
}
}, [query, fetchSuggestions])
const applyPlace = (place: MapsPlace) => {
/**
* `pick` is present only when the place came out of a ranked list, so a
* coordinate paste or a resolved Google URL contributes no row.
*/
const applyPlace = (place: MapsPlace, pick?: { mode: 'search' | 'autocomplete'; rank: number; count: number }) => {
onPick(placeToPick(place))
if (pick) {
const meta = pick.mode === 'search' ? searchMetaRef.current : acMetaRef.current
const lat = Number(place.lat)
const lng = Number(place.lng)
if (meta && Number.isFinite(lat) && Number.isFinite(lng)) {
recordPlacePick({
query: meta.query,
lang: language,
biasLat: locationBias ? (locationBias.low.lat + locationBias.high.lat) / 2 : undefined,
biasLng: locationBias ? (locationBias.low.lng + locationBias.high.lng) / 2 : undefined,
source: `${pick.mode}:${meta.source}`,
liveRank: pick.rank,
liveCount: pick.count,
pickedName: String(place.name ?? ''),
pickedLat: lat,
pickedLng: lng,
pickedPlaceId: (place.google_place_id as string) || (place.osm_id as string) || null,
})
}
}
setResults([])
setSuggestions([])
setQuery('')
@@ -148,6 +178,7 @@ export default function PlPlaceSearch({ planner, locationBias, onPick, onResolvi
}
}
const result = await mapsApi.search(trimmed, language)
searchMetaRef.current = { query: trimmed, source: result.source || 'unknown' }
setResults(result.places || [])
} catch (err: unknown) {
toast.error(getApiErrorMessage(err, t('places.mapsSearchError')))
@@ -158,6 +189,9 @@ export default function PlPlaceSearch({ planner, locationBias, onPick, onResolvi
}
const handleSelectSuggestion = async (suggestion: Suggestion) => {
// Read before the list is cleared: this is the rank the user saw.
const acRank = suggestions.findIndex(s => s.placeId === suggestion.placeId)
const acCount = suggestions.length
setSuggestions([])
const previousQuery = query
setQuery('')
@@ -180,7 +214,7 @@ export default function PlPlaceSearch({ planner, locationBias, onPick, onResolvi
place = (search.places?.[0] as MapsPlace | undefined) ?? null
}
if (place) {
applyPlace(place)
applyPlace(place, acRank >= 0 ? { mode: 'autocomplete', rank: acRank, count: acCount } : undefined)
} else {
setQuery(previousQuery)
toast.error(t('places.mapsSearchError'))
@@ -247,7 +281,7 @@ export default function PlPlaceSearch({ planner, locationBias, onPick, onResolvi
<button
key={idx}
type="button"
onClick={() => applyPlace(result)}
onClick={() => applyPlace(result, { mode: 'search', rank: idx, count: results.length })}
className="block w-full border-t border-[color:var(--m-rowbr)] px-[13px] py-[10px] text-left first:border-t-0"
>
<div className="truncate text-[0.8125rem] font-semibold text-m-ink">{String(result.name ?? '')}</div>