mirror of
https://github.com/gethomepage/homepage.git
synced 2026-09-28 15:01:17 -07:00
Performance: fetch all docker container statuses in one request (#7092)
Co-authored-by: shamoon <4887959+shamoon@users.noreply.github.com>
This commit is contained in:
@@ -4,13 +4,16 @@ import useSWR from "swr";
|
||||
export default function Status({ service, style }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { data, error } = useSWR(`/api/docker/status/${service.container}/${service.server || ""}`);
|
||||
const { data: response, error } = useSWR(`/api/docker/statuses?server=${encodeURIComponent(service.server || "")}`);
|
||||
const statusError = error ?? response?.error;
|
||||
const { statuses } = response ?? {};
|
||||
const data = statuses ? (statuses[service.container] ?? { status: "not found" }) : undefined;
|
||||
|
||||
let statusLabel = t("docker.unknown");
|
||||
let backgroundClass = "px-1.5 py-0.5 bg-theme-500/10 dark:bg-theme-900/50";
|
||||
let colorClass = "text-black/20 dark:text-white/40 ";
|
||||
|
||||
if (error) {
|
||||
if (statusError) {
|
||||
statusLabel = t("docker.error");
|
||||
colorClass = "text-rose-500/80";
|
||||
} else if (data) {
|
||||
|
||||
@@ -21,8 +21,11 @@ describe("components/services/status", () => {
|
||||
|
||||
render(<Status service={{ container: "c", server: "s" }} />);
|
||||
|
||||
expect(useSWR).toHaveBeenCalledWith("/api/docker/status/c/s");
|
||||
expect(useSWR).toHaveBeenCalledWith("/api/docker/statuses?server=s");
|
||||
expect(screen.getByText("docker.unknown")).toBeInTheDocument();
|
||||
|
||||
render(<Status service={{ container: "c" }} />);
|
||||
expect(useSWR).toHaveBeenCalledWith("/api/docker/statuses?server=");
|
||||
});
|
||||
|
||||
it("renders error when SWR fails", () => {
|
||||
@@ -33,30 +36,39 @@ describe("components/services/status", () => {
|
||||
expect(screen.getByText("docker.error")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders error when the api returns an error payload with a 200-parsed body", () => {
|
||||
useSWR.mockReturnValue({ data: { error: "query failed" }, error: undefined });
|
||||
|
||||
render(<Status service={{ container: "c", server: "s" }} />);
|
||||
|
||||
expect(screen.getByText("docker.error")).toBeInTheDocument();
|
||||
expect(screen.queryByText("docker.not_found")).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders healthy/unhealthy and partial/exited/not found statuses", () => {
|
||||
useSWR.mockReturnValue({ data: { status: "running", health: "healthy" }, error: undefined });
|
||||
useSWR.mockReturnValue({ data: { statuses: { c: { status: "running", health: "healthy" } } }, error: undefined });
|
||||
render(<Status service={{ container: "c", server: "s" }} />);
|
||||
expect(screen.getByText("docker.healthy")).toBeInTheDocument();
|
||||
|
||||
useSWR.mockReturnValue({ data: { status: "running", health: "unhealthy" }, error: undefined });
|
||||
useSWR.mockReturnValue({ data: { statuses: { c: { status: "running", health: "unhealthy" } } }, error: undefined });
|
||||
render(<Status service={{ container: "c", server: "s" }} />);
|
||||
expect(screen.getByText("docker.unhealthy")).toBeInTheDocument();
|
||||
|
||||
useSWR.mockReturnValue({ data: { status: "partial 1/2" }, error: undefined });
|
||||
useSWR.mockReturnValue({ data: { statuses: { c: { status: "partial 1/2" } } }, error: undefined });
|
||||
render(<Status service={{ container: "c", server: "s" }} />);
|
||||
expect(screen.getByText("docker.partial 1/2")).toBeInTheDocument();
|
||||
|
||||
useSWR.mockReturnValue({ data: { status: "exited" }, error: undefined });
|
||||
useSWR.mockReturnValue({ data: { statuses: { c: { status: "exited" } } }, error: undefined });
|
||||
render(<Status service={{ container: "c", server: "s" }} />);
|
||||
expect(screen.getByText("docker.exited")).toBeInTheDocument();
|
||||
|
||||
useSWR.mockReturnValue({ data: { status: "not found" }, error: undefined });
|
||||
useSWR.mockReturnValue({ data: { statuses: {} }, error: undefined });
|
||||
render(<Status service={{ container: "c", server: "s" }} />);
|
||||
expect(screen.getByText("docker.not_found")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders starting health when container is running and starting", () => {
|
||||
useSWR.mockReturnValue({ data: { status: "running", health: "starting" }, error: undefined });
|
||||
useSWR.mockReturnValue({ data: { statuses: { c: { status: "running", health: "starting" } } }, error: undefined });
|
||||
|
||||
render(<Status service={{ container: "c", server: "s" }} />);
|
||||
|
||||
@@ -64,7 +76,7 @@ describe("components/services/status", () => {
|
||||
});
|
||||
|
||||
it("renders a dot when style is dot", () => {
|
||||
useSWR.mockReturnValue({ data: { status: "running" }, error: undefined });
|
||||
useSWR.mockReturnValue({ data: { statuses: { c: { status: "running" } } }, error: undefined });
|
||||
|
||||
const { container } = render(<Status service={{ container: "c", server: "s" }} style="dot" />);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user