From dbe67ab2057d275265b39781477eccd1c1b2cb89 Mon Sep 17 00:00:00 2001 From: hamster1963 <1410514192@qq.com> Date: Fri, 18 Oct 2024 18:01:32 +0800 Subject: [PATCH] feat: init cpu chart --- .../ServerDetailChartClient.tsx | 140 ++++++ .../ClientComponents/ServerDetailClient.tsx | 429 +++++++++--------- app/[locale]/(main)/detail/[id]/page.tsx | 7 +- 3 files changed, 353 insertions(+), 223 deletions(-) create mode 100644 app/[locale]/(main)/ClientComponents/ServerDetailChartClient.tsx diff --git a/app/[locale]/(main)/ClientComponents/ServerDetailChartClient.tsx b/app/[locale]/(main)/ClientComponents/ServerDetailChartClient.tsx new file mode 100644 index 0000000..74fdcf3 --- /dev/null +++ b/app/[locale]/(main)/ClientComponents/ServerDetailChartClient.tsx @@ -0,0 +1,140 @@ +"use client" + +import AnimatedCircularProgressBar from "@/components/ui/animated-circular-progress-bar"; +import { Card, CardContent } from "@/components/ui/card"; +import useSWR from "swr"; +import { NezhaAPISafe } from "../../types/nezha-api"; +import { formatRelativeTime, formatTime, nezhaFetcher } from "@/lib/utils"; +import getEnv from "@/lib/env-entry"; +import { ChartConfig, ChartContainer, ChartTooltip, ChartTooltipContent } from "@/components/ui/chart"; +import { Area, AreaChart, CartesianGrid, XAxis, YAxis } from "recharts"; +import { useEffect, useState } from "react"; + + +type cpuChartData = { + timeStamp: string; + cpu: number; +} + +export default function ServerDetailChartClient({ + server_id, +}: { + server_id: number; +}) { + const { data, error } = useSWR( + `/api/detail?server_id=${server_id}`, + nezhaFetcher, + { + refreshInterval: Number(getEnv("NEXT_PUBLIC_NezhaFetchInterval")) || 5000, + }, + ); + + const [chartData, setChartData] = useState([] as cpuChartData[]); + + useEffect(() => { + if (data) { + const timestamp = Date.now().toString(); + const newData = [ + ...chartData, + { timeStamp: timestamp, cpu: data.status.CPU }, + ]; + if (newData.length > 30) { + newData.shift(); + } + setChartData(newData); + } + }, [data]); + + if (error) { + return ( + <> +
+

{error.message}

+

+ {/* {t("chart_fetch_error_message")} */} + fetch_error_message +

+
+ + ); + } + if (!data) return null; + + + const chartConfig = { + cpu: { + label: "CPU", + }, + } satisfies ChartConfig + + return ( +
+ + +
+
+

+ CPU +

+
+

+ {data?.status.CPU.toFixed(0)}% +

+ +
+
+ + + + + formatRelativeTime(value)} + /> + `${value}%`} + /> + {/* { + return formatTime(Number(payload[0].payload.timeStamp)); + }} />} + /> */} + + + +
+
+
+
+ ) +} \ No newline at end of file diff --git a/app/[locale]/(main)/ClientComponents/ServerDetailClient.tsx b/app/[locale]/(main)/ClientComponents/ServerDetailClient.tsx index 84f8c8b..2a076b8 100644 --- a/app/[locale]/(main)/ClientComponents/ServerDetailClient.tsx +++ b/app/[locale]/(main)/ClientComponents/ServerDetailClient.tsx @@ -12,232 +12,217 @@ import { useRouter } from "next/navigation"; import useSWR from "swr"; export default function ServerDetailClient({ - server_id, + server_id, }: { - server_id: number; + server_id: number; }) { - const router = useRouter(); - const locale = useLocale(); - const { data, error } = useSWR( - `/api/detail?server_id=${server_id}`, - nezhaFetcher, - { - refreshInterval: Number(getEnv("NEXT_PUBLIC_NezhaFetchInterval")) || 5000, - }, - ); - if (error) { - return ( - <> -
-

{error.message}

-

- {/* {t("chart_fetch_error_message")} */} - fetch_error_message -

-
- + const router = useRouter(); + const locale = useLocale(); + const { data, error } = useSWR( + `/api/detail?server_id=${server_id}`, + nezhaFetcher, + { + refreshInterval: Number(getEnv("NEXT_PUBLIC_NezhaFetchInterval")) || 5000, + }, ); - } - if (!data) return null; - return ( -
-
{ - router.push(`/${locale}/`); - }} - className="flex flex-none cursor-pointer font-semibold leading-none items-center break-all tracking-tight gap-0.5 text-xl" - > - - {data?.name} -
-
- - -
-

Status

- - {data?.online_status ? "Online" : "Offline"} - -
-
-
- - -
-

Uptime

- - {" "} - {(data?.status.Uptime / 86400).toFixed(0)} Days{" "} - -
-
-
- - - -
-

Tag

- - {" "} - {data?.tag || "Unknown"}{" "} - -
-
-
- - -
-

Arch

- - {" "} - {data?.host.Arch || "Unknown"}{" "} - -
-
-
- - -
-

Version

- - {" "} - {data?.host.Version || "Unknown"}{" "} - -
-
-
-
-
- - -
-

System

- {data?.host.Platform ? ( -
- {" "} - {data?.host.Platform || "Unknown"} -{" "} - {data?.host.PlatformVersion}{" "} + if (error) { + return ( + <> +
+

{error.message}

+

+ {/* {t("chart_fetch_error_message")} */} + fetch_error_message +

- ) : ( -
Unknown
- )} + + ); + } + if (!data) return null; + return ( +
+
{ + router.push(`/${locale}/`); + }} + className="flex flex-none cursor-pointer font-semibold leading-none items-center break-all tracking-tight gap-0.5 text-xl" + > + + {data?.name} +
+
+ + +
+

Status

+ + {data?.online_status ? "Online" : "Offline"} + +
+
+
+ + +
+

Uptime

+ + {" "} + {(data?.status.Uptime / 86400).toFixed(0)} Days{" "} + +
+
+
+ + +
+

Arch

+ + {" "} + {data?.host.Arch || "Unknown"}{" "} + +
+
+
+ + +
+

Version

+ + {" "} + {data?.host.Version || "Unknown"}{" "} + +
+
+
- - - - -
-

CPU

- {data?.host.CPU ? ( -
- {" "} - {data?.host.CPU || "Unknown"} -
- ) : ( -
Unknown
- )} +
+ + +
+

System

+ {data?.host.Platform ? ( +
+ {" "} + {data?.host.Platform || "Unknown"} -{" "} + {data?.host.PlatformVersion}{" "} +
+ ) : ( +
Unknown
+ )} +
+
+
+ + +
+

CPU

+ {data?.host.CPU ? ( +
+ {" "} + {data?.host.CPU || "Unknown"} +
+ ) : ( +
Unknown
+ )} +
+
+
- - -
-
- - -
-

CPU

-

- {data?.status.CPU.toFixed(0)}% -

- -
-
-
- - -
-

Mem

-

- {((data?.status.MemUsed / data?.host.MemTotal) * 100).toFixed( - 0, - )} - % -

- -
-
-
- - -
-

Swap

-

- {data?.status.SwapUsed - ? ( - (data?.status.SwapUsed / data?.host.SwapTotal) * - 100 - ).toFixed(0) - : 0} - % -

- -
-
-
- - -
-

Disk

-

- {((data?.status.DiskUsed / data?.host.DiskTotal) * 100).toFixed( - 0, - )} - % -

- -
-
-
-
-
- ); + {/*
+ + +
+

CPU

+

+ {data?.status.CPU.toFixed(0)}% +

+ +
+
+
+ + +
+

Mem

+

+ {((data?.status.MemUsed / data?.host.MemTotal) * 100).toFixed( + 0, + )} + % +

+ +
+
+
+ + +
+

Swap

+

+ {data?.status.SwapUsed + ? ( + (data?.status.SwapUsed / data?.host.SwapTotal) * + 100 + ).toFixed(0) + : 0} + % +

+ +
+
+
+ + +
+

Disk

+

+ {((data?.status.DiskUsed / data?.host.DiskTotal) * 100).toFixed( + 0, + )} + % +

+ +
+
+
+
*/} +
+ ); } diff --git a/app/[locale]/(main)/detail/[id]/page.tsx b/app/[locale]/(main)/detail/[id]/page.tsx index ad85bc9..f025408 100644 --- a/app/[locale]/(main)/detail/[id]/page.tsx +++ b/app/[locale]/(main)/detail/[id]/page.tsx @@ -1,5 +1,10 @@ import ServerDetailClient from "@/app/[locale]/(main)/ClientComponents/ServerDetailClient"; +import ServerDetailChartClient from "@/app/[locale]/(main)/ClientComponents/ServerDetailChartClient"; export default function Page({ params }: { params: { id: string } }) { - return ; + return
+ + +
+ ; }