9d32552457
Replace Vercel-specific infra (Cron, Blob) with self-hosted equivalents for deployment to Coolify: in-container cron scheduler (scripts/cron-runner.mjs) and disk-based upload storage served via /api/uploads/[...path]. Add Dockerfile, .dockerignore, and /api/health for container health checks. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
127 lines
5.6 KiB
TypeScript
127 lines
5.6 KiB
TypeScript
import Link from "next/link";
|
|
import { prisma } from "@/lib/db";
|
|
import { getConsoleContext, scopedPageIds } from "@/lib/console";
|
|
import { EmptyState, numberFmt, PageTitle } from "@/components/ui";
|
|
import { InsightsCharts } from "@/components/InsightsCharts";
|
|
import { RefreshInsightsButton } from "@/components/RefreshInsightsButton";
|
|
|
|
const RANGE_DAYS = 30;
|
|
|
|
export default async function InsightsPage() {
|
|
const ctx = await getConsoleContext();
|
|
const pageIds = scopedPageIds(ctx);
|
|
const since = new Date();
|
|
since.setUTCDate(since.getUTCDate() - RANGE_DAYS);
|
|
|
|
const [snapshots, topPosts] = await Promise.all([
|
|
prisma.pageInsightSnapshot.findMany({
|
|
where: { pageId: { in: pageIds }, date: { gte: since } },
|
|
orderBy: { date: "asc" },
|
|
select: { date: true, views: true, engagements: true, follows: true, followDelta: true },
|
|
}),
|
|
prisma.postInsightSnapshot.findMany({
|
|
where: { post: { pageId: { in: pageIds } }, date: { gte: since } },
|
|
orderBy: [{ date: "desc" }, { engaged: "desc" }],
|
|
distinct: ["postId"],
|
|
take: 50,
|
|
select: { impressions: true, engaged: true, reactions: true, post: { select: { id: true, title: true, page: { select: { name: true } } } } },
|
|
}),
|
|
]);
|
|
|
|
// Sum across pages per day ("ทุกเพจ" view).
|
|
const byDate = new Map<string, { date: string; views: number; engagements: number; follows: number; followDelta: number }>();
|
|
for (const s of snapshots) {
|
|
const key = s.date.toISOString().slice(0, 10);
|
|
const row = byDate.get(key) ?? { date: key, views: 0, engagements: 0, follows: 0, followDelta: 0 };
|
|
row.views += s.views;
|
|
row.engagements += s.engagements;
|
|
row.follows += s.follows;
|
|
row.followDelta += s.followDelta;
|
|
byDate.set(key, row);
|
|
}
|
|
const series = [...byDate.values()];
|
|
const last = series[series.length - 1];
|
|
const sum = (k: "views" | "engagements" | "followDelta") => series.reduce((a, r) => a + r[k], 0);
|
|
const top = [...topPosts].sort((a, b) => b.engaged - a.engaged).slice(0, 10);
|
|
|
|
return (
|
|
<>
|
|
<PageTitle
|
|
title="Insights"
|
|
subtitle={`${ctx.selected?.name ?? "ทุกเพจ"} · ${RANGE_DAYS} วันล่าสุด`}
|
|
actions={pageIds.length > 0 && <RefreshInsightsButton pageId={ctx.selected?.id ?? null} />}
|
|
/>
|
|
|
|
{series.length === 0 ? (
|
|
<section className="card">
|
|
<EmptyState icon="chart" title="ยังไม่มีข้อมูล insights" hint="ระบบเก็บข้อมูลอัตโนมัติทุกเช้า หรือกด “ดึงข้อมูลตอนนี้” เพื่อดูทันที" />
|
|
</section>
|
|
) : (
|
|
<>
|
|
<div className="grid gap-5 grid-cols-2 xl:grid-cols-4">
|
|
<div className="card card-body stat">
|
|
<span className="stat-label">ผู้ติดตามล่าสุด</span>
|
|
<span className="stat-value">{numberFmt.format(last.follows)}</span>
|
|
</div>
|
|
<div className="card card-body stat">
|
|
<span className="stat-label">ผู้ติดตามเพิ่ม/ลด</span>
|
|
<span className="stat-value" style={{ color: sum("followDelta") >= 0 ? "var(--m-green-ink)" : "var(--m-red)" }}>
|
|
{sum("followDelta") >= 0 ? "+" : ""}
|
|
{numberFmt.format(sum("followDelta"))}
|
|
</span>
|
|
</div>
|
|
<div className="card card-body stat">
|
|
<span className="stat-label">การเข้าชมเพจ</span>
|
|
<span className="stat-value">{numberFmt.format(sum("views"))}</span>
|
|
</div>
|
|
<div className="card card-body stat">
|
|
<span className="stat-label">Engagement</span>
|
|
<span className="stat-value">{numberFmt.format(sum("engagements"))}</span>
|
|
</div>
|
|
</div>
|
|
|
|
<InsightsCharts series={series} />
|
|
|
|
<section className="card">
|
|
<div className="card-head">
|
|
<h2>โพสต์ที่มี engagement สูงสุด</h2>
|
|
</div>
|
|
{top.length === 0 ? (
|
|
<EmptyState icon="post" title="ยังไม่มีข้อมูลรายโพสต์" />
|
|
) : (
|
|
<div className="overflow-x-auto">
|
|
<table className="table">
|
|
<thead>
|
|
<tr>
|
|
<th>โพสต์</th>
|
|
<th>เพจ</th>
|
|
<th className="text-right!">Impressions</th>
|
|
<th className="text-right!">Engaged</th>
|
|
<th className="text-right!">Reactions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{top.map((t) => (
|
|
<tr key={t.post.id}>
|
|
<td>
|
|
<Link href={`/posts/${t.post.id}`} className="font-medium hover:text-blue">
|
|
{t.post.title}
|
|
</Link>
|
|
</td>
|
|
<td className="text-ink-2">{t.post.page.name}</td>
|
|
<td className="num text-right">{numberFmt.format(t.impressions)}</td>
|
|
<td className="num text-right">{numberFmt.format(t.engaged)}</td>
|
|
<td className="num text-right">{numberFmt.format(t.reactions)}</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
)}
|
|
</section>
|
|
</>
|
|
)}
|
|
</>
|
|
);
|
|
}
|