38 lines
1.0 KiB
TypeScript
38 lines
1.0 KiB
TypeScript
import type { DatabaseSync } from 'node:sqlite';
|
|||
|
|
import type { Alert } from '../AlertEngine.ts';
|
||
|
|
import type { AlertProducer, ProducerFrequency } from './types.ts';
|
||
|
|
|
||
|
|
export type { AlertProducer, ProducerFrequency } from './types.ts';
|
||
|
|
|
||
|
|
const producers: AlertProducer[] = [];
|
||
|
|
|
||
|
|
/** Register a producer (called once at startup). */
|
||
|
|
export function registerProducer(p: AlertProducer): void {
|
||
|
|
producers.push(p);
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Run all producers for a given frequency tier. */
|
||
|
|
export async function runProducers(
|
||
|
|
db: DatabaseSync,
|
||
|
|
frequency: ProducerFrequency,
|
||
|
|
): Promise<Alert[]> {
|
||
|
|
const results: Alert[] = [];
|
||
|
|
const tier = producers.filter((p) => p.frequency === frequency);
|
||
|
|
|
||
|
|
for (const producer of tier) {
|
||
|
|
try {
|
||
|
|
const alerts = await producer.check(db);
|
||
|
|
results.push(...alerts);
|
||
|
|
} catch (err) {
|
||
|
|
console.error(`[alert:${producer.alertType}] check failed:`, err);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return results;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Get all registered producer types. */
|
||
|
|
export function registeredAlertTypes(): string[] {
|
||
|
|
return producers.map((p) => p.alertType);
|
||
|
|
}
|