Add nomad widget

This commit is contained in:
icyleaf 2023-04-19 14:36:43 +08:00
parent 063950af05
commit 35946f8cba
No known key found for this signature in database
GPG Key ID: B1C7C709F61ABC66
7 changed files with 106 additions and 1 deletions

View File

@ -576,5 +576,11 @@
"people_home": "People Home",
"lights_on": "Lights On",
"switches_on": "Switches On"
},
"nomad": {
"nodes": "Nodes",
"jobs": "Jobs",
"volumes": "Volumes",
"services": "Services"
}
}

View File

@ -567,5 +567,11 @@
"people_home": "People Home",
"lights_on": "Lights On",
"switches_on": "Switches On"
},
"nomad": {
"nodes": "节点",
"jobs": "任务",
"volumes": "挂载",
"services": "服务"
}
}

View File

@ -54,6 +54,8 @@ export default async function credentialedProxyHandler(req, res, map) {
} else {
headers.Authorization = `Basic ${Buffer.from(`${widget.username}:${widget.password}`).toString("base64")}`;
}
} else if (widget.type === "nomad") {
headers["X-Nomad-Token"] = `${widget.key}`;
} else {
headers["X-API-Key"] = `${widget.key}`;
}
@ -78,7 +80,7 @@ export default async function credentialedProxyHandler(req, res, map) {
if (status >= 400) {
logger.error("HTTP Error %d calling %s", status, url.toString());
}
if (status === 200) {
if (!validateWidgetData(widget, endpoint, resultData)) {
return res.status(500).json({error: {message: "Invalid data", url: sanitizeErrorURL(url), data: resultData}});

View File

@ -42,6 +42,7 @@ const components = {
navidrome: dynamic(() => import("./navidrome/component")),
nextcloud: dynamic(() => import("./nextcloud/component")),
nextdns: dynamic(() => import("./nextdns/component")),
nomad: dynamic(() => import("./nomad/component")),
npm: dynamic(() => import("./npm/component")),
nzbget: dynamic(() => import("./nzbget/component")),
octoprint: dynamic(() => import("./octoprint/component")),

View File

@ -0,0 +1,62 @@
import { useTranslation } from 'next-i18next'
import Container from "components/services/widget/container";
import Block from "components/services/widget/block";
import useWidgetAPI from "utils/proxy/use-widget-api";
function calcRunningService(total, current) {
return current.Services.length + total;
}
function calcReadyNode(total, current) {
return current.Status === "ready" ? total + 1 : total;
}
function calcRunningJob(total, current) {
return current.Status === "running" ? total + 1 : total;
}
export default function Component({ service }) {
const { t } = useTranslation();
const { widget } = service;
const { data: nodeData, error: nodeError } = useWidgetAPI(widget, "nodes");
const { data: jobData, error: jobError } = useWidgetAPI(widget, "jobs");
const { data: serviceData, error: serviceError } = useWidgetAPI(widget, "services");
const { data: volumeData, error: volumeError } = useWidgetAPI(widget, "volumes");
const { data: csiVolumeData, error: csiVolumeError } = useWidgetAPI(widget, "csi_volumes");
if (nodeError || jobError || serviceError || volumeError || csiVolumeError) {
const finalError = nodeError ?? jobError ?? serviceError ?? volumeError ?? csiVolumeError;
return <Container error={finalError} />;
}
if (!nodeData || !jobData || !serviceData || !volumeData || !csiVolumeData) {
return (
<Container service={service}>
<Block label="nomad.nodes" />
<Block label="nomad.jobs" />
<Block label="nomad.volumes" />
<Block label="nomad.services" />
</Container>
);
}
const nodes = nodeData || [];
const readyNodes = nodes.reduce(calcReadyNode, 0);
const jobs = jobData || [];
const runningJobs = jobs.reduce(calcRunningJob, 0);
const volumeTotal = volumeData.length + csiVolumeData.length;
const runningServices = (serviceData || []).reduce(calcRunningService, 0);
return (
<Container service={service}>
<Block label="nomad.nodes" value={`${readyNodes} / ${nodes.length}`} />
<Block label="nomad.jobs" value={`${runningJobs} / ${jobs.length}`} />
<Block label="nomad.volumes" value={t("common.number", { value: volumeTotal })} />
<Block label="nomad.services" value={t("common.number", { value: runningServices })} />
</Container>
);
}

View File

@ -0,0 +1,26 @@
import credentialedProxyHandler from "utils/proxy/handlers/credentialed";
const widget = {
api: "{url}/v1/{endpoint}",
proxyHandler: credentialedProxyHandler,
mappings: {
nodes: {
endpoint: "nodes",
},
jobs: {
endpoint: "jobs",
},
services: {
endpoint: "services",
},
volumes: {
endpoint: "volumes",
},
csi_volumes: {
endpoint: "volumes?type=csi",
}
},
};
export default widget;

View File

@ -37,6 +37,7 @@ import navidrome from "./navidrome/widget";
import nextcloud from "./nextcloud/widget";
import nextdns from "./nextdns/widget";
import npm from "./npm/widget";
import nomad from "./nomad/widget";
import nzbget from "./nzbget/widget";
import octoprint from "./octoprint/widget";
import omada from "./omada/widget";
@ -116,6 +117,7 @@ const widgets = {
nextcloud,
nextdns,
npm,
nomad,
nzbget,
octoprint,
omada,