use colored::Colorize;
use core::fmt;
#[cfg(target_os = "linux")]
use dusa_collection_utils::platform::functions::{create_hash, truncate};
use dusa_collection_utils::{
core::logger::LogLevel, core::types::stringy::Stringy, core::version::SoftwareVersion, log,
};
use lz4::block::compress;
use serde::{Deserialize, Serialize};
use crate::aggregator::Metrics;
#[allow(unused_imports)] use crate::{
aggregator::{AppStatus, Status},
config::AppConfig,
enviornment::definitions::{Enviornment, Enviornment_V1, Enviornment_V2},
git_actions::GitCredentials,
identity::Identifier,
};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum PortalMessage {
Discover,
IdRequest,
IdResponse(Option<Identifier>),
RegisterRequest(ManagerData),
RegisterResponse(bool),
Error(String),
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ProjectInfo {
pub project_id: Stringy,
pub identity: Identifier,
pub project_data: AppStatus,
}
#[cfg(target_os = "linux")]
impl ProjectInfo {
#[allow(deprecated)]
pub fn get_id(&self) -> Stringy {
let data = format!("{}-{}", self.identity.id, self.project_data.get_id());
let bytes = data.into_bytes();
let compressed = match compress(&bytes, None, false) {
Ok(data) => data,
Err(err) => {
log!(LogLevel::Warn, "Error compressing id: {}", err.to_string());
let fallback = b"none";
fallback.to_vec()
}
};
let encoded = base64::encode(&compressed);
let result = truncate(&encoded, 100).to_owned();
return result;
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ApiResponse<T> {
pub status: String,
pub data: Option<T>,
pub errors: Vec<ErrorInfo>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum ErrorCode {
NodeNotFound,
ProjectNotFound,
InvalidCredentials,
NotAuthorized,
InternalError,
TimedOut,
Whoops,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ErrorInfo {
pub code: ErrorCode,
pub message: String,
#[serde(default)]
pub details: serde_json::Value,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct NodeInfo {
pub identity: Identifier,
pub hostname: Stringy,
pub status: Status,
pub ip_address: std::net::IpAddr,
pub projects: Vec<Stringy>,
pub created_at: Stringy,
pub last_updated: Stringy,
}
#[cfg(target_os = "linux")]
impl NodeInfo {
pub fn get_stringy(&self) -> Stringy {
let data = format!("{}_-_{}", self.ip_address, self.identity.id);
let hash = create_hash(data);
truncate(&*hash, 20).to_owned()
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct NodeDetails {
pub identity: Identifier,
pub status: Status,
pub projects: Vec<Stringy>,
pub created_at: Stringy,
pub last_updated: Stringy,
pub manager_data: ManagerData,
}
#[cfg(target_os = "linux")]
impl NodeDetails {
pub fn get_stringy(&self) -> Stringy {
let data = format!("{}_-_{}", self.manager_data.address, self.identity.id);
let hash = create_hash(data);
truncate(&*hash, 20).to_owned()
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ManagerData {
pub identity: Identifier,
pub version: SoftwareVersion,
pub git_config: GitCredentials,
pub hostname: Stringy,
pub address: std::net::IpAddr,
pub system_apps: u32,
pub client_apps: u32,
pub warning: u32,
pub uptime: u64,
}
#[cfg(target_os = "linux")]
impl ManagerData {
pub fn get_stringy(&self) -> Stringy {
let data = format!("{}_-_{}", self.address, self.identity.id);
let hash = create_hash(data);
truncate(&*hash, 20).to_owned()
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct NodeReloadResult {
pub id: String,
pub reloaded: bool,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ProjectSummary {
pub name: Stringy,
pub status: Status,
pub version: SoftwareVersion,
pub nodes: Vec<u64>,
pub uptime: Option<u64>,
pub metrics: Option<Metrics>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ProjectDetails {
pub id: Stringy,
pub status: Status,
pub version: SoftwareVersion,
pub artisan_config: AppConfig,
pub specific_config: Option<serde_json::Value>,
pub enviornment: Option<Enviornment>,
#[serde(default)]
pub health: Option<ProjectHealth>,
#[serde(default)]
pub logs: Option<ProjectLogs>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ProjectHealth {
pub uptime: u64,
pub last_check: u64,
pub cpu_usage: Stringy,
pub ram_usage: Stringy,
pub tx_bytes: u64,
pub rx_bytes: u64,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ProjectLogs {
pub recent: Vec<LogEntry>,
}
#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, PartialOrd, Eq, Ord)]
pub struct NetworkStats {
pub rx_bytes: u64,
pub tx_bytes: u64,
pub note: Option<String>, }
#[derive(Serialize, Deserialize, Debug)]
pub struct CommandRequest {
pub command: String,
#[serde(default)]
pub params: serde_json::Value,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct CommandResponse {
#[serde(rename = "projectId")]
pub project_id: String,
#[serde(rename = "commandId")]
pub command_id: String,
pub command: String,
#[serde(default)]
pub params: serde_json::Value,
pub queued_at: u64,
pub status: Status,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct CommandStatusResponse {
#[serde(rename = "projectId")]
pub project_id: String,
#[serde(rename = "commandId")]
pub command_id: String,
pub command: String,
#[serde(default)]
pub started_at: Option<String>,
#[serde(default)]
pub finished_at: Option<String>,
pub status: String,
#[serde(default)]
pub output: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct LogEntry {
pub timestamp: String,
pub message: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct NodeLogs {
pub node_id: String,
pub logs: Vec<LogEntry>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ProjectLogResponse {
pub project_id: String,
pub logs: Vec<LogEntry>,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct InstanceLogResponse {
pub project_id: String,
pub instance_id: String,
pub lines: Vec<LogEntry>,
}
impl fmt::Display for ManagerData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}: {}\n{}: {}\n{}: {}\n{}: {}\n{}: {}\n",
"Manager Version".bold(),
self.version,
"Git Configuration".bold().green(),
self.git_config,
"System apps",
self.system_apps.to_string().bold().purple(),
"Client apps",
self.client_apps.to_string().bold().green(),
"Warnings raised",
self.warning.to_string().bold().yellow()
)
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct BillingParams {
pub instances: u64,
}
impl BillingParams {
pub fn new(data: u64) -> Self {
Self { instances: data }
}
}