mirror of
https://github.com/andrey271192/Domain_web.git
synced 2026-09-20 14:41:58 +00:00
feat: add Analytics page with scan stats API
Charts and JSON export for scan volume; completes core nav surface. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
51
src/app/api/analytics/stats/route.ts
Normal file
51
src/app/api/analytics/stats/route.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export async function GET() {
|
||||
const [total, completed, failed, last7] = await Promise.all([
|
||||
prisma.scan.count(),
|
||||
prisma.scan.count({ where: { status: "COMPLETED" } }),
|
||||
prisma.scan.count({ where: { status: "FAILED" } }),
|
||||
prisma.scan.findMany({
|
||||
where: {
|
||||
createdAt: { gte: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000) },
|
||||
},
|
||||
select: { createdAt: true, status: true },
|
||||
orderBy: { createdAt: "asc" },
|
||||
}),
|
||||
]);
|
||||
|
||||
const byDay = new Map<string, { scans: number; completed: number }>();
|
||||
for (const row of last7) {
|
||||
const key = row.createdAt.toISOString().slice(0, 10);
|
||||
const cur = byDay.get(key) ?? { scans: 0, completed: 0 };
|
||||
cur.scans += 1;
|
||||
if (row.status === "COMPLETED") cur.completed += 1;
|
||||
byDay.set(key, cur);
|
||||
}
|
||||
|
||||
const daily = [...byDay.entries()].map(([date, v]) => ({
|
||||
date,
|
||||
scans: v.scans,
|
||||
completed: v.completed,
|
||||
}));
|
||||
|
||||
const topDomains = await prisma.scan.groupBy({
|
||||
by: ["domain"],
|
||||
_count: { domain: true },
|
||||
orderBy: { _count: { domain: "desc" } },
|
||||
take: 8,
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
total,
|
||||
completed,
|
||||
failed,
|
||||
successRate: total ? Math.round((completed / total) * 100) : 0,
|
||||
daily,
|
||||
topDomains: topDomains.map((d) => ({
|
||||
domain: d.domain,
|
||||
count: d._count.domain,
|
||||
})),
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user