channels_console/
channels_guard.rs1use std::time::Instant;
2
3use prettytable::{Cell, Row, Table};
4
5use crate::{format_bytes, get_channel_stats, get_serializable_stats, resolve_label, Format};
6
7pub struct ChannelsGuardBuilder {
20 format: Format,
21}
22
23impl ChannelsGuardBuilder {
24 pub fn new() -> Self {
26 Self {
27 format: Format::default(),
28 }
29 }
30
31 pub fn format(mut self, format: Format) -> Self {
43 self.format = format;
44 self
45 }
46
47 pub fn build(self) -> ChannelsGuard {
50 ChannelsGuard {
51 start_time: Instant::now(),
52 format: self.format,
53 }
54 }
55}
56
57impl Default for ChannelsGuardBuilder {
58 fn default() -> Self {
59 Self::new()
60 }
61}
62
63pub struct ChannelsGuard {
78 start_time: Instant,
79 format: Format,
80}
81
82impl ChannelsGuard {
83 pub fn new() -> Self {
88 Self {
89 start_time: Instant::now(),
90 format: Format::default(),
91 }
92 }
93
94 pub fn format(mut self, format: Format) -> Self {
105 self.format = format;
106 self
107 }
108}
109
110impl Default for ChannelsGuard {
111 fn default() -> Self {
112 Self::new()
113 }
114}
115
116impl Drop for ChannelsGuard {
117 fn drop(&mut self) {
118 let elapsed = self.start_time.elapsed();
119 let stats = get_channel_stats();
120
121 if stats.is_empty() {
122 println!("\nNo instrumented channels found.");
123 return;
124 }
125
126 match self.format {
127 Format::Table => {
128 let mut table = Table::new();
129
130 table.add_row(Row::new(vec![
131 Cell::new("Channel"),
132 Cell::new("Type"),
133 Cell::new("State"),
134 Cell::new("Sent"),
135 Cell::new("Mem"),
136 Cell::new("Received"),
137 Cell::new("Queued"),
138 Cell::new("Mem"),
139 ]));
140
141 let mut sorted_stats: Vec<_> = stats.into_iter().collect();
142 sorted_stats.sort_by(|a, b| {
143 let la = resolve_label(a.1.id, a.1.label);
144 let lb = resolve_label(b.1.id, b.1.label);
145 la.cmp(&lb)
146 });
147
148 for (_key, channel_stats) in sorted_stats {
149 let label = resolve_label(channel_stats.id, channel_stats.label);
150 table.add_row(Row::new(vec![
151 Cell::new(&label),
152 Cell::new(&channel_stats.channel_type.to_string()),
153 Cell::new(channel_stats.state.as_str()),
154 Cell::new(&channel_stats.sent_count.to_string()),
155 Cell::new(&format_bytes(channel_stats.total_bytes())),
156 Cell::new(&channel_stats.received_count.to_string()),
157 Cell::new(&channel_stats.queued().to_string()),
158 Cell::new(&format_bytes(channel_stats.queued_bytes())),
159 ]));
160 }
161
162 println!(
163 "\n=== Channel Statistics (runtime: {:.2}s) ===",
164 elapsed.as_secs_f64()
165 );
166 table.printstd();
167 }
168 Format::Json => {
169 let serializable_stats = get_serializable_stats();
170 match serde_json::to_string(&serializable_stats) {
171 Ok(json) => println!("{}", json),
172 Err(e) => eprintln!("Failed to serialize statistics to JSON: {}", e),
173 }
174 }
175 Format::JsonPretty => {
176 let serializable_stats = get_serializable_stats();
177 match serde_json::to_string_pretty(&serializable_stats) {
178 Ok(json) => println!("{}", json),
179 Err(e) => eprintln!("Failed to serialize statistics to pretty JSON: {}", e),
180 }
181 }
182 }
183 }
184}