292 lines
9.4 KiB
TypeScript
292 lines
9.4 KiB
TypeScript
import os from "node:os";
|
|
import { redirect } from "next/navigation";
|
|
import { getTranslations } from "next-intl/server";
|
|
import {
|
|
DiagnosticRow,
|
|
InfoItem,
|
|
OnlineUsersWidget,
|
|
StatusCard,
|
|
} from "@/components/admin/dashboard";
|
|
import { fetchOpsHealth } from "@/lib/admin/ops-health";
|
|
import { fetchOpsOnlineUsers } from "@/lib/admin/ops-online-users";
|
|
import { formatDate } from "@/lib/format-date";
|
|
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { siteSettings } from "@/lib/services/site-settings";
|
|
import { CommandocentrumControls } from "./commandocentrum-controls";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
function decodeStacktrace(raw: unknown): string {
|
|
if (raw == null) return "";
|
|
try {
|
|
if (typeof raw === "string") return raw;
|
|
return Buffer.from(raw as Uint8Array).toString("utf8");
|
|
} catch {
|
|
return "";
|
|
}
|
|
}
|
|
|
|
function mb(bytes: number): string {
|
|
return `${Math.round(bytes / 1024 / 1024)} MB`;
|
|
}
|
|
|
|
function uptime(seconds: number): string {
|
|
const d = Math.floor(seconds / 86400);
|
|
const h = Math.floor((seconds % 86400) / 3600);
|
|
const m = Math.floor((seconds % 3600) / 60);
|
|
return d > 0 ? `${d}d ${h}h ${m}m` : h > 0 ? `${h}h ${m}m` : `${m}m`;
|
|
}
|
|
|
|
export default async function CommandoCentrum() {
|
|
const { session, permissions } = await getAdminContext();
|
|
if (!canAccess(permissions, PERMS.RCON_EXECUTE, session.user.rank)) {
|
|
redirect("/admin");
|
|
}
|
|
|
|
const t = await getTranslations("pages.admin.commandocentrum");
|
|
|
|
const [totalUsers, errors, health, maintenance, activity, onlineRoster] =
|
|
await Promise.all([
|
|
prisma.user.count().catch(() => -1),
|
|
prisma.emulatorErrors
|
|
.findMany({ orderBy: { timestamp: "desc" }, take: 50 })
|
|
.catch(() => []),
|
|
fetchOpsHealth(),
|
|
siteSettings.getBool("maintenance_enabled", false),
|
|
prisma.staffActivities
|
|
.findMany({ orderBy: { createdAt: "desc" }, take: 12 })
|
|
.catch(
|
|
() =>
|
|
[] as Awaited<ReturnType<typeof prisma.staffActivities.findMany>>,
|
|
),
|
|
fetchOpsOnlineUsers(40),
|
|
]);
|
|
|
|
const dbOk = health.dbOk;
|
|
const emulatorOnline = health.emulatorOk;
|
|
const onlineCount = onlineRoster.count;
|
|
const onlineUsers = onlineRoster.users;
|
|
|
|
const staffIds = Array.from(
|
|
new Set(activity.map((a) => Number(a.userId))),
|
|
).filter((n) => n > 0);
|
|
let nameById = new Map<number, string>();
|
|
if (staffIds.length) {
|
|
const users = await prisma.user
|
|
.findMany({
|
|
where: { id: { in: staffIds } },
|
|
select: { id: true, username: true },
|
|
})
|
|
.catch(() => []);
|
|
nameById = new Map(users.map((u) => [u.id, u.username]));
|
|
}
|
|
|
|
const appKeySet = !!process.env.APP_KEY;
|
|
const smtpSet = !!process.env.SMTP_HOST;
|
|
const rconSet = !!process.env.RCON_HOST;
|
|
|
|
return (
|
|
<main>
|
|
{/* ── Live status ────────────────────────────────────────── */}
|
|
<div
|
|
className="grid grid-cols-[repeat(auto-fit,minmax(180px,1fr))] gap-3.5 mb-6"
|
|
style={{ marginTop: "1.1rem" }}
|
|
>
|
|
<StatusCard
|
|
label={t("playersOnline")}
|
|
value={onlineCount}
|
|
hint={
|
|
totalUsers >= 0
|
|
? t("totalAccounts", { count: totalUsers })
|
|
: t("dbUnavailable")
|
|
}
|
|
state="neutral"
|
|
icon="👤"
|
|
/>
|
|
<StatusCard
|
|
label={t("emulator")}
|
|
value={emulatorOnline ? t("online") : t("offline")}
|
|
hint={emulatorOnline ? t("rconReachable") : t("rconNotResponding")}
|
|
state={emulatorOnline ? "ok" : "danger"}
|
|
icon="🎮"
|
|
/>
|
|
<StatusCard
|
|
label={t("database")}
|
|
value={dbOk ? t("connected") : t("down")}
|
|
hint={dbOk ? t("queriesResponding") : t("cannotReachDb")}
|
|
state={dbOk ? "ok" : "danger"}
|
|
icon="🗄️"
|
|
/>
|
|
<StatusCard
|
|
label={t("emulatorErrors")}
|
|
value={errors.length}
|
|
hint={t("last50Logged")}
|
|
state={errors.length === 0 ? "ok" : "warn"}
|
|
icon="⚠️"
|
|
/>
|
|
</div>
|
|
|
|
{/* ── Online users widget ───────────────────────────────── */}
|
|
<section id="online" className="admin-card mb-6 scroll-mt-4">
|
|
<h2 className="admin-section-title">{t("onlineUsers")}</h2>
|
|
<OnlineUsersWidget users={onlineUsers} isLoading={false} />
|
|
</section>
|
|
|
|
{/* ── Server info + diagnostics ──────────────────────────── */}
|
|
<div className="grid grid-cols-1 gap-4 md:grid-cols-2">
|
|
<section className="admin-card p-5">
|
|
<h2 className="admin-section-title">{t("serverInfo")}</h2>
|
|
<div className="grid grid-cols-[repeat(auto-fit,minmax(150px,1fr))] gap-2.5">
|
|
<InfoItem label={t("node")} value={process.version} />
|
|
<InfoItem
|
|
label={t("platform")}
|
|
value={`${process.platform} ${process.arch}`}
|
|
/>
|
|
<InfoItem label={t("appUptime")} value={uptime(process.uptime())} />
|
|
<InfoItem
|
|
label={t("heapUsed")}
|
|
value={mb(process.memoryUsage().heapUsed)}
|
|
/>
|
|
<InfoItem label={t("rss")} value={mb(process.memoryUsage().rss)} />
|
|
<InfoItem label={t("hostUptime")} value={uptime(os.uptime())} />
|
|
<InfoItem
|
|
label={t("load1m")}
|
|
value={os.loadavg()[0]?.toFixed(2) ?? "—"}
|
|
/>
|
|
<InfoItem label={t("cpus")} value={os.cpus().length} />
|
|
</div>
|
|
</section>
|
|
|
|
<section className="admin-card p-5">
|
|
<h2 className="admin-section-title">{t("systemDiagnostics")}</h2>
|
|
<div className="flex flex-col">
|
|
<DiagnosticRow
|
|
label={t("database")}
|
|
detail={dbOk ? t("connectionHealthy") : t("unreachable")}
|
|
state={dbOk ? "ok" : "danger"}
|
|
/>
|
|
<DiagnosticRow
|
|
label={t("emulatorRcon")}
|
|
detail={emulatorOnline ? t("respondingToPing") : t("noResponse")}
|
|
state={emulatorOnline ? "ok" : "warn"}
|
|
/>
|
|
<DiagnosticRow
|
|
label={t("rconConfig")}
|
|
detail={rconSet ? t("rconHostSet") : t("rconHostNotConfigured")}
|
|
state={rconSet ? "ok" : "warn"}
|
|
/>
|
|
<DiagnosticRow
|
|
label={t("twoFaEncryptionKey")}
|
|
detail={appKeySet ? t("appKeyConfigured") : t("appKeyMissing")}
|
|
state={appKeySet ? "ok" : "warn"}
|
|
/>
|
|
<DiagnosticRow
|
|
label={t("emailSmtp")}
|
|
detail={smtpSet ? t("smtpHostSet") : t("notConfigured")}
|
|
state={smtpSet ? "ok" : "warn"}
|
|
/>
|
|
<DiagnosticRow
|
|
label={t("maintenanceMode")}
|
|
detail={maintenance ? t("siteIsInMaintenance") : t("siteIsLive")}
|
|
state={maintenance ? "warn" : "ok"}
|
|
/>
|
|
</div>
|
|
</section>
|
|
</div>
|
|
|
|
<CommandocentrumControls />
|
|
|
|
{/* ── Staff activity ─────────────────────────────────────── */}
|
|
<section className="mt-6">
|
|
<h2 className="admin-section-title">{t("recentStaffActivity")}</h2>
|
|
{activity.length === 0 ? (
|
|
<div className="admin-empty">{t("noStaffActivity")}</div>
|
|
) : (
|
|
<div className="admin-card overflow-x-auto">
|
|
<table>
|
|
<thead>
|
|
<tr>
|
|
<th>{t("colWhen")}</th>
|
|
<th>{t("colStaff")}</th>
|
|
<th>{t("colAction")}</th>
|
|
<th>{t("colDescription")}</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{activity.map((a) => (
|
|
<tr key={String(a.id)}>
|
|
<td className="text-sm theme-text-muted dark:theme-text-muted whitespace-nowrap">
|
|
{formatDate(a.createdAt, "datetime-seconds")}
|
|
</td>
|
|
<td>{nameById.get(Number(a.userId)) ?? `#${a.userId}`}</td>
|
|
<td>
|
|
<span className="inline-block text-[0.6rem] font-bold px-2 py-0.5 rounded-full bg-[var(--admin-accent)]/18 text-[var(--admin-text)]">
|
|
{a.action}
|
|
</span>
|
|
</td>
|
|
<td className="text-sm theme-text-muted dark:theme-text-muted">
|
|
{a.description}
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
)}
|
|
</section>
|
|
|
|
{/* ── Emulator errors ────────────────────────────────────── */}
|
|
<section className="mt-6">
|
|
<h2 className="admin-section-title">{t("recentEmulatorErrors")}</h2>
|
|
{errors.length === 0 ? (
|
|
<div className="admin-empty">{t("noEmulatorErrors")}</div>
|
|
) : (
|
|
<div className="admin-card overflow-x-auto">
|
|
<table>
|
|
<thead>
|
|
<tr>
|
|
<th>{t("colWhen")}</th>
|
|
<th>{t("colType")}</th>
|
|
<th>{t("colVersion")}</th>
|
|
<th>{t("colStacktrace")}</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{errors.map((e) => {
|
|
const trace = decodeStacktrace(e.stacktrace);
|
|
const snippet = trace
|
|
? trace.split("\n").slice(0, 20).join("\n") +
|
|
(trace.split("\n").length > 20
|
|
? `\n… ${t("truncated")}`
|
|
: "")
|
|
: t("empty");
|
|
return (
|
|
<tr key={e.id}>
|
|
<td className="text-sm theme-text-muted dark:theme-text-muted whitespace-nowrap">
|
|
{formatDate(
|
|
e.timestamp ? new Date(e.timestamp * 1000) : null,
|
|
"datetime-seconds",
|
|
)}
|
|
</td>
|
|
<td>{e.type}</td>
|
|
<td className="text-sm theme-text-muted dark:theme-text-muted whitespace-nowrap">
|
|
{e.version}
|
|
</td>
|
|
<td>
|
|
<pre className="text-xs p-2 bg-[var(--admin-canvas)] text-[var(--color-text-readable)] rounded overflow-auto max-h-[160px]">
|
|
{snippet}
|
|
</pre>
|
|
</td>
|
|
</tr>
|
|
);
|
|
})}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
)}
|
|
</section>
|
|
</main>
|
|
);
|
|
}
|