added commentation
This commit is contained in:
@@ -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)
|
||||
|
Reference in New Issue
Block a user