diff --git a/brood/src/commands.rs b/brood/src/commands.rs index f58324f..6c9bd5e 100644 --- a/brood/src/commands.rs +++ b/brood/src/commands.rs @@ -1,5 +1,5 @@ pub mod export; pub mod ingest; pub mod path; -pub mod redirects; pub mod show; +pub mod stats; diff --git a/brood/src/commands/stats.rs b/brood/src/commands/stats.rs new file mode 100644 index 0000000..5ee1272 --- /dev/null +++ b/brood/src/commands/stats.rs @@ -0,0 +1,95 @@ +mod redirects; + +use std::io; + +use thousands::Separable; + +use crate::data::Data; + +#[derive(Debug, clap::Parser)] +enum Command { + Redirects(redirects::Cmd), +} + +/// Show interesting stats. +#[derive(Debug, clap::Parser)] +pub struct Cmd { + #[command(subcommand)] + command: Option, +} + +impl Cmd { + pub fn run(self, data: Data) -> io::Result<()> { + if let Some(cmd) = self.command { + return match cmd { + Command::Redirects(cmd) => cmd.run(data), + }; + } + + println!(); + + const W_LABEL: usize = 14; + const W_NUM: usize = 11; + + let n_pages = data.pages.len(); + let n_redirects = data.pages.iter().filter(|p| p.redirect).count(); + let n_articles = n_pages - n_redirects; + + println!( + "{:>W_LABEL$}: {:>W_NUM$}", + "Pages", + n_pages.separate_with_underscores() + ); + + println!( + "{:>W_LABEL$}: {:>W_NUM$}", + "Articles", + n_articles.separate_with_underscores() + ); + + println!( + "{:>W_LABEL$}: {:>W_NUM$}", + "Redirects", + n_redirects.separate_with_underscores() + ); + + println!(); + println!( + "{:>W_LABEL$}: {:>W_NUM$}", + "Links", + data.links.len().separate_with_underscores() + ); + + println!( + "{:>W_LABEL$}: {:>W_NUM$}", + "in parens", + data.links + .iter() + .filter(|l| l.in_parens()) + .count() + .separate_with_underscores() + ); + + println!( + "{:>W_LABEL$}: {:>W_NUM$}", + "in structures", + data.links + .iter() + .filter(|l| l.in_structure()) + .count() + .separate_with_underscores() + ); + + println!( + "{:>W_LABEL$}: {:>W_NUM$}", + "pg eligible", + data.links + .iter() + .filter(|l| !l.in_parens() && !l.in_structure()) + .count() + .separate_with_underscores() + ); + + Ok(()) + } +} diff --git a/brood/src/commands/redirects.rs b/brood/src/commands/stats/redirects.rs similarity index 96% rename from brood/src/commands/redirects.rs rename to brood/src/commands/stats/redirects.rs index aeab362..6bf2204 100644 --- a/brood/src/commands/redirects.rs +++ b/brood/src/commands/stats/redirects.rs @@ -54,19 +54,19 @@ fn follow_redirect(data: &Data, start: NodeIdx) -> Vec { nodes } -/// Show interesting redirect stats. +/// Show redirect stats. #[derive(Debug, clap::Parser)] pub struct Cmd { + /// Show more detailed info. #[arg(long, short)] long: bool, } impl Cmd { pub fn run(self, data: Data) -> io::Result<()> { - println!(">> Resolving redirects"); + println!(">> Resolve redirects"); let redirects = find_redirects(&data); - println!(); println!( "There is a total of {} redirects.", redirects.len().separate_with_underscores() diff --git a/brood/src/main.rs b/brood/src/main.rs index a84ee1b..ba71e52 100644 --- a/brood/src/main.rs +++ b/brood/src/main.rs @@ -15,7 +15,7 @@ enum Command { Export(commands::export::Cmd), Show(commands::show::Cmd), Path(commands::path::Cmd), - Redirects(commands::redirects::Cmd), + Stats(commands::stats::Cmd), } #[derive(Debug, Parser)] @@ -74,6 +74,6 @@ fn main() -> io::Result<()> { Command::Export(cmd) => cmd.run(data), Command::Show(cmd) => cmd.run(data), Command::Path(cmd) => cmd.run(data), - Command::Redirects(cmd) => cmd.run(data), + Command::Stats(cmd) => cmd.run(data), } }