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";
|
2023-09-10 23:36:54 +02:00
|
|
|
import classNames from "classnames";
|
2022-10-10 11:18:29 -07:00
|
|
|
|
2023-06-03 01:10:15 +01:00
|
|
|
import Error from "../widget/error";
|
2023-06-05 23:18:18 +01:00
|
|
|
import Resource from "../widget/resource";
|
2023-06-03 01:10:15 +01:00
|
|
|
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) {
|
2023-10-17 23:26:55 -07:00
|
|
|
return (t * 9) / 5 + 32;
|
2023-04-03 21:47:39 -07:00
|
|
|
}
|
|
|
|
|
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(
|
2023-10-18 11:44:26 -07:00
|
|
|
`/api/widgets/glances?${new URLSearchParams({ lang: i18n.language, ...options }).toString()}`,
|
2023-10-17 23:26:55 -07:00
|
|
|
{
|
2022-10-10 11:18:29 -07:00
|
|
|
refreshInterval: 1500,
|
2023-10-17 23:26:55 -07:00
|
|
|
},
|
2022-10-10 11:18:29 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
if (error || data?.error) {
|
2023-10-17 23:26:55 -07:00
|
|
|
return <Error options={options} />;
|
2022-10-10 11:18:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!data) {
|
2023-10-17 23:26:55 -07:00
|
|
|
return (
|
|
|
|
<Resources options={options} additionalClassNames="information-widget-glances">
|
|
|
|
{options.cpu !== false && <Resource icon={FiCpu} label={t("glances.wait")} percentage="0" />}
|
|
|
|
{options.mem !== false && <Resource icon={FaMemory} label={t("glances.wait")} percentage="0" />}
|
|
|
|
{options.cputemp && <Resource icon={FaThermometerHalf} label={t("glances.wait")} percentage="0" />}
|
|
|
|
{options.disk && !Array.isArray(options.disk) && (
|
|
|
|
<Resource key={options.disk} icon={FiHardDrive} label={t("glances.wait")} percentage="0" />
|
|
|
|
)}
|
|
|
|
{options.disk &&
|
|
|
|
Array.isArray(options.disk) &&
|
|
|
|
options.disk.map((disk) => (
|
|
|
|
<Resource key={`disk_${disk}`} icon={FiHardDrive} label={t("glances.wait")} percentage="0" />
|
|
|
|
))}
|
|
|
|
{options.uptime && <Resource icon={FaRegClock} label={t("glances.wait")} percentage="0" />}
|
|
|
|
{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-10-17 23:26:55 -07:00
|
|
|
const cpuSensors = data.sensors?.filter(
|
|
|
|
(s) => cpuSensorLabels.some((label) => s.label.startsWith(label)) && s.type === "temperature_core",
|
|
|
|
);
|
2023-04-03 21:47:39 -07:00
|
|
|
if (options.cputemp && cpuSensors) {
|
|
|
|
try {
|
|
|
|
mainTemp = cpuSensors.reduce((acc, s) => acc + s.value, 0) / cpuSensors.length;
|
2023-10-17 23:26:55 -07:00
|
|
|
maxTemp = Math.max(
|
|
|
|
cpuSensors.reduce((acc, s) => acc + (s.warning > 0 ? s.warning : 0), 0) / cpuSensors.length,
|
|
|
|
maxTemp,
|
|
|
|
);
|
2023-04-03 21:47:39 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-10-17 23:26:55 -07:00
|
|
|
const addedClasses = classNames("information-widget-glances", { expanded: options.expanded });
|
2023-09-10 23:36:54 +02:00
|
|
|
|
2022-10-10 11:18:29 -07:00
|
|
|
return (
|
2023-09-10 23:36:54 +02:00
|
|
|
<Resources options={options} target={settings.target ?? "_blank"} additionalClassNames={addedClasses}>
|
2023-10-17 23:26:55 -07:00
|
|
|
{options.cpu !== false && (
|
|
|
|
<Resource
|
|
|
|
icon={FiCpu}
|
|
|
|
value={t("common.number", {
|
|
|
|
value: data.cpu.total,
|
|
|
|
style: "unit",
|
|
|
|
unit: "percent",
|
|
|
|
maximumFractionDigits: 0,
|
|
|
|
})}
|
|
|
|
label={t("glances.cpu")}
|
|
|
|
expandedValue={t("common.number", {
|
|
|
|
value: data.load.min15,
|
|
|
|
style: "unit",
|
|
|
|
unit: "percent",
|
|
|
|
maximumFractionDigits: 0,
|
|
|
|
})}
|
|
|
|
expandedLabel={t("glances.load")}
|
|
|
|
percentage={data.cpu.total}
|
|
|
|
expanded={options.expanded}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{options.mem !== false && (
|
|
|
|
<Resource
|
|
|
|
icon={FaMemory}
|
|
|
|
value={t("common.bytes", {
|
|
|
|
value: data.mem.free,
|
|
|
|
maximumFractionDigits: 1,
|
|
|
|
binary: true,
|
|
|
|
})}
|
|
|
|
label={t("glances.free")}
|
|
|
|
expandedValue={t("common.bytes", {
|
|
|
|
value: data.mem.total,
|
|
|
|
maximumFractionDigits: 1,
|
|
|
|
binary: true,
|
|
|
|
})}
|
|
|
|
expandedLabel={t("glances.total")}
|
|
|
|
percentage={data.mem.percent}
|
|
|
|
expanded={options.expanded}
|
|
|
|
/>
|
|
|
|
)}
|
2023-06-03 01:10:15 +01:00
|
|
|
{disks.map((disk) => (
|
2023-10-17 23:26:55 -07:00
|
|
|
<Resource
|
|
|
|
key={`disk_${disk.mnt_point ?? disk.device_name}`}
|
2023-06-05 23:18:18 +01:00
|
|
|
icon={FiHardDrive}
|
|
|
|
value={t("common.bytes", { value: disk.free })}
|
|
|
|
label={t("glances.free")}
|
|
|
|
expandedValue={t("common.bytes", { value: disk.size })}
|
|
|
|
expandedLabel={t("glances.total")}
|
|
|
|
percentage={disk.percent}
|
|
|
|
expanded={options.expanded}
|
|
|
|
/>
|
2023-06-03 01:10:15 +01:00
|
|
|
))}
|
2023-10-17 23:26:55 -07:00
|
|
|
{options.cputemp && mainTemp > 0 && (
|
2023-06-05 23:18:18 +01:00
|
|
|
<Resource
|
|
|
|
icon={FaThermometerHalf}
|
|
|
|
value={t("common.number", {
|
2023-06-03 01:10:15 +01:00
|
|
|
value: mainTemp,
|
|
|
|
maximumFractionDigits: 1,
|
|
|
|
style: "unit",
|
2023-10-17 23:26:55 -07:00
|
|
|
unit,
|
2023-06-05 23:18:18 +01:00
|
|
|
})}
|
|
|
|
label={t("glances.temp")}
|
|
|
|
expandedValue={t("common.number", {
|
2023-06-03 01:10:15 +01:00
|
|
|
value: maxTemp,
|
|
|
|
maximumFractionDigits: 1,
|
|
|
|
style: "unit",
|
2023-10-17 23:26:55 -07:00
|
|
|
unit,
|
2023-06-05 23:18:18 +01:00
|
|
|
})}
|
|
|
|
expandedLabel={t("glances.warn")}
|
|
|
|
percentage={tempPercent}
|
|
|
|
expanded={options.expanded}
|
|
|
|
/>
|
2023-10-17 23:26:55 -07:00
|
|
|
)}
|
|
|
|
{options.uptime && data.uptime && (
|
2023-06-05 23:18:18 +01:00
|
|
|
<Resource
|
|
|
|
icon={FaRegClock}
|
|
|
|
value={data.uptime.replace(" days,", t("glances.days")).replace(/:\d\d:\d\d$/g, t("glances.hours"))}
|
|
|
|
label={t("glances.uptime")}
|
|
|
|
percentage={Math.round((new Date().getSeconds() / 60) * 100).toString()}
|
|
|
|
/>
|
2023-10-17 23:26:55 -07:00
|
|
|
)}
|
2023-06-03 01:10:15 +01:00
|
|
|
{options.label && <WidgetLabel label={options.label} />}
|
|
|
|
</Resources>
|
2022-10-10 11:18:29 -07:00
|
|
|
);
|
|
|
|
}
|