Skip to content

Commit

Permalink
Simplify extract desc
Browse files Browse the repository at this point in the history
  • Loading branch information
pawurb committed Nov 17, 2024
1 parent 3b8dcfa commit 0f9cd93
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 47 deletions.
79 changes: 33 additions & 46 deletions bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,71 +31,71 @@ pub struct PgExtrasArgs {
pub enum PgSubcommand {
#[command(about = "Diagnose common database problems")]
Diagnose(EmptyArgs),
#[command(about = extract_desc(&AllLocks::description()))]
#[command(about = &AllLocks::description())]
AllLocks(EmptyArgs),
#[command(about = extract_desc(&Bloat::description()))]
#[command(about = &Bloat::description())]
Bloat(EmptyArgs),
#[command(about = extract_desc(&Blocking::description()))]
#[command(about = &Blocking::description())]
Blocking(EmptyArgs),
#[command(about = extract_desc(&BuffercacheStats::description()))]
#[command(about = &BuffercacheStats::description())]
BuffercacheStats(EmptyArgs),
#[command(about = extract_desc(&BuffercacheUsage::description()))]
#[command(about = &BuffercacheUsage::description())]
BuffercacheUsage(EmptyArgs),
#[command(about = extract_desc(&CacheHit::description()))]
#[command(about = &CacheHit::description())]
CacheHit(EmptyArgs),
#[command(about = extract_desc(&Calls::description()))]
#[command(about = &Calls::description())]
Calls(EmptyArgs),
#[command(about = extract_desc(&Connections::description()))]
#[command(about = &Connections::description())]
Connections(EmptyArgs),
#[command(about = extract_desc(&DbSettings::description()))]
#[command(about = &DbSettings::description())]
DbSettings(EmptyArgs),
#[command(about = extract_desc(&DuplicateIndexes::description()))]
#[command(about = &DuplicateIndexes::description())]
DuplicateIndexes(EmptyArgs),
#[command(about = extract_desc(&Extensions::description()))]
#[command(about = &Extensions::description())]
Extensions(EmptyArgs),
#[command(about = extract_desc(&IndexCacheHit::description()))]
#[command(about = &IndexCacheHit::description())]
IndexCacheHit(EmptyArgs),
#[command(about = extract_desc(&IndexScans::description()))]
#[command(about = &IndexScans::description())]
IndexScans(EmptyArgs),
#[command(about = extract_desc(&IndexSize::description()))]
#[command(about = &IndexSize::description())]
IndexSize(EmptyArgs),
#[command(about = extract_desc(&IndexUsage::description()))]
#[command(about = &IndexUsage::description())]
IndexUsage(EmptyArgs),
#[command(about = extract_desc(&Indexes::description()))]
#[command(about = &Indexes::description())]
Indexes(EmptyArgs),
#[command(about = extract_desc(&Locks::description()))]
#[command(about = &Locks::description())]
Locks(EmptyArgs),
#[command(about = extract_desc(&LongRunningQueries::description()))]
#[command(about = &LongRunningQueries::description())]
LongRunningQueries(EmptyArgs),
#[command(about = extract_desc(&NullIndexes::description()))]
#[command(about = &NullIndexes::description())]
NullIndexes(EmptyArgs),
#[command(about = extract_desc(&Outliers::description()))]
#[command(about = &Outliers::description())]
Outliers(EmptyArgs),
#[command(about = extract_desc(&Mandelbrot::description()))]
#[command(about = &Mandelbrot::description())]
Mandelbrot(EmptyArgs),
#[command(about = extract_desc(&RecordsRank::description()))]
#[command(about = &RecordsRank::description())]
RecordsRank(EmptyArgs),
#[command(about = extract_desc(&SeqScans::description()))]
#[command(about = &SeqScans::description())]
SeqScans(EmptyArgs),
#[command(about = extract_desc(&SslUsed::description()))]
#[command(about = &SslUsed::description())]
SslUsed(EmptyArgs),
#[command(about = extract_desc(&TableCacheHit::description()))]
#[command(about = &TableCacheHit::description())]
TableCacheHit(EmptyArgs),
#[command(about = extract_desc(&TableIndexScans::description()))]
#[command(about = &TableIndexScans::description())]
TableIndexScans(EmptyArgs),
#[command(about = extract_desc(&TableIndexesSize::description()))]
#[command(about = &TableIndexesSize::description())]
TableIndexesSize(EmptyArgs),
#[command(about = extract_desc(&TableSize::description()))]
#[command(about = &TableSize::description())]
TableSize(EmptyArgs),
#[command(about = extract_desc(&Tables::description()))]
#[command(about = &Tables::description())]
Tables(EmptyArgs),
#[command(about = extract_desc(&TotalIndexSize::description()))]
#[command(about = &TotalIndexSize::description())]
TotalIndexSize(EmptyArgs),
#[command(about = extract_desc(&TotalTableSize::description()))]
#[command(about = &TotalTableSize::description())]
TotalTableSize(EmptyArgs),
#[command(about = extract_desc(&UnusedIndexes::description()))]
#[command(about = &UnusedIndexes::description())]
UnusedIndexes(EmptyArgs),
#[command(about = extract_desc(&VacuumStats::description()))]
#[command(about = &VacuumStats::description())]
VacuumStats(EmptyArgs),
}

Expand Down Expand Up @@ -224,16 +224,3 @@ async fn execute() -> Result<(), PgExtrasError> {

Ok(())
}

fn extract_desc(desc: &str) -> String {
if let (Some(start), Some(end)) = (desc.find("/*"), desc.find("*/")) {
let extracted = &desc[start + 2..end];
let mut trimmed = extracted.trim().to_string();
if trimmed.ends_with('.') {
trimmed.pop();
}
trimmed
} else {
desc.to_string()
}
}
22 changes: 21 additions & 1 deletion src/queries/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,27 @@ pub trait Query {
fn headers() -> prettytable::Row;
fn read_file(pg_statement_version: Option<PgStatsVersion>) -> String;
fn description() -> String {
Self::read_file(None).lines().take(1).collect()
let desc = Self::read_file(None)
.lines()
.take(1)
.next()
.unwrap_or_default()
.to_string();

extract_desc(desc)
}
}

fn extract_desc(desc: String) -> String {
if let (Some(start), Some(end)) = (desc.find("/*"), desc.find("*/")) {
let extracted = &desc[start + 2..end];
let mut trimmed = extracted.trim().to_string();
if trimmed.ends_with('.') {
trimmed.pop();
}
trimmed
} else {
desc
}
}

Expand Down

0 comments on commit 0f9cd93

Please sign in to comment.