From 0ee58c69c3dcb9c03257b8fe476fa5dc98ec5b0a Mon Sep 17 00:00:00 2001 From: Jas Singh Date: Sat, 27 Jul 2024 14:15:56 -0700 Subject: [PATCH] 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. --- README.md | 6 + modules/menus/dashboard/stats/index.ts | 144 +++++++++++------- options.ts | 3 + widget/settings/pages/config/bar/index.ts | 4 +- .../settings/pages/config/menus/dashboard.ts | 3 + 5 files changed, 99 insertions(+), 61 deletions(-) diff --git a/README.md b/README.md index f2a0173..ec4a4fb 100644 --- a/README.md +++ b/README.md @@ -145,3 +145,9 @@ Where each monitor is defined by its index (0, 1, 2 in this case) and each secti "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". + +### 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. diff --git a/modules/menus/dashboard/stats/index.ts b/modules/menus/dashboard/stats/index.ts index ca8c54c..c1ef06d 100644 --- a/modules/menus/dashboard/stats/index.ts +++ b/modules/menus/dashboard/stats/index.ts @@ -2,6 +2,7 @@ import options from "options"; import { GPU_Stat } from "lib/types/gpustat"; const { terminal } = options; +const { enable_gpu } = options.menus.dashboard.stats; const Stats = () => { const divide = ([total, free]) => free / total; @@ -61,32 +62,7 @@ const Stats = () => { }, ); - 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 gpu = Variable(0); const storage = Variable( { total: 0, used: 0, percentage: 0 }, @@ -226,41 +202,91 @@ const Stats = () => { class_name: "stat gpu", hexpand: true, vpack: "center", - 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.bind("value"), - }), - }), - ], + 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.Label({ + Widget.Box({ hpack: "end", - class_name: "stat-value gpu", - label: gpu.bind("value").as((v) => `${Math.floor(v * 100)}%`), - }), + 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({ diff --git a/options.ts b/options.ts index 0e63199..b081b11 100644 --- a/options.ts +++ b/options.ts @@ -617,6 +617,9 @@ const options = mkOptions(OPTIONS, { name: opt<"system" | string>("system"), }, }, + stats: { + enable_gpu: opt(false), + }, shortcuts: { left: { shortcut1: { diff --git a/widget/settings/pages/config/bar/index.ts b/widget/settings/pages/config/bar/index.ts index c1b77d1..f8ea914 100644 --- a/widget/settings/pages/config/bar/index.ts +++ b/widget/settings/pages/config/bar/index.ts @@ -37,8 +37,8 @@ export const BarSettings = () => { Header('Battery'), Option({ opt: options.bar.battery.label, title: 'Show Battery Percentage', type: 'boolean' }), - Header('System Tray'), - // TODO: Figure out how to hand arrays + // Header('System Tray'), + // 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' }), Header('Clock'), diff --git a/widget/settings/pages/config/menus/dashboard.ts b/widget/settings/pages/config/menus/dashboard.ts index 63d406a..8fc9d2a 100644 --- a/widget/settings/pages/config/menus/dashboard.ts +++ b/widget/settings/pages/config/menus/dashboard.ts @@ -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.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'), 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' }),