import { useTranslation } from "next-i18next";
import Error from "../components/error";
import Container from "../components/container";
import Block from "../components/block";
import useWidgetAPI from "utils/proxy/use-widget-api";
function Swap({ quicklookData, className = "" }) {
const { t } = useTranslation();
return (
quicklookData &&
quicklookData.swap !== 0 && (
{t("glances.swap")}
{t("common.number", {
value: quicklookData.swap,
style: "unit",
unit: "percent",
maximumFractionDigits: 0,
})}
)
);
}
function CPU({ quicklookData, className = "" }) {
const { t } = useTranslation();
return (
quicklookData &&
quicklookData.cpu && (
{t("glances.cpu")}
{t("common.number", {
value: quicklookData.cpu,
style: "unit",
unit: "percent",
maximumFractionDigits: 0,
})}
)
);
}
function Mem({ quicklookData, className = "" }) {
const { t } = useTranslation();
return (
quicklookData &&
quicklookData.mem && (
{t("glances.mem")}
{t("common.number", {
value: quicklookData.mem,
style: "unit",
unit: "percent",
maximumFractionDigits: 0,
})}
)
);
}
const defaultInterval = 1000;
const defaultSystemInterval = 30000; // This data (OS, hostname, distribution) is usually super stable.
export default function Component({ service }) {
const { widget } = service;
const { chart, refreshInterval = defaultInterval } = widget;
const { data: quicklookData, errorL: quicklookError } = useWidgetAPI(service.widget, "quicklook", {
refreshInterval,
});
const { data: systemData, errorL: systemError } = useWidgetAPI(service.widget, "system", {
refreshInterval: defaultSystemInterval,
});
if (quicklookError) {
return (
);
}
if (systemError) {
return (
);
}
const dataCharts = [];
if (quicklookData) {
quicklookData.percpu.forEach((cpu, index) => {
dataCharts.push({
name: `CPU ${index}`,
cpu: cpu.total,
mem: quicklookData.mem,
swap: quicklookData.swap,
proc: quicklookData.cpu,
});
});
}
return (
{quicklookData && quicklookData.cpu_name && chart && (
{quicklookData.cpu_name}
)}
{!chart && quicklookData?.swap === 0 && (
{systemData.linux_distro && `${systemData.linux_distro} - `}
{systemData.os_version && systemData.os_version}
)}
{!chart && }
{chart && (
{systemData && systemData.linux_distro && {systemData.linux_distro}
}
{systemData && systemData.os_version && {systemData.os_version}
}
{systemData && systemData.hostname && {systemData.hostname}
}
)}
{!chart && (
)}
{chart && }
{chart && }
{!chart && }
{chart && }
);
}