From c4400eb0a34e9b7e299bb18b214ad880ec87a383 Mon Sep 17 00:00:00 2001 From: Henry Dollman Date: Mon, 14 Oct 2024 18:48:19 -0400 Subject: [PATCH] refactor container-chart.tsx --- .../src/components/charts/container-chart.tsx | 93 +++++++++++-------- 1 file changed, 53 insertions(+), 40 deletions(-) diff --git a/beszel/site/src/components/charts/container-chart.tsx b/beszel/site/src/components/charts/container-chart.tsx index c821354..f2ddc18 100644 --- a/beszel/site/src/components/charts/container-chart.tsx +++ b/beszel/site/src/components/charts/container-chart.tsx @@ -59,7 +59,7 @@ export default memo(function ContainerChart({ totalUsage[key] = 0 } if (isNetChart) { - totalUsage[key] += stats[key]?.ns ?? 0 + stats[key]?.nr ?? 0 + totalUsage[key] += (stats[key]?.nr ?? 0) + (stats[key]?.ns ?? 0) } else { // @ts-ignore totalUsage[key] += stats[key]?.[dataKey] ?? 0 @@ -80,6 +80,55 @@ export default memo(function ContainerChart({ return config satisfies ChartConfig }, [chartData]) + const { toolTipFormatter, dataFunction, tickFormatter } = useMemo(() => { + const obj = {} as { + toolTipFormatter: (item: any, key: string) => React.ReactNode | string + dataFunction: (key: string, data: any) => number | null + tickFormatter: (value: any) => string + } + // tick formatter + if (chartName === 'cpu') { + obj.tickFormatter = (value) => { + const val = toFixedWithoutTrailingZeros(value, 2) + unit + return updateYAxisWidth(val) + } + } else { + obj.tickFormatter = (value) => { + const { v, u } = getSizeAndUnit(value, false) + return updateYAxisWidth(`${toFixedFloat(v, 2)}${u}${isNetChart ? '/s' : ''}`) + } + } + // tooltip formatter + if (isNetChart) { + obj.toolTipFormatter = (item: any, key: string) => { + try { + const sent = item?.payload?.[key]?.ns ?? 0 + const received = item?.payload?.[key]?.nr ?? 0 + return ( + + {decimalString(received)} MB/s + rx + + {decimalString(sent)} MB/s + tx + + ) + } catch (e) { + return null + } + } + } else { + obj.toolTipFormatter = (item: any) => decimalString(item.value) + unit + } + // data function + if (isNetChart) { + obj.dataFunction = (key: string, data: any) => (data[key]?.nr ?? 0) + (data[key]?.ns ?? 0) + } else { + obj.dataFunction = (key: string, data: any) => data[key]?.[dataKey] ?? 0 + } + return obj + }, []) + // console.log('rendered at', new Date()) return ( @@ -100,14 +149,7 @@ export default memo(function ContainerChart({ { - if (chartName === 'cpu') { - const val = toFixedWithoutTrailingZeros(value, 2) + unit - return updateYAxisWidth(val) - } - const { v, u } = getSizeAndUnit(value, false) - return updateYAxisWidth(`${toFixedFloat(v, 2)}${u}${isNetChart ? '/s' : ''}`) - }} + tickFormatter={tickFormatter} tickLine={false} axisLine={false} /> @@ -129,31 +171,7 @@ export default memo(function ContainerChart({ labelFormatter={(_, data) => formatShortDate(data[0].payload.created)} // @ts-ignore itemSorter={(a, b) => b.value - a.value} - content={ - { - if (!isNetChart) { - return decimalString(item.value) + unit - } - try { - const sent = item?.payload?.[key]?.ns ?? 0 - const received = item?.payload?.[key]?.nr ?? 0 - return ( - - {decimalString(received)} MB/s - rx - - {decimalString(sent)} MB/s - tx - - ) - } catch (e) { - return null - } - }} - /> - } + content={} /> {Object.keys(chartConfig).map((key) => { const filtered = filter && !key.includes(filter) @@ -163,12 +181,7 @@ export default memo(function ContainerChart({ { - if (isNetChart) { - return data[key]?.ns ?? 0 + data?.[key]?.nr ?? 0 - } - return data[key]?.[dataKey] ?? 0 - }} + dataKey={dataFunction.bind(null, key)} name={key} type="monotoneX" fill={chartConfig[key].color}