266 lines
9.2 KiB
TypeScript
266 lines
9.2 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useMemo, useRef, useState } from "react";
|
|
|
|
export type SnapshotLite = {
|
|
cpu_load?: { "1m"?: number; "5m"?: number; "15m"?: number };
|
|
disk?: { total?: number; free?: number; used?: number };
|
|
memory?: { total?: number; available?: number; free?: number };
|
|
uptime_seconds?: number;
|
|
};
|
|
|
|
type HistoryPoint = {
|
|
t: number;
|
|
cpu1m: number;
|
|
memPct: number;
|
|
diskPct: number;
|
|
};
|
|
|
|
const HISTORY_CAP = 24;
|
|
|
|
function pct(used: number, total: number) {
|
|
if (!total) return 0;
|
|
return Math.min(100, Math.max(0, (used / total) * 100));
|
|
}
|
|
|
|
export default function OverviewCharts({
|
|
snapshot,
|
|
theme,
|
|
tr,
|
|
compact = false,
|
|
}: {
|
|
snapshot: SnapshotLite | null;
|
|
theme: "dark" | "light";
|
|
tr: (zh: string, en: string) => string;
|
|
compact?: boolean;
|
|
}) {
|
|
const mainRef = useRef<HTMLDivElement>(null);
|
|
const sparkRef = useRef<HTMLDivElement>(null);
|
|
const [history, setHistory] = useState<HistoryPoint[]>([]);
|
|
|
|
const metrics = useMemo(() => {
|
|
if (!snapshot) return null;
|
|
const l1 = snapshot.cpu_load?.["1m"] ?? 0;
|
|
const l5 = snapshot.cpu_load?.["5m"] ?? 0;
|
|
const l15 = snapshot.cpu_load?.["15m"] ?? 0;
|
|
const memTotal = snapshot.memory?.total || 1;
|
|
const memAvail = snapshot.memory?.available ?? snapshot.memory?.free ?? 0;
|
|
const memUsed = Math.max(0, memTotal - memAvail);
|
|
const diskTotal = snapshot.disk?.total || 1;
|
|
const diskFree = snapshot.disk?.free || 0;
|
|
const diskUsed = Math.max(0, diskTotal - diskFree);
|
|
return {
|
|
l1,
|
|
l5,
|
|
l15,
|
|
memUsed,
|
|
memAvail,
|
|
memTotal,
|
|
diskUsed,
|
|
diskFree,
|
|
diskTotal,
|
|
memPct: pct(memUsed, memTotal),
|
|
diskPct: pct(diskUsed, diskTotal),
|
|
};
|
|
}, [snapshot]);
|
|
|
|
useEffect(() => {
|
|
if (!metrics) return;
|
|
const now = Date.now();
|
|
setHistory((prev) => {
|
|
const next = [...prev, { t: now, cpu1m: metrics.l1, memPct: metrics.memPct, diskPct: metrics.diskPct }];
|
|
return next.length > HISTORY_CAP ? next.slice(-HISTORY_CAP) : next;
|
|
});
|
|
}, [metrics?.l1, metrics?.memPct, metrics?.diskPct, metrics]);
|
|
|
|
useEffect(() => {
|
|
const el = mainRef.current;
|
|
if (!el || !metrics) return;
|
|
let disposed = false;
|
|
let chart: import("echarts").ECharts | null = null;
|
|
const fg = theme === "light" ? "#0f172a" : "#f4f4f5";
|
|
const muted = theme === "light" ? "#64748b" : "#a1a1aa";
|
|
const gridLine = theme === "light" ? "rgba(15,23,42,0.08)" : "rgba(255,255,255,0.06)";
|
|
|
|
import("echarts").then((echarts) => {
|
|
if (disposed || !mainRef.current) return;
|
|
chart = echarts.init(mainRef.current, undefined, { renderer: "canvas" });
|
|
chart.setOption({
|
|
animationDurationUpdate: 380,
|
|
textStyle: { color: fg, fontFamily: "ui-monospace, monospace" },
|
|
tooltip: { trigger: "item", backgroundColor: theme === "light" ? "#fff" : "#18181b", borderColor: gridLine, textStyle: { color: fg, fontSize: 11 } },
|
|
series: [
|
|
{
|
|
type: "gauge",
|
|
center: ["18%", "52%"],
|
|
radius: "62%",
|
|
min: 0,
|
|
max: Math.max(4, Math.ceil(metrics.l1 * 1.4)),
|
|
splitNumber: 4,
|
|
axisLine: {
|
|
lineStyle: {
|
|
width: 10,
|
|
color: [
|
|
[0.45, "#22d3ee"],
|
|
[0.75, "#a78bfa"],
|
|
[1, "#fb7185"],
|
|
],
|
|
},
|
|
},
|
|
pointer: { width: 4, length: "58%", itemStyle: { color: fg } },
|
|
axisTick: { show: false },
|
|
splitLine: { length: 8, lineStyle: { color: gridLine } },
|
|
axisLabel: { color: muted, fontSize: 9, distance: 14 },
|
|
detail: {
|
|
formatter: (v: number) => `${v.toFixed(2)}`,
|
|
color: fg,
|
|
fontSize: 14,
|
|
fontWeight: 700,
|
|
offsetCenter: [0, "72%"],
|
|
},
|
|
title: { show: true, offsetCenter: [0, "92%"], color: muted, fontSize: 10, text: tr("CPU 1m", "CPU 1m") },
|
|
data: [{ value: metrics.l1 }],
|
|
},
|
|
{
|
|
type: "pie",
|
|
radius: ["46%", "62%"],
|
|
center: ["50%", "52%"],
|
|
label: { show: false },
|
|
data: [
|
|
{ value: metrics.memUsed, name: tr("内存已用", "RAM used"), itemStyle: { color: "#818cf8" } },
|
|
{ value: Math.max(0, metrics.memAvail), name: tr("内存可用", "RAM free"), itemStyle: { color: theme === "light" ? "#e2e8f0" : "#27272a" } },
|
|
],
|
|
},
|
|
{
|
|
type: "pie",
|
|
radius: ["46%", "62%"],
|
|
center: ["82%", "52%"],
|
|
label: { show: false },
|
|
data: [
|
|
{ value: metrics.diskUsed, name: tr("磁盘已用", "Disk used"), itemStyle: { color: "#34d399" } },
|
|
{ value: Math.max(0, metrics.diskFree), name: tr("磁盘空闲", "Disk free"), itemStyle: { color: theme === "light" ? "#e2e8f0" : "#27272a" } },
|
|
],
|
|
},
|
|
],
|
|
graphic: [
|
|
{
|
|
type: "text",
|
|
left: "center",
|
|
top: "52%",
|
|
style: { text: `${metrics.memPct.toFixed(0)}%`, fill: fg, font: "bold 13px ui-monospace", textAlign: "center" },
|
|
},
|
|
{
|
|
type: "text",
|
|
left: "74%",
|
|
top: "52%",
|
|
style: { text: `${metrics.diskPct.toFixed(0)}%`, fill: fg, font: "bold 13px ui-monospace", textAlign: "center" },
|
|
},
|
|
{
|
|
type: "text",
|
|
left: "42%",
|
|
top: "78%",
|
|
style: { text: tr("内存", "RAM"), fill: muted, font: "10px sans-serif", textAlign: "center" },
|
|
},
|
|
{
|
|
type: "text",
|
|
left: "74%",
|
|
top: "78%",
|
|
style: { text: tr("磁盘", "Disk"), fill: muted, font: "10px sans-serif", textAlign: "center" },
|
|
},
|
|
],
|
|
});
|
|
});
|
|
|
|
const ro = new ResizeObserver(() => chart?.resize());
|
|
ro.observe(el);
|
|
return () => {
|
|
disposed = true;
|
|
ro.disconnect();
|
|
chart?.dispose();
|
|
};
|
|
}, [metrics, theme, tr]);
|
|
|
|
useEffect(() => {
|
|
const el = sparkRef.current;
|
|
if (!el || history.length < 2) return;
|
|
let disposed = false;
|
|
let chart: import("echarts").ECharts | null = null;
|
|
const muted = theme === "light" ? "#94a3b8" : "#71717a";
|
|
const gridLine = theme === "light" ? "rgba(15,23,42,0.06)" : "rgba(255,255,255,0.05)";
|
|
|
|
import("echarts").then((echarts) => {
|
|
if (disposed || !sparkRef.current) return;
|
|
chart = echarts.init(sparkRef.current, undefined, { renderer: "canvas" });
|
|
const labels = history.map((_, i) => String(i + 1));
|
|
chart.setOption({
|
|
animationDurationUpdate: 300,
|
|
grid: { left: 36, right: 12, top: 18, bottom: 22 },
|
|
tooltip: { trigger: "axis", textStyle: { fontSize: 10 } },
|
|
legend: { top: 0, right: 0, textStyle: { color: muted, fontSize: 9 }, itemWidth: 8, itemHeight: 8 },
|
|
xAxis: { type: "category", data: labels, show: false },
|
|
yAxis: { type: "value", splitLine: { lineStyle: { color: gridLine } }, axisLabel: { color: muted, fontSize: 9 } },
|
|
series: [
|
|
{
|
|
name: "CPU",
|
|
type: "line",
|
|
smooth: true,
|
|
symbol: "none",
|
|
lineStyle: { width: 2, color: "#22d3ee" },
|
|
areaStyle: { color: "rgba(34,211,238,0.12)" },
|
|
data: history.map((p) => p.cpu1m),
|
|
},
|
|
{
|
|
name: tr("内存%", "RAM%"),
|
|
type: "line",
|
|
smooth: true,
|
|
symbol: "none",
|
|
lineStyle: { width: 2, color: "#a78bfa" },
|
|
data: history.map((p) => Number(p.memPct.toFixed(1))),
|
|
},
|
|
],
|
|
});
|
|
});
|
|
|
|
const ro = new ResizeObserver(() => chart?.resize());
|
|
ro.observe(el);
|
|
return () => {
|
|
disposed = true;
|
|
ro.disconnect();
|
|
chart?.dispose();
|
|
};
|
|
}, [history, theme, tr]);
|
|
|
|
if (!snapshot || !metrics) {
|
|
return (
|
|
<div className={`animate-pulse rounded-2xl border border-white/[0.06] bg-white/[0.03] ${compact ? "h-48" : "h-64"}`} />
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className={`flex flex-col ${compact ? "gap-2" : "gap-3"}`}>
|
|
<div className="flex flex-wrap items-end justify-between gap-2 px-1">
|
|
<div>
|
|
<p className="text-xs font-semibold tracking-wide text-zinc-300">{tr("节点资源", "Node resources")}</p>
|
|
<p className="mt-0.5 text-[10px] text-zinc-500">
|
|
{tr("负载", "Load")} {metrics.l1.toFixed(2)} / {metrics.l5.toFixed(2)} / {metrics.l15.toFixed(2)} ·{" "}
|
|
{tr("内存", "RAM")} {metrics.memPct.toFixed(0)}% · {tr("磁盘", "Disk")} {metrics.diskPct.toFixed(0)}%
|
|
</p>
|
|
</div>
|
|
{!compact ? (
|
|
<div className="flex gap-3 font-mono text-[10px] text-zinc-500">
|
|
<span>5m {metrics.l5.toFixed(2)}</span>
|
|
<span>15m {metrics.l15.toFixed(2)}</span>
|
|
</div>
|
|
) : null}
|
|
</div>
|
|
<div
|
|
ref={mainRef}
|
|
className={`chart-surface w-full shrink-0 rounded-2xl border border-white/[0.06] bg-black/10 ${compact ? "h-[168px]" : "h-52 md:h-56"}`}
|
|
/>
|
|
{!compact && history.length >= 2 ? (
|
|
<div ref={sparkRef} className="chart-surface h-28 w-full shrink-0 rounded-xl border border-white/[0.05] bg-black/5" />
|
|
) : null}
|
|
</div>
|
|
);
|
|
}
|