use std::str::FromStr;
use std::time::SystemTime;
use gethostname::gethostname;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct Heartbeat {
pub heartbeat_info: HeartbeatInfo,
pub status: AgentStatus,
pub host_info: Option<HostInfo>,
pub build_info: Option<BuildInfo>,
}
impl Heartbeat {
pub fn new(hbtype: HeartbeatType, agent_status: Option<AgentStatus>) -> Heartbeat {
let mut status_or_undefined = AgentStatus::Undefined;
if let Some(value) = agent_status {
status_or_undefined = value;
}
match hbtype {
HeartbeatType::Full => Heartbeat {
heartbeat_info: HeartbeatInfo::new(hbtype),
status: status_or_undefined,
host_info: Some(HostInfo::new()),
build_info: Some(BuildInfo::new()),
},
HeartbeatType::Compact => {
todo!()
}
}
}
}
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct HeartbeatInfo {
pub hbtype: HeartbeatType,
pub heartbeat_uuid: Uuid,
pub timestamp: SystemTime,
}
impl HeartbeatInfo {
fn new(hbtype: HeartbeatType) -> HeartbeatInfo {
HeartbeatInfo {
hbtype,
heartbeat_uuid: Uuid::new_v4(),
timestamp: SystemTime::now(),
}
}
}
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub enum HeartbeatType {
Full,
Compact,
}
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct HostInfo {
pub hostname: String,
pub host_uuid: Uuid,
pub uptime: f64,
}
impl HostInfo {
fn new() -> HostInfo {
HostInfo {
hostname: gethostname().into_string().expect("Failed to get hostname"),
host_uuid: app_machine_id::get(
Uuid ::new_v3(&Uuid::NAMESPACE_DNS, b"netzwork.tbd")
)
.expect("Failed to generate app-machine-UUID"),
uptime: uptime_lib::get()
.expect("Failed to get uptime")
.as_secs_f64(),
}
}
}
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub enum AgentStatus {
Undefined,
Initializing,
AwaitingJoin,
Running,
Orphaned,
Exiting,
}
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct BuildInfo {
build_timestamp: String,
build_date: String,
git_branch: String,
git_timestamp: String,
git_date: String,
git_hash: String,
git_describe: String,
rustc_host_triple: String,
rustc_version: String,
cargo_target_triple: String,
}
impl BuildInfo {
fn new() -> BuildInfo {
BuildInfo {
build_timestamp: String::from(env!("VERGEN_BUILD_TIMESTAMP")),
build_date: String::from(env!("VERGEN_BUILD_DATE")),
git_branch: String::from(env!("VERGEN_GIT_BRANCH")),
git_timestamp: String::from(env!("VERGEN_GIT_COMMIT_TIMESTAMP")),
git_date: String::from(env!("VERGEN_GIT_COMMIT_DATE")),
git_hash: String::from(env!("VERGEN_GIT_SHA")),
git_describe: String::from(env!("VERGEN_GIT_DESCRIBE")),
rustc_host_triple: String::from(env!("VERGEN_RUSTC_HOST_TRIPLE")),
rustc_version: String::from(env!("VERGEN_RUSTC_SEMVER")),
cargo_target_triple: String::from(env!("VERGEN_CARGO_TARGET_TRIPLE")),
}
}
}