mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
Format live source modules and adopt their formatting checks
This commit is contained in:
1 parent
e968053d2c
commit
80c5a09f15
7 files changed
+537
-129
No files matched your search
+64
-20
@@ -3,49 +3,93 @@ import { coordinates, epoch, finite, LiveSourceError } from './contract.js';
|
||||
/** AIS positions are sea-surface observations; heading and course are distinct. */
|
||||
export function normalizeVesselObservation(row, reference = null) {
|
||||
const id = String(row?.mmsi || row?.input_identifier || '').trim();
|
||||
const latitude = finite(row?.lat), longitude = finite(row?.lon);
|
||||
const latitude = finite(row?.lat),
|
||||
longitude = finite(row?.lon);
|
||||
if (!id || !coordinates(latitude, longitude)) return null;
|
||||
const speedKts = finite(row.speed);
|
||||
return {
|
||||
id, reference: reference ?? id, latitude, longitude,
|
||||
id,
|
||||
reference: reference ?? id,
|
||||
latitude,
|
||||
longitude,
|
||||
name: String(row.name || row.input_name || id),
|
||||
imo: String(row.imo || ''), type: String(row.type_specific || row.type || ''),
|
||||
imo: String(row.imo || ''),
|
||||
type: String(row.type_specific || row.type || ''),
|
||||
destination: String(row.destination || ''),
|
||||
speedMps: speedKts == null ? null : speedKts * 0.514444,
|
||||
courseDeg: finite(row.course), headingDeg: finite(row.heading),
|
||||
observedAtMs: epoch(row.last_position_epoch, 1000)
|
||||
?? epoch(Date.parse(row.last_position_UTC)),
|
||||
courseDeg: finite(row.course),
|
||||
headingDeg: finite(row.heading),
|
||||
observedAtMs:
|
||||
epoch(row.last_position_epoch, 1000) ??
|
||||
epoch(Date.parse(row.last_position_UTC)),
|
||||
altitudeDatum: 'sea-surface',
|
||||
};
|
||||
}
|
||||
|
||||
export function vesselSnapshot(payload, { source = 'AISStream', coverage = 'received AIS positions', referenceFor = row => String(row.mmsi || row.input_identifier || '') } = {}) {
|
||||
if (!Array.isArray(payload?.rows)) throw new LiveSourceError('malformed', 'Malformed vessel response');
|
||||
export function vesselSnapshot(
|
||||
payload,
|
||||
{
|
||||
source = 'AISStream',
|
||||
coverage = 'received AIS positions',
|
||||
referenceFor = (row) => String(row.mmsi || row.input_identifier || ''),
|
||||
} = {},
|
||||
) {
|
||||
if (!Array.isArray(payload?.rows))
|
||||
throw new LiveSourceError('malformed', 'Malformed vessel response');
|
||||
const rows = payload.rows;
|
||||
const records = [], ids = new Set();
|
||||
const records = [],
|
||||
ids = new Set();
|
||||
for (const row of rows) {
|
||||
const record = normalizeVesselObservation(row, referenceFor(row));
|
||||
if (record && !ids.has(record.id)) { records.push(record); ids.add(record.id); }
|
||||
if (record && !ids.has(record.id)) {
|
||||
records.push(record);
|
||||
ids.add(record.id);
|
||||
}
|
||||
}
|
||||
const observedAtMs = epoch(payload?.newestPositionAt) ?? epoch(Date.parse(payload?.newestPositionAt))
|
||||
?? records.reduce((newest, row) => Math.max(newest || 0, row.observedAtMs || 0) || null, null);
|
||||
const observedAtMs =
|
||||
epoch(payload?.newestPositionAt) ??
|
||||
epoch(Date.parse(payload?.newestPositionAt)) ??
|
||||
records.reduce(
|
||||
(newest, row) => Math.max(newest || 0, row.observedAtMs || 0) || null,
|
||||
null,
|
||||
);
|
||||
return {
|
||||
records, source, coverage, complete: false, rejectedCount: rows.length - records.length,
|
||||
observedAtMs, freshness: payload?.refreshing ? 'stale' : observedAtMs == null ? 'unknown' : 'current',
|
||||
records,
|
||||
source,
|
||||
coverage,
|
||||
complete: false,
|
||||
rejectedCount: rows.length - records.length,
|
||||
observedAtMs,
|
||||
freshness: payload?.refreshing
|
||||
? 'stale'
|
||||
: observedAtMs == null
|
||||
? 'unknown'
|
||||
: 'current',
|
||||
stale: Boolean(payload?.refreshing),
|
||||
// Connection state is distinct from receipt of a usable position.
|
||||
transportStatus: typeof payload?.status === 'string' ? payload.status : null,
|
||||
transportStatus:
|
||||
typeof payload?.status === 'string' ? payload.status : null,
|
||||
lastMessageAt: payload?.lastMessageAt ?? null,
|
||||
nextAttemptAt: finite(payload?.nextAttemptAt), silentForMs: finite(payload?.silentForMs),
|
||||
reconnectAttempt: finite(payload?.reconnectAttempt), rawRowCount: rows.length,
|
||||
nextAttemptAt: finite(payload?.nextAttemptAt),
|
||||
silentForMs: finite(payload?.silentForMs),
|
||||
reconnectAttempt: finite(payload?.reconnectAttempt),
|
||||
rawRowCount: rows.length,
|
||||
};
|
||||
}
|
||||
|
||||
export function normalizeVesselTrack(samples) {
|
||||
if (!Array.isArray(samples)) return [];
|
||||
return samples.flatMap(sample => {
|
||||
const latitude = finite(sample?.lat), longitude = finite(sample?.lon);
|
||||
return samples.flatMap((sample) => {
|
||||
const latitude = finite(sample?.lat),
|
||||
longitude = finite(sample?.lon);
|
||||
if (!coordinates(latitude, longitude)) return [];
|
||||
return [{ latitude, longitude, observedAtMs: epoch(sample?.t, 1000), altitudeDatum: 'sea-surface' }];
|
||||
return [
|
||||
{
|
||||
latitude,
|
||||
longitude,
|
||||
observedAtMs: epoch(sample?.t, 1000),
|
||||
altitudeDatum: 'sea-surface',
|
||||
},
|
||||
];
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user