import Block from "components/services/widget/block";
import Container from "components/services/widget/container";
import useWidgetAPI from "utils/proxy/use-widget-api";
const ACTIVE_STATUSES = ["online", "running"];
function countResources(data, resources, type) {
const statsCount = data?.stats?.byType?.[type];
if (typeof statsCount === "number") {
return statsCount;
}
if (resources) {
return resources.filter((resource) => resource.type === type).length;
}
return undefined;
}
function countActiveResources(resources, type) {
return resources
? resources.filter((resource) => resource.type === type && ACTIVE_STATUSES.includes(resource.status)).length
: undefined;
}
function formatResourceCount(total, active) {
if (total === undefined) {
return undefined;
}
if (active === undefined) {
return total;
}
return `${active} / ${total}`;
}
export default function Component({ service }) {
const { widget } = service;
const version = widget.version ?? 1;
const { data: resourcesData, error: resourcesError } = useWidgetAPI(widget, version === 2 ? "summary" : "resources");
if (resourcesError) {
return ;
}
if (!resourcesData) {
return (
);
}
if (version === 2) {
// pulse v6 (v2 widget) returns the counts directly in the summary
return (
);
}
let resources = resourcesData.resources;
if (!resources && resourcesData.count === 0) {
resources = [];
}
const nodes = countResources(resourcesData, resources, "node");
const vms = countResources(resourcesData, resources, "vm");
const lxcs = countResources(resourcesData, resources, "container");
return (
);
}