Synology widget : working on code stability
This commit is contained in:
parent
702f05cebf
commit
a08d0c6e60
@ -415,7 +415,10 @@
|
|||||||
},
|
},
|
||||||
"synology": {
|
"synology": {
|
||||||
"uptime": "Uptime",
|
"uptime": "Uptime",
|
||||||
"volumeUsage": "Volume Usage"
|
"volumeUsage": "Volume Usage",
|
||||||
|
"cpuLoad": "CPU Load",
|
||||||
|
"memoryUsage": "Memory Usage",
|
||||||
|
"status": "Status"
|
||||||
},
|
},
|
||||||
"opnsense": {
|
"opnsense": {
|
||||||
"cpu": "CPU Load",
|
"cpu": "CPU Load",
|
||||||
|
|||||||
@ -52,8 +52,7 @@ 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 dataStorage = `${widget.service_name}.${widget.service_group}datas`;
|
||||||
const dataStorage = `${wid}datas`;
|
|
||||||
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");
|
||||||
|
|
||||||
@ -69,6 +68,8 @@ export default function Component({ service }) {
|
|||||||
<Block label="opnsense.memory" />
|
<Block label="opnsense.memory" />
|
||||||
<Block label="opnsense.wanUpload" />
|
<Block label="opnsense.wanUpload" />
|
||||||
<Block label="opnsense.wanDownload" />
|
<Block label="opnsense.wanDownload" />
|
||||||
|
<Block label="opnsense.wanUploadRate" />
|
||||||
|
<Block label="opnsense.wanDownloadRate" />
|
||||||
</Container>
|
</Container>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -20,7 +20,8 @@ export default function Component({ service }) {
|
|||||||
<Container service={service}>
|
<Container service={service}>
|
||||||
<Block label="synology.uptime" />
|
<Block label="synology.uptime" />
|
||||||
<Block label="synology.volumeUsage" />
|
<Block label="synology.volumeUsage" />
|
||||||
|
<Block label="synology.cpuLoad" />
|
||||||
|
<Block label="synology.memoryUsage" />
|
||||||
</Container>
|
</Container>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -30,7 +31,8 @@ export default function Component({ service }) {
|
|||||||
<Container service={service}>
|
<Container service={service}>
|
||||||
<Block label="synology.uptime" value={ dsData.uptime } />
|
<Block label="synology.uptime" value={ dsData.uptime } />
|
||||||
<Block label="synology.volumeUsage" value={t("common.percent", { value: dsData.usedVolume })} />
|
<Block label="synology.volumeUsage" value={t("common.percent", { value: dsData.usedVolume })} />
|
||||||
|
<Block label="synology.cpuLoad" value={t("common.percent", { value: dsData.cpuLoad })} />
|
||||||
|
<Block label="synology.memoryUsage" value={t("common.percent", { value: dsData.memoryUsage })} />
|
||||||
</Container>
|
</Container>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,43 +2,42 @@ import { formatApiCall } from "utils/proxy/api-helpers";
|
|||||||
import { httpProxy } from "utils/proxy/http";
|
import { httpProxy } from "utils/proxy/http";
|
||||||
import createLogger from "utils/logger";
|
import createLogger from "utils/logger";
|
||||||
import getServiceWidget from "utils/config/service-helpers";
|
import getServiceWidget from "utils/config/service-helpers";
|
||||||
|
import {console} from "next/dist/compiled/@edge-runtime/primitives/console";
|
||||||
|
|
||||||
const proxyName = "synologyProxyHandler";
|
const proxyName = "synologyProxyHandler";
|
||||||
|
|
||||||
const logger = createLogger(proxyName);
|
const logger = createLogger(proxyName);
|
||||||
|
|
||||||
const authApi = "{url}/webapi/entry.cgi?api=SYNO.API.Auth&version=7&method=login&account={username}&passwd={password}&format=cookie"
|
|
||||||
|
|
||||||
function formatUptime(uptime) {
|
function formatUptime(uptime) {
|
||||||
const [hour, minutes, seconds] = uptime.split(":");
|
const [hour, minutes, seconds] = uptime.split(":");
|
||||||
const days = Math.floor(hour/24);
|
const days = Math.floor(hour/24);
|
||||||
const hours = hour % 24;
|
const hours = hour % 24;
|
||||||
|
|
||||||
return `${days} days ${hours}:${minutes}:${seconds}`
|
return `${days} d ${hours}h${minutes}m${seconds}s`
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getApiInfo(api, widget) {
|
async function getApiInfo(api, widget) {
|
||||||
const infoAPI = "{url}/webapi/entry.cgi?api=SYNO.API.Info&version=1&method=query"
|
const infoAPI = "{url}/webapi/query.cgi?api=SYNO.API.Info&version=1&method=query"
|
||||||
|
|
||||||
const infoUrl = formatApiCall(infoAPI, widget);
|
const infoUrl = formatApiCall(infoAPI, widget);
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
const [status, contentType, data] = await httpProxy(infoUrl);
|
const [status, contentType, data] = await httpProxy(infoUrl);
|
||||||
|
|
||||||
let path = "Method unavailable";
|
|
||||||
let minVersion = 0;
|
|
||||||
let maxVersion = 0;
|
|
||||||
if (status === 200) {
|
if (status === 200) {
|
||||||
const json = JSON.parse(data.toString());
|
const json = JSON.parse(data.toString());
|
||||||
if (json.data[api]) {
|
if (json.data[api]) {
|
||||||
path = json.data[api].path;
|
const { path, minVersion, maxVersion } = json.data[api];
|
||||||
minVersion = json.data[api].minVersion;
|
return [ path, minVersion, maxVersion ];
|
||||||
maxVersion = json.data[api].maxVersion;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return [null, null, null];
|
||||||
return [path, minVersion, maxVersion];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function login(widget) {
|
async function login(widget) {
|
||||||
|
// eslint-disable-next-line no-unused-vars
|
||||||
|
const [path, minVersion, maxVersion] = await getApiInfo("SYNO.API.Auth", widget);
|
||||||
|
const authApi = `{url}/webapi/${path}?api=SYNO.API.Auth&version=${maxVersion}&method=login&account={username}&passwd={password}&format=cookie`
|
||||||
const loginUrl = formatApiCall(authApi, widget);
|
const loginUrl = formatApiCall(authApi, widget);
|
||||||
const [status, contentType, data] = await httpProxy(loginUrl);
|
const [status, contentType, data] = await httpProxy(loginUrl);
|
||||||
if (status !== 200) {
|
if (status !== 200) {
|
||||||
@ -69,11 +68,15 @@ export default async function synologyProxyHandler(req, res) {
|
|||||||
let [status, contentType, data] = await login(widget);
|
let [status, contentType, data] = await login(widget);
|
||||||
|
|
||||||
const { sid }=JSON.parse(data.toString()).data;
|
const { sid }=JSON.parse(data.toString()).data;
|
||||||
const api = "SYNO.Core.System";
|
let api = "SYNO.Core.System";
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
const [ path, minVersion, maxVersion] = await getApiInfo(api, widget);
|
let [ path, minVersion, maxVersion] = await getApiInfo(api, widget);
|
||||||
|
|
||||||
const storageUrl = `${widget.url}/webapi/${path}?api=${api}&version=${maxVersion}&method=info&type="storage"&_sid=${sid}`;
|
const storageUrl = `${widget.url}/webapi/${path}?api=${api}&version=${maxVersion}&method=info&type="storage"&_sid=${sid}`;
|
||||||
|
|
||||||
[status, contentType, data] = await httpProxy(storageUrl );
|
[status, contentType, data] = await httpProxy(storageUrl );
|
||||||
|
|
||||||
|
|
||||||
let usedVolume = 0;
|
let usedVolume = 0;
|
||||||
if (status !== 200) {
|
if (status !== 200) {
|
||||||
return res.status(status).set("Content-Type", contentType).send(data);
|
return res.status(status).set("Content-Type", contentType).send(data);
|
||||||
@ -86,6 +89,7 @@ export default async function synologyProxyHandler(req, res) {
|
|||||||
|
|
||||||
const healthUrl = `${widget.url}/webapi/${path}?api=${api}&version=${maxVersion}&method=info&_sid=${sid}`;
|
const healthUrl = `${widget.url}/webapi/${path}?api=${api}&version=${maxVersion}&method=info&_sid=${sid}`;
|
||||||
[status, contentType, data] = await httpProxy(healthUrl);
|
[status, contentType, data] = await httpProxy(healthUrl);
|
||||||
|
|
||||||
if (status !== 200) {
|
if (status !== 200) {
|
||||||
return res.status(status).set("Content-Type", contentType).send(data);
|
return res.status(status).set("Content-Type", contentType).send(data);
|
||||||
}
|
}
|
||||||
@ -94,11 +98,24 @@ export default async function synologyProxyHandler(req, res) {
|
|||||||
return res.status(401).json({ error: "Error getting uptime" });
|
return res.status(401).json({ error: "Error getting uptime" });
|
||||||
}
|
}
|
||||||
const uptime = formatUptime(json.data.up_time);
|
const uptime = formatUptime(json.data.up_time);
|
||||||
|
api = "SYNO.Core.System.Utilization";
|
||||||
|
// eslint-disable-next-line no-unused-vars
|
||||||
|
[ path, minVersion, maxVersion] = await getApiInfo(api, widget);
|
||||||
|
const sysUrl = `${widget.url}/webapi/${path}?api=${api}&version=${maxVersion}&method=get&_sid=${sid}`;
|
||||||
|
[status, contentType, data] = await httpProxy(sysUrl );
|
||||||
|
|
||||||
|
const memoryUsage = 100 - (100 * (parseFloat(JSON.parse(data.toString()).data.memory.avail_real) + parseFloat(JSON.parse(data.toString()).data.memory.cached)) / parseFloat(JSON.parse(data.toString()).data.memory.total_real));
|
||||||
|
const cpuLoad = parseFloat(JSON.parse(data.toString()).data.cpu.user_load) + parseFloat(JSON.parse(data.toString()).data.cpu.system_load);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const resdata = {
|
const resdata = {
|
||||||
uptime,
|
uptime,
|
||||||
usedVolume
|
usedVolume,
|
||||||
|
memoryUsage,
|
||||||
|
cpuLoad,
|
||||||
}
|
}
|
||||||
if (contentType) res.setHeader("Content-Type", contentType);
|
if (contentType) res.setHeader("Content-Type", contentType);
|
||||||
|
console.log("Response Data: ", resdata);
|
||||||
return res.status(status).send(JSON.stringify(resdata));
|
return res.status(status).send(JSON.stringify(resdata));
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user