From 10f0cb7d04ddfa9f0cf3c798db41961ac50d7ce2 Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 23 Nov 2026 15:34:42 +0100 Subject: [PATCH] fix(flight-live): keep a late earlier sector from reading as this flight departing Until a leg has departed, an aircraft in the air that is no nearer the destination than the origin is now stays the earlier sector's after the scheduled departure too, unless it is climbing out near the origin. --- .../flight-live/flight-live-poll.service.ts | 5 ++- .../nest/flight-live/flight-live.derive.ts | 34 +++++++++++++------ .../flight-live/flight-live.derive.test.ts | 32 ++++++++++++++++- 3 files changed, 59 insertions(+), 12 deletions(-) diff --git a/server/src/nest/flight-live/flight-live-poll.service.ts b/server/src/nest/flight-live/flight-live-poll.service.ts index ac2670351..3d7da4791 100644 --- a/server/src/nest/flight-live/flight-live-poll.service.ts +++ b/server/src/nest/flight-live/flight-live-poll.service.ts @@ -253,7 +253,10 @@ export class FlightLivePollService { if (!answer.ok && (answer as Extract).reason === 'budget') return; result.asked++; const at = clock(); - const picked = answer.ok ? pickAircraft(answer.aircraft, route, { now: at, siblings: head.siblings }) : null; + const departed = prev.phase === 'airborne' || prev.departed_at !== null; + const picked = answer.ok + ? pickAircraft(answer.aircraft, route, { now: at, siblings: head.siblings, departed }) + : null; let next = answer.ok ? advance(prev, picked, route, at, answer.source) : failed(prev); next = closeIfOver({ ...next, next_poll_at: nextPollAt(next, route, at) }, route, at); const notices = this.dueNotices(prev, next, head.legs, at); diff --git a/server/src/nest/flight-live/flight-live.derive.ts b/server/src/nest/flight-live/flight-live.derive.ts index 86e341363..7c63d49bc 100644 --- a/server/src/nest/flight-live/flight-live.derive.ts +++ b/server/src/nest/flight-live/flight-live.derive.ts @@ -166,14 +166,27 @@ function claimedBySibling( } /** - * Whether an aircraft in the air before the leg's scheduled departure cannot be - * this leg: it is no nearer the destination than the origin is, so it has not - * left the origin for it. That is the aircraft still flying an earlier sector - * of the same flight number, which nobody on the instance may have booked. + * Whether an aircraft in the air while the leg has not departed cannot be this + * leg: it is no nearer the destination than the origin is, so it has not left + * the origin for it. That is the aircraft still flying an earlier sector of the + * same flight number, which nobody on the instance may have booked, and which + * runs late past this leg's scheduled departure as often as not. After the + * scheduled time a departure may climb out away from the destination before it + * turns, so one climbing near the origin then is taken for the leg. */ -function airborneBeforeDeparture(ac: FeedAircraft, lat: number, lng: number, route: Route, now: number): boolean { - if (route.scheduled_departure === null || now >= route.scheduled_departure || ac.alt_baro === 'ground') return false; - return kmTo(lat, lng, route.destination) >= kmTo(route.origin.lat, route.origin.lng, route.destination); +function stillInbound( + ac: FeedAircraft, + lat: number, + lng: number, + route: Route, + now: number, + departed: boolean, +): boolean { + if (departed || route.scheduled_departure === null || ac.alt_baro === 'ground') return false; + if (kmTo(lat, lng, route.destination) < kmTo(route.origin.lat, route.origin.lng, route.destination)) return false; + if (now < route.scheduled_departure) return true; + const climbing = (finite(ac.baro_rate) ?? finite(ac.geom_rate) ?? 0) > 0; + return !(climbing && kmTo(lat, lng, route.origin) <= 60); } /** @@ -182,12 +195,12 @@ function airborneBeforeDeparture(ac: FeedAircraft, lat: number, lng: number, rou * airports (least detour over the direct distance). A callsign is reused by * the next leg of a rotation, so two can be in the air at once. An aircraft a * sibling sector of the same flight number claims at `now` is left to it, and - * so is one still on its way to the origin before the scheduled departure. + * so is one still on its way to the origin while the leg has not departed. */ export function pickAircraft( aircraft: readonly FeedAircraft[], route: Route, - sectors: { now: number; siblings: readonly SiblingSector[] } = { now: 0, siblings: [] }, + sectors: { now: number; siblings: readonly SiblingSector[]; departed?: boolean } = { now: 0, siblings: [] }, ): FeedAircraft | null { let best: FeedAircraft | null = null; let bestScore = Number.POSITIVE_INFINITY; @@ -195,7 +208,8 @@ export function pickAircraft( const lat = finite(ac.lat); const lng = finite(ac.lon); if (lat !== null && lng !== null && claimedBySibling(lat, lng, route, sectors.now, sectors.siblings)) continue; - if (lat !== null && lng !== null && airborneBeforeDeparture(ac, lat, lng, route, sectors.now)) continue; + if (lat !== null && lng !== null && stillInbound(ac, lat, lng, route, sectors.now, sectors.departed === true)) + continue; const score = lat === null || lng === null ? Number.MAX_VALUE : detourKm(lat, lng, route); if (best === null || score < bestScore) { best = ac; diff --git a/server/tests/unit/nest/flight-live/flight-live.derive.test.ts b/server/tests/unit/nest/flight-live/flight-live.derive.test.ts index aaf35856a..2120c6b5e 100644 --- a/server/tests/unit/nest/flight-live/flight-live.derive.test.ts +++ b/server/tests/unit/nest/flight-live/flight-live.derive.test.ts @@ -169,7 +169,37 @@ describe('pickAircraft and observe', () => { expect(pick(swa({ lat: 41.787, lon: -87.75 }), at('2026-07-01T14:20:00Z'))?.hex).toBe('swa'); expect(pick(swa({ alt_baro: 6000, lat: 41.77, lon: -88.1 }), at('2026-07-01T14:40:00Z'))?.hex).toBe('swa'); - expect(pick(overOhio, at('2026-07-01T14:50:00Z'))?.hex).toBe('swa'); + expect( + pickAircraft([overOhio], second, { now: at('2026-07-01T14:50:00Z'), siblings: [], departed: true })?.hex, + ).toBe('swa'); + }); + + it('FLDER-022: a late first sector is not taken for the second once its scheduled departure has passed', () => { + const at = (iso: string) => Date.parse(iso); + const mdw = { code: 'MDW', name: 'Chicago Midway', lat: 41.7868, lng: -87.7522, timezone: 'America/Chicago' }; + const den = { code: 'DEN', name: 'Denver', lat: 39.8561, lng: -104.6737, timezone: 'America/Denver' }; + const second: ScheduledLeg = { + ...LEG, + callsign: 'SWA1234', + flight_number: 'WN 1234', + origin: mdw, + destination: den, + scheduled_departure: at('2026-07-01T14:45:00Z'), + scheduled_arrival: at('2026-07-01T17:15:00Z'), + }; + const swa = (overrides: Partial) => ac({ hex: 'swa', flight: 'SWA1234', ...overrides }); + const late = at('2026-07-01T14:55:00Z'); + const pick = (aircraft: FeedAircraft) => pickAircraft([aircraft], second, { now: late, siblings: [] })?.hex ?? null; + + const overIndiana = swa({ alt_baro: 24000, lat: 41.6, lon: -86.5, baro_rate: -1500 }); + const onFinal = swa({ alt_baro: 2500, lat: 41.78, lon: -87.6, baro_rate: -800 }); + expect(pick(overIndiana)).toBeNull(); + expect(pick(onFinal)).toBeNull(); + const quiet = advance(emptyState('SWA1234', '2026-07-01', 'MDW', 'DEN'), null, second, late, 'adsb.lol'); + expect(quiet).toMatchObject({ phase: 'scheduled', departed_at: null }); + + // Off the eastbound runway and climbing before it turns west: that is the second sector leaving. + expect(pick(swa({ alt_baro: 3000, lat: 41.79, lon: -87.55, baro_rate: 2200 }))).toBe('swa'); }); it('FLDER-003: reads a report at its own time, on the ground at zero feet, the geometric height as a fallback', () => {