From 977965babb68963c798e81f33f8714fff7ea2812 Mon Sep 17 00:00:00 2001 From: Benoit Date: Wed, 11 Jan 2023 21:59:46 +0100 Subject: [PATCH 1/2] Synology widget : adding volume size --- public/locales/en/common.json | 3 ++- src/widgets/synology/component.jsx | 2 ++ src/widgets/synology/proxy.js | 6 +++--- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/public/locales/en/common.json b/public/locales/en/common.json index b1481caa..ad77cbb2 100644 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -418,7 +418,8 @@ "volumeUsage": "Volume Usage", "cpuLoad": "CPU Load", "memoryUsage": "Memory Usage", - "status": "Status" + "status": "Status", + "volumeTotal": "Total space" }, "opnsense": { "cpu": "CPU Load", diff --git a/src/widgets/synology/component.jsx b/src/widgets/synology/component.jsx index fd832c8e..95405e84 100644 --- a/src/widgets/synology/component.jsx +++ b/src/widgets/synology/component.jsx @@ -20,6 +20,7 @@ export default function Component({ service }) { + @@ -31,6 +32,7 @@ export default function Component({ service }) { + diff --git a/src/widgets/synology/proxy.js b/src/widgets/synology/proxy.js index 4c4d600a..aa1023bc 100644 --- a/src/widgets/synology/proxy.js +++ b/src/widgets/synology/proxy.js @@ -76,8 +76,6 @@ export default async function synologyProxyHandler(req, res) { [status, contentType, data] = await httpProxy(storageUrl ); - - let usedVolume = 0; if (status !== 200) { return res.status(status).set("Content-Type", contentType).send(data); } @@ -85,7 +83,8 @@ export default async function synologyProxyHandler(req, res) { if (json?.success !== true) { return res.status(401).json({ error: "Error getting volume stats" }); } - usedVolume = 100 * parseFloat(json.data.vol_info[0].used_size) / parseFloat(json.data.vol_info[0].total_size); + const totalSize = parseFloat(json.data.vol_info[0].total_size); + const usedVolume = 100 * parseFloat(json.data.vol_info[0].used_size) / parseFloat(json.data.vol_info[0].total_size); const healthUrl = `${widget.url}/webapi/${path}?api=${api}&version=${maxVersion}&method=info&_sid=${sid}`; [status, contentType, data] = await httpProxy(healthUrl); @@ -110,6 +109,7 @@ export default async function synologyProxyHandler(req, res) { const resdata = { uptime, usedVolume, + totalSize, memoryUsage, cpuLoad, } From b341320f9a4b1e9f61c3d0cb59f62dd06111cdfe Mon Sep 17 00:00:00 2001 From: Benoit Date: Thu, 12 Jan 2023 09:55:21 +0100 Subject: [PATCH 2/2] Synology widget : undoing a merge error with opnsense widget branch --- src/widgets/opnsense/component.jsx | 90 +++--------------------------- 1 file changed, 9 insertions(+), 81 deletions(-) diff --git a/src/widgets/opnsense/component.jsx b/src/widgets/opnsense/component.jsx index 698d2d30..53396b31 100644 --- a/src/widgets/opnsense/component.jsx +++ b/src/widgets/opnsense/component.jsx @@ -5,54 +5,10 @@ import Block from "components/services/widget/block"; import useWidgetAPI from "utils/proxy/use-widget-api"; export default function Component({ service }) { - function toKb(value, unit) { - switch (unit) { - case "K": - return parseInt(value, 10); - case "M": - return parseInt(value, 10) * 1024; - case "G": - return parseInt(value, 10) * 1024 * 1024; - default: - return parseInt(value, 10); - } - } - - function sumMemory(meminfos) { - let result; - let sumused=0; - let sumfree=0; - - const idused = ["Active", "Wired", "Laundry", "Buf"]; - const idfree = ["Inact", "Free"]; - const size = "([0-9]+)([KMG])"; - - for (let id = 0; id < idused.length;id+=1 ) { - const re = new RegExp(`${size } ${ idused[id] }`); - result = re.exec(meminfos); - - if (result) { - sumused += toKb(result[1], result[2]); - } - } - - for (let id = 0; id < idfree.length; id+=1 ) { - const re = new RegExp(`${size } ${ idfree[id] }`); - result = re.exec(meminfos); - - if (result) { - sumfree += toKb(result[1], result[2]); - } - } - - return 100*(sumused / (sumused + sumfree)); - - } - const { t } = useTranslation(); const { widget } = service; - const dataStorage = `${widget.service_name}.${widget.service_group}datas`; + const { data: activityData, error: activityError } = useWidgetAPI(widget, "activity"); const { data: interfaceData, error: interfaceError } = useWidgetAPI(widget, "interface"); @@ -68,8 +24,6 @@ export default function Component({ service }) { - - ); } @@ -77,44 +31,18 @@ export default function Component({ service }) { const cpuIdle = activityData.headers[2].match(/ ([0-9.]+)% idle/)[1]; const cpu = 100 - parseFloat(cpuIdle); - const memory = sumMemory(activityData.headers[3]); + const memory = activityData.headers[3].match(/Mem: (.+) Active,/)[1]; - const wanUpload = parseFloat(interfaceData.interfaces.wan['bytes transmitted']); - const wanDownload = parseFloat(interfaceData.interfaces.wan['bytes received']); - const dataStored = localStorage.getItem(dataStorage); - let datas; + const wanUpload = interfaceData.interfaces.wan['bytes transmitted']; + const wanDownload = interfaceData.interfaces.wan['bytes received']; - if (dataStored === null) { - datas = { - wanUpload : 0, - wanDownload : 0, - updateTime : 0, - wanUploadRate : 0, - wanDownloadRate : 0 - } - } else { - datas = JSON.parse(dataStored); - } - const wanUploadDiff = wanUpload - datas.wanUpload; - const wanDownloadDiff = wanDownload - datas.wanDownload; - if (wanUploadDiff > 0 || wanDownloadDiff > 0) { - const specialTimeValue = new Date().getTime(); - const timeDif = specialTimeValue - datas.updateTime; - datas.wanUploadRate = 8 * wanUploadDiff / (timeDif / 1000); - datas.wanDownloadRate = 8 * wanDownloadDiff / (timeDif / 1000); - datas.updateTime = specialTimeValue; - } - datas.wanUpload = wanUpload; - datas.wanDownload = wanDownload; - localStorage.setItem(dataStorage, JSON.stringify(datas)); return ( - - - - - - + + + + + ); }