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-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) {
|
|
|
|
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}>
|
2023-09-08 08:59:04 -07:00
|
|
|
{ options.cpu !== false && <Resource icon={FiCpu} label={t("glances.wait")} percentage="0" /> }
|
|
|
|
{ options.mem !== false && <Resource icon={FaMemory} label={t("glances.wait")} percentage="0" /> }
|
2023-06-05 23:18:18 +01:00
|
|
|
{ options.cputemp && <Resource icon={FaThermometerHalf} label={t("glances.wait")} percentage="0" /> }
|
2023-06-12 00:23:01 +01:00
|
|
|
{ options.disk && !Array.isArray(options.disk) && <Resource key={options.disk} icon={FiHardDrive} label={t("glances.wait")} percentage="0" /> }
|
2023-08-03 08:34:21 -07:00
|
|
|
{ options.disk && Array.isArray(options.disk) && options.disk.map((disk) => <Resource key={`disk_${disk}`} icon={FiHardDrive} label={t("glances.wait")} percentage="0" /> ) }
|
2023-06-05 23:18:18 +01:00
|
|
|
{ options.uptime && <Resource icon={FaRegClock} label={t("glances.wait")} percentage="0" /> }
|
|
|
|
{ options.label && <WidgetLabel label={options.label} /> }
|
2023-06-03 01:10:15 +01:00
|
|
|
</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;
|
2023-07-20 14:40:27 -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);
|
|
|
|
}
|
|
|
|
|
2022-10-10 11:18:29 -07:00
|
|
|
return (
|
2023-06-03 01:10:15 +01:00
|
|
|
<Resources options={options} target={settings.target ?? "_blank"}>
|
2023-09-08 08:59:04 -07:00
|
|
|
{options.cpu !== false && <Resource
|
2023-06-05 23:18:18 +01:00
|
|
|
icon={FiCpu}
|
|
|
|
value={t("common.number", {
|
2023-06-03 01:10:15 +01:00
|
|
|
value: data.cpu.total,
|
|
|
|
style: "unit",
|
|
|
|
unit: "percent",
|
|
|
|
maximumFractionDigits: 0,
|
2023-06-05 23:18:18 +01:00
|
|
|
})}
|
|
|
|
label={t("glances.cpu")}
|
|
|
|
expandedValue={t("common.number", {
|
2023-06-03 01:10:15 +01:00
|
|
|
value: data.load.min15,
|
|
|
|
style: "unit",
|
|
|
|
unit: "percent",
|
2023-06-05 23:18:18 +01:00
|
|
|
maximumFractionDigits: 0
|
|
|
|
})}
|
|
|
|
expandedLabel={t("glances.load")}
|
|
|
|
percentage={data.cpu.total}
|
|
|
|
expanded={options.expanded}
|
2023-09-08 08:59:04 -07:00
|
|
|
/>}
|
|
|
|
{options.mem !== false && <Resource
|
2023-06-05 23:18:18 +01:00
|
|
|
icon={FaMemory}
|
|
|
|
value={t("common.bytes", {
|
2023-06-03 01:10:15 +01:00
|
|
|
value: data.mem.free,
|
|
|
|
maximumFractionDigits: 1,
|
|
|
|
binary: true,
|
2023-06-05 23:18:18 +01:00
|
|
|
})}
|
|
|
|
label={t("glances.free")}
|
|
|
|
expandedValue={t("common.bytes", {
|
2023-06-03 01:10:15 +01:00
|
|
|
value: data.mem.total,
|
|
|
|
maximumFractionDigits: 1,
|
|
|
|
binary: true,
|
2023-06-05 23:18:18 +01:00
|
|
|
})}
|
|
|
|
expandedLabel={t("glances.total")}
|
|
|
|
percentage={data.mem.percent}
|
|
|
|
expanded={options.expanded}
|
2023-09-08 08:59:04 -07:00
|
|
|
/>}
|
2023-06-03 01:10:15 +01:00
|
|
|
{disks.map((disk) => (
|
2023-08-03 08:34:21 -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
|
|
|
))}
|
|
|
|
{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",
|
|
|
|
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",
|
|
|
|
unit
|
2023-06-05 23:18:18 +01:00
|
|
|
})}
|
|
|
|
expandedLabel={t("glances.warn")}
|
|
|
|
percentage={tempPercent}
|
|
|
|
expanded={options.expanded}
|
|
|
|
/>
|
2023-06-03 01:10:15 +01: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-06-03 01:10:15 +01:00
|
|
|
}
|
|
|
|
{options.label && <WidgetLabel label={options.label} />}
|
|
|
|
</Resources>
|
2022-10-10 11:18:29 -07:00
|
|
|
);
|
|
|
|
}
|