douyinWEB/frontend/src/components/HostResourceBar.tsx
eric 11394a2a80 Document Pro relay/time-slot ops and harden relay worker stability.
README covers YouTube RTMPS relay, time-slot mode, ops/Netdata, and global SOCKS plan. Relay pipeline keeps FFmpeg alive across probe flicker, stabilizes time-slot source selection, and fixes misleading start-button copy in the UI.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-01 10:53:29 -05:00

97 lines
2.8 KiB
TypeScript

"use client";
import { useEffect, useState } from "react";
import { fetchJson } from "@/lib/api";
type HostMetrics = {
cpu_percent?: number;
memory_percent?: number;
memory_used_gb?: number;
memory_total_gb?: number;
load1?: number;
cpu_cores?: number;
};
function barColor(pct: number) {
if (pct >= 90) return "from-rose-500 to-orange-500";
if (pct >= 75) return "from-amber-400 to-orange-400";
return "from-teal-400 to-sky-400";
}
function MetricBar({ label, pct, hint }: { label: string; pct: number; hint: string }) {
const p = Math.max(0, Math.min(100, pct));
return (
<div className="min-w-[8.5rem] flex-1 sm:min-w-[10rem] sm:flex-none">
<div className="mb-0.5 flex items-center justify-between gap-2 text-[10px] text-slate-400 sm:text-[11px]">
<span>{label}</span>
<span className="font-mono text-slate-300">
{p.toFixed(1)}% <span className="text-slate-500">· {hint}</span>
</span>
</div>
<div className="h-1.5 overflow-hidden rounded-full bg-white/10">
<div
className={`h-full rounded-full bg-gradient-to-r transition-[width] duration-500 ease-out ${barColor(p)}`}
style={{ width: `${p}%` }}
/>
</div>
</div>
);
}
export function HostResourceBar() {
const [m, setM] = useState<HostMetrics | null>(null);
const [err, setErr] = useState(false);
useEffect(() => {
let cancelled = false;
const load = async () => {
if (document.hidden) return;
try {
const d = (await fetchJson("/api/ops/host-metrics")) as HostMetrics;
if (!cancelled) {
setM(d);
setErr(false);
}
} catch {
if (!cancelled) setErr(true);
}
};
void load();
const t = setInterval(() => void load(), 20000);
const onVis = () => {
if (!document.hidden) void load();
};
document.addEventListener("visibilitychange", onVis);
return () => {
cancelled = true;
clearInterval(t);
document.removeEventListener("visibilitychange", onVis);
};
}, []);
if (err && !m) {
return <p className="mt-1 text-[10px] text-slate-600 sm:text-[11px]">主机指标暂不可用</p>;
}
const cpu = Number(m?.cpu_percent ?? 0);
const mem = Number(m?.memory_percent ?? 0);
const memHint =
m?.memory_used_gb != null && m?.memory_total_gb != null
? `${m.memory_used_gb}/${m.memory_total_gb}G`
: "—";
const loadHint =
m?.load1 != null && m?.cpu_cores
? `load ${m.load1}/${m.cpu_cores}`
: m?.load1 != null
? `load ${m.load1}`
: "";
return (
<div className="mt-1.5 flex flex-col gap-1.5 sm:mt-2 sm:flex-row sm:flex-wrap sm:items-center sm:gap-3">
<MetricBar label="CPU" pct={cpu} hint={loadHint || "实时"} />
<MetricBar label="内存" pct={mem} hint={memHint} />
</div>
);
}