relay_debug.rs (6544B)
1 use egui::ScrollArea; 2 use enostr::{RelayLogEvent, SubsDebug}; 3 4 pub struct RelayDebugView<'a> { 5 debug: &'a mut SubsDebug, 6 } 7 8 impl<'a> RelayDebugView<'a> { 9 pub fn new(debug: &'a mut SubsDebug) -> Self { 10 Self { debug } 11 } 12 } 13 14 impl RelayDebugView<'_> { 15 pub fn ui(&mut self, ui: &mut egui::Ui) { 16 ScrollArea::vertical() 17 .id_salt(ui.id().with("relays_debug")) 18 .max_height(ui.max_rect().height() / 2.0) 19 .show(ui, |ui| { 20 ui.label("Active Relays:"); 21 for (relay_str, data) in self.debug.get_data() { 22 egui::CollapsingHeader::new(format!( 23 "{} {} {}", 24 relay_str, 25 format_total(&data.count), 26 format_sec(&data.count) 27 )) 28 .default_open(true) 29 .show(ui, |ui| { 30 ui.horizontal_wrapped(|ui| { 31 for (i, sub_data) in data.sub_data.values().enumerate() { 32 ui.label(format!( 33 "Filter {} ({})", 34 i + 1, 35 format_sec(&sub_data.count) 36 )) 37 .on_hover_cursor(egui::CursorIcon::Help) 38 .on_hover_text(sub_data.filter.to_string()); 39 } 40 }) 41 }); 42 } 43 }); 44 45 ui.separator(); 46 egui::ComboBox::from_label("Show events from relay") 47 .selected_text( 48 self.debug 49 .relay_events_selection 50 .as_ref() 51 .map_or(String::new(), |s| s.clone()), 52 ) 53 .show_ui(ui, |ui| { 54 let mut make_selection = None; 55 for relay in self.debug.get_data().keys() { 56 if ui 57 .selectable_label( 58 if let Some(s) = &self.debug.relay_events_selection { 59 *s == *relay 60 } else { 61 false 62 }, 63 relay, 64 ) 65 .clicked() 66 { 67 make_selection = Some(relay.clone()); 68 } 69 } 70 if make_selection.is_some() { 71 self.debug.relay_events_selection = make_selection 72 } 73 }); 74 let show_relay_evs = 75 |ui: &mut egui::Ui, relay: Option<String>, events: Vec<RelayLogEvent>| { 76 for ev in events { 77 ui.horizontal_wrapped(|ui| { 78 if let Some(r) = &relay { 79 ui.label("relay").on_hover_text(r.clone()); 80 } 81 match ev { 82 RelayLogEvent::Send(client_message) => { 83 ui.label("SEND: "); 84 let msg = &match client_message { 85 enostr::ClientMessage::Event { .. } => "Event", 86 enostr::ClientMessage::Req { .. } => "Req", 87 enostr::ClientMessage::Close { .. } => "Close", 88 enostr::ClientMessage::Raw(_) => "Raw", 89 }; 90 91 if let Ok(json) = client_message.to_json() { 92 ui.label(*msg).on_hover_text(json) 93 } else { 94 ui.label(*msg) 95 } 96 } 97 RelayLogEvent::Recieve(e) => { 98 ui.label("RECIEVE: "); 99 match e { 100 enostr::OwnedRelayEvent::Opened => ui.label("Opened"), 101 enostr::OwnedRelayEvent::Closed => ui.label("Closed"), 102 enostr::OwnedRelayEvent::Other(s) => { 103 ui.label("Other").on_hover_text(s) 104 } 105 enostr::OwnedRelayEvent::Error(s) => { 106 ui.label("Error").on_hover_text(s) 107 } 108 enostr::OwnedRelayEvent::Message(s) => { 109 ui.label("Message").on_hover_text(s) 110 } 111 } 112 } 113 } 114 }); 115 } 116 }; 117 118 ScrollArea::vertical() 119 .id_salt(ui.id().with("events")) 120 .show(ui, |ui| { 121 if let Some(relay) = &self.debug.relay_events_selection { 122 if let Some(data) = self.debug.get_data().get(relay) { 123 show_relay_evs(ui, None, data.events.clone()); 124 } 125 } else { 126 for (relay, data) in self.debug.get_data() { 127 show_relay_evs(ui, Some(relay.clone()), data.events.clone()); 128 } 129 } 130 }); 131 132 self.debug.try_increment_stats(); 133 } 134 135 pub fn window(ctx: &egui::Context, debug: &mut SubsDebug) { 136 let mut open = true; 137 egui::Window::new("Relay Debugger") 138 .open(&mut open) 139 .show(ctx, |ui| { 140 RelayDebugView::new(debug).ui(ui); 141 }); 142 } 143 } 144 145 fn format_sec(c: &enostr::TransferStats) -> String { 146 format!( 147 "⬇{} ⬆️{}", 148 byte_to_string(c.down_sec_prior), 149 byte_to_string(c.up_sec_prior) 150 ) 151 } 152 153 fn format_total(c: &enostr::TransferStats) -> String { 154 format!( 155 "total: ⬇{} ⬆️{}", 156 byte_to_string(c.down_total), 157 byte_to_string(c.up_total) 158 ) 159 } 160 161 const MB: usize = 1_048_576; 162 const KB: usize = 1024; 163 fn byte_to_string(b: usize) -> String { 164 if b >= MB { 165 let mbs = b as f32 / MB as f32; 166 format!("{:.2} MB", mbs) 167 } else if b >= KB { 168 let kbs = b as f32 / KB as f32; 169 format!("{:.2} KB", kbs) 170 } else { 171 format!("{} B", b) 172 } 173 }