Your Name 18149083d4
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled
feat: add realtime canvas feature with admin db config
2026-02-22 02:57:32 +08:00

55 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { auth } from "@/app/(auth)/auth";
type MetricEvent = {
name: string;
ts: number;
seq?: number;
latencyMs?: number;
queueDepth?: number;
networkType?: string;
deviceClass?: string;
};
type MetricsPayload = {
sessionId: string;
events: MetricEvent[];
};
export async function POST(request: Request) {
const session = await auth();
if (!session?.user?.id) {
return Response.json({ code: "UNAUTHORIZED", message: "请先登录" }, { status: 401 });
}
let payload: MetricsPayload;
try {
payload = (await request.json()) as MetricsPayload;
} catch {
return Response.json({ code: "BAD_REQUEST", message: "无效 JSON" }, { status: 400 });
}
if (!payload?.sessionId || !Array.isArray(payload.events)) {
return Response.json({ code: "BAD_REQUEST", message: "缺少 sessionId 或 events" }, { status: 400 });
}
if (payload.events.length > 200) {
return Response.json(
{ code: "PAYLOAD_TOO_LARGE", message: "events 超过限制(200)" },
{ status: 413 }
);
}
if (payload.events.length > 0) {
const latest = payload.events[payload.events.length - 1];
console.log("[RealtimeMetrics]", {
userId: session.user.id,
sessionId: payload.sessionId,
count: payload.events.length,
latest,
});
}
return Response.json({ ok: true });
}