diff --git a/src/components/ServerDetailChart.tsx b/src/components/ServerDetailChart.tsx index 9f9630b..f05b2ed 100644 --- a/src/components/ServerDetailChart.tsx +++ b/src/components/ServerDetailChart.tsx @@ -18,6 +18,11 @@ import { useWebSocketContext } from "@/hooks/use-websocket-context"; import { useTranslation } from "react-i18next"; import { formatBytes } from "@/lib/format"; +type gpuChartData = { + timeStamp: string; + gpu: number; +}; + type cpuChartData = { timeStamp: string; cpu: number; @@ -82,9 +87,26 @@ export default function ServerDetailChart({ return ; } + const gpuStats = server.state.gpu || []; + const gpuList = server.host.gpu || []; + return ( + {gpuStats.length > 1 && gpuList.length === gpuStats.length ? ( + gpuList.map((gpu, index) => ( + + )) + ) : gpuStats.length > 0 ? ( + + ) : ( + <>> + )} @@ -94,6 +116,111 @@ export default function ServerDetailChart({ ); } +function GpuChart({ + now, + gpuStat, + gpuName, +}: { + now: number; + gpuStat: number; + gpuName?: string; +}) { + const [gpuChartData, setGpuChartData] = useState([] as gpuChartData[]); + + useEffect(() => { + if (gpuStat) { + const timestamp = Date.now().toString(); + let newData = [] as gpuChartData[]; + if (gpuChartData.length === 0) { + newData = [ + { timeStamp: timestamp, gpu: gpuStat }, + { timeStamp: timestamp, gpu: gpuStat }, + ]; + } else { + newData = [...gpuChartData, { timeStamp: timestamp, gpu: gpuStat }]; + } + if (newData.length > 30) { + newData.shift(); + } + setGpuChartData(newData); + } + }, [now, gpuStat]); + + const chartConfig = { + gpu: { + label: "GPU", + }, + } satisfies ChartConfig; + + return ( + + + + + + {!gpuName && GPU} + {gpuName && GPU: {gpuName}} + + + + {gpuStat.toFixed(0)}% + + + + + + + + formatRelativeTime(value)} + /> + `${value}%`} + /> + + + + + + + ); +} + function CpuChart({ now, data }: { now: number; data: NezhaServer }) { const [cpuChartData, setCpuChartData] = useState([] as cpuChartData[]); diff --git a/src/lib/utils.ts b/src/lib/utils.ts index a77e9a8..f1b99b9 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -13,6 +13,7 @@ export function formatNezhaInfo(now: number, serverInfo: NezhaServer) { return { ...serverInfo, cpu: serverInfo.state.cpu || 0, + gpu: serverInfo.state.gpu || [], process: serverInfo.state.process_count || 0, up: serverInfo.state.net_out_speed / 1024 / 1024 || 0, down: serverInfo.state.net_in_speed / 1024 / 1024 || 0,
GPU
GPU: {gpuName}
+ {gpuStat.toFixed(0)}% +