Initial web
This commit is contained in:
62
web/app/api/alerts/check/route.ts
Normal file
62
web/app/api/alerts/check/route.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import pool from '@/lib/db';
|
||||
import { withAuth } from '@/lib/apiHelpers';
|
||||
import { getServerLocaleMap } from '@/lib/localeServer';
|
||||
import { resolveName } from '@/lib/localization';
|
||||
import type { AlertConfig, TriggeredAlert } from '@/lib/types';
|
||||
|
||||
export const GET = withAuth(async () => {
|
||||
const alertsResult = await pool.query<AlertConfig>(
|
||||
`SELECT id, item_key, item_key_is_regex, combinator, signal_type, condition, threshold
|
||||
FROM alerts WHERE active = true`,
|
||||
);
|
||||
if (alertsResult.rows.length === 0) return NextResponse.json([]);
|
||||
|
||||
const latestResult = await pool.query<{
|
||||
combinator: string; item_key: string; green: number; red: number;
|
||||
}>(
|
||||
`SELECT DISTINCT ON (combinator, item_key) combinator, item_key, green, red
|
||||
FROM signals
|
||||
ORDER BY combinator, item_key, real_time DESC`,
|
||||
);
|
||||
|
||||
const localeMap = getServerLocaleMap();
|
||||
|
||||
const latestMap = new Map(
|
||||
latestResult.rows.map(r => [`${r.combinator}::${r.item_key}`, r]),
|
||||
);
|
||||
|
||||
const triggered: TriggeredAlert[] = [];
|
||||
|
||||
for (const alert of alertsResult.rows) {
|
||||
for (const [key, vals] of latestMap) {
|
||||
const [combinator, item_key] = key.split('::');
|
||||
|
||||
let itemMatch: boolean;
|
||||
if (alert.item_key_is_regex) {
|
||||
try {
|
||||
const re = new RegExp(alert.item_key, 'i');
|
||||
// Test against raw key and localized name
|
||||
itemMatch = re.test(item_key) || re.test(resolveName(item_key, localeMap));
|
||||
} catch {
|
||||
itemMatch = false;
|
||||
}
|
||||
} else {
|
||||
itemMatch = item_key === alert.item_key;
|
||||
}
|
||||
|
||||
if (!itemMatch || (alert.combinator && combinator !== alert.combinator)) continue;
|
||||
|
||||
const value = alert.signal_type === 'green' ? vals.green : vals.red;
|
||||
const fired = alert.condition === 'above' ? value > alert.threshold : value < alert.threshold;
|
||||
if (fired) triggered.push({
|
||||
...alert,
|
||||
current_value: value,
|
||||
combinator_match: combinator,
|
||||
matched_item_key: item_key,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json(triggered);
|
||||
});
|
||||
Reference in New Issue
Block a user