added commentation

This commit is contained in:
2025-10-01 12:07:53 +02:00
parent d994be757e
commit 8c49a63a50
13 changed files with 570 additions and 56 deletions

View File

@@ -2,6 +2,29 @@ use anyhow::Result;
use std::error::Error;
use sysinfo::System;
//! # CPU Hardware Module
//!
//! This module provides CPU information collection for WatcherAgent, including load, temperature, and system uptime.
//!
//! ## Responsibilities
//! - **CPU Detection:** Identifies CPU model and core count.
//! - **Metric Collection:** Queries CPU load, temperature, and uptime.
//! - **Error Handling:** Graceful fallback if metrics are unavailable.
//!
//! ## Units
//! - `current_load`: CPU usage as a percentage (**0.0100.0**)
//! - `current_temp`: CPU temperature in **degrees Celsius (°C)**
//! - `uptime`: System uptime in **seconds (s)**
//!
/// CPU statistics for the host system.
///
/// # Fields
/// - `name`: CPU model name (string)
/// - `cores`: Number of physical CPU cores (integer)
/// - `current_load`: CPU usage as a percentage (**0.0100.0**)
/// - `current_temp`: CPU temperature in **degrees Celsius (°C)**
/// - `uptime`: System uptime in **seconds (s)**
/// - `host_name`: Hostname of the system (string)
#[derive(Debug)]
pub struct CpuInfo {
pub name: Option<String>,
@@ -12,6 +35,10 @@ pub struct CpuInfo {
pub host_name: Option<String>,
}
/// Collects CPU information (model, cores, load, temperature, uptime).
///
/// # Returns
/// * `Result<CpuInfo, Box<dyn Error + Send + Sync>>` - CPU statistics or error if unavailable.
pub async fn get_cpu_info() -> Result<CpuInfo, Box<dyn Error + Send + Sync>> {
let mut sys = System::new_all();
@@ -33,12 +60,23 @@ pub async fn get_cpu_info() -> Result<CpuInfo, Box<dyn Error + Send + Sync>> {
})
}
/// Queries system for current CPU load (percentage).
///
/// # Arguments
/// * `sys` - Mutable reference to sysinfo::System
///
/// # Returns
/// * `Result<f64, Box<dyn Error + Send + Sync>>` - CPU load as percentage.
pub async fn get_cpu_load(sys: &mut System) -> Result<f64, Box<dyn Error + Send + Sync>> {
sys.refresh_cpu_all();
tokio::task::yield_now().await; // Allow other tasks to run
Ok(sys.global_cpu_usage() as f64)
}
/// Attempts to read CPU temperature from system sensors (Linux only).
///
/// # Returns
/// * `Result<f64, Box<dyn Error + Send + Sync>>` - CPU temperature in degrees Celsius (°C).
pub async fn get_cpu_temp() -> Result<f64, Box<dyn Error + Send + Sync>> {
println!("Attempting to get CPU temperature...");