mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 17:03:15 +00:00
fix(flight-live): keep a late inbound aircraft holding near the origin from reading as departed
After the scheduled time an aircraft near the origin that neither climbs nor descends is taken for the leg only when it was on the ground at the origin before or flies away from it.
This commit is contained in:
@@ -255,7 +255,12 @@ export class FlightLivePollService {
|
||||
const at = clock();
|
||||
const departed = prev.phase === 'airborne' || prev.departed_at !== null;
|
||||
const picked = answer.ok
|
||||
? pickAircraft(answer.aircraft, route, { now: at, siblings: head.siblings, departed })
|
||||
? pickAircraft(answer.aircraft, route, {
|
||||
now: at,
|
||||
siblings: head.siblings,
|
||||
departed,
|
||||
taxied: prev.phase === 'taxiing',
|
||||
})
|
||||
: 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);
|
||||
|
||||
@@ -175,7 +175,10 @@ function claimedBySibling(
|
||||
* 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, level at its first cleared altitude or with no vertical rate in the
|
||||
* feed, so one near the origin then is taken for the leg unless it descends.
|
||||
* feed. One near the origin then is taken for the leg when it climbs, or when
|
||||
* it neither descends nor could be arriving: it was on the ground at the origin
|
||||
* before, or it flies away from the origin. A late inbound one holding or level
|
||||
* on its approach does neither.
|
||||
*/
|
||||
function stillInbound(
|
||||
ac: FeedAircraft,
|
||||
@@ -184,13 +187,34 @@ function stillInbound(
|
||||
route: Route,
|
||||
now: number,
|
||||
departed: boolean,
|
||||
taxied: 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;
|
||||
if (kmTo(lat, lng, route.origin) > 60) return true;
|
||||
const rate = finite(ac.baro_rate) ?? finite(ac.geom_rate);
|
||||
const descending = rate !== null && rate < DESCENDING_FPM;
|
||||
return descending || kmTo(lat, lng, route.origin) > 60;
|
||||
if (rate !== null && rate < DESCENDING_FPM) return true;
|
||||
if (rate !== null && rate > 0) return false;
|
||||
return !taxied && !fliesAwayFrom(ac, lat, lng, route.origin);
|
||||
}
|
||||
|
||||
/** Whether the aircraft's track points away from `airport`, within 90 degrees of the bearing from it. */
|
||||
function fliesAwayFrom(ac: FeedAircraft, lat: number, lng: number, airport: { lat: number; lng: number }): boolean {
|
||||
const track = finite(ac.track);
|
||||
if (track === null) return false;
|
||||
const off = Math.abs(((track - bearingDeg(airport.lat, airport.lng, lat, lng) + 540) % 360) - 180);
|
||||
return off < 90;
|
||||
}
|
||||
|
||||
/** The initial great-circle bearing from the first point to the second, in degrees from north. */
|
||||
function bearingDeg(lat1: number, lng1: number, lat2: number, lng2: number): number {
|
||||
const toRad = (deg: number) => (deg * Math.PI) / 180;
|
||||
const dLng = toRad(lng2 - lng1);
|
||||
const y = Math.sin(dLng) * Math.cos(toRad(lat2));
|
||||
const x =
|
||||
Math.cos(toRad(lat1)) * Math.sin(toRad(lat2)) - Math.sin(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.cos(dLng);
|
||||
return (Math.atan2(y, x) * 180) / Math.PI;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -204,7 +228,10 @@ function stillInbound(
|
||||
export function pickAircraft(
|
||||
aircraft: readonly FeedAircraft[],
|
||||
route: Route,
|
||||
sectors: { now: number; siblings: readonly SiblingSector[]; departed?: boolean } = { now: 0, siblings: [] },
|
||||
sectors: { now: number; siblings: readonly SiblingSector[]; departed?: boolean; taxied?: boolean } = {
|
||||
now: 0,
|
||||
siblings: [],
|
||||
},
|
||||
): FeedAircraft | null {
|
||||
let best: FeedAircraft | null = null;
|
||||
let bestScore = Number.POSITIVE_INFINITY;
|
||||
@@ -212,7 +239,11 @@ 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 && stillInbound(ac, lat, lng, route, sectors.now, sectors.departed === true))
|
||||
if (
|
||||
lat !== null &&
|
||||
lng !== null &&
|
||||
stillInbound(ac, lat, lng, route, sectors.now, sectors.departed === true, sectors.taxied === true)
|
||||
)
|
||||
continue;
|
||||
const score = lat === null || lng === null ? Number.MAX_VALUE : detourKm(lat, lng, route);
|
||||
if (best === null || score < bestScore) {
|
||||
|
||||
@@ -236,6 +236,23 @@ describe('FlightLivePollService.poll', () => {
|
||||
expect(delays()).toEqual([]);
|
||||
});
|
||||
|
||||
it('FLPOLL-016: an aircraft first seen on the ground at the origin is taken as departed when it flies level away from the route', async () => {
|
||||
await seedFlight();
|
||||
feed.lookup.mockResolvedValueOnce(
|
||||
seen(aircraft({ alt_baro: 'ground', gs: 10, baro_rate: null, lat: 50.035, lon: 8.56 })),
|
||||
);
|
||||
await poll.poll(at(DEP + 5 * MIN));
|
||||
const sector = () => findRow(orm, FlightLiveStates, { callsign: 'DLH100', service_date: '2026-07-01' });
|
||||
expect(await sector()).toMatchObject({ phase: 'taxiing' });
|
||||
|
||||
// South-west of Frankfurt, level and turning back over the field towards Berlin.
|
||||
feed.lookup.mockResolvedValueOnce(
|
||||
seen(aircraft({ alt_baro: 3000, baro_rate: 0, track: 45, lat: 49.95, lon: 8.4 })),
|
||||
);
|
||||
await poll.poll(at(DEP + 20 * MIN));
|
||||
expect(await sector()).toMatchObject({ phase: 'airborne', departed_at: '2026-07-01 10:20:00' });
|
||||
});
|
||||
|
||||
it('FLPOLL-005: landed is told once and the flight is asked about no more', async () => {
|
||||
const { trip } = await seedFlight();
|
||||
feed.lookup.mockResolvedValueOnce(seen(aircraft()));
|
||||
|
||||
@@ -232,6 +232,44 @@ describe('pickAircraft and observe', () => {
|
||||
expect(next.departed_at).not.toBeNull();
|
||||
});
|
||||
|
||||
it('FLDER-024: after its scheduled time a late inbound aircraft holding or level near the origin is not taken for the leg', () => {
|
||||
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 leg: 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 late = at('2026-07-01T15:10:00Z');
|
||||
// East of Midway, on the side away from Denver, heading west back towards the field.
|
||||
const inbound = (rates: Partial<FeedAircraft>) =>
|
||||
ac({ hex: 'swa', flight: 'SWA1234', alt_baro: 5000, lat: 41.8, lon: -87.4, track: 270, ...rates });
|
||||
const pick = (aircraft: FeedAircraft, taxied = false) =>
|
||||
pickAircraft([aircraft], leg, { now: late, siblings: [], taxied })?.hex ?? null;
|
||||
|
||||
expect(pick(inbound({ baro_rate: null, geom_rate: null }))).toBeNull();
|
||||
expect(pick(inbound({ baro_rate: 0 }))).toBeNull();
|
||||
expect(pick(inbound({ baro_rate: -128 }))).toBeNull();
|
||||
expect(pick(inbound({ baro_rate: 0, track: null }))).toBeNull();
|
||||
const holding = advance(
|
||||
emptyState('SWA1234', '2026-07-01', 'MDW', 'DEN'),
|
||||
pickAircraft([inbound({ baro_rate: 0 })], leg, { now: late, siblings: [] }),
|
||||
leg,
|
||||
late,
|
||||
'adsb.lol',
|
||||
);
|
||||
expect(holding).toMatchObject({ phase: 'scheduled', departed_at: null });
|
||||
|
||||
// Seen on the ground at Midway before, or climbing, it is the leg leaving.
|
||||
expect(pick(inbound({ baro_rate: 0 }), true)).toBe('swa');
|
||||
expect(pick(inbound({ baro_rate: 1200 }))).toBe('swa');
|
||||
});
|
||||
|
||||
it('FLDER-003: reads a report at its own time, on the ground at zero feet, the geometric height as a fallback', () => {
|
||||
const now = DEP;
|
||||
expect(observe(ac(), now)).toEqual({
|
||||
|
||||
Reference in New Issue
Block a user