From c3d15a61c3734bec879172c3bedaa26e1000c4fd Mon Sep 17 00:00:00 2001 From: chain710 Date: Wed, 1 Feb 2023 23:22:59 +0800 Subject: [PATCH 1/2] Implement uptime-kuma widget --- public/locales/en/common.json | 10 ++- src/widgets/components.js | 1 + src/widgets/uptimekuma/component.jsx | 45 +++++++++++++ src/widgets/uptimekuma/proxy.js | 95 ++++++++++++++++++++++++++++ src/widgets/uptimekuma/widget.js | 8 +++ src/widgets/widgets.js | 2 + 6 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 src/widgets/uptimekuma/component.jsx create mode 100644 src/widgets/uptimekuma/proxy.js create mode 100644 src/widgets/uptimekuma/widget.js diff --git a/public/locales/en/common.json b/public/locales/en/common.json index 52db2cb40..b47163689 100644 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -447,5 +447,13 @@ "photos": "Photos", "videos": "Videos", "storage": "Storage" + }, + "uptimekuma": { + "status": "status", + "uptime": "uptime", + "good": "All Systems Operational", + "warn": "Partially Degraded Service", + "bad": "Degraded Service", + "unknown": "Unknown service status" } -} +} \ No newline at end of file diff --git a/src/widgets/components.js b/src/widgets/components.js index 43a46fa90..505807c48 100644 --- a/src/widgets/components.js +++ b/src/widgets/components.js @@ -63,6 +63,7 @@ const components = { watchtower: dynamic(() => import("./watchtower/component")), xteve: dynamic(() => import("./xteve/component")), immich: dynamic(() => import("./immich/component")), + uptimekuma: dynamic(() => import("./uptimekuma/component")), }; export default components; diff --git a/src/widgets/uptimekuma/component.jsx b/src/widgets/uptimekuma/component.jsx new file mode 100644 index 000000000..dd112db44 --- /dev/null +++ b/src/widgets/uptimekuma/component.jsx @@ -0,0 +1,45 @@ +import { useTranslation } from "next-i18next"; + +import Container from "components/services/widget/container"; +import useWidgetAPI from "utils/proxy/use-widget-api"; +import Block from "components/services/widget/block"; + +const Status = { + good: "uptimekuma.good", + warn: "uptimekuma.warn", + bad: "uptimekuma.bad", + unknown: "uptimekuma.unknown", +}; + +export default function Component({ service }) { + const { t } = useTranslation(); + + const { widget } = service; + + const { data: statusData, error: statusError } = useWidgetAPI(widget); + + if (statusError) { + return ; + } + + if (!statusData) { + return ( + + + + + ); + } + + if (statusData.icon) { + // eslint-disable-next-line no-param-reassign + service.icon = statusData.icon; + } + + return ( + + + + + ); +} diff --git a/src/widgets/uptimekuma/proxy.js b/src/widgets/uptimekuma/proxy.js new file mode 100644 index 000000000..6722de852 --- /dev/null +++ b/src/widgets/uptimekuma/proxy.js @@ -0,0 +1,95 @@ +import { httpProxy } from "utils/proxy/http"; +import getServiceWidget from "utils/config/service-helpers"; +import createLogger from "utils/logger"; + +const logger = createLogger("uptimeKumaProxyHandler"); + +async function getStatus(widget) { + const url = new URL(`${widget.url}/api/status-page/${widget.slug}`).toString(); + logger.debug("get status %s", url); + const params = { method: "GET", headers: {} }; + const [status, , data] = await httpProxy(url, params); + try { + return [status, JSON.parse(data)]; + } catch (e) { + logger.error("Error decoding status data. Data: %s", data.toString()); + return [status, null]; + } +} + +async function getHeartbeat(widget) { + const url = new URL(`${widget.url}/api/status-page/heartbeat/${widget.slug}`).toString(); + logger.debug("get heartbeat %s", url); + const params = { method: "GET", headers: {} }; + const [status, , data] = await httpProxy(url, params); + try { + return [status, JSON.parse(data)]; + } catch (e) { + logger.error("Error decoding heartbeat data. Data: %s", data.toString()); + return [status, null]; + } +} + +function statusMessage(data) { + if (!data || Object.keys(data.heartbeatList) === 0) { + return "unknown"; + } + + let result = "good"; + let hasUp = false; + Object.values(data.heartbeatList).forEach((el) => { + const index = el.length - 1; + if (el[index].status === 1) { + hasUp = true; + } else { + result = "warn"; + } + }); + + if (!hasUp) { + result = "bad"; + } + return result; +} + +function uptime(data) { + if (!data) { + return 0; + } + + const uptimeList = Object.values(data.uptimeList); + const percent = uptimeList.reduce((a, b) => a + b, 0) / uptimeList.length || 0; + return (percent * 100).toFixed(1); +} + +export default async function uptimeKumaProxyHandler(req, res) { + const { group, service } = req.query; + const widget = await getServiceWidget(group, service); + if (!widget) { + logger.debug("Invalid or missing widget for service '%s' in group '%s'", service, group); + return res.status(400).json({ error: "Invalid proxy service type" }); + } + + const [[statusCode, statusData], [heartbeatCode, heartbeatData]] = await Promise.all([ + getStatus(widget), + getHeartbeat(widget), + ]); + + if (statusCode !== 200) { + logger.error("HTTP %d getting status data error. Data: %s", statusCode, statusData); + return res.status(statusCode).send(statusData); + } + + if (heartbeatCode !== 200) { + logger.error("HTTP %d getting heartbeat data error. Data: %s", heartbeatCode, heartbeatData); + return res.status(heartbeatCode).send(heartbeatData); + } + + const icon = statusData?.config ? statusData.config.icon : null; + return res.status(200).send({ + uptime: uptime(heartbeatData), + message: statusMessage(heartbeatData), + incident: statusData?.incident ? statusData.incident.title : "", + icon: `${widget.url}${icon}`, + }); +} diff --git a/src/widgets/uptimekuma/widget.js b/src/widgets/uptimekuma/widget.js new file mode 100644 index 000000000..9687e1a40 --- /dev/null +++ b/src/widgets/uptimekuma/widget.js @@ -0,0 +1,8 @@ +// import credentialedProxyHandler from "utils/proxy/handlers/credentialed"; +import uptimeKumaProxyHandler from "./proxy"; + +const widget = { + proxyHandler: uptimeKumaProxyHandler, +}; + +export default widget; diff --git a/src/widgets/widgets.js b/src/widgets/widgets.js index 133903fbe..7da77a0a6 100644 --- a/src/widgets/widgets.js +++ b/src/widgets/widgets.js @@ -57,6 +57,7 @@ import unifi from "./unifi/widget"; import watchtower from "./watchtower/widget"; import xteve from "./xteve/widget"; import immich from "./immich/widget"; +import uptimekuma from "./uptimekuma/widget"; const widgets = { adguard, @@ -121,6 +122,7 @@ const widgets = { watchtower, xteve, immich, + uptimekuma, }; export default widgets; From 015d7dac52badcb4966076b6c7c0d669c249074b Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Thu, 2 Feb 2023 00:28:18 -0800 Subject: [PATCH 2/2] Rework uptime kuma remove proxy display more info --- public/locales/en/common.json | 11 ++-- src/widgets/uptimekuma/component.jsx | 46 ++++++++------ src/widgets/uptimekuma/proxy.js | 95 ---------------------------- src/widgets/uptimekuma/widget.js | 14 +++- 4 files changed, 45 insertions(+), 121 deletions(-) delete mode 100644 src/widgets/uptimekuma/proxy.js diff --git a/public/locales/en/common.json b/public/locales/en/common.json index b47163689..f144182f6 100644 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -449,11 +449,10 @@ "storage": "Storage" }, "uptimekuma": { - "status": "status", - "uptime": "uptime", - "good": "All Systems Operational", - "warn": "Partially Degraded Service", - "bad": "Degraded Service", - "unknown": "Unknown service status" + "up": "Sites Up", + "down": "Sites Down", + "uptime": "Uptime", + "incident": "Incident", + "m": "m" } } \ No newline at end of file diff --git a/src/widgets/uptimekuma/component.jsx b/src/widgets/uptimekuma/component.jsx index dd112db44..d71f9a63a 100644 --- a/src/widgets/uptimekuma/component.jsx +++ b/src/widgets/uptimekuma/component.jsx @@ -4,42 +4,52 @@ import Container from "components/services/widget/container"; import useWidgetAPI from "utils/proxy/use-widget-api"; import Block from "components/services/widget/block"; -const Status = { - good: "uptimekuma.good", - warn: "uptimekuma.warn", - bad: "uptimekuma.bad", - unknown: "uptimekuma.unknown", -}; - export default function Component({ service }) { const { t } = useTranslation(); const { widget } = service; - const { data: statusData, error: statusError } = useWidgetAPI(widget); + const { data: statusData, error: statusError } = useWidgetAPI(widget, "status_page"); + const { data: heartbeatData, error: heartbeatError } = useWidgetAPI(widget, "heartbeat"); - if (statusError) { - return ; + if (statusError || heartbeatError) { + return ; } - if (!statusData) { + if (!statusData || !heartbeatData) { return ( - + + + ); } - if (statusData.icon) { - // eslint-disable-next-line no-param-reassign - service.icon = statusData.icon; - } + let sitesUp = 0; + let sitesDown = 0; + Object.values(heartbeatData.heartbeatList).forEach((siteList) => { + const lastHeartbeat = siteList[siteList.length - 1]; + if (lastHeartbeat?.status === 1) { + sitesUp += 1; + } else { + sitesDown += 1; + } + }); + + // Adapted from https://github.com/bastienwirtz/homer/blob/b7cd8f9482e6836a96b354b11595b03b9c3d67cd/src/components/services/UptimeKuma.vue#L105 + const uptimeList = Object.values(heartbeatData.uptimeList); + const percent = uptimeList.reduce((a, b) => a + b, 0) / uptimeList.length || 0; + const uptime = (percent * 100).toFixed(1); + const incidentTime = statusData.incident ? (Math.abs(new Date(statusData.incident?.createdDate) - new Date()) / 1000) / (60 * 60) : null; return ( - - + + + + {incidentTime && } ); } diff --git a/src/widgets/uptimekuma/proxy.js b/src/widgets/uptimekuma/proxy.js deleted file mode 100644 index 6722de852..000000000 --- a/src/widgets/uptimekuma/proxy.js +++ /dev/null @@ -1,95 +0,0 @@ -import { httpProxy } from "utils/proxy/http"; -import getServiceWidget from "utils/config/service-helpers"; -import createLogger from "utils/logger"; - -const logger = createLogger("uptimeKumaProxyHandler"); - -async function getStatus(widget) { - const url = new URL(`${widget.url}/api/status-page/${widget.slug}`).toString(); - logger.debug("get status %s", url); - const params = { method: "GET", headers: {} }; - const [status, , data] = await httpProxy(url, params); - try { - return [status, JSON.parse(data)]; - } catch (e) { - logger.error("Error decoding status data. Data: %s", data.toString()); - return [status, null]; - } -} - -async function getHeartbeat(widget) { - const url = new URL(`${widget.url}/api/status-page/heartbeat/${widget.slug}`).toString(); - logger.debug("get heartbeat %s", url); - const params = { method: "GET", headers: {} }; - const [status, , data] = await httpProxy(url, params); - try { - return [status, JSON.parse(data)]; - } catch (e) { - logger.error("Error decoding heartbeat data. Data: %s", data.toString()); - return [status, null]; - } -} - -function statusMessage(data) { - if (!data || Object.keys(data.heartbeatList) === 0) { - return "unknown"; - } - - let result = "good"; - let hasUp = false; - Object.values(data.heartbeatList).forEach((el) => { - const index = el.length - 1; - if (el[index].status === 1) { - hasUp = true; - } else { - result = "warn"; - } - }); - - if (!hasUp) { - result = "bad"; - } - return result; -} - -function uptime(data) { - if (!data) { - return 0; - } - - const uptimeList = Object.values(data.uptimeList); - const percent = uptimeList.reduce((a, b) => a + b, 0) / uptimeList.length || 0; - return (percent * 100).toFixed(1); -} - -export default async function uptimeKumaProxyHandler(req, res) { - const { group, service } = req.query; - const widget = await getServiceWidget(group, service); - if (!widget) { - logger.debug("Invalid or missing widget for service '%s' in group '%s'", service, group); - return res.status(400).json({ error: "Invalid proxy service type" }); - } - - const [[statusCode, statusData], [heartbeatCode, heartbeatData]] = await Promise.all([ - getStatus(widget), - getHeartbeat(widget), - ]); - - if (statusCode !== 200) { - logger.error("HTTP %d getting status data error. Data: %s", statusCode, statusData); - return res.status(statusCode).send(statusData); - } - - if (heartbeatCode !== 200) { - logger.error("HTTP %d getting heartbeat data error. Data: %s", heartbeatCode, heartbeatData); - return res.status(heartbeatCode).send(heartbeatData); - } - - const icon = statusData?.config ? statusData.config.icon : null; - return res.status(200).send({ - uptime: uptime(heartbeatData), - message: statusMessage(heartbeatData), - incident: statusData?.incident ? statusData.incident.title : "", - icon: `${widget.url}${icon}`, - }); -} diff --git a/src/widgets/uptimekuma/widget.js b/src/widgets/uptimekuma/widget.js index 9687e1a40..928534b3a 100644 --- a/src/widgets/uptimekuma/widget.js +++ b/src/widgets/uptimekuma/widget.js @@ -1,8 +1,18 @@ // import credentialedProxyHandler from "utils/proxy/handlers/credentialed"; -import uptimeKumaProxyHandler from "./proxy"; +import genericProxyHandler from "utils/proxy/handlers/generic"; const widget = { - proxyHandler: uptimeKumaProxyHandler, + api: "{url}/api/{endpoint}/{slug}", + proxyHandler: genericProxyHandler, + + mappings: { + status_page: { + endpoint: "status-page", + }, + heartbeat: { + endpoint: "status-page/heartbeat", + }, + } }; export default widget;