Skip to content

Commit

Permalink
Remove duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
pawurb committed Oct 25, 2024
1 parent 1242fde commit 24917ff
Showing 1 changed file with 84 additions and 67 deletions.
151 changes: 84 additions & 67 deletions bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ use pg_extras::{
index_size, index_usage, indexes, locks, long_running_queries, mandelbrot, null_indexes,
outliers, records_rank, render_table, seq_scans, ssl_used, table_cache_hit, table_index_scans,
table_indexes_size, table_size, tables, total_index_size, total_table_size, unused_indexes,
vacuum_stats, PgExtrasError,
vacuum_stats, AllLocks, Bloat, Blocking, BuffercacheStats, BuffercacheUsage, CacheHit, Calls,
Connections, DbSettings, DuplicateIndexes, Extensions, IndexCacheHit, IndexScans, IndexSize,
IndexUsage, Indexes, Locks, LongRunningQueries, Mandelbrot, NullIndexes, Outliers,
PgExtrasError, Query, RecordsRank, SeqScans, SslUsed, TableCacheHit, TableIndexScans,
TableIndexesSize, TableSize, Tables, TotalIndexSize, TotalTableSize, UnusedIndexes,
VacuumStats,
};

#[derive(Parser, Debug)]
Expand All @@ -23,71 +28,71 @@ pub struct PgExtrasArgs {

#[derive(Subcommand, Debug)]
pub enum PgSubcommand {
/// All the current locks, regardless of their type.
#[command(about = extract_desc(&AllLocks::description()))]
AllLocks(EmptyArgs),
/// An estimation of table "bloat".
#[command(about = extract_desc(&Bloat::description()))]
Bloat(EmptyArgs),
/// Statements that are currently holding locks that other statements are waiting to be released.
#[command(about = extract_desc(&Blocking::description()))]
Blocking(EmptyArgs),
/// Relations buffered in database share buffer, ordered by percentage taken.
#[command(about = extract_desc(&BuffercacheStats::description()))]
BuffercacheStats(EmptyArgs),
/// Calculates how many blocks from which table are currently cached.
#[command(about = extract_desc(&BuffercacheUsage::description()))]
BuffercacheUsage(EmptyArgs),
/// Information on the efficiency of the buffer cache, index reads and table reads.
#[command(about = extract_desc(&CacheHit::description()))]
CacheHit(EmptyArgs),
/// Just like `outliers`, but ordered by the number of times a statement has been called.
#[command(about = extract_desc(&Calls::description()))]
Calls(EmptyArgs),
/// Returns the list of all active database connections.
#[command(about = extract_desc(&Connections::description()))]
Connections(EmptyArgs),
/// Displays values for selected PostgreSQL settings.
#[command(about = extract_desc(&DbSettings::description()))]
DbSettings(EmptyArgs),
/// Displays duplicate indexes, usually it's safe to drop one of them.
#[command(about = extract_desc(&DuplicateIndexes::description()))]
DuplicateIndexes(EmptyArgs),
/// Lists all the currently installed and available PostgreSQL extensions.
#[command(about = extract_desc(&Extensions::description()))]
Extensions(EmptyArgs),
/// The same as `cache_hit` with each table's indexes cache hit info displayed separately.
#[command(about = extract_desc(&IndexCacheHit::description()))]
IndexCacheHit(EmptyArgs),
/// Number of scans performed on indexes.
#[command(about = extract_desc(&IndexScans::description()))]
IndexScans(EmptyArgs),
/// The size of indexes, descending by size.
#[command(about = extract_desc(&IndexSize::description()))]
IndexSize(EmptyArgs),
/// Index hit rate (effective databases are at 99 percent and up).
#[command(about = extract_desc(&IndexUsage::description()))]
IndexUsage(EmptyArgs),
/// List all the indexes with their corresponding tables and columns.
#[command(about = extract_desc(&Indexes::description()))]
Indexes(EmptyArgs),
/// Queries with active exclusive locks.
#[command(about = extract_desc(&Locks::description()))]
Locks(EmptyArgs),
/// All queries longer than the threshold by descending duration.
#[command(about = extract_desc(&LongRunningQueries::description()))]
LongRunningQueries(EmptyArgs),
/// Indexes with a high ratio of NULL values.
#[command(about = extract_desc(&NullIndexes::description()))]
NullIndexes(EmptyArgs),
/// Queries that have longest execution time in aggregate.
#[command(about = extract_desc(&Outliers::description()))]
Outliers(EmptyArgs),
/// The mandelbrot set.
#[command(about = extract_desc(&Mandelbrot::description()))]
Mandelbrot(EmptyArgs),
/// All tables and the number of rows in each ordered by number of rows descending.
#[command(about = extract_desc(&RecordsRank::description()))]
RecordsRank(EmptyArgs),
/// Count of sequential scans by table descending by order.
#[command(about = extract_desc(&SeqScans::description()))]
SeqScans(EmptyArgs),
/// Check if SSL connection is used.
#[command(about = extract_desc(&SslUsed::description()))]
SslUsed(EmptyArgs),
/// Calculates your cache hit rate for reading tables.
#[command(about = extract_desc(&TableCacheHit::description()))]
TableCacheHit(EmptyArgs),
/// Count of index scans by table descending by order.
#[command(about = extract_desc(&TableIndexScans::description()))]
TableIndexScans(EmptyArgs),
/// Total size of all the indexes on each table, descending by size.
#[command(about = extract_desc(&TableIndexesSize::description()))]
TableIndexesSize(EmptyArgs),
/// Size of the tables (excluding indexes), descending by size.
#[command(about = extract_desc(&TableSize::description()))]
TableSize(EmptyArgs),
/// List all the tables.
#[command(about = extract_desc(&Tables::description()))]
Tables(EmptyArgs),
/// Total size of all indexes in MB.
#[command(about = extract_desc(&TotalIndexSize::description()))]
TotalIndexSize(EmptyArgs),
/// Size of the tables (including indexes), descending by size.
#[command(about = extract_desc(&TotalTableSize::description()))]
TotalTableSize(EmptyArgs),
/// Unused and almost unused indexes.
#[command(about = extract_desc(&UnusedIndexes::description()))]
UnusedIndexes(EmptyArgs),
/// Dead rows and whether an automatic vacuum is expected to be triggered.
#[command(about = extract_desc(&VacuumStats::description()))]
VacuumStats(EmptyArgs),
}

Expand All @@ -105,110 +110,122 @@ async fn main() {
}
}

type PG = PgSubcommand;
async fn execute() -> Result<(), PgExtrasError> {
let args = PgExtrasArgs::parse();

match args.cmd {
PgSubcommand::AllLocks(_args) => {
PG::AllLocks(_args) => {
render_table(all_locks().await?);
}
PgSubcommand::Bloat(_args) => {
PG::Bloat(_args) => {
render_table(bloat().await?);
}
PgSubcommand::Blocking(_args) => {
PG::Blocking(_args) => {
render_table(blocking(None).await?);
}
PgSubcommand::BuffercacheStats(_args) => {
PG::BuffercacheStats(_args) => {
render_table(buffercache_stats().await?);
}
PgSubcommand::BuffercacheUsage(_args) => {
PG::BuffercacheUsage(_args) => {
render_table(buffercache_usage().await?);
}
PgSubcommand::CacheHit(_args) => {
PG::CacheHit(_args) => {
render_table(cache_hit(None).await?);
}
PgSubcommand::Calls(_args) => {
PG::Calls(_args) => {
render_table(calls(None).await?);
}
PgSubcommand::Connections(_args) => {
PG::Connections(_args) => {
render_table(connections().await?);
}
PgSubcommand::DbSettings(_args) => {
PG::DbSettings(_args) => {
render_table(db_settings().await?);
}
PgSubcommand::DuplicateIndexes(_args) => {
PG::DuplicateIndexes(_args) => {
render_table(duplicate_indexes().await?);
}
PgSubcommand::Extensions(_args) => {
PG::Extensions(_args) => {
render_table(extensions().await?);
}
PgSubcommand::IndexCacheHit(_args) => {
PG::IndexCacheHit(_args) => {
render_table(index_cache_hit(None).await?);
}
PgSubcommand::IndexScans(_args) => {
PG::IndexScans(_args) => {
render_table(index_scans(None).await?);
}
PgSubcommand::IndexSize(_args) => {
PG::IndexSize(_args) => {
render_table(index_size().await?);
}
PgSubcommand::IndexUsage(_args) => {
PG::IndexUsage(_args) => {
render_table(index_usage(None).await?);
}
PgSubcommand::Indexes(_args) => {
PG::Indexes(_args) => {
render_table(indexes().await?);
}
PgSubcommand::Locks(_args) => {
PG::Locks(_args) => {
render_table(locks().await?);
}
PgSubcommand::LongRunningQueries(_args) => {
PG::LongRunningQueries(_args) => {
render_table(long_running_queries().await?);
}
PgSubcommand::NullIndexes(_args) => {
PG::NullIndexes(_args) => {
render_table(null_indexes(None).await?);
}
PgSubcommand::Outliers(_args) => {
PG::Outliers(_args) => {
render_table(outliers().await?);
}
PgSubcommand::Mandelbrot(_args) => {
PG::Mandelbrot(_args) => {
render_table(mandelbrot().await?);
}
PgSubcommand::RecordsRank(_args) => {
PG::RecordsRank(_args) => {
render_table(records_rank(None).await?);
}
PgSubcommand::SeqScans(_args) => {
PG::SeqScans(_args) => {
render_table(seq_scans(None).await?);
}
PgSubcommand::SslUsed(_args) => {
PG::SslUsed(_args) => {
render_table(ssl_used().await?);
}
PgSubcommand::TableCacheHit(_args) => {
PG::TableCacheHit(_args) => {
render_table(table_cache_hit().await?);
}
PgSubcommand::TableIndexScans(_args) => {
PG::TableIndexScans(_args) => {
render_table(table_index_scans(None).await?);
}
PgSubcommand::TableIndexesSize(_args) => {
PG::TableIndexesSize(_args) => {
render_table(table_indexes_size(None).await?);
}
PgSubcommand::TableSize(_args) => {
PG::TableSize(_args) => {
render_table(table_size().await?);
}
PgSubcommand::Tables(_args) => {
PG::Tables(_args) => {
render_table(tables(None).await?);
}
PgSubcommand::TotalIndexSize(_args) => {
PG::TotalIndexSize(_args) => {
render_table(total_index_size().await?);
}
PgSubcommand::TotalTableSize(_args) => {
PG::TotalTableSize(_args) => {
render_table(total_table_size().await?);
}
PgSubcommand::UnusedIndexes(_args) => {
PG::UnusedIndexes(_args) => {
render_table(unused_indexes(None).await?);
}
PgSubcommand::VacuumStats(_args) => {
PG::VacuumStats(_args) => {
render_table(vacuum_stats().await?);
}
}

Ok(())
}

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

0 comments on commit 24917ff

Please sign in to comment.