All checks were successful
Rust Cross-Platform Build / Detect Rust Project (push) Successful in 5s
Rust Cross-Platform Build / Run Tests (push) Successful in 1m14s
Rust Cross-Platform Build / Build (x86_64-unknown-linux-gnu) (push) Successful in 3m18s
Rust Cross-Platform Build / Build (x86_64-pc-windows-gnu) (push) Successful in 4m4s
Rust Cross-Platform Build / Set Tag Name (push) Successful in 5s
Rust Cross-Platform Build / Build and Push Docker Image (push) Successful in 2m19s
Rust Cross-Platform Build / Workflow Summary (push) Successful in 2s
Rust Cross-Platform Build / Create Tag (push) Successful in 5s
266 lines
8.6 KiB
Rust
266 lines
8.6 KiB
Rust
/// # Models Module
|
||
///
|
||
/// This module defines all data structures (DTOs) used for communication between WatcherAgent and the backend server, as well as hardware metrics and Docker container info.
|
||
///
|
||
/// ## Responsibilities
|
||
/// - **DTOs:** Define payloads for registration, metrics, heartbeat, and server commands.
|
||
/// - **Units:** All struct fields are documented with their units for clarity and API compatibility.
|
||
/// - **Docker Info:** Structures for representing Docker container state and statistics.
|
||
///
|
||
/// ## Usage
|
||
/// These types are serialized/deserialized for HTTP communication and used throughout the agent for data exchange.
|
||
use crate::docker::stats;
|
||
|
||
use serde::{Deserialize, Serialize};
|
||
|
||
/// Registration data sent to the backend server.
|
||
///
|
||
/// ## Units
|
||
/// - `id`: Unique server identifier (integer)
|
||
/// - `ip_address`: IPv4 or IPv6 address (string)
|
||
/// - `cpu_type`: CPU model name (string)
|
||
/// - `cpu_cores`: Number of physical CPU cores (integer)
|
||
/// - `gpu_type`: GPU model name (string)
|
||
/// - `ram_size`: Total RAM size in **megabytes (MB)**
|
||
#[derive(Serialize, Debug)]
|
||
pub struct RegistrationDto {
|
||
#[serde(rename = "id")]
|
||
pub server_id: u16,
|
||
#[serde(rename = "ipAddress")]
|
||
pub ip_address: String,
|
||
#[serde(rename = "cpuType")]
|
||
pub cpu_type: String,
|
||
#[serde(rename = "cpuCores")]
|
||
pub cpu_cores: i32,
|
||
#[serde(rename = "gpuType")]
|
||
pub gpu_type: String,
|
||
#[serde(rename = "ramSize")]
|
||
pub ram_size: f64,
|
||
}
|
||
|
||
/// Hardware and network metrics data sent to the backend server.
|
||
///
|
||
/// ## Units
|
||
/// - `server_id`: Unique server identifier (integer)
|
||
/// - `ip_address`: IPv4 or IPv6 address (string)
|
||
/// - `cpu_load`: CPU usage as a percentage (**0.0–100.0**)
|
||
/// - `cpu_temp`: CPU temperature in **degrees Celsius (°C)**
|
||
/// - `gpu_load`: GPU usage as a percentage (**0.0–100.0**)
|
||
/// - `gpu_temp`: GPU temperature in **degrees Celsius (°C)**
|
||
/// - `gpu_vram_size`: Total GPU VRAM in **bytes**
|
||
/// - `gpu_vram_load`: GPU Usage of VRAM as a percentage (**0.0–100.0**)
|
||
/// - `ram_load`: RAM usage as a percentage (**0.0–100.0**)
|
||
/// - `ram_size`: Total RAM in **bytes**
|
||
/// - `disk_size`: Total disk size in **bytes**
|
||
/// - `disk_usage`: Used disk space in **bytes**
|
||
/// - `disk_temp`: Disk temperature in **degrees Celsius (°C)** (if available)
|
||
/// - `net_rx`: Network receive rate in **bytes per second (B/s)**
|
||
/// - `net_tx`: Network transmit rate in **bytes per second (B/s)**
|
||
#[derive(Serialize, Debug)]
|
||
pub struct MetricDto {
|
||
#[serde(rename = "serverId")]
|
||
pub server_id: u16,
|
||
#[serde(rename = "ipAddress")]
|
||
pub ip_address: String,
|
||
#[serde(rename = "cpu_Load")]
|
||
pub cpu_load: f64,
|
||
#[serde(rename = "cpu_Temp")]
|
||
pub cpu_temp: f64,
|
||
#[serde(rename = "gpu_Load")]
|
||
pub gpu_load: f64,
|
||
#[serde(rename = "gpu_Temp")]
|
||
pub gpu_temp: f64,
|
||
#[serde(rename = "gpu_Vram_Size")]
|
||
pub gpu_vram_size: f64,
|
||
#[serde(rename = "gpu_Vram_Load")]
|
||
pub gpu_vram_load: f64,
|
||
#[serde(rename = "ram_Load")]
|
||
pub ram_load: f64,
|
||
#[serde(rename = "ram_Size")]
|
||
pub ram_size: f64,
|
||
#[serde(rename = "disk_Size")]
|
||
pub disk_size: f64,
|
||
#[serde(rename = "disk_Usage")]
|
||
pub disk_usage: f64,
|
||
#[serde(rename = "disk_Temp")]
|
||
pub disk_temp: f64,
|
||
#[serde(rename = "net_In")]
|
||
pub net_rx: f64,
|
||
#[serde(rename = "net_Out")]
|
||
pub net_tx: f64,
|
||
}
|
||
|
||
/// Detailed disk information for each detected disk.
|
||
///
|
||
/// ## Units
|
||
/// - `disk_total_space`: Total disk space in **bytes**
|
||
/// - `disk_available_space`: Available disk space in **bytes**
|
||
/// - `disk_used_space`: Used disk space in **bytes**
|
||
/// - `component_disk_temperature`: Disk temperature in **degrees Celsius (°C)**
|
||
#[derive(Serialize, Debug)]
|
||
pub struct DiskInfoDetailed {
|
||
pub disk_name: String,
|
||
pub disk_kind: String,
|
||
pub disk_total_space: f64,
|
||
pub disk_available_space: f64,
|
||
pub disk_used_space: f64,
|
||
pub disk_mount_point: String,
|
||
pub component_disk_label: String,
|
||
pub component_disk_temperature: f32,
|
||
}
|
||
|
||
/// Response containing server ID and IP address.
|
||
///
|
||
/// ## Units
|
||
/// - `id`: Unique server identifier (integer)
|
||
/// - `ip_address`: IPv4 or IPv6 address (string)
|
||
#[derive(Deserialize)]
|
||
pub struct IdResponse {
|
||
pub id: u16,
|
||
#[serde(rename = "ipAddress")]
|
||
pub ip_address: String,
|
||
}
|
||
|
||
/// Heartbeat message data sent to the backend server.
|
||
///
|
||
/// ## Units
|
||
/// - `ip_address`: IPv4 or IPv6 address (string)
|
||
#[derive(Serialize)]
|
||
pub struct HeartbeatDto {
|
||
#[serde(rename = "IpAddress")]
|
||
pub ip_address: String,
|
||
}
|
||
|
||
/// Hardware summary data for diagnostics and registration.
|
||
///
|
||
/// ## Units
|
||
/// - `cpu_type`: CPU model name (string)
|
||
/// - `cpu_cores`: Number of physical CPU cores (integer)
|
||
/// - `gpu_type`: GPU model name (string)
|
||
/// - `ram_size`: Total RAM size in **megabytes (MB)**
|
||
/// - `ip_address`: IPv4 or IPv6 address (string)
|
||
#[derive(Serialize, Debug)]
|
||
pub struct HardwareDto {
|
||
pub cpu_type: String,
|
||
pub cpu_cores: i32,
|
||
pub gpu_type: String,
|
||
pub ram_size: f64,
|
||
pub ip_address: String,
|
||
}
|
||
|
||
/// Command message received from the backend server.
|
||
///
|
||
/// ## Fields
|
||
/// - `message_type`: Type of command (e.g., "update_image", "restart_container", "stop_agent")
|
||
/// - `data`: Command payload (arbitrary JSON)
|
||
/// - `message_id`: Unique identifier for acknowledgment
|
||
#[derive(Debug, Deserialize, Clone)]
|
||
pub struct ServerMessage {
|
||
// Define your message structure here
|
||
pub message_type: String,
|
||
pub data: serde_json::Value,
|
||
pub message_id: String, // Add an ID for acknowledgment
|
||
}
|
||
|
||
/// Acknowledgment payload sent to the backend server for command messages.
|
||
///
|
||
/// ## Fields
|
||
/// - `message_id`: Unique identifier of the acknowledged message
|
||
/// - `status`: Status string ("success", "error", etc.)
|
||
/// - `details`: Additional details or error messages
|
||
#[derive(Debug, Serialize, Clone)]
|
||
pub struct Acknowledgment {
|
||
pub message_id: String,
|
||
pub status: String, // "success" or "error"
|
||
pub details: String,
|
||
}
|
||
|
||
/// Docker container information for agent and managed containers.
|
||
///
|
||
/// ## Fields
|
||
/// - `ID`: Container ID (first 12 hex digits, integer)
|
||
/// - `image`: Docker image name (string)
|
||
/// - `Name`: Container name (string)
|
||
/// - `Status`: Container status ("running", "stopped", etc.)
|
||
/// - `_net_in`: Network receive rate in **bytes per second (B/s)**
|
||
/// - `_net_out`: Network transmit rate in **bytes per second (B/s)**
|
||
/// - `_cpu_load`: CPU usage as a percentage (**0.0–100.0**)
|
||
#[derive(Debug, Serialize, Clone)]
|
||
pub struct DockerRegistrationDto {
|
||
/// Unique server identifier (integer)
|
||
pub server_id: u16,
|
||
/// Number of currently running containers
|
||
// pub container_count: usize, --- IGNORE ---
|
||
/// 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>,
|
||
}
|
||
|
||
#[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
|
||
/// image: docker image name
|
||
/// name: container name
|
||
/// network: network stats
|
||
/// cpu: cpu stats
|
||
/// ram: ram stats
|
||
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)]
|
||
pub struct DockerContainerInfo {
|
||
pub container: Option<DockerContainer>,
|
||
pub status: Option<String>, // "running";"stopped";others
|
||
pub network: Option<stats::ContainerNetworkInfo>,
|
||
pub cpu: Option<stats::ContainerCpuInfo>,
|
||
pub ram: Option<stats::ContainerMemoryInfo>,
|
||
}
|
||
|
||
#[derive(Debug, Serialize, Clone)]
|
||
pub struct DockerContainer {
|
||
pub id: String,
|
||
pub image: Option<String>,
|
||
pub name: Option<String>,
|
||
}
|