added docker registration dto
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user