Skip to content

feat: implement hardware monitoring updates with event listener and refactor related hooks#1227

Merged
shm11C3 merged 17 commits into
developfrom
refactor/#1111
Mar 8, 2026
Merged

feat: implement hardware monitoring updates with event listener and refactor related hooks#1227
shm11C3 merged 17 commits into
developfrom
refactor/#1111

Conversation

@shm11C3

@shm11C3 shm11C3 commented Mar 7, 2026

Copy link
Copy Markdown
Owner

This pull request introduces a unified event-driven system for hardware monitoring updates, refactors the GPU sampling logic to include source metadata, and streamlines the way hardware usage data is communicated from the backend to the frontend. The changes enable more robust and extensible hardware monitoring, simplify frontend hooks, and improve testability and traceability of GPU metrics.

Backend: Hardware Monitoring Event System

  • Introduced a new HardwareMonitorUpdate struct (with CPU, memory, GPU usage, and per-processor usage) and registered it as a Tauri event, enabling real-time hardware updates to be emitted from the backend to the frontend. (src-tauri/src/models/hardware.rs, src-tauri/src/lib.rs, src-tauri/src/workers/system_monitor.rs) [1] [2] [3] [4] [5] [6] [7]

  • Refactored the system and GPU sampling functions to return structured data (SystemSample, GpuSample) and included a source string in GpuSample for improved traceability of GPU metrics across platforms (Windows, Linux, macOS). (src-tauri/src/services/monitoring_service.rs) [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

Frontend: Usage Data Handling Simplification

  • Replaced multiple usage-specific hooks (useUsageUpdater) with a single event-driven hook (useHardwareEventListener) that listens for the new hardware update event, simplifying state management and reducing boilerplate. (src/App.tsx) [1] [2]

Testing & Type Updates

  • Updated all GPU history-related tests and logic to use the new GpuSample tuple format (including the source field), ensuring consistency and future extensibility. (src-tauri/src/services/monitoring_service.rs) [1] [2] [3] [4] [5]

  • Removed obsolete usage updater hooks and related test code, focusing tests on the new event-driven approach. (src/features/hardware/hooks/useHardwareData.test.ts) [1] [2]

Copilot AI review requested due to automatic review settings March 7, 2026 19:23
@github-actions github-actions Bot added rust Pull requests that update Rust code frontend labels Mar 7, 2026
@github-actions

github-actions Bot commented Mar 7, 2026

Copy link
Copy Markdown
Contributor

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 99.09% (🎯 60%) 990 / 999
🔵 Statements 98.87% (🎯 60%) 1051 / 1063
🔵 Functions 98.79% (🎯 60%) 245 / 248
🔵 Branches 93.96% (🎯 60%) 327 / 348
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/features/hardware/hooks/useHardwareEventListener.ts 100% 100% 100% 100%
src/features/hardware/store/chart.ts 100% 100% 100% 100%
Generated in workflow #2447 for commit ed938e9 by the Vitest Coverage Report Action

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR switches hardware usage updates (CPU/RAM/GPU/per-core usage) from frontend polling to a backend-pushed Tauri event, and refactors the frontend hooks/tests accordingly. This fits the app’s monitoring pipeline by moving periodic sampling to the Rust worker and distributing results to the React UI via tauri-specta events.

Changes:

  • Add a new hardware-monitor-update event emitted by the Rust SystemMonitorController and exposed to the frontend bindings.
  • Introduce useHardwareEventListener to update Jotai chart history atoms from backend events (replacing the 4 polling hooks).
  • Remove useUsageUpdater and update/replace related test coverage.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/rspc/bindings.ts Adds generated event typings + events.hardwareMonitorUpdate listener binding.
src/features/hardware/types/hardwareDataType.ts Removes now-unused ChartDataHardwareType type.
src/features/hardware/hooks/useHardwareEventListener.ts New hook to consume backend hardware update events and update chart state.
src/features/hardware/hooks/useHardwareEventListener.test.ts Adds tests for event-driven history/source updates.
src/features/hardware/hooks/useHardwareData.ts Removes useUsageUpdater; keeps useHardwareUpdater for temp/fan polling.
src/features/hardware/hooks/useHardwareData.test.ts Removes useUsageUpdater tests and related mocks.
src/App.tsx Replaces multiple polling hooks with useHardwareEventListener.
src-tauri/src/workers/system_monitor.rs Emits HardwareMonitorUpdate events after each sampling tick.
src-tauri/src/services/monitoring_service.rs Refactors sampling to return samples (system + GPU) and includes GPU “source” in the GPU sample tuple.
src-tauri/src/models/hardware.rs Adds HardwareMonitorUpdate event payload type (specta + tauri-specta event).
src-tauri/src/lib.rs Registers the new event with collect_events! and passes AppHandle into the monitor worker.

Comment thread src/features/hardware/hooks/useHardwareEventListener.test.ts Outdated
Comment thread src/features/hardware/hooks/useHardwareEventListener.ts Outdated
Comment thread src/features/hardware/hooks/useHardwareEventListener.ts Outdated
Comment thread src/features/hardware/hooks/useHardwareEventListener.ts
@github-actions

github-actions Bot commented Mar 7, 2026

Copy link
Copy Markdown
Contributor

Rust Backend Coverage Report

Coverage Details
Filename                                         Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover    Branches   Missed Branches     Cover
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
_tests/commands/background_image_test.rs              39                 0   100.00%           6                 0   100.00%          21                 0   100.00%           0                 0         -
_tests/commands/settings_test.rs                     220                 0   100.00%          18                 0   100.00%         166                 0   100.00%           0                 0         -
commands/background_image.rs                          22                 7    68.18%          11                 5    54.55%          19                 7    63.16%           0                 0         -
commands/hardware.rs                                  65                65     0.00%          20                20     0.00%          65                65     0.00%           0                 0         -
commands/settings.rs                                 526               526     0.00%          94                94     0.00%         448               448     0.00%           0                 0         -
commands/system.rs                                     6                 6     0.00%           3                 3     0.00%           5                 5     0.00%           0                 0         -
commands/ui.rs                                        17                17     0.00%           2                 2     0.00%          13                13     0.00%           0                 0         -
commands/updater.rs                                   97                97     0.00%          15                15     0.00%          66                66     0.00%           0                 0         -
enums/error.rs                                       105                 0   100.00%           8                 0   100.00%          89                 0   100.00%           0                 0         -
enums/hardware.rs                                    188                 1    99.47%          15                 0   100.00%         114                 0   100.00%           0                 0         -
enums/settings.rs                                    415                 6    98.55%          24                 0   100.00%         279                 0   100.00%           0                 0         -
infrastructure/database/db.rs                         21                21     0.00%           2                 2     0.00%          12                12     0.00%           0                 0         -
infrastructure/database/gpu_archive.rs                49                49     0.00%           4                 4     0.00%          18                18     0.00%           0                 0         -
infrastructure/database/hardware_archive.rs           41                41     0.00%           4                 4     0.00%          21                21     0.00%           0                 0         -
infrastructure/database/migration.rs                   8                 8     0.00%           1                 1     0.00%          37                37     0.00%           0                 0         -
infrastructure/database/process_stats.rs              41                41     0.00%           4                 4     0.00%          29                29     0.00%           0                 0         -
infrastructure/providers/linux/dmidecode.rs          229                15    93.45%          16                 3    81.25%         319                14    95.61%           0                 0         -
infrastructure/providers/linux/drm_sys.rs            143               143     0.00%          13                13     0.00%          87                87     0.00%           0                 0         -
infrastructure/providers/linux/hwmon.rs              119                94    21.01%           8                 6    25.00%          68                56    17.65%           0                 0         -
infrastructure/providers/linux/kernel.rs             165                22    86.67%          19                 2    89.47%         161                 8    95.03%           0                 0         -
infrastructure/providers/linux/lspci.rs               23                23     0.00%           1                 1     0.00%          12                12     0.00%           0                 0         -
infrastructure/providers/linux/net_sys.rs            171               171     0.00%          13                13     0.00%          93                93     0.00%           0                 0         -
infrastructure/providers/linux/procfs.rs             261                24    90.80%          25                 3    88.00%         222                19    91.44%           0                 0         -
infrastructure/providers/sysinfo_provider.rs          54                54     0.00%           2                 2     0.00%          45                45     0.00%           0                 0         -
lib.rs                                               209               209     0.00%           5                 5     0.00%         114               114     0.00%           0                 0         -
main.rs                                                3                 3     0.00%           1                 1     0.00%           3                 3     0.00%           0                 0         -
models/hardware.rs                                   133                 0   100.00%          11                 0   100.00%          83                 0   100.00%           0                 0         -
models/settings.rs                                   190                 0   100.00%          11                 0   100.00%         163                 0   100.00%           0                 0         -
platform/factory.rs                                   18                18     0.00%           4                 4     0.00%          15                15     0.00%           0                 0         -
platform/linux/cache.rs                               53                53     0.00%           4                 4     0.00%          38                38     0.00%           0                 0         -
platform/linux/gpu.rs                                139               139     0.00%          13                13     0.00%         102               102     0.00%           0                 0         -
platform/linux/memory.rs                              43                43     0.00%           6                 6     0.00%          41                41     0.00%           0                 0         -
platform/linux/mod.rs                                 34                34     0.00%          11                11     0.00%          70                70     0.00%           0                 0         -
platform/linux/network.rs                              4                 4     0.00%           1                 1     0.00%           4                 4     0.00%           0                 0         -
services/archive_service.rs                         1127               154    86.34%          90                15    83.33%         654               134    79.51%           0                 0         -
services/background_image_service.rs                 165                96    41.82%          16                10    37.50%          93                59    36.56%           0                 0         -
services/cpu_service.rs                               32                32     0.00%           4                 4     0.00%          15                15     0.00%           0                 0         -
services/gpu_service.rs                               37                37     0.00%          10                10     0.00%          31                31     0.00%           0                 0         -
services/hardware_service.rs                          67                67     0.00%           5                 5     0.00%          43                43     0.00%           0                 0         -
services/language_service.rs                         101                 0   100.00%          18                 0   100.00%          57                 0   100.00%           0                 0         -
services/memory_service.rs                            22                22     0.00%           4                 4     0.00%          15                15     0.00%           0                 0         -
services/monitoring_service.rs                       776               138    82.22%          54                15    72.22%         438                91    79.22%           0                 0         -
services/motherboard_service.rs                       10                10     0.00%           3                 3     0.00%           7                 7     0.00%           0                 0         -
services/network_service.rs                            9                 9     0.00%           1                 1     0.00%           7                 7     0.00%           0                 0         -
services/process_service.rs                           86                86     0.00%           5                 5     0.00%          50                50     0.00%           0                 0         -
services/settings_service.rs                         246               129    47.56%          30                14    53.33%         234               129    44.87%           0                 0         -
services/system_service.rs                            22                22     0.00%           2                 2     0.00%          12                12     0.00%           0                 0         -
services/ui_service.rs                                45                45     0.00%           8                 8     0.00%          36                36     0.00%           0                 0         -
utils/color.rs                                        66                 1    98.48%           4                 0   100.00%          26                 0   100.00%           0                 0         -
utils/file.rs                                        224                 5    97.77%          14                 0   100.00%         144                 4    97.22%           0                 0         -
utils/formatter.rs                                   195                 8    95.90%          16                 0   100.00%         160                12    92.50%           0                 0         -
utils/ip.rs                                           65                 0   100.00%           5                 0   100.00%          33                 0   100.00%           0                 0         -
utils/logger.rs                                       71                71     0.00%           1                 1     0.00%          38                38     0.00%           0                 0         -
utils/rounding.rs                                     68                 0   100.00%           7                 0   100.00%          41                 0   100.00%           0                 0         -
utils/tauri.rs                                       138                 0   100.00%          17                 0   100.00%          82                 0   100.00%           0                 0         -
workers/hardware_archive.rs                           52                52     0.00%           6                 6     0.00%          36                36     0.00%           0                 0         -
workers/mod.rs                                        24                24     0.00%           2                 2     0.00%          16                16     0.00%           0                 0         -
workers/system_monitor.rs                            151               151     0.00%          11                11     0.00%         106               106     0.00%           0                 0         -
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                                               7650              3093    59.57%         728               358    50.82%        5416              2283    57.85%           0                 0         -

Copilot AI review requested due to automatic review settings March 7, 2026 19:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 5 comments.

Comment thread src/features/hardware/hooks/useHardwareEventListener.test.ts
Comment thread src/features/hardware/hooks/useHardwareEventListener.test.ts Outdated
Comment thread src/features/hardware/hooks/useHardwareData.test.ts Outdated
Comment thread src-tauri/src/workers/system_monitor.rs Outdated
Comment thread src-tauri/src/workers/system_monitor.rs
Copilot AI review requested due to automatic review settings March 7, 2026 20:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.

Comment thread src/features/hardware/hooks/useHardwareEventListener.test.ts
Comment thread src/features/hardware/hooks/useHardwareEventListener.ts Outdated
Comment thread src/rspc/bindings.ts
Comment thread src-tauri/src/workers/system_monitor.rs Outdated
Copilot AI review requested due to automatic review settings March 8, 2026 04:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

Comment thread src-tauri/src/services/monitoring_service.rs Outdated
Copilot AI review requested due to automatic review settings March 8, 2026 05:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.

Comment thread src/features/hardware/hooks/useHardwareEventListener.ts Outdated
Comment thread src-tauri/src/workers/system_monitor.rs Outdated
@shm11C3 shm11C3 linked an issue Mar 8, 2026 that may be closed by this pull request
Copilot AI review requested due to automatic review settings March 8, 2026 07:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 8 comments.

Comment thread src/features/hardware/hooks/useHardwareData.ts Outdated
Comment thread src/features/hardware/hooks/useHardwareEventListener.ts
Comment thread src/features/hardware/hooks/useHardwareEventListener.ts
Comment thread src/features/hardware/hooks/useHardwareEventListener.ts
Comment thread src/features/hardware/hooks/useHardwareData.test.ts Outdated
Comment thread src/features/hardware/hooks/useHardwareData.test.ts Outdated
Comment thread src-tauri/src/workers/system_monitor.rs
Comment thread src/features/hardware/hooks/useHardwareEventListener.ts
Copilot AI review requested due to automatic review settings March 8, 2026 09:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 19 out of 19 changed files in this pull request and generated 8 comments.

Comment thread src/rspc/bindings.ts Outdated
Comment thread src/features/hardware/hooks/useHardwareEventListener.ts
Comment thread src/features/hardware/hooks/useHardwareEventListener.ts
Comment thread src-tauri/src/platform/traits.rs
Comment thread src-tauri/src/models/hardware.rs
Comment thread src/features/hardware/hooks/useHardwareEventListener.ts
Comment thread src/features/hardware/store/chart.ts
Comment thread src-tauri/src/workers/system_monitor.rs
Copilot AI review requested due to automatic review settings March 8, 2026 09:26
@shm11C3 shm11C3 enabled auto-merge (squash) March 8, 2026 09:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 17 out of 17 changed files in this pull request and generated 4 comments.

Comments suppressed due to low confidence (2)

src-tauri/src/commands/hardware.rs:133

  • The doc comment above get_gpu_memory_usage is for CPU usage history (and even describes GPU-memory best-effort semantics), while the function actually returns GPU memory usage. This incorrect doc will also flow into the generated TS bindings; please swap/fix the Rust doc comments so each command’s docs match its behavior.
///
/// ## Get CPU usage history
///
/// - param state: `tauri::State<AppState>` Application state
/// - param seconds: `u32` Number of seconds to retrieve
/// - **Platform support**: Currently implemented only on macOS. On other
///   platforms, or where the underlying APIs are not available, this will
///   return `Ok(None)` instead of failing.
/// - **Best-effort behavior**: If the GPU memory metrics cannot be queried
///   (e.g. unsupported hardware, missing permissions, or transient errors),
///   the function returns `Ok(None)` to indicate that the data is not
///   available, rather than treating this as a hard error.
/// - **Return format**: When successful, the `GpuMemoryUsage` fields contain
///   human-readable, formatted size strings (for example, `"1.5 GB"`) rather
///   than raw byte counts.
///
/// Returns:
/// - `Ok(Some(GpuMemoryUsage))` when GPU memory usage data is available.
/// - `Ok(None)` when the metric is unsupported or currently unavailable.
/// - `Err(String)` only for unexpected internal failures.

src/rspc/bindings.ts:124

  • The command JSDoc for getCpuUsageHistory currently describes GPU memory usage. Since this file is generated by tauri-specta (see header), the fix should be applied to the Rust command docs (src-tauri/src/commands/hardware.rs) and then bindings should be regenerated, rather than patching docs here.
/**
 * ## Get realtime GPU memory usage (best-effort)
 * 
 * This command attempts to retrieve current GPU memory usage information
 * on a best-effort, platform-dependent basis.
 * 
 */
async getCpuUsageHistory(seconds: number) : Promise<number[]> {
    return await TAURI_INVOKE("get_cpu_usage_history", { seconds });

Comment on lines +75 to +77
if (gpuDedicatedMemoryUsageKb != null) {
setGpuDedicatedMemory(gpuDedicatedMemoryUsageKb);
}

Copilot AI Mar 8, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gpuDedicatedMemoryKbAtom is only updated when gpuDedicatedMemoryUsageKb != null, so a later event with null (transient query failure, GPU change, etc.) will leave a stale non-null value in the UI. Update the atom on every event (set to the new value, including null) so the displayed memory usage can clear correctly.

Suggested change
if (gpuDedicatedMemoryUsageKb != null) {
setGpuDedicatedMemory(gpuDedicatedMemoryUsageKb);
}
setGpuDedicatedMemory(gpuDedicatedMemoryUsageKb);

Copilot uses AI. Check for mistakes.
Comment on lines +38 to +50
(event: {
payload: {
cpuUsage: number;
memoryUsage: number;
gpuUsage: number | null;
gpuName: string | null;
gpuTemperature: number | null;
gpuSource: string | null;
processorsUsage: number[];
gpuDedicatedMemoryUsageKb: number | null;
gpuCoolerLevel: number | null;
};
}) => {

Copilot AI Mar 8, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The event payload shape is duplicated inline here instead of reusing the generated HardwareMonitorUpdate type from @/rspc/bindings. Reusing the shared type (or a typed event wrapper) would avoid drift if the backend event schema changes.

Copilot uses AI. Check for mistakes.
Comment on lines 144 to +148
///
/// ## Get GPU fan speed
///
#[command]
#[specta::specta]
pub async fn get_nvidia_gpu_cooler() -> Result<Vec<models::hardware::NameValue>, String> {
use crate::services::gpu_service;

gpu_service::fetch_nvidia_gpu_cooler().await
}

///
/// ## Get CPU usage history
/// ## Get realtime GPU memory usage (best-effort)
///
/// - param state: `tauri::State<AppState>` Application state
/// - param seconds: `u32` Number of seconds to retrieve
/// This command attempts to retrieve current GPU memory usage information
/// on a best-effort, platform-dependent basis.

Copilot AI Mar 8, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc comment above get_cpu_usage_history describes “realtime GPU memory usage” (best-effort), but this function returns CPU usage history. Please correct the doc comment (and then regenerate src/rspc/bindings.ts) to avoid misleading API docs for frontend callers.

Copilot uses AI. Check for mistakes.
Comment on lines +16 to +18
const padHistory = (arr: (number | null)[]): number[] => {
const padded = Array(Math.max(chartConfig.historyLengthSec - arr.length, 0))
.fill(null)

Copilot AI Mar 8, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

padHistory pads with null but is typed to return number[], which hides the presence of nulls from callers and can lead to unsafe downstream usage. Return (number | null)[] here (and align the history atom / chart prop types accordingly) or change the padding value to a numeric sentinel if the rest of the UI truly requires number[].

Suggested change
const padHistory = (arr: (number | null)[]): number[] => {
const padded = Array(Math.max(chartConfig.historyLengthSec - arr.length, 0))
.fill(null)
const padHistory = (arr: number[]): number[] => {
const padded = Array(Math.max(chartConfig.historyLengthSec - arr.length, 0))
.fill(0)

Copilot uses AI. Check for mistakes.
@shm11C3 shm11C3 merged commit d852652 into develop Mar 8, 2026
26 checks passed
@shm11C3 shm11C3 deleted the refactor/#1111 branch March 8, 2026 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

frontend rust Pull requests that update Rust code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor: migrate IPC polling to Tauri event push model

2 participants