2023-08-02 06:04:54 +03:00
|
|
|
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";
|
|
|
|
|
2023-09-06 13:53:39 +03:00
|
|
|
function Swap({ quicklookData, className = "" }) {
|
2023-08-02 06:04:54 +03:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
2023-10-17 23:26:55 -07:00
|
|
|
return (
|
|
|
|
quicklookData &&
|
|
|
|
quicklookData.swap !== 0 && (
|
|
|
|
<div className="text-xs flex place-content-between">
|
|
|
|
<div className={className}>{t("glances.swap")}</div>
|
|
|
|
<div className={className}>
|
|
|
|
{t("common.number", {
|
|
|
|
value: quicklookData.swap,
|
|
|
|
style: "unit",
|
|
|
|
unit: "percent",
|
|
|
|
maximumFractionDigits: 0,
|
|
|
|
})}
|
|
|
|
</div>
|
2023-09-06 13:53:39 +03:00
|
|
|
</div>
|
2023-10-17 23:26:55 -07:00
|
|
|
)
|
2023-09-06 13:53:39 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function CPU({ quicklookData, className = "" }) {
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
2023-10-17 23:26:55 -07:00
|
|
|
return (
|
|
|
|
quicklookData &&
|
|
|
|
quicklookData.cpu && (
|
|
|
|
<div className="text-xs flex place-content-between">
|
|
|
|
<div className={className}>{t("glances.cpu")}</div>
|
|
|
|
<div className={className}>
|
|
|
|
{t("common.number", {
|
|
|
|
value: quicklookData.cpu,
|
|
|
|
style: "unit",
|
|
|
|
unit: "percent",
|
|
|
|
maximumFractionDigits: 0,
|
|
|
|
})}
|
|
|
|
</div>
|
2023-09-06 13:53:39 +03:00
|
|
|
</div>
|
2023-10-17 23:26:55 -07:00
|
|
|
)
|
2023-09-06 13:53:39 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function Mem({ quicklookData, className = "" }) {
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
2023-10-17 23:26:55 -07:00
|
|
|
return (
|
|
|
|
quicklookData &&
|
|
|
|
quicklookData.mem && (
|
|
|
|
<div className="text-xs flex place-content-between">
|
|
|
|
<div className={className}>{t("glances.mem")}</div>
|
|
|
|
<div className={className}>
|
|
|
|
{t("common.number", {
|
|
|
|
value: quicklookData.mem,
|
|
|
|
style: "unit",
|
|
|
|
unit: "percent",
|
|
|
|
maximumFractionDigits: 0,
|
|
|
|
})}
|
|
|
|
</div>
|
2023-09-06 13:53:39 +03:00
|
|
|
</div>
|
2023-10-17 23:26:55 -07:00
|
|
|
)
|
2023-09-06 13:53:39 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function Component({ service }) {
|
|
|
|
const { widget } = service;
|
|
|
|
const { chart } = widget;
|
|
|
|
|
2023-10-17 23:26:55 -07:00
|
|
|
const { data: quicklookData, errorL: quicklookError } = useWidgetAPI(service.widget, "quicklook", {
|
2023-08-02 06:04:54 +03:00
|
|
|
refreshInterval: 1000,
|
|
|
|
});
|
|
|
|
|
2023-10-17 23:26:55 -07:00
|
|
|
const { data: systemData, errorL: systemError } = useWidgetAPI(service.widget, "system", {
|
2023-08-02 06:04:54 +03:00
|
|
|
refreshInterval: 30000,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (quicklookError) {
|
2023-10-17 23:26:55 -07:00
|
|
|
return (
|
|
|
|
<Container chart={chart}>
|
|
|
|
<Error error={quicklookError} />
|
|
|
|
</Container>
|
|
|
|
);
|
2023-08-02 06:04:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (systemError) {
|
2023-10-17 23:26:55 -07:00
|
|
|
return (
|
|
|
|
<Container chart={chart}>
|
|
|
|
<Error error={systemError} />
|
|
|
|
</Container>
|
|
|
|
);
|
2023-08-02 06:04:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 (
|
2023-09-06 13:53:39 +03:00
|
|
|
<Container chart={chart} className="bg-gradient-to-br from-theme-500/30 via-theme-600/20 to-theme-700/10">
|
2023-08-02 06:04:54 +03:00
|
|
|
<Block position="top-3 right-3">
|
2023-09-06 13:53:39 +03:00
|
|
|
{quicklookData && quicklookData.cpu_name && chart && (
|
2023-10-17 23:26:55 -07:00
|
|
|
<div className="text-[0.6rem] opacity-50">{quicklookData.cpu_name}</div>
|
2023-08-02 06:04:54 +03:00
|
|
|
)}
|
2023-09-06 13:53:39 +03:00
|
|
|
|
2023-10-17 23:26:55 -07:00
|
|
|
{!chart && quicklookData?.swap === 0 && (
|
|
|
|
<div className="text-[0.6rem] opacity-50">{quicklookData.cpu_name}</div>
|
2023-08-02 06:04:54 +03:00
|
|
|
)}
|
2023-09-06 13:53:39 +03:00
|
|
|
|
2023-10-17 23:26:55 -07:00
|
|
|
<div className="w-[4rem]">{!chart && <Swap quicklookData={quicklookData} className="opacity-25" />}</div>
|
2023-08-02 06:04:54 +03:00
|
|
|
</Block>
|
|
|
|
|
2023-09-06 13:53:39 +03:00
|
|
|
{chart && (
|
|
|
|
<Block position="bottom-3 left-3">
|
2023-10-17 23:26:55 -07:00
|
|
|
{systemData && systemData.linux_distro && <div className="text-xs opacity-50">{systemData.linux_distro}</div>}
|
|
|
|
{systemData && systemData.os_version && <div className="text-xs opacity-50">{systemData.os_version}</div>}
|
|
|
|
{systemData && systemData.hostname && <div className="text-xs opacity-75">{systemData.hostname}</div>}
|
2023-09-06 13:53:39 +03:00
|
|
|
</Block>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{!chart && (
|
|
|
|
<Block position="bottom-3 left-3 w-[3rem]">
|
|
|
|
<CPU quicklookData={quicklookData} className="opacity-75" />
|
|
|
|
</Block>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<Block position="bottom-3 right-3 w-[4rem]">
|
2023-10-17 23:26:55 -07:00
|
|
|
{chart && <CPU quicklookData={quicklookData} className="opacity-50" />}
|
2023-09-06 13:53:39 +03:00
|
|
|
|
2023-10-17 23:26:55 -07:00
|
|
|
{chart && <Mem quicklookData={quicklookData} className="opacity-50" />}
|
|
|
|
{!chart && <Mem quicklookData={quicklookData} className="opacity-75" />}
|
2023-09-06 13:53:39 +03:00
|
|
|
|
2023-10-17 23:26:55 -07:00
|
|
|
{chart && <Swap quicklookData={quicklookData} className="opacity-50" />}
|
2023-08-02 06:04:54 +03:00
|
|
|
</Block>
|
|
|
|
</Container>
|
|
|
|
);
|
|
|
|
}
|