OverviewPage.tsx 5.89 KB
import { Activity, Bot, ListChecks, RefreshCw, ShieldCheck, Workflow } from "lucide-react";
import { useCallback, useEffect, useState } from "react";
import { Badge, Card, CardHeader, Empty, Spinner, Stat } from "@/components/ui";
import { go } from "@/lib/nav";
import { errText, fmtTime, onto, PRIORITY_TONE, STATUS_LABEL, type RuntimeOverview } from "@/lib/onto";
import { toast } from "@/store/useAppStore";

const VIA: Record<string, string> = { agent: "外部助手", console: "在线调试", ask: "提问" };

/**
 * 运行时总览:本体上线后,Agent 怎么在用它 —— MCP 调用量与成功率、最常用的工具、
 * 待审批动作(按优先级)、流程卡运行、数据管道同步状态。
 */
export function OverviewPage() {
  const [d, setD] = useState<RuntimeOverview | null>(null);
  const load = useCallback(() => { void onto.runtimeOverview().then(setD).catch((e) => toast("error", errText(e))); }, []);
  useEffect(load, [load]);
  if (!d) return <Spinner />;
  const rate = d.mcp.success_rate === null ? "-" : `${Math.round(d.mcp.success_rate * 100)}%`;
  const live = Object.entries(d.runcards).reduce((a, [, n]) => a + n, 0);
  return (
    <div className="space-y-4">
      <div className="flex items-center gap-2">
        <span className="flex-1" />
        <button className="btn btn-sm" onClick={load}><RefreshCw className="h-3.5 w-3.5" />刷新</button>
      </div>
      <Card className="grid gap-4 p-4 sm:grid-cols-5">
        <Stat label="24h MCP 调用" value={d.mcp.calls_24h} hint={Object.entries(d.mcp.by_via).map(([k, v]) => `${VIA[k] || k} ${v}`).join(" · ") || "暂无"} />
        <Stat label="成功率" value={rate} tone={d.mcp.success_rate !== null && d.mcp.success_rate < 0.9 ? "text-warn" : ""} />
        <Stat label="待审批动作" value={d.approvals.pending} tone={d.approvals.pending ? "text-warn" : ""}
              hint={Object.entries(d.approvals.by_priority).map(([k, v]) => `${k} ${v}`).join(" · ")} />
        <Stat label="流程卡运行" value={live} hint={Object.entries(d.runcards).map(([k, v]) => `${STATUS_LABEL[k] || k} ${v}`).join(" · ")} />
        <Stat label="关系边" value={d.pipelines.edges} hint={d.pipelines.synced_at ? `同步于 ${fmtTime(d.pipelines.synced_at)}` : "未同步"} />
      </Card>
      <div className="grid gap-4 lg:grid-cols-2">
        <Card>
          <CardHeader title={<span className="flex items-center gap-2"><Bot className="h-4 w-4" />最常用的工具(24h)</span>}
                      right={<button className="btn btn-ghost btn-sm" onClick={() => go("mcp")}>MCP 接入</button>} />
          {!d.mcp.top_tools.length ? <Empty title="还没有调用" /> : (
            <table className="w-full text-label">
              <thead className="text-label text-muted"><tr className="border-b border-line">
                <th className="px-4 py-2 text-left">工具</th><th className="px-2 text-right">调用</th><th className="px-2 text-right">失败</th><th className="px-4 text-right">平均耗时</th></tr></thead>
              <tbody>{d.mcp.top_tools.map((t) => (
                <tr key={t.tool} className="border-b border-line/60">
                  <td className="px-4 py-1.5 font-mono">{t.tool}</td><td className="px-2 text-right tabular-nums">{t.calls}</td>
                  <td className="px-2 text-right tabular-nums">{t.failed ? <span className="text-bad">{t.failed}</span> : 0}</td>
                  <td className="px-4 text-right tabular-nums text-muted">{t.avg_ms} ms</td></tr>
              ))}</tbody>
            </table>
          )}
        </Card>
        <Card>
          <CardHeader title={<span className="flex items-center gap-2"><ShieldCheck className="h-4 w-4" />待审批(按优先级)</span>}
                      right={<button className="btn btn-ghost btn-sm" onClick={() => go("inbox")}>去审批</button>} />
          <div className="divide-y divide-line">
            {!d.approvals.top.length && <Empty title="没有待审批的动作" />}
            {d.approvals.top.map((r) => (
              <button key={r.id} className="flex w-full items-center gap-2 px-4 py-2 text-left text-label hover:bg-ink/[0.03]" onClick={() => go("inbox", { run: r.id })}>
                <Badge tone={PRIORITY_TONE[r.priority]}>{r.priority}</Badge>
                <span className="font-medium">{r.action_name}</span>
                <span className="min-w-0 flex-1 truncate text-muted">{r.reason}</span>
                <span className="text-caption text-muted">{fmtTime(r.created_at)}</span>
              </button>
            ))}
          </div>
        </Card>
        <Card>
          <CardHeader title={<span className="flex items-center gap-2"><Workflow className="h-4 w-4" />关系索引</span>}
                      desc="发布后和每小时自动刷新" />
          <div className="divide-y divide-line">
            {!d.pipelines.items.length && <Empty title="还没有数据管道" />}
            {d.pipelines.items.map((p) => (
              <div key={p.id} className="flex items-center gap-2 px-4 py-2 text-label">
                <Activity className={`h-3.5 w-3.5 ${p.status === "failed" ? "text-bad" : p.status === "ok" ? "text-ok" : "text-muted"}`} />
                <span className="font-medium">{p.object_type}</span>
                <span className="text-muted">{p.last_rows} 个 · {p.last_edges} 边</span>
                <span className="ml-auto text-caption text-muted">{fmtTime(p.last_run_at)}</span>
              </div>
            ))}
          </div>
        </Card>
        <Card>
          <CardHeader title={<span className="flex items-center gap-2"><ListChecks className="h-4 w-4" />动作执行</span>} />
          <div className="flex flex-wrap gap-2 p-4">
            {Object.entries(d.actions).map(([k, v]) => <Badge key={k}>{STATUS_LABEL[k] || k} {v}</Badge>)}
            {!Object.keys(d.actions).length && <p className="text-label text-muted">还没有动作</p>}
          </div>
        </Card>
      </div>
    </div>
  );
}