Files
peardata/server/services/alerts.js
T
Raven Scott 015d92a257
Release rolling / release (push) Has been cancelled
CI / test (push) Has been cancelled
first commit
2026-07-18 16:17:38 -04:00

52 lines
1.4 KiB
JavaScript

/**
* Alert state machine wrapping AnomalyEngine configs.
*/
import { getAnomalyEngine } from './anomaly.js'
/**
* @returns {import('../../shared/data-model.js').AlertState[]}
*/
export function listAlerts() {
const engine = getAnomalyEngine()
return engine.listConfigs().map((cfg) => {
const status = engine.status.get(cfg.id) || 'CLEAR'
return {
id: cfg.id,
name: cfg.id,
chart: cfg.chart,
dimension: cfg.dimension,
status,
value: null,
units: '',
info: cfg.info || '',
lastStatusChange: Date.now(),
config: cfg,
}
})
}
export function getAlert(id) {
return listAlerts().find((a) => a.id === id) || null
}
export function setAlertConfig(cfg) {
return getAnomalyEngine().setConfig(cfg)
}
/** Soft ack — clears active status until next breach. */
export function ackAlert(id) {
const engine = getAnomalyEngine()
if (!engine.configs.has(id)) return { success: false, error: 'unknown alert' }
engine.status.set(id, 'CLEAR')
return { success: true, id }
}
export function silenceAlert(id, opts = {}) {
const engine = getAnomalyEngine()
const cfg = engine.configs.get(id)
if (!cfg) return { success: false, error: 'unknown alert' }
const until = opts.until || Date.now() + (opts.ttlMs || 3600_000)
engine.setConfig({ ...cfg, enabled: false, _silencedUntil: until })
return { success: true, id, until }
}