diff --git a/README.md b/README.md index 6f5ddb8..c0a27c3 100644 --- a/README.md +++ b/README.md @@ -173,14 +173,14 @@ The same as `cache_hit` with each table's cache hit info displayed seperately. ### `db_settings` ```rust -struct DbSetting { +struct DbSettings { name: String, setting: String, unit: String, short_desc: String, } -db_settings() -> Result, PgExtrasError> +db_settings() -> Result, PgExtrasError> name | setting | unit | ------------------------------+---------+------+ diff --git a/src/lib.rs b/src/lib.rs index b6c2fe5..4292455 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,7 +9,7 @@ pub use structs::buffercache_usage::BuffercacheUsage; pub use structs::cache_hit::CacheHit; pub use structs::calls::Calls; pub use structs::connections::Connections; -pub use structs::db_settings::DbSetting; +pub use structs::db_settings::DbSettings; pub use structs::duplicate_indexes::DuplicateIndexes; pub use structs::extensions::Extensions; pub use structs::index_cache_hit::IndexCacheHit; @@ -24,7 +24,7 @@ pub use structs::null_indexes::NullIndexes; pub use structs::outliers::Outliers; pub use structs::records_rank::RecordsRank; pub use structs::seq_scans::SeqScans; -pub use structs::shared::{get_default_schema, Tabular}; +pub use structs::shared::{get_default_schema, Query, Tabular}; pub use structs::ssl_used::SslUsed; pub use structs::table_cache_hit::TableCacheHit; pub use structs::table_index_scans::TableIndexScans; @@ -52,63 +52,63 @@ pub fn render_table(items: Vec) { } pub async fn bloat() -> Result, PgExtrasError> { - let query = Query::read_file(Query::Bloat); + let query = Query::read_file(Bloat::FILE_NAME); get_rows(query).await } pub async fn blocking(limit: Option) -> Result, PgExtrasError> { let limit = limit.unwrap_or("10".to_string()); - let query = Query::read_file(Query::Blocking).replace("%{limit}", limit.as_str()); + let query = Query::read_file(Blocking::FILE_NAME).replace("%{limit}", limit.as_str()); get_rows(&query).await } pub async fn calls(limit: Option) -> Result, PgExtrasError> { let limit = limit.unwrap_or("10".to_string()); - let query = Query::read_file(Query::Calls).replace("%{limit}", limit.as_str()); + let query = Query::read_file(Calls::FILE_NAME).replace("%{limit}", limit.as_str()); get_rows(&query).await } pub async fn extensions() -> Result, PgExtrasError> { - let query = Query::read_file(Query::Extensions); + let query = Query::read_file(Extensions::FILE_NAME); get_rows(query).await } pub async fn table_cache_hit() -> Result, PgExtrasError> { - let query = Query::read_file(Query::TableCacheHit); + let query = Query::read_file(TableCacheHit::FILE_NAME); get_rows(query).await } pub async fn tables(schema: Option) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); - let query = Query::read_file(Query::Tables).replace("%{schema}", &schema_name); + let query = Query::read_file(Tables::FILE_NAME).replace("%{schema}", &schema_name); get_rows(&query).await } pub async fn index_cache_hit(schema: Option) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); - let query = Query::read_file(Query::IndexCacheHit).replace("%{schema}", &schema_name); + let query = Query::read_file(IndexCacheHit::FILE_NAME).replace("%{schema}", &schema_name); get_rows(&query).await } pub async fn indexes() -> Result, PgExtrasError> { - let query = Query::read_file(Query::Indexes); + let query = Query::read_file(Indexes::FILE_NAME); get_rows(query).await } pub async fn index_size() -> Result, PgExtrasError> { - let query = Query::read_file(Query::IndexSize); + let query = Query::read_file(IndexSize::FILE_NAME); get_rows(query).await } pub async fn index_usage(schema: Option) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); - let query = Query::read_file(Query::IndexUsage).replace("%{schema}", &schema_name); + let query = Query::read_file(IndexUsage::FILE_NAME).replace("%{schema}", &schema_name); get_rows(&query).await } pub async fn index_scans(schema: Option) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); - let query = Query::read_file(Query::IndexScans).replace("%{schema}", &schema_name); + let query = Query::read_file(IndexScans::FILE_NAME).replace("%{schema}", &schema_name); get_rows(&query).await } @@ -116,45 +116,45 @@ pub async fn null_indexes( min_relation_size_mb: Option, ) -> Result, PgExtrasError> { let min_relation_size_mb = min_relation_size_mb.unwrap_or("0".to_string()); - let query = Query::read_file(Query::NullIndexes) + let query = Query::read_file(NullIndexes::FILE_NAME) .replace("%{min_relation_size_mb}", &min_relation_size_mb); get_rows(&query).await } pub async fn locks() -> Result, PgExtrasError> { - let query = Query::read_file(Query::Locks); + let query = Query::read_file(Locks::FILE_NAME); get_rows(query).await } pub async fn all_locks() -> Result, PgExtrasError> { - let query = Query::read_file(Query::AllLocks); + let query = Query::read_file(AllLocks::FILE_NAME); get_rows(query).await } pub async fn long_running_queries() -> Result, PgExtrasError> { - let query = Query::read_file(Query::LongRunningQueries); + let query = Query::read_file(LongRunningQueries::FILE_NAME); get_rows(query).await } pub async fn mandelbrot() -> Result, PgExtrasError> { - let query = Query::read_file(Query::Mandelbrot); + let query = Query::read_file(Mandelbrot::FILE_NAME); get_rows(query).await } pub async fn outliers() -> Result, PgExtrasError> { - let query = Query::read_file(Query::Outliers); + let query = Query::read_file(Outliers::FILE_NAME); get_rows(query).await } pub async fn records_rank(schema: Option) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); - let query = Query::read_file(Query::RecordsRank).replace("%{schema}", schema_name.as_str()); + let query = Query::read_file(RecordsRank::FILE_NAME).replace("%{schema}", schema_name.as_str()); get_rows(&query).await } pub async fn seq_scans(schema: Option) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); - let query = Query::read_file(Query::SeqScans).replace("%{schema}", schema_name.as_str()); + let query = Query::read_file(SeqScans::FILE_NAME).replace("%{schema}", schema_name.as_str()); get_rows(&query).await } @@ -162,7 +162,8 @@ pub async fn table_index_scans( schema: Option, ) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); - let query = Query::read_file(Query::TableIndexScans).replace("%{schema}", schema_name.as_str()); + let query = + Query::read_file(TableIndexScans::FILE_NAME).replace("%{schema}", schema_name.as_str()); get_rows(&query).await } @@ -171,148 +172,73 @@ pub async fn table_indexes_size( ) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); let query = - Query::read_file(Query::TableIndexesSize).replace("%{schema}", schema_name.as_str()); + Query::read_file(TableIndexesSize::FILE_NAME).replace("%{schema}", schema_name.as_str()); get_rows(&query).await } pub async fn table_size() -> Result, PgExtrasError> { - let query = Query::read_file(Query::TableSize); + let query = Query::read_file(TableSize::FILE_NAME); get_rows(query).await } pub async fn total_index_size() -> Result, PgExtrasError> { - let query = Query::read_file(Query::TotalIndexSize); + let query = Query::read_file(TotalIndexSize::FILE_NAME); get_rows(query).await } pub async fn total_table_size() -> Result, PgExtrasError> { - let query = Query::read_file(Query::TotalTableSize); + let query = Query::read_file(TotalTableSize::FILE_NAME); get_rows(query).await } pub async fn unused_indexes(schema: Option) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); - let query = Query::read_file(Query::UnusedIndexes).replace("%{schema}", schema_name.as_str()); + let query = + Query::read_file(UnusedIndexes::FILE_NAME).replace("%{schema}", schema_name.as_str()); get_rows(&query).await } pub async fn duplicate_indexes() -> Result, PgExtrasError> { - let query = Query::read_file(Query::DuplicateIndexes); + let query = Query::read_file(DuplicateIndexes::FILE_NAME); get_rows(query).await } pub async fn vacuum_stats() -> Result, PgExtrasError> { - let query = Query::read_file(Query::VacuumStats); + let query = Query::read_file(VacuumStats::FILE_NAME); get_rows(query).await } pub async fn buffercache_stats() -> Result, PgExtrasError> { - let query = Query::read_file(Query::BuffercacheStats); + let query = Query::read_file(BuffercacheStats::FILE_NAME); get_rows(query).await } pub async fn buffercache_usage() -> Result, PgExtrasError> { - let query = Query::read_file(Query::BuffercacheUsage); + let query = Query::read_file(BuffercacheUsage::FILE_NAME); get_rows(query).await } pub async fn ssl_used() -> Result, PgExtrasError> { - let query = Query::read_file(Query::SslUsed); + let query = Query::read_file(SslUsed::FILE_NAME); get_rows(query).await } pub async fn connections() -> Result, PgExtrasError> { - let query = Query::read_file(Query::Connections); + let query = Query::read_file(Connections::FILE_NAME); get_rows(query).await } pub async fn cache_hit(schema: Option) -> Result, PgExtrasError> { let schema_name = schema.unwrap_or(get_default_schema()); - let query = Query::read_file(Query::CacheHit).replace("%{schema}", schema_name.as_str()); + let query = Query::read_file(CacheHit::FILE_NAME).replace("%{schema}", schema_name.as_str()); get_rows(&query).await } -pub async fn db_settings() -> Result, PgExtrasError> { - let query = Query::read_file(Query::DbSettings); +pub async fn db_settings() -> Result, PgExtrasError> { + let query = Query::read_file(DbSettings::FILE_NAME); get_rows(query).await } -enum Query { - CacheHit, - Bloat, - Blocking, - Calls, - Extensions, - TableCacheHit, - Tables, - IndexCacheHit, - DbSettings, - Indexes, - IndexSize, - IndexUsage, - IndexScans, - NullIndexes, - Locks, - AllLocks, - LongRunningQueries, - Mandelbrot, - Outliers, - RecordsRank, - SeqScans, - TableIndexScans, - TableIndexesSize, - TableSize, - TotalIndexSize, - TotalTableSize, - UnusedIndexes, - DuplicateIndexes, - VacuumStats, - BuffercacheStats, - BuffercacheUsage, - SslUsed, - Connections, -} - -impl Query { - pub fn read_file(query: Query) -> &'static str { - match query { - Query::CacheHit => include_str!("queries/cache_hit.sql"), - Query::DbSettings => include_str!("queries/db_settings.sql"), - Query::BuffercacheStats => include_str!("queries/buffercache_stats.sql"), - Query::BuffercacheUsage => include_str!("queries/buffercache_usage.sql"), - Query::SslUsed => include_str!("queries/ssl_used.sql"), - Query::Connections => include_str!("queries/connections.sql"), - Query::Bloat => include_str!("queries/bloat.sql"), - Query::Blocking => include_str!("queries/blocking.sql"), - Query::Calls => include_str!("queries/calls.sql"), - Query::Extensions => include_str!("queries/extensions.sql"), - Query::TableCacheHit => include_str!("queries/table_cache_hit.sql"), - Query::Tables => include_str!("queries/tables.sql"), - Query::IndexCacheHit => include_str!("queries/index_cache_hit.sql"), - Query::Indexes => include_str!("queries/indexes.sql"), - Query::IndexSize => include_str!("queries/index_size.sql"), - Query::IndexUsage => include_str!("queries/index_usage.sql"), - Query::IndexScans => include_str!("queries/index_scans.sql"), - Query::NullIndexes => include_str!("queries/null_indexes.sql"), - Query::Locks => include_str!("queries/locks.sql"), - Query::AllLocks => include_str!("queries/all_locks.sql"), - Query::LongRunningQueries => include_str!("queries/long_running_queries.sql"), - Query::Mandelbrot => include_str!("queries/mandelbrot.sql"), - Query::Outliers => include_str!("queries/outliers.sql"), - Query::RecordsRank => include_str!("queries/records_rank.sql"), - Query::SeqScans => include_str!("queries/seq_scans.sql"), - Query::TableIndexScans => include_str!("queries/table_index_scans.sql"), - Query::TableIndexesSize => include_str!("queries/table_indexes_size.sql"), - Query::TableSize => include_str!("queries/table_size.sql"), - Query::TotalIndexSize => include_str!("queries/total_index_size.sql"), - Query::TotalTableSize => include_str!("queries/total_table_size.sql"), - Query::UnusedIndexes => include_str!("queries/unused_indexes.sql"), - Query::DuplicateIndexes => include_str!("queries/duplicate_indexes.sql"), - Query::VacuumStats => include_str!("queries/vacuum_stats.sql"), - } - } -} - #[derive(Error, Debug)] #[non_exhaustive] pub enum PgExtrasError { diff --git a/src/structs/all_locks.rs b/src/structs/all_locks.rs index aa7d84c..ee50f9b 100644 --- a/src/structs/all_locks.rs +++ b/src/structs/all_locks.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{get_default_interval, Tabular}; +use crate::structs::shared::{get_default_interval, Query, Tabular}; use sqlx::postgres::{types::PgInterval, PgRow}; use sqlx::Row; @@ -15,6 +15,8 @@ pub struct AllLocks { } impl Tabular for AllLocks { + const FILE_NAME: Query = Query::AllLocks; + fn new(row: &PgRow) -> Self { Self { pid: row.try_get("pid").unwrap_or_default(), diff --git a/src/structs/bloat.rs b/src/structs/bloat.rs index 3ad0441..9e55e73 100644 --- a/src/structs/bloat.rs +++ b/src/structs/bloat.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::types::BigDecimal; use sqlx::Row; @@ -13,6 +13,8 @@ pub struct Bloat { } impl Tabular for Bloat { + const FILE_NAME: Query = Query::Bloat; + fn new(row: &PgRow) -> Self { Self { typefield: row.try_get("type").unwrap_or_default(), diff --git a/src/structs/blocking.rs b/src/structs/blocking.rs index 1e53866..a4de073 100644 --- a/src/structs/blocking.rs +++ b/src/structs/blocking.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{get_default_interval, Tabular}; +use crate::structs::shared::{get_default_interval, Query, Tabular}; use sqlx::postgres::{types::PgInterval, PgRow}; use sqlx::Row; @@ -15,6 +15,8 @@ pub struct Blocking { } impl Tabular for Blocking { + const FILE_NAME: Query = Query::Blocking; + fn new(row: &PgRow) -> Self { Self { blocked_pid: row.try_get("blocked_pid").unwrap_or_default(), diff --git a/src/structs/buffercache_stats.rs b/src/structs/buffercache_stats.rs index af7e9f8..a0d44fd 100644 --- a/src/structs/buffercache_stats.rs +++ b/src/structs/buffercache_stats.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::Row; @@ -11,6 +11,8 @@ pub struct BuffercacheStats { } impl Tabular for BuffercacheStats { + const FILE_NAME: Query = Query::BuffercacheStats; + fn new(row: &PgRow) -> Self { Self { relname: row.try_get("relname").unwrap_or_default(), diff --git a/src/structs/buffercache_usage.rs b/src/structs/buffercache_usage.rs index 26102b0..7da6310 100644 --- a/src/structs/buffercache_usage.rs +++ b/src/structs/buffercache_usage.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::Row; @@ -9,6 +9,8 @@ pub struct BuffercacheUsage { } impl Tabular for BuffercacheUsage { + const FILE_NAME: Query = Query::BuffercacheUsage; + fn new(row: &PgRow) -> Self { Self { relname: row.try_get("relname").unwrap_or_default(), diff --git a/src/structs/cache_hit.rs b/src/structs/cache_hit.rs index 891fff2..ad7d9f3 100644 --- a/src/structs/cache_hit.rs +++ b/src/structs/cache_hit.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::types::BigDecimal; use sqlx::Row; @@ -10,6 +10,8 @@ pub struct CacheHit { } impl Tabular for CacheHit { + const FILE_NAME: Query = Query::CacheHit; + fn new(row: &PgRow) -> Self { Self { name: row.try_get("name").unwrap_or_default(), diff --git a/src/structs/calls.rs b/src/structs/calls.rs index 75891fb..c5a033e 100644 --- a/src/structs/calls.rs +++ b/src/structs/calls.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{get_default_interval, Tabular}; +use crate::structs::shared::{get_default_interval, Query, Tabular}; use sqlx::postgres::{types::PgInterval, PgRow}; use sqlx::Row; @@ -12,6 +12,8 @@ pub struct Calls { } impl Tabular for Calls { + const FILE_NAME: Query = Query::Calls; + fn new(row: &PgRow) -> Self { Self { qry: row.try_get("qry").unwrap_or_default(), diff --git a/src/structs/connections.rs b/src/structs/connections.rs index 84521d3..01280af 100644 --- a/src/structs/connections.rs +++ b/src/structs/connections.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::Row; @@ -10,6 +10,8 @@ pub struct Connections { } impl Tabular for Connections { + const FILE_NAME: Query = Query::Connections; + fn new(row: &PgRow) -> Self { Self { username: row.try_get("username").unwrap_or_default(), diff --git a/src/structs/db_settings.rs b/src/structs/db_settings.rs index 43c52f2..5127987 100644 --- a/src/structs/db_settings.rs +++ b/src/structs/db_settings.rs @@ -1,16 +1,18 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::Row; #[derive(Debug, Clone)] -pub struct DbSetting { +pub struct DbSettings { name: String, setting: String, unit: String, short_desc: String, } -impl Tabular for DbSetting { +impl Tabular for DbSettings { + const FILE_NAME: Query = Query::DbSettings; + fn new(row: &PgRow) -> Self { Self { name: row.try_get("name").unwrap_or_default(), diff --git a/src/structs/duplicate_indexes.rs b/src/structs/duplicate_indexes.rs index d57bb85..a9401ad 100644 --- a/src/structs/duplicate_indexes.rs +++ b/src/structs/duplicate_indexes.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::Row; @@ -12,6 +12,8 @@ pub struct DuplicateIndexes { } impl Tabular for DuplicateIndexes { + const FILE_NAME: Query = Query::DuplicateIndexes; + fn new(row: &PgRow) -> Self { Self { size: row.try_get("size").unwrap_or_default(), diff --git a/src/structs/extensions.rs b/src/structs/extensions.rs index 3bd1eec..26525b3 100644 --- a/src/structs/extensions.rs +++ b/src/structs/extensions.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::Row; @@ -11,6 +11,8 @@ pub struct Extensions { } impl Tabular for Extensions { + const FILE_NAME: Query = Query::Extensions; + fn new(row: &PgRow) -> Self { Self { name: row.try_get("name").unwrap_or_default(), diff --git a/src/structs/index_cache_hit.rs b/src/structs/index_cache_hit.rs index be8d59e..b7c3a39 100644 --- a/src/structs/index_cache_hit.rs +++ b/src/structs/index_cache_hit.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::Row; @@ -12,6 +12,8 @@ pub struct IndexCacheHit { } impl Tabular for IndexCacheHit { + const FILE_NAME: Query = Query::IndexCacheHit; + fn new(row: &PgRow) -> Self { Self { name: row.try_get("name").unwrap_or_default(), diff --git a/src/structs/index_scans.rs b/src/structs/index_scans.rs index f8bbce1..3fc5950 100644 --- a/src/structs/index_scans.rs +++ b/src/structs/index_scans.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::Row; @@ -12,6 +12,8 @@ pub struct IndexScans { } impl Tabular for IndexScans { + const FILE_NAME: Query = Query::IndexScans; + fn new(row: &PgRow) -> Self { Self { schemaname: row.try_get("schemaname").unwrap_or_default(), diff --git a/src/structs/index_size.rs b/src/structs/index_size.rs index 0cfd1b1..4ffa32b 100644 --- a/src/structs/index_size.rs +++ b/src/structs/index_size.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::Row; @@ -10,6 +10,8 @@ pub struct IndexSize { } impl Tabular for IndexSize { + const FILE_NAME: Query = Query::IndexSize; + fn new(row: &PgRow) -> Self { Self { name: row.try_get("name").unwrap_or_default(), diff --git a/src/structs/index_usage.rs b/src/structs/index_usage.rs index 5ec3769..c4fc0df 100644 --- a/src/structs/index_usage.rs +++ b/src/structs/index_usage.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::Row; @@ -10,6 +10,8 @@ pub struct IndexUsage { } impl Tabular for IndexUsage { + const FILE_NAME: Query = Query::IndexUsage; + fn new(row: &PgRow) -> Self { Self { relname: row.try_get("relname").unwrap_or_default(), diff --git a/src/structs/indexes.rs b/src/structs/indexes.rs index 85df282..93c6bd5 100644 --- a/src/structs/indexes.rs +++ b/src/structs/indexes.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::Row; @@ -11,6 +11,8 @@ pub struct Indexes { } impl Tabular for Indexes { + const FILE_NAME: Query = Query::Indexes; + fn new(row: &PgRow) -> Self { Self { schemaname: row.try_get("schemaname").unwrap_or_default(), diff --git a/src/structs/locks.rs b/src/structs/locks.rs index a290a04..1143e08 100644 --- a/src/structs/locks.rs +++ b/src/structs/locks.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{get_default_interval, Tabular}; +use crate::structs::shared::{get_default_interval, Query, Tabular}; use sqlx::postgres::{types::PgInterval, PgRow}; use sqlx::Row; @@ -15,6 +15,8 @@ pub struct Locks { } impl Tabular for Locks { + const FILE_NAME: Query = Query::Locks; + fn new(row: &PgRow) -> Self { Self { pid: row.try_get("pid").unwrap_or_default(), diff --git a/src/structs/long_running_queries.rs b/src/structs/long_running_queries.rs index 9fa6ee8..9bec66a 100644 --- a/src/structs/long_running_queries.rs +++ b/src/structs/long_running_queries.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{get_default_interval, Tabular}; +use crate::structs::shared::{get_default_interval, Query, Tabular}; use sqlx::postgres::{types::PgInterval, PgRow}; use sqlx::Row; @@ -10,6 +10,8 @@ pub struct LongRunningQueries { } impl Tabular for LongRunningQueries { + const FILE_NAME: Query = Query::LongRunningQueries; + fn new(row: &PgRow) -> Self { Self { pid: row.try_get("pid").unwrap_or_default(), diff --git a/src/structs/mandelbrot.rs b/src/structs/mandelbrot.rs index 4991a3f..49d8582 100644 --- a/src/structs/mandelbrot.rs +++ b/src/structs/mandelbrot.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::Row; @@ -8,6 +8,8 @@ pub struct Mandelbrot { } impl Tabular for Mandelbrot { + const FILE_NAME: Query = Query::Mandelbrot; + fn new(row: &PgRow) -> Self { Self { array_to_string: row.try_get("array_to_string").unwrap_or_default(), diff --git a/src/structs/null_indexes.rs b/src/structs/null_indexes.rs index d0ae628..1961370 100644 --- a/src/structs/null_indexes.rs +++ b/src/structs/null_indexes.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::types::Oid; use sqlx::postgres::PgRow; use sqlx::Row; @@ -17,6 +17,8 @@ pub struct NullIndexes { } impl Tabular for NullIndexes { + const FILE_NAME: Query = Query::NullIndexes; + fn new(row: &PgRow) -> Self { Self { oid: row.try_get("oid").unwrap_or_default(), diff --git a/src/structs/outliers.rs b/src/structs/outliers.rs index 227de2b..caf7b39 100644 --- a/src/structs/outliers.rs +++ b/src/structs/outliers.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::{get_default_interval, Tabular}; +use crate::structs::shared::{get_default_interval, Query, Tabular}; use sqlx::postgres::{types::PgInterval, PgRow}; use sqlx::Row; @@ -12,6 +12,8 @@ pub struct Outliers { } impl Tabular for Outliers { + const FILE_NAME: Query = Query::Outliers; + fn new(row: &PgRow) -> Self { Self { total_exec_time: row diff --git a/src/structs/records_rank.rs b/src/structs/records_rank.rs index 4551790..b7b1525 100644 --- a/src/structs/records_rank.rs +++ b/src/structs/records_rank.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::Row; @@ -9,6 +9,8 @@ pub struct RecordsRank { } impl Tabular for RecordsRank { + const FILE_NAME: Query = Query::RecordsRank; + fn new(row: &PgRow) -> Self { Self { name: row.try_get("name").unwrap_or_default(), diff --git a/src/structs/seq_scans.rs b/src/structs/seq_scans.rs index f51aa29..feabace 100644 --- a/src/structs/seq_scans.rs +++ b/src/structs/seq_scans.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::Row; @@ -9,6 +9,8 @@ pub struct SeqScans { } impl Tabular for SeqScans { + const FILE_NAME: Query = Query::SeqScans; + fn new(row: &PgRow) -> Self { Self { name: row.try_get("name").unwrap_or_default(), diff --git a/src/structs/shared.rs b/src/structs/shared.rs index da5bdf0..6f210e8 100644 --- a/src/structs/shared.rs +++ b/src/structs/shared.rs @@ -2,11 +2,88 @@ use sqlx::postgres::{types::PgInterval, PgRow}; use std::env; pub trait Tabular { + const FILE_NAME: Query; fn new(row: &PgRow) -> Self; fn to_row(&self) -> prettytable::Row; fn headers() -> prettytable::Row; } +pub enum Query { + CacheHit, + Bloat, + Blocking, + Calls, + Extensions, + TableCacheHit, + Tables, + IndexCacheHit, + DbSettings, + Indexes, + IndexSize, + IndexUsage, + IndexScans, + NullIndexes, + Locks, + AllLocks, + LongRunningQueries, + Mandelbrot, + Outliers, + RecordsRank, + SeqScans, + TableIndexScans, + TableIndexesSize, + TableSize, + TotalIndexSize, + TotalTableSize, + UnusedIndexes, + DuplicateIndexes, + VacuumStats, + BuffercacheStats, + BuffercacheUsage, + SslUsed, + Connections, +} + +impl Query { + pub fn read_file(query: Query) -> &'static str { + match query { + Self::CacheHit => include_str!("../queries/cache_hit.sql"), + Self::DbSettings => include_str!("../queries/db_settings.sql"), + Self::BuffercacheStats => include_str!("../queries/buffercache_stats.sql"), + Self::BuffercacheUsage => include_str!("../queries/buffercache_usage.sql"), + Self::SslUsed => include_str!("../queries/ssl_used.sql"), + Self::Connections => include_str!("../queries/connections.sql"), + Self::Bloat => include_str!("../queries/bloat.sql"), + Self::Blocking => include_str!("../queries/blocking.sql"), + Self::Calls => include_str!("../queries/calls.sql"), + Self::Extensions => include_str!("../queries/extensions.sql"), + Self::TableCacheHit => include_str!("../queries/table_cache_hit.sql"), + Self::Tables => include_str!("../queries/tables.sql"), + Self::IndexCacheHit => include_str!("../queries/index_cache_hit.sql"), + Self::Indexes => include_str!("../queries/indexes.sql"), + Self::IndexSize => include_str!("../queries/index_size.sql"), + Self::IndexUsage => include_str!("../queries/index_usage.sql"), + Self::IndexScans => include_str!("../queries/index_scans.sql"), + Self::NullIndexes => include_str!("../queries/null_indexes.sql"), + Self::Locks => include_str!("../queries/locks.sql"), + Self::AllLocks => include_str!("../queries/all_locks.sql"), + Self::LongRunningQueries => include_str!("../queries/long_running_queries.sql"), + Self::Mandelbrot => include_str!("../queries/mandelbrot.sql"), + Self::Outliers => include_str!("../queries/outliers.sql"), + Self::RecordsRank => include_str!("../queries/records_rank.sql"), + Self::SeqScans => include_str!("../queries/seq_scans.sql"), + Self::TableIndexScans => include_str!("../queries/table_index_scans.sql"), + Self::TableIndexesSize => include_str!("../queries/table_indexes_size.sql"), + Self::TableSize => include_str!("../queries/table_size.sql"), + Self::TotalIndexSize => include_str!("../queries/total_index_size.sql"), + Self::TotalTableSize => include_str!("../queries/total_table_size.sql"), + Self::UnusedIndexes => include_str!("../queries/unused_indexes.sql"), + Self::DuplicateIndexes => include_str!("../queries/duplicate_indexes.sql"), + Self::VacuumStats => include_str!("../queries/vacuum_stats.sql"), + } + } +} + pub fn get_default_interval() -> PgInterval { PgInterval { microseconds: 0, diff --git a/src/structs/ssl_used.rs b/src/structs/ssl_used.rs index e803ef9..15edb3a 100644 --- a/src/structs/ssl_used.rs +++ b/src/structs/ssl_used.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::Row; @@ -8,6 +8,8 @@ pub struct SslUsed { } impl Tabular for SslUsed { + const FILE_NAME: Query = Query::SslUsed; + fn new(row: &PgRow) -> Self { Self { ssl_used: row.try_get("ssl_used").unwrap_or(false), diff --git a/src/structs/table_cache_hit.rs b/src/structs/table_cache_hit.rs index 7ad09bb..b6451d1 100644 --- a/src/structs/table_cache_hit.rs +++ b/src/structs/table_cache_hit.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::Row; @@ -12,6 +12,8 @@ pub struct TableCacheHit { } impl Tabular for TableCacheHit { + const FILE_NAME: Query = Query::TableCacheHit; + fn new(row: &PgRow) -> Self { Self { name: row.try_get("name").unwrap_or_default(), diff --git a/src/structs/table_index_scans.rs b/src/structs/table_index_scans.rs index 980bbd1..5ad5753 100644 --- a/src/structs/table_index_scans.rs +++ b/src/structs/table_index_scans.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::Row; @@ -9,6 +9,8 @@ pub struct TableIndexScans { } impl Tabular for TableIndexScans { + const FILE_NAME: Query = Query::TableIndexScans; + fn new(row: &PgRow) -> Self { Self { name: row.try_get("name").unwrap_or_default(), diff --git a/src/structs/table_indexes_size.rs b/src/structs/table_indexes_size.rs index 34c8d3a..90cae28 100644 --- a/src/structs/table_indexes_size.rs +++ b/src/structs/table_indexes_size.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::Row; @@ -9,6 +9,8 @@ pub struct TableIndexesSize { } impl Tabular for TableIndexesSize { + const FILE_NAME: Query = Query::TableIndexesSize; + fn new(row: &PgRow) -> Self { Self { table: row.try_get("table").unwrap_or_default(), diff --git a/src/structs/table_size.rs b/src/structs/table_size.rs index 5f21f0f..d3c29a7 100644 --- a/src/structs/table_size.rs +++ b/src/structs/table_size.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::Row; @@ -10,6 +10,8 @@ pub struct TableSize { } impl Tabular for TableSize { + const FILE_NAME: Query = Query::TableSize; + fn new(row: &PgRow) -> Self { Self { name: row.try_get("name").unwrap_or_default(), diff --git a/src/structs/tables.rs b/src/structs/tables.rs index dd30bd9..6c6c8e4 100644 --- a/src/structs/tables.rs +++ b/src/structs/tables.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::Row; @@ -9,6 +9,8 @@ pub struct Tables { } impl Tabular for Tables { + const FILE_NAME: Query = Query::Tables; + fn new(row: &PgRow) -> Self { Self { tablename: row.try_get("tablename").unwrap_or_default(), diff --git a/src/structs/total_index_size.rs b/src/structs/total_index_size.rs index aea58d8..3ef2684 100644 --- a/src/structs/total_index_size.rs +++ b/src/structs/total_index_size.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::Row; @@ -8,6 +8,8 @@ pub struct TotalIndexSize { } impl Tabular for TotalIndexSize { + const FILE_NAME: Query = Query::TotalIndexSize; + fn new(row: &PgRow) -> Self { Self { size: row.try_get("size").unwrap_or_default(), diff --git a/src/structs/total_table_size.rs b/src/structs/total_table_size.rs index 25b4979..29aa593 100644 --- a/src/structs/total_table_size.rs +++ b/src/structs/total_table_size.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::Row; @@ -9,6 +9,8 @@ pub struct TotalTableSize { } impl Tabular for TotalTableSize { + const FILE_NAME: Query = Query::TotalTableSize; + fn new(row: &PgRow) -> Self { Self { name: row.try_get("name").unwrap_or_default(), diff --git a/src/structs/unused_indexes.rs b/src/structs/unused_indexes.rs index 9591b96..7507020 100644 --- a/src/structs/unused_indexes.rs +++ b/src/structs/unused_indexes.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::Row; @@ -11,6 +11,8 @@ pub struct UnusedIndexes { } impl Tabular for UnusedIndexes { + const FILE_NAME: Query = Query::UnusedIndexes; + fn new(row: &PgRow) -> Self { Self { table: row.try_get("table").unwrap_or_default(), diff --git a/src/structs/vacuum_stats.rs b/src/structs/vacuum_stats.rs index caf437c..8a521b7 100644 --- a/src/structs/vacuum_stats.rs +++ b/src/structs/vacuum_stats.rs @@ -1,4 +1,4 @@ -use crate::structs::shared::Tabular; +use crate::structs::shared::{Query, Tabular}; use sqlx::postgres::PgRow; use sqlx::Row; @@ -15,6 +15,8 @@ pub struct VacuumStats { } impl Tabular for VacuumStats { + const FILE_NAME: Query = Query::VacuumStats; + fn new(row: &PgRow) -> Self { Self { schema: row.try_get("schema").unwrap_or_default(),