iggy_cli/commands/binary_consumer_offsets/
get_consumer_offset.rs1use crate::commands::cli_command::{CliCommand, PRINT_TARGET};
20use anyhow::Context;
21use async_trait::async_trait;
22use comfy_table::Table;
23use iggy_common::Client;
24use iggy_common::{Consumer, ConsumerKind, Identifier};
25use tracing::{Level, event};
26
27pub struct GetConsumerOffsetCmd {
28 consumer: Consumer,
29 stream_id: Identifier,
30 topic_id: Identifier,
31 partition_id: u32,
32}
33
34impl GetConsumerOffsetCmd {
35 pub fn new(
36 consumer_id: Identifier,
37 stream_id: Identifier,
38 topic_id: Identifier,
39 partition_id: u32,
40 kind: ConsumerKind,
41 ) -> Self {
42 Self {
43 consumer: Consumer {
44 kind,
45 id: consumer_id,
46 },
47 stream_id,
48 topic_id,
49 partition_id,
50 }
51 }
52
53 pub fn get_consumer_info(&self) -> String {
54 match self.consumer.kind {
55 ConsumerKind::Consumer => {
56 format!("consumer with ID: {}", self.consumer.id)
57 }
58 ConsumerKind::ConsumerGroup => format!("consumer group with ID: {}", self.consumer.id),
59 }
60 }
61}
62
63#[async_trait]
64impl CliCommand for GetConsumerOffsetCmd {
65 fn explain(&self) -> String {
66 format!(
67 "get consumer offset for {} for stream with ID: {} and topic with ID: {} and partition with ID: {}",
68 self.get_consumer_info(),
69 self.stream_id,
70 self.topic_id,
71 self.partition_id,
72 )
73 }
74
75 async fn execute_cmd(&mut self, client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
76 let consumer_offset = client.get_consumer_offset(&self.consumer, &self.stream_id, &self.topic_id, Some(self.partition_id)).await.with_context(|| {
77 format!(
78 "Problem getting consumer offset for {} for stream with ID: {} and topic with ID: {} and partition with ID: {}",
79 self.get_consumer_info(), self.stream_id, self.topic_id, self.partition_id
80 )
81 })?;
82
83 if consumer_offset.is_none() {
84 event!(target: PRINT_TARGET, Level::INFO, "Consumer offset for {} for stream with ID: {} and topic with ID: {} and partition with ID: {} was not found", self.get_consumer_info(), self.stream_id, self.topic_id, self.partition_id);
85 return Ok(());
86 }
87
88 let consumer_offset = consumer_offset.unwrap();
89 let mut table = Table::new();
90
91 table.set_header(vec!["Property", "Value"]);
92 table.add_row(vec![
93 "Consumer ID",
94 format!("{}", self.consumer.id).as_str(),
95 ]);
96 table.add_row(vec!["Stream ID", format!("{}", self.stream_id).as_str()]);
97 table.add_row(vec!["Topic ID", format!("{}", self.topic_id).as_str()]);
98 table.add_row(vec![
99 "Partition ID",
100 format!("{}", consumer_offset.partition_id).as_str(),
101 ]);
102 table.add_row(vec![
103 "Current offset",
104 format!("{}", consumer_offset.current_offset).as_str(),
105 ]);
106 table.add_row(vec![
107 "Stored offset",
108 format!("{}", consumer_offset.stored_offset).as_str(),
109 ]);
110
111 event!(target: PRINT_TARGET, Level::INFO, "{table}");
112
113 Ok(())
114 }
115}