use serde::Serialize;
use crate::client::GatewayApi;
use crate::client::redundancy::{ActiveNode, RedundancyStatusWire, active_node};
use crate::error::CoreError;
#[derive(Debug, Serialize)]
pub struct ActiveNodeReport {
pub active: &'static str,
pub this_role: String,
pub peer_role: String,
pub peer_profile: String,
pub peer: RedundancyStatusWire,
}
#[derive(Debug, Serialize)]
pub struct RedundancyStatusResult {
pub status: RedundancyStatusWire,
#[serde(skip_serializing_if = "Option::is_none")]
pub active_node: Option<ActiveNodeReport>,
}
pub async fn redundancy_status(api: &dyn GatewayApi) -> Result<RedundancyStatusResult, CoreError> {
Ok(RedundancyStatusResult {
status: api.redundancy_status().await?,
active_node: None,
})
}
pub async fn redundancy_status_pair(
this: &dyn GatewayApi,
peer: &dyn GatewayApi,
peer_profile: &str,
) -> Result<RedundancyStatusResult, CoreError> {
let this_status = this.redundancy_status().await?;
let peer_status = peer.redundancy_status().await?;
let active = match active_node(&this_status, &peer_status) {
ActiveNode::OnlyA => "this",
ActiveNode::OnlyB => "peer",
ActiveNode::Both => "both",
ActiveNode::Neither => "neither",
};
let report = ActiveNodeReport {
active,
this_role: this_status.role.clone(),
peer_role: peer_status.role.clone(),
peer_profile: peer_profile.to_string(),
peer: peer_status,
};
Ok(RedundancyStatusResult {
status: this_status,
active_node: Some(report),
})
}