2022-09-26 00:35:54 +03:00
|
|
|
import useSWR from "swr";
|
|
|
|
import { useTranslation } from "next-i18next";
|
|
|
|
|
2022-09-26 15:25:10 +03:00
|
|
|
import Container from "components/services/widget/container";
|
|
|
|
import Block from "components/services/widget/block";
|
2022-09-26 12:04:37 +03:00
|
|
|
import { formatProxyUrl } from "utils/proxy/api-helpers";
|
2022-09-26 00:35:54 +03:00
|
|
|
|
|
|
|
export default function Component({ service }) {
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
|
|
const config = service.widget;
|
|
|
|
|
|
|
|
const { data: containersData, error: containersError } = useSWR(
|
|
|
|
formatProxyUrl(config, `docker/containers/json`, {
|
|
|
|
all: 1,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
if (containersError) {
|
2022-09-26 15:25:10 +03:00
|
|
|
return <Container error={t("widget.api_error")} />;
|
2022-09-26 00:35:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!containersData) {
|
|
|
|
return (
|
2022-09-26 15:25:10 +03:00
|
|
|
<Container>
|
2022-09-26 00:35:54 +03:00
|
|
|
<Block label={t("portainer.running")} />
|
|
|
|
<Block label={t("portainer.stopped")} />
|
|
|
|
<Block label={t("portainer.total")} />
|
2022-09-26 15:25:10 +03:00
|
|
|
</Container>
|
2022-09-26 00:35:54 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (containersData.error) {
|
2022-09-26 15:25:10 +03:00
|
|
|
return <Container error={t("widget.api_error")} />;
|
2022-09-26 00:35:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const running = containersData.filter((c) => c.State === "running").length;
|
|
|
|
const stopped = containersData.filter((c) => c.State === "exited").length;
|
|
|
|
const total = containersData.length;
|
|
|
|
|
|
|
|
return (
|
2022-09-26 15:25:10 +03:00
|
|
|
<Container>
|
2022-09-26 00:35:54 +03:00
|
|
|
<Block label={t("portainer.running")} value={running} />
|
|
|
|
<Block label={t("portainer.stopped")} value={stopped} />
|
|
|
|
<Block label={t("portainer.total")} value={total} />
|
2022-09-26 15:25:10 +03:00
|
|
|
</Container>
|
2022-09-26 00:35:54 +03:00
|
|
|
);
|
|
|
|
}
|