feat(admin): marketplace console + analytics degraded banners
Co-authored-by: Cursor <[email protected]>
This commit is contained in:
1 parent
f150aea8b9
commit
025af147cd
9 files changed
+373
-57
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import { MessageSquare, Shield, Terminal, Users } from "lucide-react";
|
||||
import { getLocale, getTranslations } from "next-intl/server";
|
||||
import { redirect } from "next/navigation";
|
||||
import { getLocale, getTranslations } from "next-intl/server";
|
||||
import { StatsCard } from "@/components/admin/stats-card";
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
||||
@@ -23,28 +23,35 @@ type ActivityData = {
|
||||
topChatters: { userId: number; username: string; messages: number }[];
|
||||
};
|
||||
|
||||
async function safeCount(fn: () => Promise<number>): Promise<number> {
|
||||
async function safeCount(
|
||||
fn: () => Promise<number>,
|
||||
): Promise<{ value: number; ok: boolean }> {
|
||||
try {
|
||||
return await fn();
|
||||
return { value: await fn(), ok: true };
|
||||
} catch {
|
||||
return 0;
|
||||
return { value: 0, ok: false };
|
||||
}
|
||||
}
|
||||
|
||||
async function safeRawCount(sql: Promise<{ count: bigint }[]>): Promise<number> {
|
||||
async function safeRawCount(
|
||||
sql: Promise<{ count: bigint }[]>,
|
||||
): Promise<{ value: number; ok: boolean }> {
|
||||
try {
|
||||
const rows = await sql;
|
||||
return Number(rows[0]?.count ?? 0);
|
||||
return { value: Number(rows[0]?.count ?? 0), ok: true };
|
||||
} catch {
|
||||
return 0;
|
||||
return { value: 0, ok: false };
|
||||
}
|
||||
}
|
||||
|
||||
async function getActivityData(locale: string): Promise<ActivityData> {
|
||||
async function getActivityData(
|
||||
locale: string,
|
||||
): Promise<ActivityData & { degraded: boolean }> {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const todayStart = now - (now % 86400);
|
||||
const weekAgo = now - 7 * 86400;
|
||||
const rangeStart = todayStart - 6 * 86400;
|
||||
let degraded = false;
|
||||
|
||||
const emptyDays: DayRow[] = [];
|
||||
for (let i = 6; i >= 0; i--) {
|
||||
@@ -99,7 +106,7 @@ async function getActivityData(locale: string): Promise<ActivityData> {
|
||||
banMap = toMap(banRows);
|
||||
regMap = toMap(regRows);
|
||||
} catch {
|
||||
/* keep empty maps */
|
||||
degraded = true;
|
||||
}
|
||||
|
||||
const dailyData = emptyDays.map((day, idx) => {
|
||||
@@ -132,11 +139,13 @@ async function getActivityData(locale: string): Promise<ActivityData> {
|
||||
),
|
||||
]);
|
||||
|
||||
for (const r of [weekChats, weekCommands, weekBans, weekRegistrations]) {
|
||||
if (!r.ok) degraded = true;
|
||||
}
|
||||
|
||||
let topChatters: ActivityData["topChatters"] = [];
|
||||
try {
|
||||
const rows = await prisma.$queryRaw<
|
||||
{ userId: number; messages: bigint }[]
|
||||
>`
|
||||
const rows = await prisma.$queryRaw<{ userId: number; messages: bigint }[]>`
|
||||
SELECT user_from_id AS userId, COUNT(*) AS messages
|
||||
FROM chatlogs_room WHERE timestamp >= ${weekAgo}
|
||||
GROUP BY user_from_id ORDER BY messages DESC LIMIT 10
|
||||
@@ -160,15 +169,17 @@ async function getActivityData(locale: string): Promise<ActivityData> {
|
||||
});
|
||||
} catch {
|
||||
topChatters = [];
|
||||
degraded = true;
|
||||
}
|
||||
|
||||
return {
|
||||
dailyData,
|
||||
weekChats,
|
||||
weekCommands,
|
||||
weekBans,
|
||||
weekRegistrations,
|
||||
weekChats: weekChats.value,
|
||||
weekCommands: weekCommands.value,
|
||||
weekBans: weekBans.value,
|
||||
weekRegistrations: weekRegistrations.value,
|
||||
topChatters,
|
||||
degraded,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -180,10 +191,16 @@ export default async function ActivityPage() {
|
||||
|
||||
const locale = await getLocale();
|
||||
const t = await getTranslations("pages.admin.analytics.activity");
|
||||
const tRoot = await getTranslations("pages.admin.analytics");
|
||||
const data = await getActivityData(locale);
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{data.degraded ? (
|
||||
<div className="rounded-lg bg-[var(--admin-error-subtle)] p-3 text-xs text-[var(--admin-error)]">
|
||||
{tRoot("loadError")}
|
||||
</div>
|
||||
) : null}
|
||||
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-4">
|
||||
<StatsCard
|
||||
title={t("chatMessages")}
|
||||
@@ -221,7 +238,9 @@ export default async function ActivityPage() {
|
||||
<table className="w-full text-sm">
|
||||
<thead>
|
||||
<tr className="border-b text-muted-foreground">
|
||||
<th className="text-left py-2 font-medium">{t("colDay")}</th>
|
||||
<th className="text-left py-2 font-medium">
|
||||
{t("colDay")}
|
||||
</th>
|
||||
<th className="text-right py-2 font-medium">
|
||||
{t("colChats")}
|
||||
</th>
|
||||
@@ -276,8 +295,7 @@ export default async function ActivityPage() {
|
||||
{i + 1}
|
||||
</span>
|
||||
<span className="flex-1 text-sm font-medium">
|
||||
{user.username ||
|
||||
t("userFallback", { id: user.userId })}
|
||||
{user.username || t("userFallback", { id: user.userId })}
|
||||
</span>
|
||||
<span className="text-sm tabular-nums text-muted-foreground">
|
||||
{t("msgCount", {
|
||||
|
||||
Reference in new issue
Block a user