diff --git a/client/src/Pages/StatusPage/Create/index.tsx b/client/src/Pages/StatusPage/Create/index.tsx index 3b64738e5..1574c21c1 100644 --- a/client/src/Pages/StatusPage/Create/index.tsx +++ b/client/src/Pages/StatusPage/Create/index.tsx @@ -21,7 +21,7 @@ import { import { useTheme } from "@mui/material/styles"; import { useTranslation } from "react-i18next"; -import { useEffect, useState } from "react"; +import { useEffect, useMemo, useState } from "react"; import { useForm, Controller } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { useStatusPageForm } from "@/Hooks/useStatusPageForm"; @@ -39,6 +39,22 @@ interface TimezoneOption { name: string; } +const MONITOR_TYPE_KEYS: Record = { + http: "pages.common.monitors.monitorTypes.optionHttp", + ping: "pages.common.monitors.monitorTypes.optionPing", + docker: "pages.common.monitors.monitorTypes.optionDocker", + port: "pages.common.monitors.monitorTypes.optionPort", + game: "pages.common.monitors.monitorTypes.optionGame", + grpc: "pages.common.monitors.monitorTypes.optionGrpc", + websocket: "pages.common.monitors.monitorTypes.optionWebSocket", + hardware: "pages.common.monitors.monitorTypes.optionHardware", +}; + +const getMonitorTypeLabel = (type: string, t: (key: string) => string): string => { + const i18nMonitorKey = MONITOR_TYPE_KEYS[type]; + return i18nMonitorKey ? t(i18nMonitorKey) : type; +}; + const CreateStatusPage = () => { const theme = useTheme(); const { t } = useTranslation(); @@ -84,25 +100,22 @@ const CreateStatusPage = () => { }, [defaults, reset]); const watchedMonitorIds: string[] = form.watch("monitors") ?? []; - useEffect(() => { + const computedTypes: MonitorDisplayType[] = useMemo(() => { const selectedMonitors = (watchedMonitorIds ?? []) .map((id) => monitors.find((m) => m.id === id)) .filter((m): m is Monitor => m !== undefined); - const typesSet: Set = new Set(); + const typesSet = new Set(); selectedMonitors.forEach((m) => { - if (m.type === "hardware") { - typesSet.add("infrastructure"); - } else { - typesSet.add("uptime"); - } + typesSet.add(m.type === "hardware" ? "infrastructure" : "uptime"); }); - const computedTypes: MonitorDisplayType[] = typesSet.size - ? Array.from(typesSet) - : ["uptime"]; + return typesSet.size ? Array.from(typesSet) : ["uptime"]; + }, [JSON.stringify(watchedMonitorIds), monitors]); + + useEffect(() => { form.setValue("type", computedTypes); - }, [watchedMonitorIds, monitors, form]); + }, [computedTypes]); const onError = (errors: any) => { logger.debug("Status page validation errors", errors); @@ -332,7 +345,7 @@ const CreateStatusPage = () => { {`${monitor.name} (${t(`pages.common.monitors.monitorTypes.option${monitor?.type.charAt(0).toUpperCase() + monitor?.type.slice(1)}`)})`} + >{`${monitor.name} (${getMonitorTypeLabel(monitor.type, t)})`} { diff --git a/client/src/Pages/StatusPage/Status/Components/InfrastructureMetrics.tsx b/client/src/Pages/StatusPage/Status/Components/InfrastructureMetrics.tsx index 9be17105c..32a15d64b 100644 --- a/client/src/Pages/StatusPage/Status/Components/InfrastructureMetrics.tsx +++ b/client/src/Pages/StatusPage/Status/Components/InfrastructureMetrics.tsx @@ -10,19 +10,28 @@ import useMediaQuery from "@mui/material/useMediaQuery"; import Box from "@mui/material/Box"; import { LAYOUT, SPACING } from "@/Utils/Theme/constants"; +const GAUGE_RADIUS = 60; +const GAUGE_STROKE_WIDTH = 12; +const PERCENTAGE_MULTIPLIER = 100; + interface StatusPageMonitor extends Monitor { checks?: Monitor["recentChecks"]; - infrastructureCPU?: number; - infrastructureMemory?: number; - infrastructureDisk?: number; } -interface MetricDetailRowProps { +interface MetricDetail { label: string; value: string; } -const MetricDetailRow = ({ label, value }: MetricDetailRowProps) => { +interface MetricConfig { + key: string; + label: string; + hasData: boolean; + progress: number; + details: MetricDetail[]; +} + +const MetricDetailRow = ({ label, value }: MetricDetail) => { const theme = useTheme(); return ( { interface MetricItemProps { label: string; progress: number; - details?: MetricDetailRowProps[]; + details?: MetricDetail[]; } const MetricItem = ({ label, progress, details }: MetricItemProps) => { @@ -75,8 +84,8 @@ const MetricItem = ({ label, progress, details }: MetricItemProps) => { > {label} @@ -98,6 +107,92 @@ const MetricItem = ({ label, progress, details }: MetricItemProps) => { ); }; +type LatestCheck = NonNullable[number]; + +const buildCpuMetric = ( + check: LatestCheck, + t: (key: string) => string +): MetricConfig | null => { + if (!check.cpu || typeof check.cpu.usage_percent !== "number") { + return null; + } + const usagePercent = (check.cpu.usage_percent ?? 0) * PERCENTAGE_MULTIPLIER; + return { + key: "cpu", + label: t("pages.statusPages.monitorsList.infrastructure.cpu"), + hasData: true, + progress: usagePercent, + details: [ + { + label: t("pages.statusPages.monitorsList.infrastructure.usage"), + value: `${usagePercent.toFixed(2)}%`, + }, + ], + }; +}; + +const buildMemoryMetric = ( + check: LatestCheck, + t: (key: string) => string +): MetricConfig | null => { + if ( + !check.memory || + typeof check.memory.usage_percent !== "number" || + typeof check.memory.used_bytes !== "number" || + typeof check.memory.total_bytes !== "number" + ) { + return null; + } + return { + key: "memory", + label: t("pages.statusPages.monitorsList.infrastructure.memory"), + hasData: true, + progress: (check.memory.usage_percent ?? 0) * PERCENTAGE_MULTIPLIER, + details: [ + { + label: t("pages.statusPages.monitorsList.infrastructure.used"), + value: prettyBytes(check.memory.used_bytes ?? 0), + }, + { + label: t("pages.statusPages.monitorsList.infrastructure.total"), + value: prettyBytes(check.memory.total_bytes ?? 0), + }, + ], + }; +}; + +const buildDiskMetric = ( + check: LatestCheck, + t: (key: string) => string +): MetricConfig | null => { + if (!check.disk || check.disk.length === 0) { + return null; + } + const disks = check.disk; + const avgUsagePercent = + (disks.reduce((acc, disk) => acc + (disk?.usage_percent ?? 0), 0) / disks.length) * + PERCENTAGE_MULTIPLIER; + const totalUsedBytes = disks.reduce((acc, disk) => acc + (disk?.used_bytes ?? 0), 0); + const totalTotalBytes = disks.reduce((acc, disk) => acc + (disk?.total_bytes ?? 0), 0); + + return { + key: "disk", + label: t("pages.statusPages.monitorsList.infrastructure.disk"), + hasData: true, + progress: avgUsagePercent, + details: [ + { + label: t("pages.statusPages.monitorsList.infrastructure.used"), + value: prettyBytes(totalUsedBytes), + }, + { + label: t("pages.statusPages.monitorsList.infrastructure.total"), + value: prettyBytes(totalTotalBytes), + }, + ], + }; +}; + export const InfrastructureMetrics = ({ monitor }: { monitor: StatusPageMonitor }) => { const theme = useTheme(); const { t } = useTranslation(); @@ -115,68 +210,22 @@ export const InfrastructureMetrics = ({ monitor }: { monitor: StatusPageMonitor ); } - const metrics = [ - { - key: t("pages.statusPages.monitorsList.infrastructure.cpu"), - labelKey: t("pages.statusPages.monitorsList.infrastructure.cpu"), - hasData: latestCheck.cpu && typeof latestCheck.cpu.usage_percent === "number", - progress: latestCheck.cpu?.usage_percent ? latestCheck.cpu.usage_percent * 100 : 0, - details: [ - { - label: t("pages.statusPages.monitorsList.infrastructure.usage"), - value: `${((latestCheck.cpu?.usage_percent || 0) * 100).toFixed(2)}%`, - }, - ], - }, - { - key: t("pages.statusPages.monitorsList.infrastructure.memoryText"), - labelKey: t("pages.statusPages.monitorsList.infrastructure.memory"), - hasData: - latestCheck.memory && - typeof latestCheck.memory.usage_percent === "number" && - typeof latestCheck.memory.used_bytes === "number" && - typeof latestCheck.memory.total_bytes === "number", - progress: latestCheck.memory?.usage_percent - ? latestCheck.memory.usage_percent * 100 - : 0, - details: [ - { - label: t("pages.statusPages.monitorsList.infrastructure.used"), - value: prettyBytes(latestCheck.memory?.used_bytes || 0), - }, - { - label: t("pages.statusPages.monitorsList.infrastructure.total"), - value: prettyBytes(latestCheck.memory?.total_bytes || 0), - }, - ], - }, - { - key: t("pages.statusPages.monitorsList.infrastructure.disk"), - labelKey: t("pages.statusPages.monitorsList.infrastructure.disk"), - hasData: latestCheck.disk && latestCheck.disk.length > 0, - progress: latestCheck.disk - ? (latestCheck.disk.reduce((acc, disk) => acc + (disk?.usage_percent || 0), 0) / - latestCheck.disk.length) * - 100 - : 0, - details: [ - { - label: t("pages.statusPages.monitorsList.infrastructure.used"), - value: prettyBytes( - latestCheck?.disk?.reduce((acc, disk) => acc + (disk?.used_bytes || 0), 0) ?? - 0 - ), - }, - { - label: t("pages.statusPages.monitorsList.infrastructure.total"), - value: prettyBytes( - latestCheck?.disk?.reduce((acc, disk) => acc + (disk?.total_bytes || 0), 0) ?? - 0 - ), - }, - ], - }, - ]; + const metrics: MetricConfig[] = [ + buildCpuMetric(latestCheck, t), + buildMemoryMetric(latestCheck, t), + buildDiskMetric(latestCheck, t), + ].filter((m): m is MetricConfig => m !== null); + + if (metrics.length === 0) { + return ( + + {t("pages.statusPages.monitorsList.noData")} + + ); + } return ( - {metrics.map( - ({ key, labelKey, hasData, progress, details }) => - hasData && ( - - ) - )} + {metrics.map(({ key, label, progress, details }) => ( + + ))} ); };