Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion service/CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ Airframes coverage aggregation service. Subscribes to NATS processed messages ac
and discarded positions by reason, and `cellsInMemory`.
- No per-station rate limit (busy receivers are legitimate). `/api/v1/health` reports
`outOfRange` positions by radio family (`near` = within twice the range limit,
`far` = beyond it)
`far` = beyond it), and `outOfRangeStations`: the 20 stations with the most such
discards and the furthest distance each saw (a mislocated station shows up here)
- Imported area feeds (station idents in `AREA_FEED_STATIONS`, default `NCT`) are
treated like satellite relays: every position is kept, with no range limit and
no range or direction for the station
Expand Down
46 changes: 42 additions & 4 deletions service/src/aggregation/coverage-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ export class CoverageStore {
* position or a wrongly located station).
*/
public outOfRange: Record<string, { near: number; far: number }> = {};
/**
* The same discards per station, with the furthest distance seen. A station
* with many `far` discards is usually located in the wrong place (GeoIP) or
* relays a feed from elsewhere, which the per-family totals cannot show.
*/
private outOfRangeByStation = new Map<number, { near: number; far: number; maxDistanceKm: number }>();

constructor() {
// Initialize sliding windows for each resolution x window combination
Expand Down Expand Up @@ -155,7 +161,7 @@ export class CoverageStore {
// the event as proof the station is receiving and drop the position.
if (!isPlausibleRange(event.sourceType, distance)) {
this.discardPosition('out_of_range');
this.countOutOfRange(event.sourceType, distance);
this.countOutOfRange(event.sourceType, distance, event.stationId);
hasValidTarget = false;
directional = false;
distance = 0;
Expand Down Expand Up @@ -232,11 +238,43 @@ export class CoverageStore {
this.positionsDiscarded[reason] = (this.positionsDiscarded[reason] ?? 0) + 1;
}

private countOutOfRange(sourceType: string, distanceKm: number): void {
private countOutOfRange(sourceType: string, distanceKm: number, stationId: number): void {
const family = sourceFamily(sourceType);
const far = distanceKm > maxRangeKm(sourceType) * 2;
const entry = (this.outOfRange[family] ??= { near: 0, far: 0 });
if (distanceKm <= maxRangeKm(sourceType) * 2) entry.near++;
else entry.far++;
let station = this.outOfRangeByStation.get(stationId);
if (!station) {
station = { near: 0, far: 0, maxDistanceKm: 0 };
this.outOfRangeByStation.set(stationId, station);
}
if (far) {
entry.far++;
station.far++;
} else {
entry.near++;
station.near++;
}
if (distanceKm > station.maxDistanceKm) station.maxDistanceKm = distanceKm;
}

/** Stations with the most out-of-range positions since start, worst first */
getOutOfRangeStations(limit = 20): Array<{
stationId: number; ident: string; sourceType: string; near: number; far: number; maxDistanceKm: number;
}> {
return [...this.outOfRangeByStation]
.sort(([, a], [, b]) => (b.near + b.far) - (a.near + a.far))
.slice(0, limit)
.map(([stationId, counts]) => {
const meta = this.stationMap.get(stationId);
return {
stationId,
ident: meta?.ident ?? '',
sourceType: meta?.sourceType ?? '',
near: counts.near,
far: counts.far,
maxDistanceKm: Math.round(counts.maxDistanceKm),
};
});
}

private updateStationCoverage(
Expand Down
1 change: 1 addition & 0 deletions service/src/api/routes/health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ healthRoutes.get('/health', (c) => {
rejections: stats.rejections,
positionsDiscarded: stats.positionsDiscarded,
outOfRange: stats.outOfRange,
outOfRangeStations: store.getOutOfRangeStations(),
stationsTracked: stats.stationsTracked,
activeCells: stats.activeCells,
cellsInMemory: store.getCellsInMemory(),
Expand Down
4 changes: 4 additions & 0 deletions service/test/ingest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,5 +247,9 @@ describe('ingest', () => {
expect(store.eventsWithPosition).toBe(1);
expect(store.positionsDiscarded).toEqual({ out_of_range: 2 });
expect(store.outOfRange).toEqual({ ais: { near: 1, far: 1 } });
// and names the station they came from
const [worst] = store.getOutOfRangeStations();
expect(worst).toMatchObject({ stationId: 22, ident: 'TEST-22', sourceType: 'ais', near: 1, far: 1 });
expect(worst.maxDistanceKm).toBeGreaterThan(5000);
});
});