added commentation
This commit is contained in:
@@ -1,3 +1,16 @@
|
||||
|
||||
|
||||
//! # Metrics Module
|
||||
//!
|
||||
//! This module orchestrates the collection and reporting of hardware and network metrics for WatcherAgent.
|
||||
//!
|
||||
//! ## Responsibilities
|
||||
//! - **Metric Collection:** Gathers real-time statistics from all hardware subsystems (CPU, GPU, RAM, disk, network).
|
||||
//! - **Reporting:** Periodically sends metrics to the backend server using the API module.
|
||||
//! - **Error Handling:** Robust to hardware failures and network errors, with retry logic and logging.
|
||||
//!
|
||||
//! ## Usage
|
||||
//! The [`Collector`] struct is instantiated in the main loop and runs as a background task, continuously collecting and reporting metrics.
|
||||
use std::error::Error;
|
||||
use std::time::Duration;
|
||||
|
||||
@@ -6,13 +19,31 @@ use crate::hardware::network::NetworkMonitor;
|
||||
use crate::hardware::HardwareInfo;
|
||||
use crate::models::MetricDto;
|
||||
|
||||
|
||||
/// Main orchestrator for hardware and network metric collection and reporting.
|
||||
///
|
||||
/// The `Collector` struct manages the state required to collect metrics and send them to the backend server. It maintains a network monitor for bandwidth tracking, the agent's server ID, and its IP address.
|
||||
///
|
||||
/// # Fields
|
||||
/// - `network_monitor`: Tracks network usage rates (rx/tx).
|
||||
/// - `server_id`: Unique server ID assigned by the backend.
|
||||
/// - `ip_address`: IP address of the agent.
|
||||
pub struct Collector {
|
||||
network_monitor: NetworkMonitor,
|
||||
server_id: i32,
|
||||
ip_address: String,
|
||||
}
|
||||
|
||||
|
||||
impl Collector {
|
||||
/// Creates a new `Collector` instance for metric collection and reporting.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `server_id` - The server ID assigned by the backend.
|
||||
/// * `ip_address` - The IP address of the agent.
|
||||
///
|
||||
/// # Returns
|
||||
/// A new `Collector` ready to collect and report metrics.
|
||||
pub fn new(server_id: i32, ip_address: String) -> Self {
|
||||
Self {
|
||||
network_monitor: NetworkMonitor::new(),
|
||||
@@ -21,6 +52,15 @@ impl Collector {
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs the main metrics collection loop, periodically sending metrics to the backend server.
|
||||
///
|
||||
/// This function continuously collects hardware and network metrics, sends them to the backend, and handles errors gracefully. It uses a configurable interval and retries on failures.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `base_url` - The base URL of the backend server.
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if metrics are sent successfully.
|
||||
pub async fn run(&mut self, base_url: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
loop {
|
||||
println!(
|
||||
@@ -40,6 +80,12 @@ impl Collector {
|
||||
}
|
||||
}
|
||||
|
||||
/// Collects hardware and network metrics from all subsystems.
|
||||
///
|
||||
/// This function queries the hardware module for CPU, GPU, RAM, disk, and network statistics, and packages them into a [`MetricDto`] for reporting.
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Result<MetricDto, Box<dyn Error + Send + Sync>>` - The collected metrics or an error if hardware info is unavailable.
|
||||
pub async fn collect(&mut self) -> Result<MetricDto, Box<dyn Error + Send + Sync>> {
|
||||
let hardware = match HardwareInfo::collect().await {
|
||||
Ok(hw) => hw,
|
||||
|
Reference in New Issue
Block a user