fix(uptime): сбросить снимок при смене пары и сохранить историю
This commit is contained in:
1 parent
8627459bfa
commit
e4f951175c
12 files changed
+142
-20
No files matched your search
@@ -1,16 +1,21 @@
|
||||
import { matchesSpeedProbeEndpoints, type SpeedProbeEndpoints } from "./speed-run-persistence.js"
|
||||
import type { SpeedProbeWrite } from "@mmapp/contracts"
|
||||
export type SpeedProbePatch = SpeedProbeWrite & { sortOrder: number; updatedAt: string }
|
||||
export type SpeedProbeTransaction = {
|
||||
listIds: () => Promise<string[]>
|
||||
listProbes: () => Promise<SpeedProbeEndpoints[]>
|
||||
remove: (id: string) => Promise<void>
|
||||
upsert: (row: SpeedProbePatch, createdAt: string) => Promise<void>
|
||||
upsert: (row: SpeedProbePatch, createdAt: string, resetObservations: boolean) => Promise<void>
|
||||
}
|
||||
export type SpeedProbeStore = { transaction: (run: (tx: SpeedProbeTransaction) => Promise<void>) => Promise<void> }
|
||||
/** The adapter must lock the catalog and commit all configuration changes atomically. Observations are never written here. */
|
||||
/** The adapter must lock the catalog and commit all configuration changes atomically. Observations are preserved for the same pair and reset when its endpoints change. */
|
||||
export async function replaceSpeedProbes(store: SpeedProbeStore, probes: SpeedProbeWrite[], now = new Date().toISOString()): Promise<void> {
|
||||
await store.transaction(async tx => {
|
||||
const nextIds = new Set(probes.map(row => row.id))
|
||||
for (const id of await tx.listIds()) if (!nextIds.has(id)) await tx.remove(id)
|
||||
for (const [sortOrder, row] of probes.entries()) await tx.upsert({ ...row, sortOrder, updatedAt: now }, now)
|
||||
const previous = new Map((await tx.listProbes()).map(row => [row.id, row]))
|
||||
for (const id of previous.keys()) if (!nextIds.has(id)) await tx.remove(id)
|
||||
for (const [sortOrder, row] of probes.entries()) {
|
||||
const reset = !matchesSpeedProbeEndpoints(previous.get(row.id), row)
|
||||
await tx.upsert({ ...row, sortOrder, updatedAt: now }, now, reset)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user