import { listMonitors } from './monitors.ts'
import type { DatadogAuthOptions } from './core.ts'
export type ScheduledStatusInput = DatadogAuthOptions & {
pageSize?: number
}
/**
* Read-only monitor state counts. The same no-argument contract the disabled
* scheduled job uses. Never mutates Datadog.
*
* @example
* import status from 'kody:@kody/datadog/scheduled-status'
* const result = await status()
*/
export async function runMonitorStatus(input: ScheduledStatusInput = {}) {
const listed = await listMonitors({ ...input, pageSize: input.pageSize ?? 100 })
const byState: Record<string, number> = {}
for (const monitor of listed.monitors) {
const state = String(monitor.overallState || 'Unknown')
byState[state] = (byState[state] ?? 0) + 1
}
return {
ok: true,
count: listed.count,
byState,
alerting: listed.monitors
.filter((monitor) => {
const state = String(monitor.overallState || '').toLowerCase()
return state === 'alert' || state === 'warn' || state === 'warning'
})
.slice(0, 20)
.map((monitor) => ({
id: monitor.id,
name: monitor.name,
overallState: monitor.overallState,
})),
}
}
export default async function scheduledStatus(input: ScheduledStatusInput = {}) {
return await runMonitorStatus(input)
}