iggy_cli/commands/binary_consumer_groups/
get_consumer_group.rs1use crate::commands::cli_command::{CliCommand, PRINT_TARGET};
20use anyhow::Context;
21use async_trait::async_trait;
22use comfy_table::{Table, presets::ASCII_NO_BORDERS};
23use iggy_common::Client;
24use iggy_common::Identifier;
25use tracing::{Level, event};
26
27pub struct GetConsumerGroupCmd {
28 stream_id: Identifier,
29 topic_id: Identifier,
30 group_id: Identifier,
31}
32
33impl GetConsumerGroupCmd {
34 pub fn new(stream_id: Identifier, topic_id: Identifier, consumer_group_id: Identifier) -> Self {
35 Self {
36 stream_id,
37 topic_id,
38 group_id: consumer_group_id,
39 }
40 }
41}
42
43#[async_trait]
44impl CliCommand for GetConsumerGroupCmd {
45 fn explain(&self) -> String {
46 format!(
47 "get consumer group with ID: {} for topic with ID: {} and stream with ID: {}",
48 self.group_id, self.topic_id, self.stream_id,
49 )
50 }
51
52 async fn execute_cmd(&mut self, client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
53 let consumer_group = client
54 .get_consumer_group(&self.stream_id, &self.topic_id, &self.group_id)
55 .await
56 .with_context(|| {
57 format!(
58 "Problem getting consumer group with ID: {} for topic with ID: {} and stream with ID: {}",
59 self.group_id, self.topic_id, self.stream_id
60 )
61 })?;
62
63 if consumer_group.is_none() {
64 event!(target: PRINT_TARGET, Level::INFO, "Consumer group with ID: {} was not found", self.group_id);
65 return Ok(());
66 }
67
68 let consumer_group = consumer_group.unwrap();
69 let mut table = Table::new();
70
71 table.set_header(vec!["Property", "Value"]);
72 table.add_row(vec![
73 "Consumer group id",
74 format!("{}", consumer_group.id).as_str(),
75 ]);
76 table.add_row(vec!["Consumer group name", consumer_group.name.as_str()]);
77 table.add_row(vec![
78 "Partitions count",
79 format!("{}", consumer_group.partitions_count).as_str(),
80 ]);
81 table.add_row(vec![
82 "Members count",
83 format!("{}", consumer_group.members_count).as_str(),
84 ]);
85
86 if consumer_group.members_count > 0 {
87 let mut members_table = Table::new();
88 members_table.load_preset(ASCII_NO_BORDERS);
89 members_table.set_header(vec!["Member id", "Partitions count", "Partitions"]);
90 for member in consumer_group.members {
91 members_table.add_row(vec![
92 format!("{}", member.id).as_str(),
93 format!("{}", member.partitions_count).as_str(),
94 member
95 .partitions
96 .iter()
97 .map(|i| format!("{i}"))
98 .collect::<Vec<String>>()
99 .join(", ")
100 .as_str(),
101 ]);
102 }
103 table.add_row(vec!["Members", members_table.to_string().as_str()]);
104 }
105
106 event!(target: PRINT_TARGET, Level::INFO,"{table}");
107
108 Ok(())
109 }
110}