use clap::Parser;
use rocketmq_error::Result as CanonicalResult;
use rocketmq_model::common::lite;
use rocketmq_protocol::protocol::body::get_lite_topic_info_response_body::GetLiteTopicInfoResponseBody;
use rocketmq_runtime::common::time_utils::time_millis_to_human_string2;
use crate::commands::CommandExecute;
use rocketmq_admin_core::client_adapter::services::lite::LiteService;
use rocketmq_admin_core::client_adapter::services::lite::LiteTopicInfoQueryRequest;
use rocketmq_admin_core::client_adapter::services::lite::LiteTopicInfoQueryResult;
#[derive(Debug, Clone, Parser)]
pub struct GetLiteTopicInfoSubCommand {
#[arg(short = 'p', long = "parentTopic", required = true, help = "Parent topic name")]
parent_topic: String,
#[arg(short = 'l', long = "liteTopic", required = true, help = "Lite topic name")]
lite_topic: String,
#[arg(short = 's', long = "showClientId", help = "Show all clientId")]
show_client_id: bool,
}
impl CommandExecute for GetLiteTopicInfoSubCommand {
async fn execute(
&self,
credentials: Option<rocketmq_admin_core::core::security::AdminCredentials>,
client_runtime: std::sync::Arc<rocketmq_admin_core::client_adapter::ClientRuntime>,
) -> CanonicalResult<()> {
let result = LiteService::query_lite_topic_info_by_request_with_credentials(
self.request()?,
credentials,
client_runtime.clone(),
)
.await?;
Self::print_result(&result, self.show_client_id);
Ok(())
}
}
impl GetLiteTopicInfoSubCommand {
fn request(&self) -> CanonicalResult<LiteTopicInfoQueryRequest> {
LiteTopicInfoQueryRequest::try_new(self.parent_topic.clone(), self.lite_topic.clone())
}
fn print_result(result: &LiteTopicInfoQueryResult, show_client_id: bool) {
let lmq_name = lite::to_lmq_name(result.parent_topic.as_str(), result.lite_topic.as_str()).unwrap_or_default();
println!(
"Lite Topic Info: [{}] [{}] [{}]",
result.parent_topic, result.lite_topic, lmq_name
);
println!(
"{:<50} {:<14} {:<14} {:<30} {:<12} {:<18}",
"#Broker Name", "#MinOffset", "#MaxOffset", "#LastUpdate", "#Sharding", "#SubClientCount"
);
for entry in &result.entries {
if let Some(body) = &entry.body {
Self::print_row(entry.broker_name.as_str(), body, show_client_id);
} else {
println!(
"[{}] error: {}",
entry.broker_name,
entry.error.as_deref().unwrap_or_default()
);
}
}
}
fn print_row(broker_name: &str, body: &GetLiteTopicInfoResponseBody, show_client_id: bool) {
let (min_offset, max_offset, last_update) = if let Some(topic_offset) = body.topic_offset() {
(
topic_offset.get_min_offset(),
topic_offset.get_max_offset(),
topic_offset.get_last_update_timestamp(),
)
} else {
(0, 0, 0)
};
let last_update_str = if last_update > 0 {
time_millis_to_human_string2(last_update)
} else {
"-".to_string()
};
let display_name: String = if broker_name.chars().count() > 40 {
broker_name.chars().take(40).collect()
} else {
broker_name.to_string()
};
println!(
"{:<50} {:<14} {:<14} {:<30} {:<12} {:<18}",
display_name,
min_offset,
max_offset,
last_update_str,
body.sharding_to_broker(),
body.subscriber().len()
);
if show_client_id {
let display_list: Vec<String> = body
.subscriber()
.iter()
.map(|client_group| format!("{}@{}", client_group.client_id, client_group.group))
.collect();
println!("{:?}", display_list);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn get_lite_topic_info_sub_command_builds_request() {
let cmd = GetLiteTopicInfoSubCommand::try_parse_from([
"getLiteTopicInfo",
"-p",
" ParentTopic ",
"-l",
" LiteTopic ",
"-s",
])
.unwrap();
let request = cmd.request().unwrap();
assert_eq!(request.parent_topic().as_str(), "ParentTopic");
assert_eq!(request.lite_topic().as_str(), "LiteTopic");
assert!(cmd.show_client_id);
}
}