- Add getTopWafRulesWithHosts() and getWafEventCountries() model queries - WAF stats API now returns topRules with per-host breakdown and byCountry - Analytics: replace WAF rules table with bar chart + host chip details - Analytics: add WAF column (amber) to Top Countries table Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
28 lines
1.1 KiB
TypeScript
28 lines
1.1 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { requireUser } from '@/src/lib/auth';
|
|
import { INTERVAL_SECONDS } from '@/src/lib/analytics-db';
|
|
import { countWafEventsInRange, getTopWafRulesWithHosts, getWafEventCountries } from '@/src/lib/models/waf-events';
|
|
|
|
function resolveRange(params: URLSearchParams): { from: number; to: number } {
|
|
const fromParam = params.get('from');
|
|
const toParam = params.get('to');
|
|
if (fromParam && toParam) {
|
|
return { from: parseInt(fromParam, 10), to: parseInt(toParam, 10) };
|
|
}
|
|
const interval = params.get('interval') ?? '1h';
|
|
const to = Math.floor(Date.now() / 1000);
|
|
const from = to - (INTERVAL_SECONDS[interval as keyof typeof INTERVAL_SECONDS] ?? INTERVAL_SECONDS['1h']);
|
|
return { from, to };
|
|
}
|
|
|
|
export async function GET(req: NextRequest) {
|
|
await requireUser();
|
|
const { from, to } = resolveRange(req.nextUrl.searchParams);
|
|
const [total, topRules, byCountry] = await Promise.all([
|
|
countWafEventsInRange(from, to),
|
|
getTopWafRulesWithHosts(from, to, 10),
|
|
getWafEventCountries(from, to),
|
|
]);
|
|
return NextResponse.json({ total, topRules, byCountry });
|
|
}
|