22 lines
1.2 KiB
TypeScript
22 lines
1.2 KiB
TypeScript
import type { PingProbeWrite } from "@mmapp/contracts"
|
|
export type PingProbePatch = Omit<PingProbeWrite, "filter"> & { probeFilter: string; sortOrder: number; updatedAt: string }
|
|
export type PingProbeTransaction = {
|
|
listIds: () => Promise<string[]>
|
|
remove: (id: string) => Promise<void>
|
|
upsert: (row: PingProbePatch, createdAt: string) => Promise<void>
|
|
}
|
|
export type PingProbeStore = { transaction: (run: (tx: PingProbeTransaction) => Promise<void>) => Promise<void> }
|
|
export async function replacePingProbes(store: PingProbeStore, probes: PingProbeWrite[], 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()) {
|
|
const { filter, intervalSec, showOnDashboard, ...config } = row
|
|
await tx.upsert({ ...config, probeFilter: filter, sortOrder, updatedAt: now,
|
|
...(intervalSec === undefined ? {} : { intervalSec: intervalSec > 0 ? Math.max(5, intervalSec) : 0 }),
|
|
...(showOnDashboard === undefined ? {} : { showOnDashboard }),
|
|
}, now)
|
|
}
|
|
})
|
|
}
|