Files
MikrotikManager/lib/speed-probe-snapshot.ts
T

16 lines
946 B
TypeScript

import { emptySpeedProbeObservations, type SpeedProbeRead } from "@mmapp/contracts"
type Observations = Pick<SpeedProbeRead, keyof typeof emptySpeedProbeObservations>
type Pair = { id: string; srcServerId: string; dstServerId: string }
/** Publish confirmed configuration while preserving the latest snapshot only for the same pair. */
export function publishSpeedProbeSnapshots<T extends Pair & Partial<Observations>>(previous: readonly T[], next: readonly T[]) {
const byId = new Map(previous.map(row => [row.id, row]))
return next.map(row => {
const old = byId.get(row.id)
if (!old || old.srcServerId !== row.srcServerId || old.dstServerId !== row.dstServerId) {
return { ...row, ...emptySpeedProbeObservations }
}
const observations = Object.fromEntries(Object.keys(emptySpeedProbeObservations).map(key => [key, old[key as keyof Observations] ?? null])) as Observations
return { ...row, ...observations }
})
}