From f830faf8af313b44ff756fee38affbef73198617 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Sun, 5 Mar 2023 20:53:30 -0800 Subject: [PATCH] update glances widget --- public/locales/en/common.json | 6 ++- src/components/widgets/glances/glances.jsx | 42 +++++++++++++++--- src/pages/api/widgets/glances.js | 50 ++++++++++++++++------ 3 files changed, 78 insertions(+), 20 deletions(-) diff --git a/public/locales/en/common.json b/public/locales/en/common.json index a121a7e3b..3eb3c4786 100755 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -305,7 +305,11 @@ "glances": { "cpu": "CPU", "mem": "MEM", - "wait": "Please wait" + "wait": "Please wait", + "temp": "TEMP", + "uptime": "UP", + "days": "d", + "hours": "h" }, "quicklaunch": { "bookmark": "Bookmark", diff --git a/src/components/widgets/glances/glances.jsx b/src/components/widgets/glances/glances.jsx index a48cef50c..d91cb2633 100644 --- a/src/components/widgets/glances/glances.jsx +++ b/src/components/widgets/glances/glances.jsx @@ -1,6 +1,6 @@ import useSWR from "swr"; import { BiError } from "react-icons/bi"; -import { FaMemory } from "react-icons/fa"; +import { FaMemory, FaRegClock, FaThermometerHalf } from "react-icons/fa"; import { FiCpu } from "react-icons/fi"; import { useTranslation } from "next-i18next"; @@ -64,6 +64,9 @@ export default function Widget({ options }) { ); } + const unit = options.units === "imperial" ? "fahrenheit" : "celsius"; + const mainTemp = (options.cputemp && data.sensors && unit === "celsius") ? data.sensors.find(s => s.label.includes("cpu_thermal")).value : data.sensors.find(s => s.label.includes("cpu_thermal")).value * 5/9 + 32; + return (
@@ -73,7 +76,7 @@ export default function Widget({ options }) {
{t("common.number", { - value: data.cpu, + value: data.quicklook.cpu, style: "unit", unit: "percent", maximumFractionDigits: 0, @@ -81,7 +84,7 @@ export default function Widget({ options }) {
{t("glances.cpu")}
- +
@@ -90,7 +93,7 @@ export default function Widget({ options }) {
{t("common.number", { - value: data.mem, + value: data.quicklook.mem, style: "unit", unit: "percent", maximumFractionDigits: 0, @@ -98,9 +101,38 @@ export default function Widget({ options }) {
{t("glances.mem")}
- +
+ {options.cputemp && + (
+ +
+ +
+ {t("common.number", { + value: mainTemp, + maximumFractionDigits: 1, + style: "unit", + unit + })} +
+
{t("glances.temp")}
+
+
+
)} + {options.uptime && data.uptime && + (
+ +
+ +
+ {data.uptime.replace(" days,", t("glances.days")).replace(/:\d\d:\d\d$/g, t("glances.hours"))} +
+
{t("glances.uptime")}
+
+
+
)} {options.label && (
{options.label}
diff --git a/src/pages/api/widgets/glances.js b/src/pages/api/widgets/glances.js index 5d4622b8d..46be14a05 100644 --- a/src/pages/api/widgets/glances.js +++ b/src/pages/api/widgets/glances.js @@ -4,19 +4,16 @@ import { getPrivateWidgetOptions } from "utils/config/widget-helpers"; const logger = createLogger("glances"); -export default async function handler(req, res) { - const { index } = req.query; - - const privateWidgetOptions = await getPrivateWidgetOptions("glances", index); - +async function retrieveFromGlancesAPI(privateWidgetOptions, endpoint) { + let errorMessage; const url = privateWidgetOptions?.url; if (!url) { - const errorMessage = "Missing Glances URL"; + errorMessage = "Missing Glances URL"; logger.error(errorMessage); - return res.status(400).json({ error: errorMessage }); + throw new Error(errorMessage); } - const apiUrl = `${url}/api/3/quicklook`; + const apiUrl = `${url}/api/3/${endpoint}`; const headers = { "Accept-Encoding": "application/json" }; @@ -25,16 +22,41 @@ export default async function handler(req, res) { } const params = { method: "GET", headers }; - const [status, contentType, data] = await httpProxy(apiUrl, params); + const [status, , data] = await httpProxy(apiUrl, params); if (status === 401) { - logger.error("Authorization failure getting data from glances API. Data: %s", data); + errorMessage = `Authorization failure getting data from glances API. Data: ${data.toString()}` + logger.error(errorMessage); + throw new Error(errorMessage); } - + if (status !== 200) { - logger.error("HTTP %d getting data from glances API. Data: %s", status, data); + errorMessage = `HTTP ${status} getting data from glances API. Data: ${data.toString()}` + logger.error(errorMessage); + throw new Error(errorMessage); } - if (contentType) res.setHeader("Content-Type", contentType); - return res.status(status).send(data); + return JSON.parse(Buffer.from(data).toString()); +} + +export default async function handler(req, res) { + const { index } = req.query; + + const privateWidgetOptions = await getPrivateWidgetOptions("glances", index); + + try { + const quicklookData = await retrieveFromGlancesAPI(privateWidgetOptions, "quicklook"); + + const data = { + quicklook: quicklookData + } + + data.uptime = await retrieveFromGlancesAPI(privateWidgetOptions, "uptime"); + + data.sensors = await retrieveFromGlancesAPI(privateWidgetOptions, "sensors"); + + return res.status(200).send(data); + } catch (e) { + return res.status(400).json({ error: e.message }); + } }