419 lines
14 KiB
TypeScript
419 lines
14 KiB
TypeScript
"use client";
|
||
|
||
// Hub introspection: gateway health, frame window, credentials, audit trail.
|
||
|
||
import { Activity, Cable, EyeOff, KeyRound, ScrollText, Settings } from "lucide-react";
|
||
import Link from "next/link";
|
||
import { useState } from "react";
|
||
|
||
import { useLive } from "@/components/LiveProvider";
|
||
import {
|
||
Badge,
|
||
CopyButton,
|
||
Dot,
|
||
Empty,
|
||
ErrorNote,
|
||
Field,
|
||
Input,
|
||
JsonBlock,
|
||
KV,
|
||
Pager,
|
||
Panel,
|
||
PixelButton,
|
||
Select,
|
||
Spinner,
|
||
useConfirm,
|
||
usePaged,
|
||
useToast,
|
||
} from "@/components/ui";
|
||
import { api } from "@/lib/api";
|
||
import { relTime, shortId, uptime } from "@/lib/format";
|
||
import { useAsync } from "@/lib/useAsync";
|
||
import type { ApiKey } from "@/lib/types";
|
||
|
||
function tsText(value: number): string {
|
||
if (!value) return "-";
|
||
return new Date(value * 1000).toLocaleTimeString();
|
||
}
|
||
|
||
function KeyForm({ onDone }: { onDone: (key: ApiKey) => void }) {
|
||
const [name, setName] = useState("console");
|
||
const [scope, setScope] = useState<"admin" | "read">("admin");
|
||
const [busy, setBusy] = useState(false);
|
||
const [error, setError] = useState("");
|
||
|
||
return (
|
||
<div className="flex flex-wrap items-end gap-2">
|
||
<Field label="名称" className="min-w-[160px] flex-1">
|
||
<Input value={name} onChange={(event) => setName(event.target.value)} />
|
||
</Field>
|
||
<Field label="Scope" className="w-32">
|
||
<Select value={scope} onChange={(event) => setScope(event.target.value as "admin" | "read")}>
|
||
<option value="admin">admin</option>
|
||
<option value="read">read</option>
|
||
</Select>
|
||
</Field>
|
||
<PixelButton
|
||
variant="primary"
|
||
disabled={busy || !name.trim()}
|
||
onClick={async () => {
|
||
setBusy(true);
|
||
setError("");
|
||
try {
|
||
onDone(await api.createKey(name.trim(), scope));
|
||
setName("console");
|
||
} catch (exc) {
|
||
setError(exc instanceof Error ? exc.message : String(exc));
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
}}
|
||
>
|
||
<KeyRound size={12} />
|
||
新建密钥
|
||
</PixelButton>
|
||
<ErrorNote error={error} />
|
||
</div>
|
||
);
|
||
}
|
||
|
||
export default function SettingsPage() {
|
||
const { revision } = useLive();
|
||
const confirm = useConfirm();
|
||
const toast = useToast();
|
||
const meta = useAsync(() => api.meta(), []);
|
||
const gateway = useAsync(() => api.gatewayStatus(), [revision]);
|
||
const keys = useAsync(() => api.keys(), [revision]);
|
||
const audit = useAsync(() => api.audit({ limit: 100 }), [revision]);
|
||
const [kind, setKind] = useState("");
|
||
const [limit, setLimit] = useState("100");
|
||
const frames = useAsync(
|
||
() => api.frames({ kind, limit: Number(limit) || 100 }),
|
||
[kind, limit, revision]
|
||
);
|
||
const [issued, setIssued] = useState<ApiKey | null>(null);
|
||
const [days, setDays] = useState("7");
|
||
const gwPage = usePaged(gateway.data?.connections ?? [], 10);
|
||
const keyPage = usePaged(keys.data ?? [], 10);
|
||
const framePage = usePaged(frames.data ?? [], 20);
|
||
const auditPage = usePaged(audit.data ?? [], 20);
|
||
|
||
return (
|
||
<div className="grid gap-3">
|
||
<div className="grid gap-3 lg:grid-cols-2">
|
||
<Panel title="Hub" icon={<Settings size={13} />}>
|
||
{meta.loading && !meta.data ? (
|
||
<Spinner />
|
||
) : (
|
||
<KV
|
||
rows={[
|
||
["应用", (meta.data?.app ?? "-") + " v" + (meta.data?.version ?? "-")],
|
||
["公开地址", meta.data?.url ?? "-"],
|
||
["WebSocket", meta.data?.ws_url ?? "-"],
|
||
["鉴权", meta.data?.auth_required ? "已启用" : "已关闭(dev)"],
|
||
[
|
||
"Drivers",
|
||
<span key="drivers">
|
||
{(meta.data?.drivers ?? []).length} 个
|
||
{" · "}
|
||
<Link className="pixel-link" href="/connect">
|
||
接入页看清单 →
|
||
</Link>
|
||
</span>,
|
||
],
|
||
]}
|
||
/>
|
||
)}
|
||
</Panel>
|
||
|
||
<Panel title="Gateway" icon={<Cable size={13} />} bodyClass="p-0">
|
||
{gateway.loading && !gateway.data ? (
|
||
<Spinner />
|
||
) : (
|
||
<>
|
||
<div className="flex flex-wrap gap-2 p-2">
|
||
<Badge tone="accent">运行 {uptime(gateway.data?.uptime_s ?? 0)}</Badge>
|
||
<Badge tone="green">
|
||
<Dot tone="green" />
|
||
连接 {gateway.data?.connector_connections ?? 0}
|
||
</Badge>
|
||
<Badge>已发布 {gateway.data?.bus?.published ?? 0}</Badge>
|
||
<Badge tone={(gateway.data?.bus?.dropped ?? 0) > 0 ? "yellow" : "gray"}>
|
||
丢弃 {gateway.data?.bus?.dropped ?? 0}
|
||
</Badge>
|
||
<Badge>订阅者 {gateway.data?.bus?.subscribers ?? 0}</Badge>
|
||
</div>
|
||
<div className="overflow-x-auto">
|
||
<table className="pixel-table">
|
||
<thead>
|
||
<tr>
|
||
<th>Agent</th>
|
||
<th>远端</th>
|
||
<th>待处理</th>
|
||
<th>最后活动</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{gwPage.rows.map((row, index) => (
|
||
<tr key={String(row.agent_id) + index}>
|
||
<td className="pixel-mono">{shortId(String(row.agent_id))}</td>
|
||
<td className="pixel-muted pixel-mono text-[12px]">{String(row.remote || "-")}</td>
|
||
<td className="pixel-mono">{String(row.pending_requests ?? 0)}</td>
|
||
<td className="pixel-muted pixel-mono text-[12px]">{tsText(Number(row.last_seen ?? 0))}</td>
|
||
</tr>
|
||
))}
|
||
{gwPage.total === 0 && (
|
||
<tr>
|
||
<td className="pixel-muted" colSpan={4}>
|
||
没有外拨连接
|
||
</td>
|
||
</tr>
|
||
)}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<Pager
|
||
page={ gwPage.page }
|
||
pageCount={ gwPage.pageCount }
|
||
total={ gwPage.total }
|
||
pageSize={ gwPage.pageSize }
|
||
onPage={ gwPage.setPage }
|
||
onPageSize={ gwPage.setPageSize }
|
||
/>
|
||
</>
|
||
)}
|
||
</Panel>
|
||
</div>
|
||
|
||
<Panel
|
||
title="API 密钥(控制台面)"
|
||
icon={<KeyRound size={13} />}
|
||
bodyClass="p-0"
|
||
>
|
||
<div className="border-b-2 border-[var(--pixel-border)] p-2">
|
||
<KeyForm
|
||
onDone={(key) => {
|
||
setIssued(key);
|
||
keys.reload();
|
||
}}
|
||
/>
|
||
{issued?.token && (
|
||
<div className="mt-2 flex items-start gap-2">
|
||
<pre className="pixel-pre flex-1 break-all whitespace-pre-wrap">{issued.token}</pre>
|
||
<CopyButton text={issued.token} />
|
||
<PixelButton size="sm" onClick={() => setIssued(null)}>
|
||
隐藏
|
||
</PixelButton>
|
||
</div>
|
||
)}
|
||
</div>
|
||
<div className="overflow-x-auto">
|
||
<table className="pixel-table">
|
||
<thead>
|
||
<tr>
|
||
<th>名称</th>
|
||
<th>Scope</th>
|
||
<th>预览</th>
|
||
<th>创建</th>
|
||
<th>最近使用</th>
|
||
<th>状态</th>
|
||
<th />
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{keyPage.rows.map((key) => (
|
||
<tr key={key.id}>
|
||
<td>{key.name}</td>
|
||
<td>
|
||
<Badge tone={key.scope === "admin" ? "accent" : "gray"}>{key.scope}</Badge>
|
||
</td>
|
||
<td className="pixel-muted pixel-mono text-[12px]">{key.key_preview}</td>
|
||
<td className="pixel-muted pixel-mono text-[12px]">{relTime(key.created_at)}</td>
|
||
<td className="pixel-muted pixel-mono text-[12px]">{relTime(key.last_used_at)}</td>
|
||
<td>
|
||
<Badge tone={key.revoked ? "red" : "green"}>{key.revoked ? "revoked" : "active"}</Badge>
|
||
</td>
|
||
<td className="text-right">
|
||
{!key.revoked && (
|
||
<PixelButton
|
||
size="sm"
|
||
icon
|
||
variant="danger"
|
||
title="吊销"
|
||
onClick={async () => {
|
||
const ok = await confirm({
|
||
title: "吊销密钥 " + key.name + " ?",
|
||
confirmLabel: "吊销",
|
||
danger: true,
|
||
});
|
||
if (!ok) return;
|
||
try {
|
||
await api.revokeKey(key.id);
|
||
keys.reload();
|
||
} catch (exc) {
|
||
toast("error", exc instanceof Error ? exc.message : String(exc));
|
||
}
|
||
}}
|
||
>
|
||
<EyeOff size={12} />
|
||
</PixelButton>
|
||
)}
|
||
</td>
|
||
</tr>
|
||
))}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<Pager
|
||
page={ keyPage.page }
|
||
pageCount={ keyPage.pageCount }
|
||
total={ keyPage.total }
|
||
pageSize={ keyPage.pageSize }
|
||
onPage={ keyPage.setPage }
|
||
onPageSize={ keyPage.setPageSize }
|
||
/>
|
||
</Panel>
|
||
|
||
<Panel
|
||
title="Gateway 帧窗口"
|
||
icon={<Activity size={13} />}
|
||
bodyClass="p-0"
|
||
actions={
|
||
<div className="flex items-center gap-2">
|
||
<Input
|
||
className="w-40"
|
||
placeholder="kind 过滤,如 task.run"
|
||
value={kind}
|
||
onChange={(event) => setKind(event.target.value)}
|
||
/>
|
||
<Select value={limit} onChange={(event) => setLimit(event.target.value)}>
|
||
{["50", "100", "300"].map((item) => (
|
||
<option key={item} value={item}>
|
||
{item}
|
||
</option>
|
||
))}
|
||
</Select>
|
||
</div>
|
||
}
|
||
>
|
||
{frames.error && <ErrorNote error={frames.error} />}
|
||
<div className="overflow-x-auto">
|
||
<table className="pixel-table">
|
||
<thead>
|
||
<tr>
|
||
<th>时间</th>
|
||
<th>kind</th>
|
||
<th>topic</th>
|
||
<th>actor</th>
|
||
<th>agent</th>
|
||
<th>task/run</th>
|
||
<th>seq</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{framePage.rows.map((frame) => (
|
||
<tr key={frame.id}>
|
||
<td className="pixel-muted pixel-mono text-[12px]">{tsText(frame.ts)}</td>
|
||
<td>
|
||
<Badge tone="accent">{frame.kind}</Badge>
|
||
</td>
|
||
<td className="pixel-muted pixel-mono text-[12px]">{frame.topic}</td>
|
||
<td className="pixel-muted pixel-mono text-[12px]">{frame.actor}</td>
|
||
<td className="pixel-muted pixel-mono text-[12px]">{frame.agent_id ? shortId(frame.agent_id) : ""}</td>
|
||
<td className="pixel-muted pixel-mono text-[12px]">
|
||
{frame.task_id ? shortId(frame.task_id, 6) : frame.run_id ? shortId(frame.run_id, 6) : ""}
|
||
</td>
|
||
<td className="pixel-mono">{frame.seq ?? ""}</td>
|
||
</tr>
|
||
))}
|
||
{framePage.total === 0 && (
|
||
<tr>
|
||
<td className="pixel-muted" colSpan={7}>
|
||
环形缓冲区里暂无帧
|
||
</td>
|
||
</tr>
|
||
)}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<Pager
|
||
page={ framePage.page }
|
||
pageCount={ framePage.pageCount }
|
||
total={ framePage.total }
|
||
pageSize={ framePage.pageSize }
|
||
onPage={ framePage.setPage }
|
||
onPageSize={ framePage.setPageSize }
|
||
/>
|
||
<div className="flex flex-wrap items-center gap-2 border-t-2 border-[var(--pixel-border)] p-2">
|
||
<span className="pixel-muted text-[12px]">清理离线 Agent</span>
|
||
<Input className="w-20" value={days} onChange={(event) => setDays(event.target.value)} />
|
||
<PixelButton
|
||
onClick={async () => {
|
||
try {
|
||
const result = await api.purgeAgents(Number(days) || 7);
|
||
toast("ok", "已清理 " + result.removed + " 个离线 Agent");
|
||
keys.reload();
|
||
} catch (exc) {
|
||
toast("error", exc instanceof Error ? exc.message : String(exc));
|
||
}
|
||
}}
|
||
>
|
||
执行
|
||
</PixelButton>
|
||
</div>
|
||
</Panel>
|
||
|
||
<Panel title="审计日志" icon={<ScrollText size={13} />} bodyClass="p-0">
|
||
{auditPage.total === 0 ? (
|
||
<Empty title="暂无审计记录" />
|
||
) : (
|
||
<>
|
||
<div className="overflow-x-auto">
|
||
<table className="pixel-table">
|
||
<thead>
|
||
<tr>
|
||
<th>时间</th>
|
||
<th>Actor</th>
|
||
<th>Action</th>
|
||
<th>对象</th>
|
||
<th>Detail</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{auditPage.rows.map((entry) => (
|
||
<tr key={entry.id}>
|
||
<td className="pixel-muted pixel-mono text-[12px]">{tsText(entry.ts)}</td>
|
||
<td className="pixel-mono text-[12px]">{entry.actor}</td>
|
||
<td>
|
||
<Badge tone="accent">{entry.action}</Badge>
|
||
</td>
|
||
<td className="pixel-muted pixel-mono text-[12px]">
|
||
{entry.target_type}:{shortId(entry.target_id, 6)}
|
||
</td>
|
||
<td className="pixel-muted pixel-mono max-w-[380px] truncate text-[12px]">
|
||
{JSON.stringify(entry.detail ?? {})}
|
||
</td>
|
||
</tr>
|
||
))}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<Pager
|
||
page={ auditPage.page }
|
||
pageCount={ auditPage.pageCount }
|
||
total={ auditPage.total }
|
||
pageSize={ auditPage.pageSize }
|
||
onPage={ auditPage.setPage }
|
||
onPageSize={ auditPage.setPageSize }
|
||
/>
|
||
</>
|
||
)}
|
||
</Panel>
|
||
|
||
<Panel title="最近帧原始 JSON(调试)">
|
||
<JsonBlock className="max-h-64" value={(frames.data ?? []).slice(0, 3)} />
|
||
</Panel>
|
||
</div>
|
||
);
|
||
}
|