8.5 월간집계: 월별 지표 산출 배치/차트/다운로드(CSV) o
This commit is contained in:
19
src/app/api/stats/monthly/route.ts
Normal file
19
src/app/api/stats/monthly/route.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const { searchParams } = new URL(req.url);
|
||||
const year = Number(searchParams.get("year")) || new Date().getFullYear();
|
||||
const month = Number(searchParams.get("month")) || new Date().getMonth() + 1; // 1-12
|
||||
const start = new Date(year, month - 1, 1);
|
||||
const end = new Date(year, month, 1);
|
||||
|
||||
const posts = await prisma.post.count({ where: { createdAt: { gte: start, lt: end } } });
|
||||
const comments = await prisma.comment.count({ where: { createdAt: { gte: start, lt: end } } });
|
||||
const users = await prisma.user.count({ where: { createdAt: { gte: start, lt: end } } });
|
||||
const reports = await prisma.report.count({ where: { createdAt: { gte: start, lt: end } } });
|
||||
|
||||
return NextResponse.json({ year, month, metrics: { posts, comments, users, reports } });
|
||||
}
|
||||
|
||||
|
||||
25
src/app/stats/monthly/page.tsx
Normal file
25
src/app/stats/monthly/page.tsx
Normal file
@@ -0,0 +1,25 @@
|
||||
"use client";
|
||||
import useSWR from "swr";
|
||||
|
||||
const fetcher = (url: string) => fetch(url).then((r) => r.json());
|
||||
|
||||
export default function MonthlyStatsPage({ searchParams }: { searchParams?: { year?: string; month?: string } }) {
|
||||
const year = searchParams?.year ?? String(new Date().getFullYear());
|
||||
const month = searchParams?.month ?? String(new Date().getMonth() + 1);
|
||||
const { data } = useSWR<{ year: number; month: number; metrics: { posts: number; comments: number; users: number; reports: number } }>(`/api/stats/monthly?year=${year}&month=${month}`, fetcher);
|
||||
const m = data?.metrics;
|
||||
return (
|
||||
<div>
|
||||
<h1>월간 집계</h1>
|
||||
<div>대상 월: {year}.{month}</div>
|
||||
<ul>
|
||||
<li>게시글: {m?.posts ?? 0}</li>
|
||||
<li>댓글: {m?.comments ?? 0}</li>
|
||||
<li>신규 사용자: {m?.users ?? 0}</li>
|
||||
<li>신고: {m?.reports ?? 0}</li>
|
||||
</ul>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user