2022-10-10 11:18:29 -07:00
|
|
|
import useSWR from "swr";
|
2023-05-22 13:50:58 -04:00
|
|
|
import { useContext } from "react";
|
2023-03-05 20:53:30 -08:00
|
|
|
import { FaMemory, FaRegClock, FaThermometerHalf } from "react-icons/fa";
|
2023-05-22 13:50:58 -04:00
|
|
|
import { FiCpu, FiHardDrive } from "react-icons/fi";
|
2022-10-10 11:18:29 -07:00
|
|
|
import { useTranslation } from "next-i18next";
|
|
|
|
|
|
|
|
import UsageBar from "../resources/usage-bar";
|
2023-06-03 01:10:15 +01:00
|
|
|
import Error from "../widget/error";
|
|
|
|
import SingleResource from "../widget/single_resource";
|
|
|
|
import WidgetIcon from "../widget/widget_icon";
|
|
|
|
import ResourceValue from "../widget/resource_value";
|
|
|
|
import ResourceLabel from "../widget/resource_label";
|
|
|
|
import Resources from "../widget/resources";
|
|
|
|
import WidgetLabel from "../widget/widget_label";
|
2022-10-10 11:18:29 -07:00
|
|
|
|
2023-05-22 13:50:58 -04:00
|
|
|
import { SettingsContext } from "utils/contexts/settings";
|
|
|
|
|
2023-04-18 11:37:58 -07:00
|
|
|
const cpuSensorLabels = ["cpu_thermal", "Core", "Tctl"];
|
2023-04-03 21:47:39 -07:00
|
|
|
|
|
|
|
function convertToFahrenheit(t) {
|
|
|
|
return t * 9/5 + 32
|
|
|
|
}
|
|
|
|
|
2022-10-10 11:18:29 -07:00
|
|
|
export default function Widget({ options }) {
|
|
|
|
const { t, i18n } = useTranslation();
|
2023-05-22 13:50:58 -04:00
|
|
|
const { settings } = useContext(SettingsContext);
|
2022-10-10 11:18:29 -07:00
|
|
|
|
|
|
|
const { data, error } = useSWR(
|
|
|
|
`/api/widgets/glances?${new URLSearchParams({ lang: i18n.language, ...options }).toString()}`, {
|
|
|
|
refreshInterval: 1500,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if (error || data?.error) {
|
2023-04-16 00:05:50 +01:00
|
|
|
return <Error options={options} />
|
2022-10-10 11:18:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!data) {
|
2023-06-03 01:10:15 +01:00
|
|
|
return <Resources options={options}>
|
|
|
|
<SingleResource>
|
|
|
|
<WidgetIcon icon={FiCpu} />
|
|
|
|
<ResourceLabel>{t("glances.wait")}</ResourceLabel>
|
|
|
|
<UsageBar percent="0" />
|
|
|
|
</SingleResource>
|
|
|
|
<SingleResource>
|
|
|
|
<WidgetIcon icon={FaMemory} />
|
|
|
|
<ResourceLabel>{t("glances.wait")}</ResourceLabel>
|
|
|
|
<UsageBar percent="0" />
|
|
|
|
</SingleResource>
|
|
|
|
{options.cputemp &&
|
|
|
|
<SingleResource>
|
|
|
|
<WidgetIcon icon={FaThermometerHalf} />
|
|
|
|
<ResourceLabel>{t("glances.wait")}</ResourceLabel>
|
|
|
|
<UsageBar percent="0" />
|
|
|
|
</SingleResource>
|
|
|
|
}
|
|
|
|
{options.uptime &&
|
|
|
|
<SingleResource>
|
|
|
|
<WidgetIcon icon={FaRegClock} />
|
|
|
|
<ResourceLabel>{t("glances.wait")}</ResourceLabel>
|
|
|
|
<UsageBar percent="0" />
|
|
|
|
</SingleResource>
|
|
|
|
}
|
|
|
|
{options.label && <WidgetLabel label={options.label} />}
|
|
|
|
</Resources>;
|
2022-10-10 11:18:29 -07:00
|
|
|
}
|
|
|
|
|
2023-03-05 20:53:30 -08:00
|
|
|
const unit = options.units === "imperial" ? "fahrenheit" : "celsius";
|
2023-04-03 21:47:39 -07:00
|
|
|
let mainTemp = 0;
|
2023-03-31 10:37:41 -07:00
|
|
|
let maxTemp = 80;
|
2023-04-03 21:47:39 -07:00
|
|
|
const cpuSensors = data.sensors?.filter(s => cpuSensorLabels.some(label => s.label.startsWith(label)) && s.type === "temperature_core");
|
|
|
|
if (options.cputemp && cpuSensors) {
|
|
|
|
try {
|
|
|
|
mainTemp = cpuSensors.reduce((acc, s) => acc + s.value, 0) / cpuSensors.length;
|
|
|
|
maxTemp = Math.max(cpuSensors.reduce((acc, s) => acc + s.warning, 0) / cpuSensors.length, maxTemp);
|
|
|
|
if (unit === "fahrenheit") {
|
|
|
|
mainTemp = convertToFahrenheit(mainTemp);
|
|
|
|
maxTemp = convertToFahrenheit(maxTemp);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// cpu sensor retrieval failed
|
|
|
|
}
|
2023-03-29 14:57:31 -07:00
|
|
|
}
|
2023-03-31 10:37:41 -07:00
|
|
|
const tempPercent = Math.round((mainTemp / maxTemp) * 100);
|
2023-03-05 20:53:30 -08:00
|
|
|
|
2023-05-22 13:50:58 -04:00
|
|
|
let disks = [];
|
|
|
|
|
|
|
|
if (options.disk) {
|
|
|
|
disks = Array.isArray(options.disk)
|
|
|
|
? options.disk.map((disk) => data.fs.find((d) => d.mnt_point === disk)).filter((d) => d)
|
|
|
|
: [data.fs.find((d) => d.mnt_point === options.disk)].filter((d) => d);
|
|
|
|
}
|
|
|
|
|
2022-10-10 11:18:29 -07:00
|
|
|
return (
|
2023-06-03 01:10:15 +01:00
|
|
|
<Resources options={options} target={settings.target ?? "_blank"}>
|
|
|
|
<SingleResource>
|
|
|
|
<WidgetIcon icon={FiCpu} />
|
|
|
|
<ResourceValue>{t("common.number", {
|
|
|
|
value: data.cpu.total,
|
|
|
|
style: "unit",
|
|
|
|
unit: "percent",
|
|
|
|
maximumFractionDigits: 0,
|
|
|
|
})}</ResourceValue>
|
|
|
|
<ResourceLabel>{t("glances.cpu")}</ResourceLabel>
|
|
|
|
<ResourceValue>{t("common.number", {
|
|
|
|
value: data.load.min15,
|
|
|
|
style: "unit",
|
|
|
|
unit: "percent",
|
|
|
|
maximumFractionDigits: 0,
|
|
|
|
})}</ResourceValue>
|
|
|
|
<ResourceLabel>{t("glances.load")}</ResourceLabel>
|
|
|
|
<UsageBar percent={data.cpu.total} />
|
|
|
|
</SingleResource>
|
|
|
|
<SingleResource>
|
|
|
|
<WidgetIcon icon={FaMemory} />
|
|
|
|
<ResourceValue>{t("common.bytes", {
|
|
|
|
value: data.mem.free,
|
|
|
|
maximumFractionDigits: 1,
|
|
|
|
binary: true,
|
|
|
|
})}</ResourceValue>
|
|
|
|
<ResourceLabel>{t("glances.free")}</ResourceLabel>
|
|
|
|
<ResourceValue>{t("common.bytes", {
|
|
|
|
value: data.mem.total,
|
|
|
|
maximumFractionDigits: 1,
|
|
|
|
binary: true,
|
|
|
|
})}</ResourceValue>
|
|
|
|
<ResourceLabel>{t("glances.total")}</ResourceLabel>
|
|
|
|
<UsageBar percent={data.mem.percent} />
|
|
|
|
</SingleResource>
|
|
|
|
{disks.map((disk) => (
|
|
|
|
<SingleResource key={disk.mnt_point}>
|
|
|
|
<WidgetIcon icon={FiHardDrive} />
|
|
|
|
<ResourceValue>{t("common.bytes", { value: disk.free })}</ResourceValue>
|
|
|
|
<ResourceLabel>{t("glances.free")}</ResourceLabel>
|
|
|
|
<ResourceValue>{t("common.bytes", { value: disk.size })}</ResourceValue>
|
|
|
|
<ResourceLabel>{t("glances.total")}</ResourceLabel>
|
|
|
|
<UsageBar percent={disk.percent} />
|
|
|
|
</SingleResource>
|
|
|
|
))}
|
|
|
|
{options.cputemp && mainTemp > 0 &&
|
|
|
|
<SingleResource>
|
|
|
|
<WidgetIcon icon={FaThermometerHalf} />
|
|
|
|
<ResourceValue>{t("common.number", {
|
|
|
|
value: mainTemp,
|
|
|
|
maximumFractionDigits: 1,
|
|
|
|
style: "unit",
|
|
|
|
unit
|
|
|
|
})}</ResourceValue>
|
|
|
|
<ResourceLabel>{t("glances.temp")}</ResourceLabel>
|
|
|
|
<ResourceValue>{t("common.number", {
|
|
|
|
value: maxTemp,
|
|
|
|
maximumFractionDigits: 1,
|
|
|
|
style: "unit",
|
|
|
|
unit
|
|
|
|
})}</ResourceValue>
|
|
|
|
<ResourceLabel>{t("glances.warn")}</ResourceLabel>
|
|
|
|
<UsageBar percent={tempPercent} />
|
|
|
|
</SingleResource>
|
|
|
|
}
|
|
|
|
{options.uptime && data.uptime &&
|
|
|
|
<SingleResource>
|
|
|
|
<WidgetIcon icon={FaRegClock} />
|
|
|
|
<ResourceValue>{data.uptime.replace(" days,", t("glances.days")).replace(/:\d\d:\d\d$/g, t("glances.hours"))}</ResourceValue>
|
|
|
|
<ResourceLabel>{t("glances.uptime")}</ResourceLabel>
|
|
|
|
<UsageBar percent={Math.round((new Date().getSeconds() / 60) * 100)} />
|
|
|
|
</SingleResource>
|
|
|
|
}
|
|
|
|
{options.label && <WidgetLabel label={options.label} />}
|
|
|
|
</Resources>
|
2022-10-10 11:18:29 -07:00
|
|
|
);
|
|
|
|
}
|