OPNsense widget : all datas are locally stored in a single localStorage object.
This commit is contained in:
parent
fc263be8ad
commit
86f21afe1e
@ -52,11 +52,8 @@ export default function Component({ service }) {
|
|||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
const { widget } = service;
|
const { widget } = service;
|
||||||
const wid = widget.service_name + "." + widget.service_group;
|
const wid = `${widget.service_name}.${widget.service_group}`;
|
||||||
const rateStorage = `${wid}rate`;
|
const dataStorage = `${wid}datas`;
|
||||||
const dataStorage = `${wid}data`;
|
|
||||||
const timeStorage = `${wid}time`;
|
|
||||||
console.log("service: ", service, " wid: ", wid);
|
|
||||||
const { data: activityData, error: activityError } = useWidgetAPI(widget, "activity");
|
const { data: activityData, error: activityError } = useWidgetAPI(widget, "activity");
|
||||||
const { data: interfaceData, error: interfaceError } = useWidgetAPI(widget, "interface");
|
const { data: interfaceData, error: interfaceError } = useWidgetAPI(widget, "interface");
|
||||||
|
|
||||||
@ -81,58 +78,42 @@ export default function Component({ service }) {
|
|||||||
const cpu = 100 - parseFloat(cpuIdle);
|
const cpu = 100 - parseFloat(cpuIdle);
|
||||||
const memory = sumMemory(activityData.headers[3]);
|
const memory = sumMemory(activityData.headers[3]);
|
||||||
|
|
||||||
const wanUpload = interfaceData.interfaces.wan['bytes transmitted'];
|
const wanUpload = parseFloat(interfaceData.interfaces.wan['bytes transmitted']);
|
||||||
const wanDownload = interfaceData.interfaces.wan['bytes received'];
|
const wanDownload = parseFloat(interfaceData.interfaces.wan['bytes received']);
|
||||||
const datas = localStorage.getItem(dataStorage);
|
const dataStored = localStorage.getItem(dataStorage);
|
||||||
|
let datas;
|
||||||
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 (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) {
|
if (wanUploadDiff > 0 || wanDownloadDiff > 0) {
|
||||||
const specialTimeValue = new Date().getTime();
|
const specialTimeValue = new Date().getTime();
|
||||||
console.log("Special time: ", specialTimeValue);
|
const timeDif = specialTimeValue - datas.updateTime;
|
||||||
const specialTime = localStorage.getItem(timeStorage);
|
datas.wanUploadRate = 8 * wanUploadDiff / (timeDif / 1000);
|
||||||
if (specialTime !== null) {
|
datas.wanDownloadRate = 8 * wanDownloadDiff / (timeDif / 1000);
|
||||||
const specialTimeObj = JSON.parse(specialTime);
|
datas.updateTime = specialTimeValue;
|
||||||
const timeDif = specialTimeValue - specialTimeObj.specialtime;
|
|
||||||
wanUploadRate = 8 * wanUploadDiff / (timeDif / 1000);
|
|
||||||
wanDownloadRate = 8 * wanDownloadDiff / (timeDif / 1000);
|
|
||||||
localStorage.setItem(rateStorage, JSON.stringify({
|
|
||||||
wanUploadRate,
|
|
||||||
wanDownloadRate
|
|
||||||
}));
|
|
||||||
console.log("Time diff: ", timeDif, "wanUploadRate: ", wanUploadRate, "wanDownloadRate: ", wanDownloadRate);
|
|
||||||
}
|
}
|
||||||
localStorage.setItem(timeStorage, JSON.stringify({specialtime: specialTimeValue}));
|
datas.wanUpload = wanUpload;
|
||||||
} else {
|
datas.wanDownload = wanDownload;
|
||||||
const rate = localStorage.getItem(rateStorage);
|
localStorage.setItem(dataStorage, JSON.stringify(datas));
|
||||||
if (rate !== null) {
|
|
||||||
const rateObj = JSON.parse(rate);
|
|
||||||
wanUploadRate = rateObj.wanUploadRate;
|
|
||||||
wanDownloadRate = rateObj.wanDownloadRate;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
console.log("wanUploadDiff:", wanUploadDiff);
|
|
||||||
console.log("wanDownloadDiff:", wanDownloadDiff);
|
|
||||||
}
|
|
||||||
localStorage.setItem(dataStorage, JSON.stringify({
|
|
||||||
wanUpload,
|
|
||||||
wanDownload,
|
|
||||||
time: Date.now()}));
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Container service={service}>
|
<Container service={service}>
|
||||||
<Block label="opnsense.cpu" value={t("common.percent", { value: cpu.toFixed(2)})} />
|
<Block label="opnsense.cpu" value={t("common.percent", { value: cpu.toFixed(2)})} />
|
||||||
<Block label="opnsense.memory" value={t("common.percent", { value: memory})} />
|
<Block label="opnsense.memory" value={t("common.percent", { value: memory})} />
|
||||||
<Block label="opnsense.wanUpload" value={t("common.bytes", { value: wanUpload})} />
|
<Block label="opnsense.wanUpload" value={t("common.bytes", { value: datas.wanUpload})} />
|
||||||
<Block label="opnsense.wanDownload" value={t("common.bytes", { value: wanDownload })} />
|
<Block label="opnsense.wanDownload" value={t("common.bytes", { value: datas.wanDownload })} />
|
||||||
<Block label="opnsense.wanUploadRate" value={t("common.bitrate", { value: wanUploadRate})} />
|
<Block label="opnsense.wanUploadRate" value={t("common.bitrate", { value: datas.wanUploadRate})} />
|
||||||
<Block label="opnsense.wanDownloadRate" value={t("common.bitrate", { value: wanDownloadRate})} />
|
<Block label="opnsense.wanDownloadRate" value={t("common.bitrate", { value: datas.wanDownloadRate})} />
|
||||||
</Container>
|
</Container>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user