all functions established; needed to remove orphan functions

This commit is contained in:
2025-08-08 23:28:05 +02:00
parent abfa0b6fc0
commit 93a40fe584
6 changed files with 22 additions and 14 deletions

View File

@@ -1,7 +1,7 @@
use std::time::Duration;
use crate::hardware::HardwareInfo;
use crate::models::{HardwareDto, HeartbeatDto, IdResponse, MetricDto, RegistrationDto};
use crate::models::{HeartbeatDto, IdResponse, MetricDto, RegistrationDto};
use anyhow::Result;
use reqwest::{Client, StatusCode};
use std::error::Error;

View File

@@ -19,7 +19,7 @@ pub async fn get_memory_info() -> Result<MemoryInfo> {
})
}
pub fn get_memory_usage(sys: &mut System) -> f64 {
pub fn _get_memory_usage(sys: &mut System) -> f64 {
sys.refresh_memory();
(sys.used_memory() as f64 / sys.total_memory() as f64) * 100.0
}

View File

@@ -5,7 +5,7 @@ mod cpu;
mod disk;
mod gpu;
mod memory;
mod network;
pub(crate) mod network;
pub use cpu::get_cpu_info;
pub use disk::get_disk_info;
@@ -20,16 +20,19 @@ pub struct HardwareInfo {
pub memory: memory::MemoryInfo,
pub disk: disk::DiskInfo,
pub network: network::NetworkInfo,
network_monitor: network::NetworkMonitor,
}
impl HardwareInfo {
pub async fn collect() -> anyhow::Result<Self, Box<dyn Error>> {
let mut network_monitor = network::NetworkMonitor::new();
Ok(Self {
cpu: get_cpu_info().await?,
gpu: get_gpu_info().await?,
memory: get_memory_info().await?,
disk: get_disk_info().await?,
network: get_network_info().await?,
network: get_network_info(&mut network_monitor).await?,
network_monitor,
})
}
}

View File

@@ -5,10 +5,11 @@ use std::time::Instant;
#[derive(Debug)]
pub struct NetworkInfo {
pub interfaces: Option<Vec<String>>,
pub rx_bytes: Option<f64>,
pub tx_bytes: Option<f64>,
pub rx_rate: Option<f64>,
pub tx_rate: Option<f64>,
}
#[derive(Debug)]
pub struct NetworkMonitor {
prev_rx: u64,
prev_tx: u64,
@@ -24,7 +25,7 @@ impl NetworkMonitor {
}
}
pub fn get_usage(&mut self) -> Result<(f64, f64), Box<dyn Error>> {
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();
self.last_update = Instant::now();
@@ -48,12 +49,12 @@ impl NetworkMonitor {
}
}
pub async fn get_network_info() -> Result<NetworkInfo, Box<dyn Error>> {
let (rx, tx) = get_network_bytes()?;
pub async fn get_network_info(monitor: &mut NetworkMonitor) -> Result<NetworkInfo, Box<dyn Error>> {
let (rx_rate, tx_rate) = monitor.update_usage()?;
Ok(NetworkInfo {
interfaces: Some(get_network_interfaces()),
rx_bytes: Some(rx as f64),
tx_bytes: Some(tx as f64),
rx_rate: Some(rx_rate as f64),
tx_rate: Some(tx_rate as f64),
})
}

View File

@@ -2,10 +2,12 @@ use std::error::Error;
use std::time::Duration;
use crate::api;
use crate::hardware::network::NetworkMonitor;
use crate::hardware::HardwareInfo;
use crate::models::MetricDto;
pub struct Collector {
network_monitor: NetworkMonitor,
server_id: i32,
ip_address: String,
}
@@ -13,6 +15,7 @@ pub struct Collector {
impl Collector {
pub fn new(server_id: i32, ip_address: String) -> Self {
Self {
network_monitor: NetworkMonitor::new(),
server_id,
ip_address,
}
@@ -28,6 +31,8 @@ impl Collector {
pub async fn collect(&mut self) -> Result<MetricDto, Box<dyn Error>> {
let hardware = HardwareInfo::collect().await?;
// Collect network usage
let (_, _) = self.network_monitor.update_usage().unwrap_or((0.0, 0.0));
Ok(MetricDto {
server_id: self.server_id,
@@ -43,8 +48,8 @@ impl Collector {
disk_size: hardware.disk.total.unwrap_or_default(),
disk_usage: hardware.disk.used.unwrap_or_default(),
disk_temp: 0.0, // not supported
net_rx: hardware.network.rx_bytes.unwrap_or_default(),
net_tx: hardware.network.tx_bytes.unwrap_or_default(),
net_rx: hardware.network.rx_rate.unwrap_or_default(),
net_tx: hardware.network.tx_rate.unwrap_or_default(),
})
}
}

View File

@@ -1,5 +1,4 @@
use serde::{Deserialize, Serialize};
use tokio::time::Instant;
// Data structures matching the C# DTOs
#[derive(Serialize, Debug)]