added commentation
This commit is contained in:
@@ -2,6 +2,24 @@ use std::error::Error;
|
||||
use std::result::Result;
|
||||
use std::time::Instant;
|
||||
|
||||
//! # Network Hardware Module
|
||||
//!
|
||||
//! This module provides network information collection for WatcherAgent, including interface enumeration and bandwidth statistics.
|
||||
//!
|
||||
//! ## Responsibilities
|
||||
//! - **Interface Detection:** Lists all network interfaces.
|
||||
//! - **Bandwidth Monitoring:** Tracks receive/transmit rates using a rolling monitor.
|
||||
//! - **Error Handling:** Graceful fallback if metrics are unavailable.
|
||||
//!
|
||||
//! ## Units
|
||||
//! - `rx_rate`, `tx_rate`: Network bandwidth in **bytes per second (B/s)**
|
||||
//!
|
||||
/// Network statistics for the host system.
|
||||
///
|
||||
/// # Fields
|
||||
/// - `interfaces`: List of network interface names (strings)
|
||||
/// - `rx_rate`: Receive bandwidth in **bytes per second (B/s)**
|
||||
/// - `tx_rate`: Transmit bandwidth in **bytes per second (B/s)**
|
||||
#[derive(Debug)]
|
||||
pub struct NetworkInfo {
|
||||
pub interfaces: Option<Vec<String>>,
|
||||
@@ -9,6 +27,13 @@ pub struct NetworkInfo {
|
||||
pub tx_rate: Option<f64>,
|
||||
}
|
||||
|
||||
|
||||
/// Rolling monitor for network bandwidth statistics.
|
||||
///
|
||||
/// # Fields
|
||||
/// - `prev_rx`: Previous received bytes
|
||||
/// - `prev_tx`: Previous transmitted bytes
|
||||
/// - `last_update`: Timestamp of last update
|
||||
#[derive(Debug)]
|
||||
pub struct NetworkMonitor {
|
||||
prev_rx: u64,
|
||||
@@ -23,6 +48,7 @@ impl Default for NetworkMonitor {
|
||||
}
|
||||
|
||||
impl NetworkMonitor {
|
||||
/// Creates a new `NetworkMonitor` for bandwidth tracking.
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
prev_rx: 0,
|
||||
@@ -31,6 +57,10 @@ impl NetworkMonitor {
|
||||
}
|
||||
}
|
||||
|
||||
/// Updates the network usage statistics and returns current rx/tx rates.
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<(f64, f64), Box<dyn Error>>` - Tuple of (rx_rate, tx_rate) in bytes per second.
|
||||
pub fn update_usage(&mut self) -> Result<(f64, f64), Box<dyn Error>> {
|
||||
let (current_rx, current_tx) = get_network_bytes()?;
|
||||
let elapsed = self.last_update.elapsed().as_secs_f64();
|
||||
@@ -55,6 +85,13 @@ impl NetworkMonitor {
|
||||
}
|
||||
}
|
||||
|
||||
/// Collects network information (interfaces, rx/tx rates) using a monitor.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `monitor` - Mutable reference to a `NetworkMonitor`
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<NetworkInfo, Box<dyn Error>>` - Network statistics or error if unavailable.
|
||||
pub async fn get_network_info(monitor: &mut NetworkMonitor) -> Result<NetworkInfo, Box<dyn Error>> {
|
||||
let (rx_rate, tx_rate) = monitor.update_usage()?;
|
||||
Ok(NetworkInfo {
|
||||
|
Reference in New Issue
Block a user