added docker registration dto

This commit is contained in:
2025-10-09 10:39:52 +02:00
parent 711083daa0
commit c7bce926e9
3 changed files with 94 additions and 16 deletions

View File

@@ -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<DockerCollectMetricDto> = 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<DockerRegistrationDto, Box<dyn Error + Send + Sync>> {
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

View File

@@ -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<DockerMetricDto, Box<dyn Error + Send + Sync>> {
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,

View File

@@ -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<DockerContainer>,
pub containers: String, // Vec<DockerContainer>,
}
#[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<DockerContainerInfo>,
pub containers: String, // Vec<DockerContainerInfo>,
}
#[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<f64>,
}
#[derive(Debug, Serialize, Clone)]
pub struct DockerContainerRamDto {
pub cpu_load: Option<f64>,
}
#[derive(Debug, Serialize, Clone)]
pub struct DockerContainerNetworkDto {
pub net_in: Option<f64>,
pub net_out: Option<f64>,
}
#[derive(Debug, Serialize, Clone)]
@@ -234,4 +262,4 @@ pub struct DockerContainer {
pub id: String,
pub image: Option<String>,
pub name: Option<String>,
}
}