use crate::{client::Client, proto, Result};
use tracing::debug;
#[derive(Debug)]
pub struct TopologyBuilder(Client);
impl TopologyBuilder {
pub fn new(client: Client) -> Self {
TopologyBuilder(client)
}
#[tracing::instrument(skip(self), name = "topology", err)]
pub async fn send(mut self) -> Result<TopologyResponse> {
let req = proto::TopologyRequest {};
debug!(?req, "sending request");
let res = self
.0
.gateway_client
.topology(tonic::Request::new(req))
.await?;
Ok(TopologyResponse(res.into_inner()))
}
}
#[derive(Debug)]
pub struct TopologyResponse(proto::TopologyResponse);
impl TopologyResponse {
pub fn brokers(&self) -> Vec<BrokerInfo> {
self.0
.brokers
.iter()
.map(|proto| BrokerInfo(proto.clone()))
.collect()
}
pub fn cluster_size(&self) -> u32 {
self.0.cluster_size as u32
}
pub fn partitions_count(&self) -> u32 {
self.0.partitions_count as u32
}
pub fn replication_factor(&self) -> u32 {
self.0.replication_factor as u32
}
pub fn gateway_version(&self) -> &str {
&self.0.gateway_version
}
}
#[derive(Debug)]
pub struct BrokerInfo(proto::BrokerInfo);
impl BrokerInfo {
pub fn node_id(&self) -> i32 {
self.0.node_id
}
pub fn host(&self) -> &str {
&self.0.host
}
pub fn port(&self) -> u32 {
self.0.port as u32
}
pub fn partitions(&self) -> Vec<Partition> {
self.0
.partitions
.iter()
.map(|proto| Partition(proto.clone()))
.collect()
}
pub fn version(&self) -> &str {
&self.0.version
}
}
#[derive(Debug)]
pub struct Partition(proto::Partition);
impl Partition {
pub fn partition_id(&self) -> i32 {
self.0.partition_id
}
pub fn role(&self) -> i32 {
self.0.role
}
pub fn health(&self) -> i32 {
self.0.health
}
}