From 2dc10624d199d2c9fc520d6431988763338f04c8 Mon Sep 17 00:00:00 2001 From: SunnyCloudy Date: Sat, 23 Mar 2024 00:17:38 +0200 Subject: [PATCH] fix glances weirdness --- docs/widgets/services/glances.md | 3 ++ src/utils/config/service-helpers.js | 2 + src/widgets/glances/metrics/cpu.jsx | 66 +++++++++++++++++++++++----- src/widgets/glances/metrics/info.jsx | 5 ++- 4 files changed, 64 insertions(+), 12 deletions(-) diff --git a/docs/widgets/services/glances.md b/docs/widgets/services/glances.md index 134dcb5f6..69676bc65 100644 --- a/docs/widgets/services/glances.md +++ b/docs/widgets/services/glances.md @@ -19,6 +19,9 @@ widget: password: pass # optional if auth enabled in Glances metric: cpu diskUnits: bytes # optional, bytes (default) or bbytes. Only applies to disk + tempUnits: imperial # optional, metric (default) or imperial. Only applies to cpu + refreshInterval: 5000 # optional - in milliseconds, defaults to 1000 or more, depending on the metric + pointsLimit: 15 # optional, defaults to 15 ``` _Please note, this widget does not need an `href`, `icon` or `description` on its parent service. To achieve the same effect as the examples above, see as an example:_ diff --git a/src/utils/config/service-helpers.js b/src/utils/config/service-helpers.js index c4ca2a65b..9fa733ba0 100644 --- a/src/utils/config/service-helpers.js +++ b/src/utils/config/service-helpers.js @@ -398,6 +398,7 @@ export function cleanServiceGroups(groups) { metric, pointsLimit, diskUnits, + tempUnits, // glances, customapi, iframe refreshInterval, @@ -537,6 +538,7 @@ export function cleanServiceGroups(groups) { if (refreshInterval) cleanedService.widget.refreshInterval = refreshInterval; if (pointsLimit) cleanedService.widget.pointsLimit = pointsLimit; if (diskUnits) cleanedService.widget.diskUnits = diskUnits; + if (tempUnits) cleanedService.widget.tempUnits = tempUnits; } if (type === "mjpeg") { if (stream) cleanedService.widget.stream = stream; diff --git a/src/widgets/glances/metrics/cpu.jsx b/src/widgets/glances/metrics/cpu.jsx index c36aba9d2..b159eb6f1 100644 --- a/src/widgets/glances/metrics/cpu.jsx +++ b/src/widgets/glances/metrics/cpu.jsx @@ -12,6 +12,11 @@ const Chart = dynamic(() => import("../components/chart"), { ssr: false }); const defaultPointsLimit = 15; const defaultInterval = 1000; +const cpuSensorLabels = ["cpu_thermal", "Core", "Tctl"]; + +function convertToFahrenheit(t) { + return (t * 9) / 5 + 32; +} export default function Component({ service }) { const { t } = useTranslation(); @@ -24,7 +29,34 @@ export default function Component({ service }) { refreshInterval: Math.max(defaultInterval, refreshInterval), }); - const { data: systemData, error: systemError } = useWidgetAPI(service.widget, "system"); + const { data: sensorData, error: sensorError } = useWidgetAPI(service.widget, "sensors", { + refreshInterval: Math.max(defaultInterval, refreshInterval), + }); + + const { data: quicklookData, error: quicklookError } = useWidgetAPI(service.widget, "quicklook"); + + const unit = widget.tempUnits === "imperial" ? "fahrenheit" : "celsius"; + let mainTemp = 0; + let maxTemp = 80; + const cpuSensors = sensorData?.filter( + (s) => cpuSensorLabels.some((label) => s.label.startsWith(label)) && s.type === "temperature_core", + ); + + if (cpuSensors) { + try { + mainTemp = cpuSensors.reduce((acc, s) => acc + s.value, 0) / cpuSensors.length; + maxTemp = Math.max( + cpuSensors.reduce((acc, s) => acc + (s.warning > 0 ? s.warning : 0), 0) / cpuSensors.length, + maxTemp, + ); + if (unit === "fahrenheit") { + mainTemp = convertToFahrenheit(mainTemp); + maxTemp = convertToFahrenheit(maxTemp); + } + } catch (e) { + // cpu sensor retrieval failed + } + } useEffect(() => { if (data) { @@ -71,22 +103,34 @@ export default function Component({ service }) { /> )} - {!chart && systemData && !systemError && ( + {!chart && quicklookData && !quicklookError && ( -
- {systemData.linux_distro && `${systemData.linux_distro} - `} - {systemData.os_version && systemData.os_version} +
+ {quicklookData.cpu_name && quicklookData.cpu_name}
)} - {systemData && !systemError && ( + {quicklookData && !quicklookError && ( - {systemData.linux_distro && chart &&
{systemData.linux_distro}
} - - {systemData.os_version && chart &&
{systemData.os_version}
} - - {systemData.hostname &&
{systemData.hostname}
} + {mainTemp > 0 && ( +
+ {t("common.number", { + value: mainTemp, + maximumFractionDigits: 1, + style: "unit", + unit, + })} ({t("glances.warn")} @  + {t("common.number", { + value: maxTemp, + maximumFractionDigits: 1, + style: "unit", + unit, + })}) +
+ )} + + {quicklookData.cpu_name && chart &&
{quicklookData.cpu_name}
}
)} diff --git a/src/widgets/glances/metrics/info.jsx b/src/widgets/glances/metrics/info.jsx index e7555bced..9a161d1eb 100644 --- a/src/widgets/glances/metrics/info.jsx +++ b/src/widgets/glances/metrics/info.jsx @@ -122,7 +122,10 @@ export default function Component({ service }) { )} {!chart && quicklookData?.swap === 0 && ( -
{quicklookData.cpu_name}
+
+ {systemData.linux_distro && `${systemData.linux_distro} - `} + {systemData.os_version && systemData.os_version} +
)}
{!chart && }