add Statistik component and API route
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { query } from '@/lib/db';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const kuppel = searchParams.get('kuppel') || 'West';
|
||||
const year = parseInt(searchParams.get('year') || String(new Date().getFullYear()), 10);
|
||||
|
||||
try {
|
||||
// 1) Monatliche Besucherzahlen nach ArtFuehrung
|
||||
const monthlyRows = await query(
|
||||
`SELECT
|
||||
MONTH(Beginn) AS monat,
|
||||
ArtFuehrung,
|
||||
SUM(Besucher) AS besucher,
|
||||
COUNT(*) AS anzahl
|
||||
FROM logbuch
|
||||
WHERE Kuppel = ? AND YEAR(Beginn) = ?
|
||||
GROUP BY MONTH(Beginn), ArtFuehrung
|
||||
ORDER BY monat, ArtFuehrung`,
|
||||
[kuppel, year]
|
||||
) as { monat: number; ArtFuehrung: string; besucher: number; anzahl: number }[];
|
||||
|
||||
// 2) Kumulierte Besucher im Jahr
|
||||
const cumulativeRows = await query(
|
||||
`SELECT SUM(Besucher) AS total FROM logbuch WHERE Kuppel = ? AND YEAR(Beginn) = ?`,
|
||||
[kuppel, year]
|
||||
) as { total: number | null }[];
|
||||
|
||||
// 3) Anzahl Führungstage (distinct Datum)
|
||||
const tageRows = await query(
|
||||
`SELECT COUNT(DISTINCT DATE(Beginn)) AS tage FROM logbuch WHERE Kuppel = ? AND YEAR(Beginn) = ?`,
|
||||
[kuppel, year]
|
||||
) as { tage: number }[];
|
||||
|
||||
// 4) Kumulierte Besucher über alle Kuppeln
|
||||
const allCumulativeRows = await query(
|
||||
`SELECT SUM(Besucher) AS total FROM logbuch WHERE YEAR(Beginn) = ?`,
|
||||
[year]
|
||||
) as { total: number | null }[];
|
||||
|
||||
// 5) Führungstage über alle Kuppeln
|
||||
const allTageRows = await query(
|
||||
`SELECT COUNT(DISTINCT DATE(Beginn)) AS tage FROM logbuch WHERE YEAR(Beginn) = ?`,
|
||||
[year]
|
||||
) as { tage: number }[];
|
||||
|
||||
return NextResponse.json({
|
||||
monthly: monthlyRows.map((r) => ({
|
||||
monat: Number(r.monat),
|
||||
ArtFuehrung: r.ArtFuehrung,
|
||||
besucher: Number(r.besucher),
|
||||
anzahl: Number(r.anzahl),
|
||||
})),
|
||||
cumulative: Number(cumulativeRows[0]?.total ?? 0),
|
||||
tage: Number(tageRows[0]?.tage ?? 0),
|
||||
allCumulative: Number(allCumulativeRows[0]?.total ?? 0),
|
||||
allTage: Number(allTageRows[0]?.tage ?? 0),
|
||||
year,
|
||||
kuppel,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('GET /api/statistik:', error);
|
||||
return NextResponse.json({ error: 'Datenbankfehler' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user