Initial web

This commit is contained in:
Caesar2011
2026-05-17 19:55:53 +02:00
parent 6e3499812e
commit 20ed6ee9fb
58 changed files with 8541 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
import { NextRequest, NextResponse } from 'next/server';
import pool from '@/lib/db';
import { withAuth } from '@/lib/apiHelpers';
export const PUT = withAuth(async (req: NextRequest, { params }) => {
const { id } = await params;
const body = await req.json();
const { item_key, item_key_is_regex, combinator, signal_type, condition, threshold, active } = body;
const result = await pool.query(
`UPDATE alerts SET
item_key = COALESCE($1, item_key),
item_key_is_regex = COALESCE($2, item_key_is_regex),
combinator = $3,
signal_type = COALESCE($4, signal_type),
condition = COALESCE($5, condition),
threshold = COALESCE($6, threshold),
active = COALESCE($7, active)
WHERE id = $8
RETURNING *`,
[item_key, item_key_is_regex, combinator, signal_type, condition, threshold, active, id],
);
if (result.rowCount === 0) return NextResponse.json({ error: 'Not found' }, { status: 404 });
return NextResponse.json(result.rows[0]);
});
export const DELETE = withAuth(async (_req: NextRequest, { params }) => {
const { id } = await params;
const result = await pool.query('DELETE FROM alerts WHERE id = $1', [id]);
if (result.rowCount === 0) return NextResponse.json({ error: 'Not found' }, { status: 404 });
return NextResponse.json({ ok: true });
});

View 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);
});

View File

@@ -0,0 +1,30 @@
import { NextRequest, NextResponse } from 'next/server';
import pool from '@/lib/db';
import { withAuth } from '@/lib/apiHelpers';
export const GET = withAuth(async () => {
const result = await pool.query('SELECT * FROM alerts ORDER BY created_at DESC');
return NextResponse.json(result.rows);
});
export const POST = withAuth(async (req: NextRequest) => {
const body = await req.json();
const {
item_key, item_key_is_regex = false,
combinator = null, signal_type = 'green', condition, threshold,
} = body;
if (!item_key || !condition || threshold === undefined) {
return NextResponse.json(
{ error: 'item_key, condition, threshold required' },
{ status: 400 },
);
}
const result = await pool.query(
`INSERT INTO alerts (item_key, item_key_is_regex, combinator, signal_type, condition, threshold)
VALUES ($1,$2,$3,$4,$5,$6) RETURNING *`,
[item_key, item_key_is_regex, combinator, signal_type, condition, threshold],
);
return NextResponse.json(result.rows[0], { status: 201 });
});