This commit is contained in:
jpmvaz
2026-09-13 20:20:06 +01:00
commit 6994063c71
93 changed files with 7613 additions and 0 deletions
@@ -0,0 +1,65 @@
export const dynamic = 'force-dynamic';
import { NextResponse } from 'next/server';
import prisma from '@/lib/db';
import { invalidateSettingsCache } from '@/lib/settings';
export async function GET() {
try {
let settings = await prisma.settings.findUnique({
where: { id: 'main' },
});
if (!settings) {
settings = await prisma.settings.create({
data: {
id: 'main',
apiKey: process.env.API_FOOTBALL_API_KEY || null,
},
});
}
return NextResponse.json(settings);
} catch (error) {
console.error('Failed to get settings:', error);
return NextResponse.json(
{ error: 'Failed to get settings' },
{ status: 500 }
);
}
}
export async function PUT(request: Request) {
try {
const data = await request.json();
// Update league settings based on selection
let leagueId = data.leagueId;
let season = data.season;
if (data.selectedLeague === 'euro_2026') {
leagueId = 4; // EURO Championship
season = 2024;
} else if (data.selectedLeague === 'primeira_liga') {
leagueId = 94;
season = 2024;
}
const settings = await prisma.settings.upsert({
where: { id: 'main' },
update: { ...data, leagueId, season },
create: { id: 'main', ...data, leagueId, season },
});
// Bust the settings cache so API routes pick up the new values immediately
invalidateSettingsCache();
return NextResponse.json(settings);
} catch (error) {
console.error('Failed to update settings:', error);
return NextResponse.json(
{ error: 'Failed to update settings' },
{ status: 500 }
);
}
}
+69
View File
@@ -0,0 +1,69 @@
export const dynamic = 'force-dynamic';
import { NextResponse } from 'next/server';
import prisma from '@/lib/db';
export async function GET() {
try {
const twentyFourHoursAgo = new Date(Date.now() - 24 * 60 * 60 * 1000);
const startOfDay = new Date();
startOfDay.setHours(0, 0, 0, 0);
// Run all queries in parallel instead of sequentially
const [
totalRequests,
requestsByEndpoint,
recentRequests,
avgDuration,
successfulRequests,
requestsToday,
] = await Promise.all([
prisma.apiRequestLog.count(),
prisma.apiRequestLog.groupBy({
by: ['endpoint'],
_count: { id: true },
}),
prisma.apiRequestLog.findMany({
where: { createdAt: { gte: twentyFourHoursAgo } },
orderBy: { createdAt: 'asc' },
select: { createdAt: true }, // only fetch what we need
}),
prisma.apiRequestLog.aggregate({ _avg: { duration: true } }),
prisma.apiRequestLog.count({ where: { status: { gte: 200, lt: 300 } } }),
prisma.apiRequestLog.count({ where: { createdAt: { gte: startOfDay } } }),
]);
// Group by hour
const hourlyStats: Record<string, number> = {};
for (const req of recentRequests) {
const hour = new Date(req.createdAt).toISOString().slice(0, 13) + ':00';
hourlyStats[hour] = (hourlyStats[hour] ?? 0) + 1;
}
const chartData = Object.entries(hourlyStats).map(([time, count]) => ({
time,
count,
}));
const successRate =
totalRequests > 0 ? (successfulRequests / totalRequests) * 100 : 100;
return NextResponse.json({
totalRequests,
requestsToday,
requestsByEndpoint: requestsByEndpoint.map((r) => ({
endpoint: r.endpoint,
count: r._count.id,
})),
chartData,
avgResponseTime: Math.round(avgDuration._avg.duration ?? 0),
successRate: Math.round(successRate * 100) / 100,
});
} catch (error) {
console.error('Failed to get stats:', error);
return NextResponse.json(
{ error: 'Failed to get stats' },
{ status: 500 }
);
}
}