changed Dtos and Docker structs

This commit is contained in:
2025-10-04 22:05:28 +02:00
parent bb55b46c34
commit b35cac0dbe
8 changed files with 73 additions and 38 deletions

View File

@@ -308,7 +308,7 @@ pub async fn listening_to_server(
///
/// # Returns
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if acknowledgment is sent successfully.
async fn send_acknowledgment(
pub async fn send_acknowledgment(
client: &reqwest::Client,
base_url: &str,
message_id: &str,
@@ -339,3 +339,27 @@ async fn send_acknowledgment(
Ok(())
}
pub async fn send_docker_metrics(
base_url: &str,
docker_metrics: &MetricDto,
) -> Result<(), Box<dyn Error + Send + Sync>> {
let client = Client::new();
let url = format!("{}/monitoring/docker-metric", base_url);
println!("Metrics: {:?}", docker_metrics);
match client.post(&url).json(&docker_metrics).send().await {
Ok(res) => println!(
"✅ Sent metrics for server {} | Status: {}",
docker_metrics.server_id,
res.status()
),
Err(err) => eprintln!("❌ Failed to send metrics: {}", err),
}
Ok(())
}
pub async fn broadcast_docker_containers() {
// Placeholder for future implementation
}

View File

@@ -4,7 +4,7 @@
//!
use crate::docker::stats;
use crate::docker::stats::{ContainerCpuInfo, ContainerNetworkInfo};
use crate::models::{DockerContainerDto, DockerContainerRegistrationDto};
use crate::models::{DockerRegistrationDto, DockerMetricDto, DockerContainer};
use bollard::query_parameters::{
CreateImageOptions, ListContainersOptions, RestartContainerOptions,
@@ -20,7 +20,7 @@ use std::error::Error;
///
/// # Returns
/// * `Vec<DockerContainer>` - Vector of Docker container info.
pub async fn get_available_containers(docker: &Docker) -> Vec<DockerContainerDto> {
pub async fn get_available_containers(docker: &Docker) -> Vec<DockerContainer> {
println!("=== DOCKER CONTAINER LIST ===");
let options = Some(ListContainersOptions {
@@ -51,26 +51,7 @@ pub async fn get_available_containers(docker: &Docker) -> Vec<DockerContainerDto
.map(|img| img.to_string())
.unwrap_or_else(|| "unknown".to_string());
/*let status = container
.status
.as_ref()
.map(|s| match s.to_lowercase().as_str() {
s if s.contains("up") || s.contains("running") => "running".to_string(),
s if s.contains("exited") || s.contains("stopped") => {
"stopped".to_string()
}
_ => s.to_string(),
})
.unwrap_or_else(|| "unknown".to_string());
println!(
" - ID: {}, Image: {}, Name: {}",
short_id,
container.image.unwrap(),
name
);*/
Some(DockerContainerDto {
Some(DockerContainer {
id: short_id.to_string(),
image,
name: name,

View File

@@ -11,7 +11,7 @@ pub mod container;
pub mod serverclientcomm;
pub mod stats;
use crate::models::{DockerContainerDto, DockerContainerMetricDto};
use crate::models::{DockerRegistrationDto, DockerMetricDto, DockerContainer};
use bollard::{query_parameters::InspectContainerOptions, Docker};
use std::error::Error;
@@ -49,14 +49,14 @@ impl DockerManager {
/// Finds the Docker container running the agent by image name
pub async fn get_client_container(
&self,
) -> Result<Option<DockerContainerDto>, Box<dyn Error + Send + Sync>> {
) -> Result<Option<DockerContainer>, Box<dyn Error + Send + Sync>> {
let containers = container::get_available_containers(&self.docker).await;
let client_image = "watcher-agent";
Ok(containers
.into_iter()
.find(|c| c.image.contains(client_image))
.map(|container| DockerContainerDto {
.map(|container| DockerContainer {
id: container.id,
image: container.image,
name: container.name,
@@ -89,12 +89,12 @@ impl DockerManager {
/// Gets all available containers as DTOs for registration
pub async fn get_containers_for_registration(
&self,
) -> Result<Vec<DockerContainerDto>, Box<dyn Error + Send + Sync>> {
) -> Result<Vec<DockerContainer>, Box<dyn Error + Send + Sync>> {
let containers = container::get_available_containers(&self.docker).await;
Ok(containers
.into_iter()
.map(|container| DockerContainerDto {
.map(|container| DockerContainer {
id: container.id,
image: container.image,
name: container.name,
@@ -105,7 +105,7 @@ impl DockerManager {
/// Gets container metrics for all containers
pub async fn get_container_metrics(
&self,
) -> Result<Vec<DockerContainerMetricDto>, Box<dyn Error + Send + Sync>> {
) -> Result<Vec<DockerContainer>, Box<dyn Error + Send + Sync>> {
let containers = container::get_available_containers(&self.docker).await;
let mut metrics = Vec::new();
@@ -147,7 +147,7 @@ impl DockerManager {
};
metrics.push(DockerContainerMetricDto {
id: container.id,
server_id: container.id,
status: status,
network: network_stats,
cpu: cpu_stats,
@@ -185,7 +185,7 @@ impl DockerManager {
}
// Keep these as utility functions if needed, but they should use DockerManager internally
impl DockerContainerDto {
impl DockerContainer {
/// Returns the container ID
pub fn id(&self) -> &str {
&self.id

View File

View File

View File

View File

View File

@@ -186,22 +186,52 @@ pub struct Acknowledgment {
/// - `_net_out`: Network transmit rate in **bytes per second (B/s)**
/// - `_cpu_load`: CPU usage as a percentage (**0.0100.0**)
#[derive(Debug, Serialize, Clone)]
pub struct DockerContainerRegistrationDto {
pub struct DockerRegistrationDto {
/// Unique server identifier (integer)
pub server_id: u32,
pub containers: Vec<DockerContainerDto>,
/// 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,
}
#[derive(Debug, Serialize, Clone)]
pub struct DockerContainerDto {
pub id: String,
pub image: String,
pub name: String,
pub struct DockerMetricDto {
pub server_id: String,
/// 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
/// image: docker image name
/// name: container name
/// network: network stats
/// cpu: cpu stats
/// ram: ram stats
pub containers: String,
}
#[derive(Debug, Serialize, Clone)]
pub struct DockerContainerMetricDto {
pub struct DockerContainer {
pub id: String,
pub status: String, // "running";"stopped";others
pub image: String,
pub name: String,
pub network: stats::ContainerNetworkInfo,
pub cpu: stats::ContainerCpuInfo,
//pub ram: stats::ContainerRamInfo,
}
#[derive(Debug, Serialize, Clone)]
pub struct Docker {
pub containers: Vec<DockerContainer>,
}