From 314cee081a25750a82275c12caaf87f5e4d73cd7 Mon Sep 17 00:00:00 2001 From: Henry Dollman Date: Wed, 18 Sep 2024 12:47:06 -0400 Subject: [PATCH] system info refactoring --- beszel/internal/entities/system/system.go | 11 +- beszel/site/src/components/routes/system.tsx | 111 +++++++++---------- beszel/site/src/components/ui/icons.tsx | 13 +++ 3 files changed, 72 insertions(+), 63 deletions(-) create mode 100644 beszel/site/src/components/ui/icons.tsx diff --git a/beszel/internal/entities/system/system.go b/beszel/internal/entities/system/system.go index 41f9909..ee5b239 100644 --- a/beszel/internal/entities/system/system.go +++ b/beszel/internal/entities/system/system.go @@ -45,12 +45,11 @@ type NetIoStats struct { } type Info struct { - Hostname string `json:"h"` - KernelVersion string `json:"k"` - - Cores int `json:"c"` - Threads int `json:"t"` - CpuModel string `json:"m"` + Hostname string `json:"h"` + KernelVersion string `json:"k,omitempty"` + Cores int `json:"c"` + Threads int `json:"t"` + CpuModel string `json:"m"` // Os string `json:"o"` Uptime uint64 `json:"u"` Cpu float64 `json:"cpu"` diff --git a/beszel/site/src/components/routes/system.tsx b/beszel/site/src/components/routes/system.tsx index 7fc4603..5d5e581 100644 --- a/beszel/site/src/components/routes/system.tsx +++ b/beszel/site/src/components/routes/system.tsx @@ -12,7 +12,6 @@ import { MonitorIcon, StretchHorizontalIcon, XIcon, - BinaryIcon } from 'lucide-react' import ChartTimeSelect from '../charts/chart-time-select' import { @@ -27,6 +26,7 @@ import { scaleTime } from 'd3-scale' import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '../ui/tooltip' import { Button, buttonVariants } from '../ui/button' import { Input } from '../ui/input' +import { TuxIcon } from '../ui/icons' const CpuChart = lazy(() => import('../charts/cpu-chart')) const ContainerCpuChart = lazy(() => import('../charts/container-cpu-chart')) @@ -200,13 +200,37 @@ export default function SystemDetail({ name }: { name: string }) { setDockerNetChartData(dockerNetData) }, []) - const uptime = useMemo(() => { - let uptime = system.info?.u || 0 - if (uptime < 172800) { - return `${Math.trunc(uptime / 3600)} hours` + // values for system info bar + const systemInfo = useMemo(() => { + if (!system.info) { + return [] } - return `${Math.trunc(system.info?.u / 86400)} days` - }, [system.info?.u]) + console.log('doing') + let uptime: number | string = system.info.u + if (system.info.u < 172800) { + uptime = `${Math.trunc(uptime / 3600)} hours` + } else { + uptime = `${Math.trunc(system.info?.u / 86400)} days` + } + return [ + { value: system.host, Icon: GlobeIcon }, + { + value: system.info.h, + Icon: MonitorIcon, + label: 'Hostname', + // hide if hostname is same as host or name + hide: system.info.h === system.host || system.info.h === system.name, + }, + { value: uptime, Icon: ClockArrowUp, label: 'Uptime' }, + { value: system.info.k, Icon: TuxIcon, label: 'Kernel' }, + { value: `${system.info.m} (${system.info.c}c/${system.info.t}t)`, Icon: CpuIcon }, + ] as { + value: string | number | undefined + label?: string + Icon: any + hide?: boolean + }[] + }, [system.info]) /** Space for tooltip if more than 12 containers */ const bottomSpacing = useMemo(() => { @@ -253,58 +277,31 @@ export default function SystemDetail({ name }: { name: string }) { {system.status} - -
- {system.host} -
- {/* show hostname if it's different than host or name */} - {system.info?.h && system.info.h != system.host && system.info.h != system.name && ( - - - - -
- {system.info.h} -
-
- Hostname -
-
- )} - {/* kernel */} - - - - -
- {system.info.k} -
-
- Kernel -
-
- {system.info?.u && ( - - - - -
- {uptime} -
-
- Uptime -
-
- )} - {system.info?.m && ( - <> - + {systemInfo.map(({ value, label, Icon, hide }, i) => { + if (hide || !value) { + return null + } + const content = (
- - {system.info.m} ({system.info.c}c/{system.info.t}t) + {value}
- - )} + ) + return ( +
+ + {label ? ( + + + {content} + {label} + + + ) : ( + content + )} +
+ ) + })}
diff --git a/beszel/site/src/components/ui/icons.tsx b/beszel/site/src/components/ui/icons.tsx new file mode 100644 index 0000000..4b66b13 --- /dev/null +++ b/beszel/site/src/components/ui/icons.tsx @@ -0,0 +1,13 @@ +import { SVGProps } from 'react' + +// linux-logo-bold from https://github.com/phosphor-icons/core (MIT license) +export function TuxIcon(props: SVGProps) { + return ( + + + + ) +}