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

@@ -3,6 +3,24 @@ use std::error::Error;
use anyhow::Result;
use sysinfo::System;
//! # Memory Hardware Module
//!
//! This module provides memory information collection for WatcherAgent, including total, used, and free RAM.
//!
//! ## Responsibilities
//! - **Memory Detection:** Queries system for total, used, and free RAM.
//! - **Usage Calculation:** Computes memory usage percentage.
//! - **Error Handling:** Graceful fallback if metrics are unavailable.
//!
//! ## Units
//! - `total`, `used`, `free`: RAM in **megabytes (MB)**
//!
/// Memory statistics for the host system.
///
/// # Fields
/// - `total`: Total RAM in **megabytes (MB)**
/// - `used`: Used RAM in **megabytes (MB)**
/// - `free`: Free RAM in **megabytes (MB)**
#[derive(Debug)]
pub struct MemoryInfo {
pub total: Option<f64>,
@@ -10,6 +28,10 @@ pub struct MemoryInfo {
pub free: Option<f64>,
}
/// Collects memory information (total, used, free RAM).
///
/// # Returns
/// * `Result<MemoryInfo>` - Memory statistics or error if unavailable.
pub async fn get_memory_info() -> Result<MemoryInfo> {
let mut sys = System::new();
sys.refresh_memory();
@@ -21,6 +43,13 @@ pub async fn get_memory_info() -> Result<MemoryInfo> {
})
}
/// Computes memory usage percentage from sysinfo::System.
///
/// # Arguments
/// * `sys` - Mutable reference to sysinfo::System
///
/// # Returns
/// * `Result<f64, Box<dyn Error + Send + Sync>>` - Memory usage as percentage.
pub fn _get_memory_usage(sys: &mut System) -> Result<f64, Box<dyn Error + Send + Sync>> {
sys.refresh_memory();
Ok((sys.used_memory() as f64 / sys.total_memory() as f64) * 100.0)