From c7bce926e9a7eae23a01c2508b324d8743ed5369 Mon Sep 17 00:00:00 2001 From: donpat1to Date: Thu, 9 Oct 2025 10:39:52 +0200 Subject: [PATCH] added docker registration dto --- WatcherAgent/src/docker/mod.rs | 60 ++++++++++++++++++++++++++++++++-- WatcherAgent/src/metrics.rs | 8 ++--- WatcherAgent/src/models.rs | 42 ++++++++++++++++++++---- 3 files changed, 94 insertions(+), 16 deletions(-) diff --git a/WatcherAgent/src/docker/mod.rs b/WatcherAgent/src/docker/mod.rs index b11aaf7..71bf942 100644 --- a/WatcherAgent/src/docker/mod.rs +++ b/WatcherAgent/src/docker/mod.rs @@ -11,8 +11,11 @@ pub mod container; pub mod serverclientcomm; pub mod stats; -use crate::models::{DockerContainer, DockerContainerInfo, DockerMetricDto, DockerRegistrationDto}; -use bollard::{query_parameters::InspectContainerOptions, Docker}; +use crate::models::{ + DockerCollectMetricDto, DockerContainer, DockerContainerCpuDto, DockerContainerInfo, + DockerContainerNetworkDto, DockerContainerRamDto, DockerMetricDto, DockerRegistrationDto, +}; +use bollard::Docker; use std::error::Error; /// Main Docker manager that holds the Docker client and provides all operations @@ -128,7 +131,7 @@ impl DockerManager { let containers = self.get_containers().await?; let (cpu_stats, net_stats, mem_stats) = stats::get_container_stats(&self.docker).await?; - let container_infos: Vec<_> = containers + let container_infos_total: Vec<_> = containers .into_iter() .map(|container| { let cpu = cpu_stats @@ -154,6 +157,43 @@ impl DockerManager { }) .collect(); + let container_infos: Vec = container_infos_total + .into_iter() + .map(|info| DockerCollectMetricDto { + id: Some(info.container.unwrap().id).unwrap_or("".to_string()), + cpu: info + .cpu + .unwrap() + .cpu_usage_percent + .map(|load| DockerContainerCpuDto { + cpu_load: Some(load), + }) + .unwrap_or(DockerContainerCpuDto { cpu_load: None }), + ram: info + .ram + .unwrap() + .memory_usage_percent + .map(|load| DockerContainerRamDto { + cpu_load: Some(load), + }) + .unwrap_or(DockerContainerRamDto { cpu_load: None }), + network: DockerContainerNetworkDto { + net_in: info + .network + .as_ref() + .unwrap() + .rx_bytes + .map(|bytes| bytes as f64) + .or(Some(0.0)), + net_out: info + .network + .unwrap() + .tx_bytes + .map(|bytes| bytes as f64) + .or(Some(0.0)), + }, + }) + .collect(); let dto = DockerMetricDto { server_id: 0, containers: serde_json::to_string(&container_infos)?, @@ -161,6 +201,20 @@ impl DockerManager { Ok(dto) } + + pub async fn create_registration_dto( + &self, + ) -> Result> { + let containers = self.get_containers().await?; + let container_count = containers.len(); + let dto = DockerRegistrationDto { + server_id: 0, + container_count, + containers: serde_json::to_string(&containers)?, + }; + + Ok(dto) + } } // Keep these as utility functions if needed, but they should use DockerManager internally diff --git a/WatcherAgent/src/metrics.rs b/WatcherAgent/src/metrics.rs index 9908fb9..7101225 100644 --- a/WatcherAgent/src/metrics.rs +++ b/WatcherAgent/src/metrics.rs @@ -10,10 +10,9 @@ /// ## 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::sync::Arc; use std::time::Duration; -use crate::{api, docker}; +use crate::api; use crate::docker::DockerManager; //use crate::docker::DockerInfo; use crate::hardware::network::NetworkMonitor; @@ -129,10 +128,7 @@ impl Collector { /// NOTE: This is a compilation-safe stub. Implement the Docker collection using your /// DockerManager API and container helpers when available. pub async fn docker_collect(&self) -> Result> { - let metrics = self - .docker_manager - .collect_metrics() - .await?; + let metrics = self.docker_manager.collect_metrics().await?; Ok(DockerMetricDto { server_id: self.server_id, containers: metrics.containers, diff --git a/WatcherAgent/src/models.rs b/WatcherAgent/src/models.rs index 40c3b81..efb3768 100644 --- a/WatcherAgent/src/models.rs +++ b/WatcherAgent/src/models.rs @@ -189,26 +189,28 @@ pub struct Acknowledgment { pub struct DockerRegistrationDto { /// Unique server identifier (integer) pub server_id: u16, + /// Number of currently running containers + pub container_count: usize, /// json stringified array of DockerContainer - /// + /// /// ## Json Example /// json format: [{"id":"234dsf234","image":"nginx:latest","name":"webserver"},...] - /// + /// /// ## Fields /// id: unique container ID (first 12 hex digits) /// image: docker image name /// name: container name - pub containers: String // Vec, + pub containers: String, // Vec, } #[derive(Debug, Serialize, Clone)] pub struct DockerMetricDto { pub server_id: u16, /// json stringified array of DockerContainer - /// + /// /// ## Json Example /// json format: [{"id":"234dsf234","status":"running","image":"nginx:latest","name":"webserver","network":{"net_in":1024,"net_out":2048},"cpu":{"cpu_load":12.5},"ram":{"ram_load":10.0}},...] - /// + /// /// ## Fields /// id: unique container ID (first 12 hex digits) /// status: "running";"stopped";others @@ -217,7 +219,33 @@ pub struct DockerMetricDto { /// network: network stats /// cpu: cpu stats /// ram: ram stats - pub containers: String // Vec, + pub containers: String, // Vec, +} + +#[derive(Debug, Serialize, Clone)] + +pub struct DockerCollectMetricDto { + pub id: String, + pub cpu: DockerContainerCpuDto, + pub ram: DockerContainerRamDto, + pub network: DockerContainerNetworkDto, +} + +#[derive(Debug, Serialize, Clone)] +pub struct DockerContainerCpuDto { + pub cpu_load: Option, +} + +#[derive(Debug, Serialize, Clone)] +pub struct DockerContainerRamDto { + pub cpu_load: Option, +} + +#[derive(Debug, Serialize, Clone)] + +pub struct DockerContainerNetworkDto { + pub net_in: Option, + pub net_out: Option, } #[derive(Debug, Serialize, Clone)] @@ -234,4 +262,4 @@ pub struct DockerContainer { pub id: String, pub image: Option, pub name: Option, -} \ No newline at end of file +}