HyprPanel now checks for the gpustat dependency and provides a notification if missing rather than crash. (#43)

Solves #42
This commit is contained in:
Jas Singh
2024-07-30 01:45:55 -07:00
committed by GitHub
parent 0787e00336
commit 26f61ed513

View File

@@ -1,5 +1,6 @@
import options from "options"; import options from "options";
import { GPU_Stat } from "lib/types/gpustat"; import { GPU_Stat } from "lib/types/gpustat";
import { dependencies } from "lib/utils";
const { terminal } = options; const { terminal } = options;
const { enable_gpu } = options.menus.dashboard.stats; const { enable_gpu } = options.menus.dashboard.stats;
@@ -64,6 +65,110 @@ const Stats = () => {
const gpu = Variable(0); const gpu = Variable(0);
const GPUStat = Widget.Box({
child: enable_gpu.bind("value").as((gpStat) => {
if (!gpStat || !dependencies("gpustat")) {
return Widget.Box();
}
return Widget.Box({
vertical: true,
children: [
Widget.Box({
class_name: "stat gpu",
hexpand: true,
vpack: "center",
setup: self => {
const getGpuUsage = () => {
if (!enable_gpu.value) {
gpu.value = 0;
return;
}
Utils.execAsync("gpustat --json")
.then((out) => {
if (typeof out !== "string") {
return 0;
}
try {
const data = JSON.parse(out);
const totalGpu = 100;
const usedGpu =
data.gpus.reduce((acc: number, gpu: GPU_Stat) => {
return acc + gpu["utilization.gpu"]
}, 0) / data.gpus.length;
gpu.value = divide([totalGpu, usedGpu]);
} catch (e) {
console.error("Error getting GPU stats:", e);
gpu.value = 0;
}
})
.catch((err) => {
console.error(`An error occurred while fetching GPU stats: ${err}`)
})
}
self.poll(2000, getGpuUsage)
Utils.merge([gpu.bind("value"), enable_gpu.bind("value")], (gpu, enableGpu) => {
if (!enableGpu) {
return self.children = [];
}
return self.children = [
Widget.Button({
on_primary_click: terminal.bind("value").as(term => {
return () => {
App.closeWindow("dashboardmenu");
Utils.execAsync(`bash -c "${term} -e btop"`).catch(
(err) => `Failed to open btop: ${err}`,
);
}
}),
label: "󰢮",
}),
Widget.Button({
on_primary_click: terminal.bind("value").as(term => {
return () => {
App.closeWindow("dashboardmenu");
Utils.execAsync(`bash -c "${term} -e btop"`).catch(
(err) => `Failed to open btop: ${err}`,
);
}
}),
child: Widget.LevelBar({
class_name: "stats-bar",
hexpand: true,
vpack: "center",
value: gpu,
}),
}),
]
})
},
}),
Widget.Box({
hpack: "end",
children: Utils.merge([gpu.bind("value"), enable_gpu.bind("value")], (gpuUsed, enableGpu) => {
if (!enableGpu) {
return [];
}
return [
Widget.Label({
class_name: "stat-value gpu",
label: `${Math.floor(gpuUsed * 100)}%`,
})
];
})
})
]
})
})
});
const storage = Variable( const storage = Variable(
{ total: 0, used: 0, percentage: 0 }, { total: 0, used: 0, percentage: 0 },
{ {
@@ -195,100 +300,7 @@ const Stats = () => {
}), }),
], ],
}), }),
Widget.Box({ GPUStat,
vertical: true,
children: [
Widget.Box({
class_name: "stat gpu",
hexpand: true,
vpack: "center",
setup: self => {
const getGpuUsage = () => {
if (!enable_gpu.value) {
gpu.value = 0;
return;
}
Utils.execAsync("gpustat --json")
.then((out) => {
if (typeof out !== "string") {
return 0;
}
try {
const data = JSON.parse(out);
const totalGpu = 100;
const usedGpu =
data.gpus.reduce((acc: number, gpu: GPU_Stat) => {
return acc + gpu["utilization.gpu"]
}, 0) / data.gpus.length;
gpu.value = divide([totalGpu, usedGpu]);
} catch (e) {
console.error("Error getting GPU stats:", e);
gpu.value = 0;
}
})
.catch((err) => {
console.error(`An error occurred while fetching GPU stats: ${err}`)
})
}
self.poll(2000, getGpuUsage)
Utils.merge([gpu.bind("value"), enable_gpu.bind("value")], (gpu, enableGpu) => {
if (!enableGpu) {
return self.children = [];
}
return self.children = [
Widget.Button({
on_primary_click: terminal.bind("value").as(term => {
return () => {
App.closeWindow("dashboardmenu");
Utils.execAsync(`bash -c "${term} -e btop"`).catch(
(err) => `Failed to open btop: ${err}`,
);
}
}),
label: "󰢮",
}),
Widget.Button({
on_primary_click: terminal.bind("value").as(term => {
return () => {
App.closeWindow("dashboardmenu");
Utils.execAsync(`bash -c "${term} -e btop"`).catch(
(err) => `Failed to open btop: ${err}`,
);
}
}),
child: Widget.LevelBar({
class_name: "stats-bar",
hexpand: true,
vpack: "center",
value: gpu,
}),
}),
]
})
},
}),
Widget.Box({
hpack: "end",
children: Utils.merge([gpu.bind("value"), enable_gpu.bind("value")], (gpuUsed, enableGpu) => {
if (!enableGpu) {
return [];
}
return [
Widget.Label({
class_name: "stat-value gpu",
label: `${Math.floor(gpuUsed * 100)}%`,
})
];
})
})
],
}),
Widget.Box({ Widget.Box({
vertical: true, vertical: true,
children: [ children: [