Merge relay debug view fixes & more strict args #711

Ken Sedgwick (5):
      drive-by compiler warning fixes
      drive-by clippy fixes
      add derive Debug to some things
      panic on unknown CLI arguments
      move RelayDebugView to notedeck crate and restore --relay-debug

William Casarin (4):
      clippy: fix lint
      args: skip creation of vec
      nix: fix android build

Link: https://github.com/damus-io/notedeck/pull/711
This commit is contained in:
William Casarin
2025-02-10 17:03:18 -08:00
13 changed files with 129 additions and 17 deletions

View File

@@ -1,12 +1,13 @@
use crate::persist::{AppSizeHandler, ZoomHandler};
use crate::{
Accounts, AppContext, Args, DataPath, DataPathType, Directory, FileKeyStorage, ImageCache,
KeyStorageType, NoteCache, ThemeHandler, UnknownIds,
KeyStorageType, NoteCache, RelayDebugView, ThemeHandler, UnknownIds,
};
use egui::ThemePreference;
use enostr::RelayPool;
use nostrdb::{Config, Ndb, Transaction};
use std::cell::RefCell;
use std::collections::BTreeSet;
use std::path::Path;
use std::rc::Rc;
use tracing::{error, info};
@@ -29,6 +30,7 @@ pub struct Notedeck {
app: Option<Rc<RefCell<dyn App>>>,
zoom: ZoomHandler,
app_size: AppSizeHandler,
unrecognized_args: BTreeSet<String>,
}
fn margin_top(narrow: bool) -> f32 {
@@ -82,8 +84,14 @@ impl eframe::App for Notedeck {
self.zoom.try_save_zoom_factor(ctx);
self.app_size.try_save_app_size(ctx);
if self.args.relay_debug && self.pool.debug.is_none() {
self.pool.use_debug();
if self.args.relay_debug {
if self.pool.debug.is_none() {
self.pool.use_debug();
}
if let Some(debug) = &mut self.pool.debug {
RelayDebugView::window(ctx, debug);
}
}
#[cfg(feature = "profiling")]
@@ -106,7 +114,8 @@ impl Notedeck {
#[cfg(feature = "profiling")]
setup_profiling();
let parsed_args = Args::parse(args);
// Skip the first argument, which is the program name.
let (parsed_args, unrecognized_args) = Args::parse(&args[1..]);
let data_path = parsed_args
.datapath
@@ -203,6 +212,7 @@ impl Notedeck {
app: None,
zoom,
app_size,
unrecognized_args,
}
}
@@ -236,4 +246,8 @@ impl Notedeck {
pub fn theme(&self) -> ThemePreference {
self.theme.load()
}
pub fn unrecognized_args(&self) -> &BTreeSet<String> {
&self.unrecognized_args
}
}

View File

@@ -1,3 +1,5 @@
use std::collections::BTreeSet;
use enostr::{Keypair, Pubkey, SecretKey};
use tracing::error;
@@ -18,7 +20,9 @@ pub struct Args {
}
impl Args {
pub fn parse(args: &[String]) -> Self {
// parse arguments, return set of unrecognized args
pub fn parse(args: &[String]) -> (Self, BTreeSet<String>) {
let mut unrecognized_args = BTreeSet::new();
let mut res = Args {
relays: vec![],
is_mobile: None,
@@ -112,11 +116,13 @@ impl Args {
res.use_keystore = false;
} else if arg == "--relay-debug" {
res.relay_debug = true;
} else {
unrecognized_args.insert(arg.clone());
}
i += 1;
}
res
(res, unrecognized_args)
}
}

View File

@@ -10,6 +10,7 @@ mod muted;
pub mod note;
mod notecache;
mod persist;
pub mod relay_debug;
pub mod relayspec;
mod result;
pub mod storage;
@@ -34,6 +35,7 @@ pub use muted::{MuteFun, Muted};
pub use note::{NoteRef, RootIdError, RootNoteId, RootNoteIdBuf};
pub use notecache::{CachedNote, NoteCache};
pub use persist::*;
pub use relay_debug::RelayDebugView;
pub use relayspec::RelaySpec;
pub use result::Result;
pub use storage::{

View File

@@ -0,0 +1,173 @@
use egui::ScrollArea;
use enostr::{RelayLogEvent, SubsDebug};
pub struct RelayDebugView<'a> {
debug: &'a mut SubsDebug,
}
impl<'a> RelayDebugView<'a> {
pub fn new(debug: &'a mut SubsDebug) -> Self {
Self { debug }
}
}
impl RelayDebugView<'_> {
pub fn ui(&mut self, ui: &mut egui::Ui) {
ScrollArea::vertical()
.id_salt(ui.id().with("relays_debug"))
.max_height(ui.max_rect().height() / 2.0)
.show(ui, |ui| {
ui.label("Active Relays:");
for (relay_str, data) in self.debug.get_data() {
egui::CollapsingHeader::new(format!(
"{} {} {}",
relay_str,
format_total(&data.count),
format_sec(&data.count)
))
.default_open(true)
.show(ui, |ui| {
ui.horizontal_wrapped(|ui| {
for (i, sub_data) in data.sub_data.values().enumerate() {
ui.label(format!(
"Filter {} ({})",
i + 1,
format_sec(&sub_data.count)
))
.on_hover_cursor(egui::CursorIcon::Help)
.on_hover_text(sub_data.filter.to_string());
}
})
});
}
});
ui.separator();
egui::ComboBox::from_label("Show events from relay")
.selected_text(
self.debug
.relay_events_selection
.as_ref()
.map_or(String::new(), |s| s.clone()),
)
.show_ui(ui, |ui| {
let mut make_selection = None;
for relay in self.debug.get_data().keys() {
if ui
.selectable_label(
if let Some(s) = &self.debug.relay_events_selection {
*s == *relay
} else {
false
},
relay,
)
.clicked()
{
make_selection = Some(relay.clone());
}
}
if make_selection.is_some() {
self.debug.relay_events_selection = make_selection
}
});
let show_relay_evs =
|ui: &mut egui::Ui, relay: Option<String>, events: Vec<RelayLogEvent>| {
for ev in events {
ui.horizontal_wrapped(|ui| {
if let Some(r) = &relay {
ui.label("relay").on_hover_text(r.clone());
}
match ev {
RelayLogEvent::Send(client_message) => {
ui.label("SEND: ");
let msg = &match client_message {
enostr::ClientMessage::Event { .. } => "Event",
enostr::ClientMessage::Req { .. } => "Req",
enostr::ClientMessage::Close { .. } => "Close",
enostr::ClientMessage::Raw(_) => "Raw",
};
if let Ok(json) = client_message.to_json() {
ui.label(*msg).on_hover_text(json)
} else {
ui.label(*msg)
}
}
RelayLogEvent::Recieve(e) => {
ui.label("RECIEVE: ");
match e {
enostr::OwnedRelayEvent::Opened => ui.label("Opened"),
enostr::OwnedRelayEvent::Closed => ui.label("Closed"),
enostr::OwnedRelayEvent::Other(s) => {
ui.label("Other").on_hover_text(s)
}
enostr::OwnedRelayEvent::Error(s) => {
ui.label("Error").on_hover_text(s)
}
enostr::OwnedRelayEvent::Message(s) => {
ui.label("Message").on_hover_text(s)
}
}
}
}
});
}
};
ScrollArea::vertical()
.id_salt(ui.id().with("events"))
.show(ui, |ui| {
if let Some(relay) = &self.debug.relay_events_selection {
if let Some(data) = self.debug.get_data().get(relay) {
show_relay_evs(ui, None, data.events.clone());
}
} else {
for (relay, data) in self.debug.get_data() {
show_relay_evs(ui, Some(relay.clone()), data.events.clone());
}
}
});
self.debug.try_increment_stats();
}
pub fn window(ctx: &egui::Context, debug: &mut SubsDebug) {
let mut open = true;
egui::Window::new("Relay Debugger")
.open(&mut open)
.show(ctx, |ui| {
RelayDebugView::new(debug).ui(ui);
});
}
}
fn format_sec(c: &enostr::TransferStats) -> String {
format!(
"{} ⬆️{}",
byte_to_string(c.down_sec_prior),
byte_to_string(c.up_sec_prior)
)
}
fn format_total(c: &enostr::TransferStats) -> String {
format!(
"total: ⬇{} ⬆️{}",
byte_to_string(c.down_total),
byte_to_string(c.up_total)
)
}
const MB: usize = 1_048_576;
const KB: usize = 1024;
fn byte_to_string(b: usize) -> String {
if b >= MB {
let mbs = b as f32 / MB as f32;
format!("{:.2} MB", mbs)
} else if b >= KB {
let kbs = b as f32 / KB as f32;
format!("{:.2} KB", kbs)
} else {
format!("{} B", b)
}
}