Add service ping
This commit is contained in:
parent
c7d1c05e6e
commit
a1b03ea762
@ -4,6 +4,7 @@ import { useContext, useState } from "react";
|
|||||||
|
|
||||||
import Status from "./status";
|
import Status from "./status";
|
||||||
import Widget from "./widget";
|
import Widget from "./widget";
|
||||||
|
import Ping from "./ping";
|
||||||
|
|
||||||
import Docker from "widgets/docker/component";
|
import Docker from "widgets/docker/component";
|
||||||
import { SettingsContext } from "utils/contexts/settings";
|
import { SettingsContext } from "utils/contexts/settings";
|
||||||
@ -102,11 +103,18 @@ export default function Item({ service }) {
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{service.ping && (
|
||||||
|
<div className="flex-shrink-0 flex items-center justify-center w-5 mr-4 cursor-pointer">
|
||||||
|
<Ping service={service} />
|
||||||
|
<span className="sr-only">Ping status</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
{service.container && (
|
{service.container && (
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={() => (statsOpen ? closeStats() : setStatsOpen(true))}
|
onClick={() => (statsOpen ? closeStats() : setStatsOpen(true))}
|
||||||
className="flex-shrink-0 flex items-center justify-center w-12 cursor-pointer"
|
className="flex-shrink-0 flex items-center justify-center w-5 mr-4 cursor-pointer"
|
||||||
>
|
>
|
||||||
<Status service={service} />
|
<Status service={service} />
|
||||||
<span className="sr-only">View container stats</span>
|
<span className="sr-only">View container stats</span>
|
||||||
|
|||||||
35
src/components/services/ping.jsx
Normal file
35
src/components/services/ping.jsx
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import { BsArrowDownCircle, BsArrowUpCircle } from "react-icons/bs";
|
||||||
|
import useSWR from "swr";
|
||||||
|
|
||||||
|
export default function Ping({ service }) {
|
||||||
|
const { data, error } = useSWR(`/api/ping?${new URLSearchParams({ping: service.ping}).toString()}`, {
|
||||||
|
refreshInterval: 5000
|
||||||
|
});
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
return <div className="w-3 h-3 text-xs text-rose-300 dark:text-rose-500" title={data.status}>↓</div>;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!data) {
|
||||||
|
return <div className="w-3 h-3 text-[10px] text-black/20 dark:text-white/40">PING</div>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const statusText = `${service.ping}: HTTP status ${data.status}`;
|
||||||
|
|
||||||
|
if (data && data.status !== 200) {
|
||||||
|
return (
|
||||||
|
<div className="w-3 h-3 text-xs text-rose-300 dark:text-rose-500" title={statusText}>
|
||||||
|
<BsArrowDownCircle />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data && data.status === 200) {
|
||||||
|
return (
|
||||||
|
<div className="w-3 h-3 text-xs text-emerald-300 dark:text-emerald-500" title={statusText}>
|
||||||
|
<BsArrowUpCircle />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
22
src/pages/api/ping.js
Normal file
22
src/pages/api/ping.js
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import { servicesFromConfig } from "utils/config/service-helpers";
|
||||||
|
import createLogger from "utils/logger";
|
||||||
|
import { httpProxy } from "utils/proxy/http";
|
||||||
|
|
||||||
|
const logger = createLogger("ping");
|
||||||
|
|
||||||
|
export default async function handler(req, res) {
|
||||||
|
const { ping: pingURL } = req.query;
|
||||||
|
|
||||||
|
if (!pingURL) {
|
||||||
|
logger.debug("No ping URL specified");
|
||||||
|
return res.status(400).send({
|
||||||
|
error: "No ping URL given",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const [status] = await httpProxy(pingURL);
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
status: status,
|
||||||
|
});
|
||||||
|
}
|
||||||
@ -96,7 +96,7 @@ export async function httpProxy(url, params = {}) {
|
|||||||
return [status, contentType, data, responseHeaders];
|
return [status, contentType, data, responseHeaders];
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
logger.error("Error calling %s//%s%s...", url.protocol, url.hostname, url.pathname);
|
logger.error("Error calling %s//%s%s...", constructedUrl.protocol, constructedUrl.hostname, constructedUrl.pathname);
|
||||||
logger.error(err);
|
logger.error(err);
|
||||||
return [500, "application/json", { error: "Unexpected error" }, null];
|
return [500, "application/json", { error: "Unexpected error" }, null];
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user