57 lines
3.0 KiB
TypeScript
57 lines
3.0 KiB
TypeScript
/** The manual REST contracts include a deferred, global alert-engine run.
|
|
* A scoped collector alone is therefore not an equivalent implementation. */
|
|
export interface ManualCollectionSettings {
|
|
lastCollectedAt?: string | null
|
|
lastDurationMs?: number | null
|
|
lastError?: string | null
|
|
}
|
|
export interface ManualDataCollectionDependencies {
|
|
collectServersRestPing: () => Promise<unknown>
|
|
collectUptime: () => Promise<unknown>
|
|
collectPingGroup: (probeIds: string[]) => Promise<{ polled: number }>
|
|
readServersRestPingSettings: () => Promise<ManualCollectionSettings>
|
|
readUptimeSettings: () => Promise<ManualCollectionSettings>
|
|
scheduleGlobalAlerts: () => void
|
|
}
|
|
|
|
function settingsReceipt(row: ManualCollectionSettings) {
|
|
return { ok: true as const, lastCollectedAt: row.lastCollectedAt ?? null,
|
|
lastDurationMs: row.lastDurationMs ?? null, lastError: row.lastError || null }
|
|
}
|
|
function uptimePart(raw:unknown,rows:"servers"|"probes"){
|
|
const value=raw&&typeof raw==="object"?raw as Record<string,unknown>:undefined
|
|
return {reported:!!value&&Array.isArray(value[rows]),skipped:value?.skipped===true,fatalError:!!value?.fatalError,count:Array.isArray(value?.[rows])?value[rows].length:0,
|
|
...(rows==="probes"?{skippedByInterval:typeof value?.skippedByInterval==="number"&&Number.isSafeInteger(value.skippedByInterval)&&value.skippedByInterval>=0?value.skippedByInterval:0}:{})}
|
|
}
|
|
|
|
/** No retries: collector completion may already have written samples, and the
|
|
* alert hook may already be queued even when reading the receipt fails. */
|
|
export function createManualDataCollection(deps: ManualDataCollectionDependencies) {
|
|
return {
|
|
collectServersRestPing: async () => {
|
|
// Ensure the singleton before the collector updates its run receipt.
|
|
await deps.readServersRestPingSettings()
|
|
const snapshot=await deps.collectServersRestPing()
|
|
deps.scheduleGlobalAlerts()
|
|
const skipped=!!snapshot&&typeof snapshot==="object"&&"skipped"in snapshot&&snapshot.skipped===true
|
|
return {...settingsReceipt(await deps.readServersRestPingSettings()),skipped}
|
|
},
|
|
collectUptime: async () => {
|
|
const snapshot=await deps.collectUptime()
|
|
deps.scheduleGlobalAlerts()
|
|
const value=snapshot&&typeof snapshot==="object"?snapshot as Record<string,unknown>:undefined
|
|
const parts={resources:uptimePart(value?.resources,"servers"),ping:uptimePart(value?.ping,"probes")}
|
|
const skipped=parts.resources.skipped||parts.ping.skipped
|
|
return {...settingsReceipt(await deps.readUptimeSettings()),parts,skipped,partial:skipped||Object.values(parts).some(part=>!part.reported||part.fatalError)}
|
|
},
|
|
collectPingGroup: async (probeIds: string[]) => {
|
|
// Deduplication, the 64-ID cap, shared ping mutex and source-enabled
|
|
// filtering belong to collectPingForProbeIds, for every caller.
|
|
const { polled } = await deps.collectPingGroup(probeIds)
|
|
if (polled > 0) deps.scheduleGlobalAlerts()
|
|
return { ok: true as const, polled }
|
|
},
|
|
}
|
|
}
|
|
export type ManualDataCollection = ReturnType<typeof createManualDataCollection>
|