Added an option to opt into tracking GPU stats in the dashboard. (#10)

* Added an option to opt in to GPU usage.

* Catch and log error if exists during GPU stat monitoring.

* Removed unimplemented category header for System Tray in options

* Update the readme to indicate this change.
This commit is contained in:
Jas Singh
2024-07-27 14:15:56 -07:00
committed by GitHub
parent 42ccd573be
commit 0ee58c69c3
5 changed files with 99 additions and 61 deletions

View File

@@ -145,3 +145,9 @@ Where each monitor is defined by its index (0, 1, 2 in this case) and each secti
"systray" "systray"
``` ```
Since the text-box in the options dialog isn't sufficient, it is recommended that you create this JSON configuration in a text editor elsewhere and paste it into the layout text-box under Configuration > Bar > "Bar Layouts for Monitors". Since the text-box in the options dialog isn't sufficient, it is recommended that you create this JSON configuration in a text editor elsewhere and paste it into the layout text-box under Configuration > Bar > "Bar Layouts for Monitors".
### Additional Configuration
#### GPU Tracking
If you have an NVidia GPU, you can track your GPU usage in your Dashboard by going to your `Settings > Configuration > Dashboard Menu > Track GPU` and turning it on.

View File

@@ -2,6 +2,7 @@ import options from "options";
import { GPU_Stat } from "lib/types/gpustat"; import { GPU_Stat } from "lib/types/gpustat";
const { terminal } = options; const { terminal } = options;
const { enable_gpu } = options.menus.dashboard.stats;
const Stats = () => { const Stats = () => {
const divide = ([total, free]) => free / total; const divide = ([total, free]) => free / total;
@@ -61,32 +62,7 @@ const Stats = () => {
}, },
); );
const gpu = Variable(0, { const gpu = Variable(0);
poll: [
2000,
"gpustat --json",
(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;
return divide([totalGpu, usedGpu]);
} catch (e) {
console.error("Error getting GPU stats:", e);
return 0;
}
},
],
});
const storage = Variable( const storage = Variable(
{ total: 0, used: 0, percentage: 0 }, { total: 0, used: 0, percentage: 0 },
@@ -226,41 +202,91 @@ const Stats = () => {
class_name: "stat gpu", class_name: "stat gpu",
hexpand: true, hexpand: true,
vpack: "center", vpack: "center",
children: [ setup: self => {
Widget.Button({ const getGpuUsage = () => {
on_primary_click: terminal.bind("value").as(term => { if (!enable_gpu.value) {
return () => { gpu.value = 0;
App.closeWindow("dashboardmenu"); return;
Utils.execAsync(`bash -c "${term} -e btop"`).catch( }
(err) => `Failed to open btop: ${err}`, Utils.execAsync("gpustat --json")
); .then((out) => {
} if (typeof out !== "string") {
}), return 0;
label: "󰢮", }
}), try {
Widget.Button({ const data = JSON.parse(out);
on_primary_click: terminal.bind("value").as(term => {
return () => { const totalGpu = 100;
App.closeWindow("dashboardmenu"); const usedGpu =
Utils.execAsync(`bash -c "${term} -e btop"`).catch( data.gpus.reduce((acc: number, gpu: GPU_Stat) => {
(err) => `Failed to open btop: ${err}`,
); return acc + gpu["utilization.gpu"]
} }, 0) / data.gpus.length;
}),
child: Widget.LevelBar({ gpu.value = divide([totalGpu, usedGpu]);
class_name: "stats-bar", } catch (e) {
hexpand: true, console.error("Error getting GPU stats:", e);
vpack: "center", gpu.value = 0;
value: gpu.bind("value"), }
}), })
}), .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.Label({ Widget.Box({
hpack: "end", hpack: "end",
class_name: "stat-value gpu", children: Utils.merge([gpu.bind("value"), enable_gpu.bind("value")], (gpuUsed, enableGpu) => {
label: gpu.bind("value").as((v) => `${Math.floor(v * 100)}%`), if (!enableGpu) {
}), return [];
}
return [
Widget.Label({
class_name: "stat-value gpu",
label: `${Math.floor(gpuUsed * 100)}%`,
})
];
})
})
], ],
}), }),
Widget.Box({ Widget.Box({

View File

@@ -617,6 +617,9 @@ const options = mkOptions(OPTIONS, {
name: opt<"system" | string>("system"), name: opt<"system" | string>("system"),
}, },
}, },
stats: {
enable_gpu: opt(false),
},
shortcuts: { shortcuts: {
left: { left: {
shortcut1: { shortcut1: {

View File

@@ -37,8 +37,8 @@ export const BarSettings = () => {
Header('Battery'), Header('Battery'),
Option({ opt: options.bar.battery.label, title: 'Show Battery Percentage', type: 'boolean' }), Option({ opt: options.bar.battery.label, title: 'Show Battery Percentage', type: 'boolean' }),
Header('System Tray'), // Header('System Tray'),
// TODO: Figure out how to hand arrays // TODO: Figure out how to handle arrays
// Option({ opt: options.bar.systray.ignore, title: 'Ignore', subtitle: 'Comma separated string of apps to ignore in the tray', type: 'string' }), // Option({ opt: options.bar.systray.ignore, title: 'Ignore', subtitle: 'Comma separated string of apps to ignore in the tray', type: 'string' }),
Header('Clock'), Header('Clock'),

View File

@@ -22,6 +22,9 @@ export const DashboardMenuSettings = () => {
Option({ opt: options.menus.dashboard.powermenu.logout, title: 'Logout Command', type: 'string' }), Option({ opt: options.menus.dashboard.powermenu.logout, title: 'Logout Command', type: 'string' }),
Option({ opt: options.menus.dashboard.powermenu.sleep, title: 'Sleep Command', type: 'string' }), Option({ opt: options.menus.dashboard.powermenu.sleep, title: 'Sleep Command', type: 'string' }),
Header('Resource Usage Metrics'),
Option({ opt: options.menus.dashboard.stats.enable_gpu, title: 'Track GPU', subtitle: 'NOTE: This is currently only available for NVidia GPUs and requires \'python-gpustat\'.', type: 'boolean' }),
Header('Shortcuts'), Header('Shortcuts'),
Option({ opt: options.menus.dashboard.shortcuts.left.shortcut1.icon, title: 'Left - Shortcut 1 (Icon)', type: 'string' }), Option({ opt: options.menus.dashboard.shortcuts.left.shortcut1.icon, title: 'Left - Shortcut 1 (Icon)', type: 'string' }),
Option({ opt: options.menus.dashboard.shortcuts.left.shortcut1.command, title: 'Left - Shortcut 1 (Command)', type: 'string' }), Option({ opt: options.menus.dashboard.shortcuts.left.shortcut1.command, title: 'Left - Shortcut 1 (Command)', type: 'string' }),