OPNsense widget : reporting a correct value for memory usage, added two fields for wan upload & download rate. Still working on those, code is not ready to use.

This commit is contained in:
Benoit 2022-12-29 13:47:11 +01:00
parent b44a5ac54e
commit 5bdcc34299
2 changed files with 48 additions and 3 deletions

View File

@ -407,6 +407,9 @@
"memory": "Memory Used", "memory": "Memory Used",
"uptime": "Uptime", "uptime": "Uptime",
"wanUpload": "WAN Upload", "wanUpload": "WAN Upload",
"wanDownload": "WAN Download" "wanDownload": "WAN Download",
"wanUploadRate": "WAN Upload Rate",
"wanDownloadRate": "WAN Download Rate"
} }
} }

View File

@ -6,7 +6,6 @@ import useWidgetAPI from "utils/proxy/use-widget-api";
export default function Component({ service }) { export default function Component({ service }) {
function toKb(value, unit) { function toKb(value, unit) {
console.log("toKB : value: ", value, " Unit:", unit);
switch (unit) { switch (unit) {
case "K": case "K":
return parseInt(value, 10); return parseInt(value, 10);
@ -80,6 +79,47 @@ export default function Component({ service }) {
const wanUpload = interfaceData.interfaces.wan['bytes transmitted']; const wanUpload = interfaceData.interfaces.wan['bytes transmitted'];
const wanDownload = interfaceData.interfaces.wan['bytes received']; const wanDownload = interfaceData.interfaces.wan['bytes received'];
const datas = localStorage.getItem('opnsensewidgetdatas');
let wanUploadRate = 0;
let wanDownloadRate = 0;
if (datas !== null) {
const datasObj = JSON.parse(datas);
console.log("Dataobj:", datasObj);
const wanUploadDiff = wanUpload - datasObj.wanUpload;
const wanDownloadDiff = wanDownload - datasObj.wanDownload;
if (wanUploadDiff > 0 || wanDownloadDiff > 0) {
const specialTimeValue = new Date().getTime();
console.log("Special time: ", specialTimeValue);
const specialTime = localStorage.getItem('opnsensewidgettime');
if (specialTime !== null) {
const specialTimeObj = JSON.parse(specialTime);
const timeDif = specialTimeValue - specialTimeObj.specialtime;
wanUploadRate = 8 * wanUploadDiff / (timeDif / 1000);
wanDownloadRate = 8 * wanDownloadDiff / (timeDif / 1000);
localStorage.setItem('opnsensewidgetrate', JSON.stringify({
wanUploadRate,
wanDownloadRate
}));
console.log("Time diff: ", timeDif, "wanUploadRate: ", wanUploadRate, "wanDownloadRate: ", wanDownloadRate);
}
localStorage.setItem('opnsensewidgettime', JSON.stringify({specialtime: specialTimeValue}));
} else {
const rate = localStorage.getItem('opnsensewidgetrate');
if (rate !== null) {
const rateObj = JSON.parse(rate);
wanUploadRate = rateObj.wanUploadRate;
wanDownloadRate = rateObj.wanDownloadRate;
}
}
console.log("wanUploadDiff:", wanUploadDiff);
console.log("wanDownloadDiff:", wanDownloadDiff);
}
localStorage.setItem('opnsensewidgetdatas', JSON.stringify({
wanUpload,
wanDownload,
time: Date.now()}));
return ( return (
<Container service={service}> <Container service={service}>
@ -88,6 +128,8 @@ export default function Component({ service }) {
<Block label="opnsense.wanUpload" value={t("common.bytes", { value: wanUpload})} /> <Block label="opnsense.wanUpload" value={t("common.bytes", { value: wanUpload})} />
<Block label="opnsense.wanDownload" value={t("common.bytes", { value: wanDownload })} /> <Block label="opnsense.wanDownload" value={t("common.bytes", { value: wanDownload })} />
<Block label="opnsense.wanUploadRate" value={t("common.bitrate", { value: wanUploadRate})} />
<Block label="opnsense.wanDownloadRate" value={t("common.bitrate", { value: wanDownloadRate})} />
</Container> </Container>
); );
} }