fix(atlas): stop counting a connection airport as a country visited

The role IN ('from','to') filter from #1486 is correct and still in place, but it
cannot see the case this issue actually reports. Joining AirTrail legs into one
booking with a layover stop is opt-in, so legs imported separately store the hub
as the legitimate `to` of one booking and the `from` of the next, which no role
filter can tell apart from two real arrivals.

Pair them where the countries are counted instead: an arrival and a departure at
the same airport, in different bookings of the same trip, within 24 h of each
other are a connection, and neither endpoint counts as reached. 24 h is the same
window the importer already uses to chain same-PNR legs. Doing it on the read side
means the databases that already hold these flights heal on the next request.

Only flights pair, and never cancelled ones, so a rental car picked up at the
airport still counts its country (#1366). A booking that returns to where the
chain started is a return flight, and that check compares distance rather than
airport identity, so flying home into a different airport of the same metro area
does not erase the country you spent the day in. A country with a place logged in
it, or marked by hand, is untouched.

Closes #1535
This commit is contained in:
Maurice
2026-08-17 14:20:58 +02:00
committed by Maurice
parent 1a0834b866
commit 54693474f1
6 changed files with 942 additions and 15 deletions
+52 -9
View File
@@ -14,8 +14,19 @@ import {
} from './atlas-geo';
import { KNOWN_COUNTRIES } from './known-countries';
import { cityFromAddress } from './city-from-address';
import { transferEndpointIds } from './transfer-endpoints';
import type { FlightEndpointRow } from './transfer-endpoints';
import { haversineKm } from '../common/geo';
/**
* A reservation endpoint plus the two booking columns that decide whether it may
* take part in layover pairing at all (see transfer-endpoints.ts).
*/
type EndpointRow = FlightEndpointRow & {
reservation_type: string | null;
reservation_status: string | null;
};
export type CreateBucketData = {
name: string;
lat?: number | null;
@@ -314,19 +325,33 @@ export class AtlasService {
// via resolvePlaceCountries above and would otherwise be missed (#1366).
// Only 'from'/'to' legs count as actually reached — a 'stop' is an intermediate
// connection/layover (e.g. a plane change) the traveler never really visited.
const endpoints = this.db
const endpointRows = this.db
.prepare(
`
SELECT DISTINCT r.trip_id, e.lat, e.lng
SELECT DISTINCT e.id, e.reservation_id, r.trip_id, e.role, e.code, e.lat, e.lng, e.local_date, e.local_time,
r.type AS reservation_type, r.status AS reservation_status,
CASE e.role
WHEN 'to' THEN COALESCE(r.reservation_end_time, r.reservation_time)
ELSE COALESCE(r.reservation_time, r.reservation_end_time)
END AS fallback_time
FROM reservation_endpoints e
JOIN reservations r ON e.reservation_id = r.id
WHERE r.trip_id IN (${tripIds.map(() => '?').join(',')}) AND e.role IN ('from', 'to')
`,
)
.all(...tripIds) as { trip_id: number; lat: number; lng: number }[];
.all(...tripIds) as EndpointRow[];
// Selecting trip_id makes the DISTINCT per trip, so the same airport comes back once
// per booking. Collapse to one entry per coordinate before resolving countries —
// A layover the traveler never left is only marked 'stop' while both its legs sit in
// one booking. Split over two bookings it is a plain 'to' plus 'from' at the same
// airport, so it has to be recognised from the times instead (#1535). Only flights
// pair: a rental car picked up where a flight landed has its own from/to there and
// must keep contributing its country (#1366).
const transfers = transferEndpointIds(
endpointRows.filter((e) => e.reservation_type === 'flight' && e.reservation_status !== 'cancelled'),
);
const endpoints = endpointRows.filter((e) => !transfers.has(e.id));
// Collapse to one entry per coordinate before resolving countries —
// getCountryFromCoords is a point-in-polygon scan and used to run once per point.
const endpointStatus = new Map<string, { lat: number; lng: number; status: VisitStatus }>();
for (const e of endpoints) {
@@ -962,9 +987,16 @@ export class AtlasService {
// planned place there) was never counted as visited (#1366). Resolve each endpoint
// coordinate to a country and fold it in too.
// Only 'from'/'to' legs count as actually reached — a 'stop' is an intermediate
// connection/layover (e.g. a plane change) the traveler never really visited (#1486).
const endpoints = this.db.all<{ lat: number; lng: number }>(`
SELECT DISTINCT e.lat, e.lng
// connection/layover (e.g. a plane change) the traveler never really visited (#1486),
// and the same layover split over two bookings is recognised from its ground time
// instead, because there it is stored as a legitimate 'to'/'from' pair (#1535).
const endpointRows = this.db.all<EndpointRow>(`
SELECT DISTINCT e.id, e.reservation_id, r.trip_id, e.role, e.code, e.lat, e.lng, e.local_date, e.local_time,
r.type AS reservation_type, r.status AS reservation_status,
CASE e.role
WHEN 'to' THEN COALESCE(r.reservation_end_time, r.reservation_time)
ELSE COALESCE(r.reservation_time, r.reservation_end_time)
END AS fallback_time
FROM reservation_endpoints e
JOIN reservations r ON e.reservation_id = r.id
JOIN trips t ON r.trip_id = t.id
@@ -973,7 +1005,18 @@ export class AtlasService {
AND COALESCE(t.start_date, t.end_date) IS NOT NULL
AND COALESCE(t.start_date, t.end_date) <= date('now')
`, userId, userId);
for (const e of endpoints) {
const transfers = transferEndpointIds(
endpointRows.filter(e => e.reservation_type === 'flight' && e.reservation_status !== 'cancelled')
);
// The DISTINCT no longer collapses per coordinate now that the endpoint id has to be
// in the projection, so collapse here instead: getCountryFromCoords is a
// point-in-polygon scan and must stay at one run per coordinate.
const endpointCoords = new Map<string, { lat: number; lng: number }>();
for (const e of endpointRows) {
if (transfers.has(e.id)) continue;
endpointCoords.set(`${e.lat},${e.lng}`, { lat: e.lat, lng: e.lng });
}
for (const e of endpointCoords.values()) {
const code = getCountryFromCoords(e.lat, e.lng);
if (code) countryCodes.add(code.toUpperCase());
}
+305
View File
@@ -0,0 +1,305 @@
/**
* Layover detection for the two country-deriving Atlas queries.
*
* A plane change is not a visit, so #1486 taught both queries to skip
* reservation_endpoints rows with role='stop'. That only reaches a layover
* stored INSIDE one booking. When the legs of one journey land in the database as
* two separate reservations (an AirTrail import whose flights never chained, or
* legs imported one by one before the join feature existed), the hub is the
* legitimate 'to' of the first booking and the legitimate 'from' of the second,
* and no role filter can tell that apart from a real destination (#1535).
*
* What does tell it apart is the time on the ground: leaving the same airport
* again within 24 h is a connection, not a stay. The window is deliberately the
* one both importers already apply when they merge the legs of one journey
* (orderConnectionChain in airtrail-import.service.ts, kitinerary-mapper.ts), so
* Atlas and the importers agree on what counts as one connection.
*
* Read-side only, hence no migration: both queries derive their countries live on
* every request, so a database that already holds the split bookings heals as
* soon as this ships.
*/
import { haversineKm } from '../common/geo';
/** Ground time up to which an onward departure is a connection rather than a stay. */
export const MAX_LAYOVER_MS = 24 * 3600 * 1000;
/**
* Without clocks only calendar dates are left. Leaving the day after arriving is
* still an overnight connection; a wider gap can't be told apart from a genuine
* stopover in the stored data, so it keeps counting as visited.
*/
export const MAX_CLOCKLESS_DAY_GAP = 1;
/**
* How far from where the arriving leg started the departing leg may land and
* still count as the flight home rather than the next hop. A return is regularly
* booked into the sibling airport of the same city (Charleroi instead of
* Zaventem, Gatwick instead of Heathrow), and that day was spent at the
* destination, not in transit.
*
* A radius is the right shape for that question: the country of a coordinate
* would be both too coarse (Belfast to Dublin to London would read as a return
* and start counting Ireland as visited) and too expensive, since
* getCountryFromCoords scans admin polygons and this runs inside the pairing
* loop. 100 km covers every sibling pair worth naming (CRL/BRU 45 km, LGW/LHR
* 40 km, EWR/JFK 34 km, HND/NRT 60 km) and stays below the distance between two
* cities that each own their traffic, so Amsterdam to Frankfurt to Brussels
* (170 km) keeps pairing as the connection it is.
*/
const RETURN_RADIUS_KM = 100;
/** One reservation_endpoints row, joined to the columns its booking contributes. */
export interface FlightEndpointRow {
id: number;
reservation_id: number;
trip_id: number;
role: string;
code: string | null;
lat: number;
lng: number;
local_date: string | null;
local_time: string | null;
/** The booking's own time column for this side of the leg, used for the date only. */
fallback_time: string | null;
}
/** Local calendar date plus, when the leg carried an instant, the local clock. */
interface Stamp {
date: string | null;
time: string | null;
}
/** Where one end of a booking actually is, for the airports the importer geocoded. */
interface Coords {
lat: number;
lng: number;
}
/** The match keys and positions of one booking's own two ends, shared by all of its endpoints. */
interface BookingKeys {
from: Set<string>;
to: Set<string>;
fromCoords: Coords[];
toCoords: Coords[];
}
interface Endpoint {
row: FlightEndpointRow;
booking: BookingKeys;
/** The buckets this endpoint belongs to, already scoped to its trip. */
keys: string[];
/** Midnight of the leg's local date. */
dayMs: number;
/** The leg's exact instant, null when it carried no clock. */
exactMs: number | null;
/** What a bucket of departures is ordered by: the instant if there is one, else the day. */
sortMs: number;
}
const DATE_ONLY = /^\d{4}-\d{2}-\d{2}$/;
const DATE_PREFIX = /^(\d{4}-\d{2}-\d{2})/;
const CLOCK = /^\d{2}:\d{2}/;
const DAY_MS = 24 * 3600 * 1000;
/**
* How far past the arrival's midnight the sorted departures still have to be
* walked. Two days, because each half of a pair is anchored on its own date: a
* clockless arrival still pairs with a departure at 23:45 the next day, and an
* arrival at 23:50 still pairs with a date-only departure the day after. This
* bounds the scan only, isConnection stays the one place that decides.
*/
const SCAN_WINDOW_MS = 2 * DAY_MS;
/**
* Every key an endpoint can be matched on. Two rows for one airport need not
* agree on their shape: an imported leg carries the IATA/ICAO code, a booking
* typed in by hand may only have been geocoded. Indexing both and pairing on
* either match covers the mixed case, and the coordinate key is rounded because
* two geocodes of the same airport differ in the last decimals.
*/
function endpointKeys(e: FlightEndpointRow): string[] {
const keys: string[] = [];
const code = e.code?.trim();
if (code) keys.push(`c:${code.toUpperCase()}`);
if (Number.isFinite(e.lat) && Number.isFinite(e.lng)) keys.push(`p:${e.lat.toFixed(2)},${e.lng.toFixed(2)}`);
return keys;
}
/**
* An endpoint only has local_date/local_time when its leg carried a usable
* instant; localParts() returns nulls otherwise, which is exactly the date-only
* AirTrail flight that fails to chain in the first place. So fall back to the
* booking's own time column, but take its DATE half only: for an arrival that
* column can hold the departure airport's local clock (airtrail.mapper.ts), which
* says nothing about the time on the ground here.
*/
function stampOf(e: FlightEndpointRow): Stamp {
if (e.local_date && DATE_ONLY.test(e.local_date)) {
const time = e.local_time && CLOCK.test(e.local_time) ? e.local_time.slice(0, 5) : null;
return { date: e.local_date, time };
}
const fallback = e.fallback_time?.match(DATE_PREFIX);
return { date: fallback ? fallback[1] : null, time: null };
}
function utcMs(date: string, time: string | null): number {
return Date.parse(`${date}T${time ?? '00:00'}:00Z`);
}
/**
* The instants the pairing compares, read once per endpoint instead of once per
* pair. null means the leg can never be either half of a pair: without a date
* that parses, neither branch below has anything to measure.
*
* A clock that got past the shape check can still be nonsense, and the resulting
* NaN is kept rather than downgraded to "no clock": a pair where both sides claim
* an instant has always been judged on the instant alone.
*/
function instantsOf(stamp: Stamp): { dayMs: number; exactMs: number | null; sortMs: number } | null {
if (!stamp.date) return null;
const dayMs = utcMs(stamp.date, null);
if (!Number.isFinite(dayMs)) return null;
const exactMs = stamp.time === null ? null : utcMs(stamp.date, stamp.time);
return { dayMs, exactMs, sortMs: exactMs !== null && Number.isFinite(exactMs) ? exactMs : dayMs };
}
/**
* Is the departure the onward leg of the arrival? Both endpoints sit at the same
* airport and therefore in the same timezone, so the naive local stamps compare
* as real instants and their difference is the true time on the ground. That is
* the only reason this is sound, so never reuse it across two airports.
*/
function isConnection(arrival: Endpoint, departure: Endpoint): boolean {
if (arrival.exactMs !== null && departure.exactMs !== null) {
const gap = departure.exactMs - arrival.exactMs;
return Number.isFinite(gap) && gap >= 0 && gap <= MAX_LAYOVER_MS;
}
const days = (departure.dayMs - arrival.dayMs) / DAY_MS;
return days >= 0 && days <= MAX_CLOCKLESS_DAY_GAP;
}
function intersects(a: Set<string>, b: Set<string>): boolean {
for (const key of a) {
if (b.has(key)) return true;
}
return false;
}
/**
* Is the departing booking heading back to where the arriving one started? Then
* the traveler spent the time in between at the destination, and the same call is
* the one orderConnectionChain makes before it joins two legs.
*
* The airport keys answer that for a return into the exact same airport; the
* distance covers the sibling airport of the same city, which the keys can never
* match. Legs the importer had no coordinates for fall back to the key match.
*/
function returnsToOrigin(departure: Endpoint, arrival: Endpoint): boolean {
if (intersects(departure.booking.to, arrival.booking.from)) return true;
for (const landed of departure.booking.toCoords) {
for (const started of arrival.booking.fromCoords) {
if (haversineKm(landed.lat, landed.lng, started.lat, started.lng) <= RETURN_RADIUS_KM) return true;
}
}
return false;
}
/** First entry of a bucket sorted by sortMs that is not earlier than `from`. */
function firstFrom(bucket: Endpoint[], from: number): number {
let low = 0;
let high = bucket.length;
while (low < high) {
const mid = (low + high) >>> 1;
if (bucket[mid].sortMs < from) low = mid + 1;
else high = mid;
}
return low;
}
/**
* The endpoint ids that are only a connection airport, spread over two bookings.
*
* Callers pass flight endpoints of non-cancelled bookings only: a car rental
* picked up right where a flight landed has its own from/to at that airport and
* would otherwise pair with the arrival and swallow the destination country
* (#1366).
*/
export function transferEndpointIds(rows: FlightEndpointRow[]): Set<number> {
const transfers = new Set<number>();
if (rows.length < 2) return transfers;
const landings: Endpoint[] = [];
const departures = new Map<string, Endpoint[]>();
const bookings = new Map<number, BookingKeys>();
for (const row of rows) {
if (row.role !== 'from' && row.role !== 'to') continue;
const keys = endpointKeys(row);
if (keys.length === 0) continue;
let booking = bookings.get(row.reservation_id);
if (!booking) {
booking = { from: new Set<string>(), to: new Set<string>(), fromCoords: [], toCoords: [] };
bookings.set(row.reservation_id, booking);
}
const arriving = row.role === 'to';
const ownKeys = arriving ? booking.to : booking.from;
for (const key of keys) ownKeys.add(key);
if (Number.isFinite(row.lat) && Number.isFinite(row.lng)) {
(arriving ? booking.toCoords : booking.fromCoords).push({ lat: row.lat, lng: row.lng });
}
// A leg without a readable date pairs with nothing, but its keys and its
// position had to be recorded above: they describe the booking either way.
const instants = instantsOf(stampOf(row));
if (!instants) continue;
// Two trips that happen to touch the same airport are two journeys: an
// arrival home from one and a departure on the next is not a layover. The
// trip belongs in the bucket key rather than in a per-pair check, so the
// journeys of an imported flight log are never compared against each other.
const endpoint: Endpoint = { row, booking, keys: keys.map((key) => `${row.trip_id}:${key}`), ...instants };
if (arriving) {
landings.push(endpoint);
continue;
}
for (const key of endpoint.keys) {
const bucket = departures.get(key);
if (bucket) bucket.push(endpoint);
else departures.set(key, [endpoint]);
}
}
// In time order, so an arrival can stop looking once the ground time is out of
// reach. Ten years of flight log imported into one trip put hundreds of
// departures on a home airport, and comparing every arrival against all of them
// cost more than a second on both request paths that call this.
for (const bucket of departures.values()) bucket.sort((a, b) => a.sortMs - b.sortMs);
for (const arrival of landings) {
const until = arrival.dayMs + SCAN_WINDOW_MS;
// An endpoint carrying both a code and a coordinate sits in two buckets, so
// without this the same pair would be judged twice.
const seen = arrival.keys.length > 1 ? new Set<Endpoint>() : null;
for (const key of arrival.keys) {
const left = departures.get(key);
if (!left) continue;
for (let i = firstFrom(left, arrival.dayMs); i < left.length; i++) {
const departure = left[i];
if (departure.sortMs > until) break;
if (seen) {
if (seen.has(departure)) continue;
seen.add(departure);
}
if (departure.row.reservation_id === arrival.row.reservation_id) continue;
if (returnsToOrigin(departure, arrival)) continue;
if (!isConnection(arrival, departure)) continue;
transfers.add(arrival.row.id);
transfers.add(departure.row.id);
}
}
}
return transfers;
}