From a1b03ea762b48397abc51de4df9fa0ea86db5fd9 Mon Sep 17 00:00:00 2001 From: Michael Shamoon <4887959+shamoon@users.noreply.github.com> Date: Wed, 12 Oct 2022 14:32:22 -0700 Subject: [PATCH 1/4] Add service ping --- src/components/services/item.jsx | 10 ++++++++- src/components/services/ping.jsx | 35 ++++++++++++++++++++++++++++++++ src/pages/api/ping.js | 22 ++++++++++++++++++++ src/utils/proxy/http.js | 2 +- 4 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 src/components/services/ping.jsx create mode 100644 src/pages/api/ping.js diff --git a/src/components/services/item.jsx b/src/components/services/item.jsx index ed0cef2d..25e94266 100644 --- a/src/components/services/item.jsx +++ b/src/components/services/item.jsx @@ -4,6 +4,7 @@ import { useContext, useState } from "react"; import Status from "./status"; import Widget from "./widget"; +import Ping from "./ping"; import Docker from "widgets/docker/component"; import { SettingsContext } from "utils/contexts/settings"; @@ -102,11 +103,18 @@ export default function Item({ service }) { )} + {service.ping && ( +
+ + Ping status +
+ )} + {service.container && ( - )} +
+ {service.container && ( + + )} + + {service.ping && ( +
+ + Ping status +
+ )} +
{service.container && service.server && ( diff --git a/src/components/services/ping.jsx b/src/components/services/ping.jsx index 951a624d..18ad8696 100644 --- a/src/components/services/ping.jsx +++ b/src/components/services/ping.jsx @@ -1,33 +1,42 @@ -import { BsArrowDownCircle, BsArrowUpCircle } from "react-icons/bs"; +import { useTranslation } from "react-i18next"; import useSWR from "swr"; export default function Ping({ service }) { + const { t } = useTranslation(); const { data, error } = useSWR(`/api/ping?${new URLSearchParams({ping: service.ping}).toString()}`, { refreshInterval: 5000 }); if (error) { - return
; + return ( +
+
{t("ping.error")}
+
+ ); } if (!data) { - return
PING
; + return ( +
+
{t("ping.ping")}
+
+ ); } const statusText = `${service.ping}: HTTP status ${data.status}`; if (data && data.status !== 200) { return ( -
- +
+
{data.status}
); } if (data && data.status === 200) { return ( -
- +
+
{data.status}
); } diff --git a/src/components/services/status.jsx b/src/components/services/status.jsx index dc903408..2d07e49e 100644 --- a/src/components/services/status.jsx +++ b/src/components/services/status.jsx @@ -1,19 +1,36 @@ +import { useTranslation } from "react-i18next"; import useSWR from "swr"; export default function Status({ service }) { + const { t } = useTranslation(); + const { data, error } = useSWR(`/api/docker/status/${service.container}/${service.server || ""}`); if (error) { - return
; +
+
{t("docker.error")}
+
} if (data && data.status === "running") { - return
; + return ( +
+
{data.status}
+
+ ); } - if (data && data.status === "not found") { - return
; + if (data && (data.status === "not found" || data.status === "exited")) { + return ( +
+
{data.status}
+
+ ); } - return
; + return ( +
+
{t("docker.unknown")}
+
+ ); } diff --git a/src/pages/api/ping.js b/src/pages/api/ping.js index 548f1d9b..46ffda65 100644 --- a/src/pages/api/ping.js +++ b/src/pages/api/ping.js @@ -1,4 +1,3 @@ -import { servicesFromConfig } from "utils/config/service-helpers"; import createLogger from "utils/logger"; import { httpProxy } from "utils/proxy/http"; @@ -17,6 +16,6 @@ export default async function handler(req, res) { const [status] = await httpProxy(pingURL); return res.status(200).json({ - status: status, + status, }); } From 5eb4778f53a9b40ee24bb5b15921b53aa3cac4d3 Mon Sep 17 00:00:00 2001 From: Michael Shamoon <4887959+shamoon@users.noreply.github.com> Date: Fri, 14 Oct 2022 14:19:15 -0700 Subject: [PATCH 3/4] Use HEAD request --- src/pages/api/ping.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pages/api/ping.js b/src/pages/api/ping.js index 46ffda65..8c919f0a 100644 --- a/src/pages/api/ping.js +++ b/src/pages/api/ping.js @@ -13,7 +13,9 @@ export default async function handler(req, res) { }); } - const [status] = await httpProxy(pingURL); + const [status] = await httpProxy(pingURL, { + method: "HEAD" + }); return res.status(200).json({ status, From 8eca0011db1b23d3d464682452f7ed9ec385303d Mon Sep 17 00:00:00 2001 From: Michael Shamoon <4887959+shamoon@users.noreply.github.com> Date: Fri, 14 Oct 2022 14:25:59 -0700 Subject: [PATCH 4/4] Show latency if status 200 --- src/components/services/item.jsx | 14 +++++++------- src/components/services/ping.jsx | 4 ++-- src/pages/api/ping.js | 7 ++++++- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/components/services/item.jsx b/src/components/services/item.jsx index a4b54ad3..1959df3a 100644 --- a/src/components/services/item.jsx +++ b/src/components/services/item.jsx @@ -104,6 +104,13 @@ export default function Item({ service }) { )}
+ {service.ping && ( +
+ + Ping status +
+ )} + {service.container && ( )} - - {service.ping && ( -
- - Ping status -
- )}
diff --git a/src/components/services/ping.jsx b/src/components/services/ping.jsx index 18ad8696..e3056232 100644 --- a/src/components/services/ping.jsx +++ b/src/components/services/ping.jsx @@ -4,7 +4,7 @@ import useSWR from "swr"; export default function Ping({ service }) { const { t } = useTranslation(); const { data, error } = useSWR(`/api/ping?${new URLSearchParams({ping: service.ping}).toString()}`, { - refreshInterval: 5000 + refreshInterval: 30000 }); if (error) { @@ -36,7 +36,7 @@ export default function Ping({ service }) { if (data && data.status === 200) { return (
-
{data.status}
+
{t("common.ms", { value: data.latency, style: "unit", unit: "millisecond", unitDisplay: "narrow", maximumFractionDigits: 0 })}
); } diff --git a/src/pages/api/ping.js b/src/pages/api/ping.js index 8c919f0a..79c7da0c 100644 --- a/src/pages/api/ping.js +++ b/src/pages/api/ping.js @@ -1,3 +1,5 @@ +import { performance } from "perf_hooks"; + import createLogger from "utils/logger"; import { httpProxy } from "utils/proxy/http"; @@ -12,12 +14,15 @@ export default async function handler(req, res) { error: "No ping URL given", }); } - + + const startTime = performance.now(); const [status] = await httpProxy(pingURL, { method: "HEAD" }); + const endTime = performance.now(); return res.status(200).json({ status, + latency: endTime - startTime }); }