diff --git a/src/lib.rs b/src/lib.rs index c100539..c99d98b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ use postgres::{Client, NoTls, Row}; -use std::{env, fs}; -mod structs; +use std::env; +pub mod structs; use structs::all_locks::AllLocks; use structs::bloat::Bloat; use structs::blocking::Blocking; @@ -232,9 +232,42 @@ pub fn db_settings() -> Vec { get_rows(&query).iter().map(DbSetting::new).collect() } -fn read_file(filename: &str) -> String { - fs::read_to_string(format!("src/queries/{}.sql", filename)) - .unwrap_or_else(|_| panic!("Error reading the '{}' file", filename)) +pub fn read_file(filename: &str) -> &'static str { + match filename { + "cache_hit" => include_str!("queries/cache_hit.sql"), + "bloat" => include_str!("queries/bloat.sql"), + "blocking" => include_str!("queries/blocking.sql"), + "calls" => include_str!("queries/calls.sql"), + "extensions" => include_str!("queries/extensions.sql"), + "table_cache_hit" => include_str!("queries/table_cache_hit.sql"), + "tables" => include_str!("queries/tables.sql"), + "index_cache_hit" => include_str!("queries/index_cache_hit.sql"), + "indexes" => include_str!("queries/indexes.sql"), + "index_size" => include_str!("queries/index_size.sql"), + "index_usage" => include_str!("queries/index_usage.sql"), + "index_scans" => include_str!("queries/index_scans.sql"), + "null_indexes" => include_str!("queries/null_indexes.sql"), + "locks" => include_str!("queries/locks.sql"), + "all_locks" => include_str!("queries/all_locks.sql"), + "long_running_queries" => include_str!("queries/long_running_queries.sql"), + "mandelbrot" => include_str!("queries/mandelbrot.sql"), + "outliers" => include_str!("queries/outliers.sql"), + "records_rank" => include_str!("queries/records_rank.sql"), + "seq_scans" => include_str!("queries/seq_scans.sql"), + "table_index_scans" => include_str!("queries/table_index_scans.sql"), + "table_indexes_size" => include_str!("queries/table_indexes_size.sql"), + "table_size" => include_str!("queries/table_size.sql"), + "total_index_size" => include_str!("queries/total_index_size.sql"), + "total_table_size" => include_str!("queries/total_table_size.sql"), + "unused_indexes" => include_str!("queries/unused_indexes.sql"), + "duplicate_indexes" => include_str!("queries/duplicate_indexes.sql"), + "vacuum_stats" => include_str!("queries/vacuum_stats.sql"), + "buffercache_stats" => include_str!("queries/buffercache_stats.sql"), + "buffercache_usage" => include_str!("queries/buffercache_usage.sql"), + "ssl_used" => include_str!("queries/ssl_used.sql"), + "connections" => include_str!("queries/connections.sql"), + _ => panic!("Unknown file: {}", filename), + } } fn get_rows(query: &str) -> Vec { diff --git a/src/structs/bloat.rs b/src/structs/bloat.rs index 967aa58..8929786 100644 --- a/src/structs/bloat.rs +++ b/src/structs/bloat.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; use rust_decimal::prelude::*; use rust_decimal_macros::dec; diff --git a/src/structs/buffercache_stats.rs b/src/structs/buffercache_stats.rs index 065054b..af981d4 100644 --- a/src/structs/buffercache_stats.rs +++ b/src/structs/buffercache_stats.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; use rust_decimal::prelude::*; use rust_decimal_macros::dec; diff --git a/src/structs/buffercache_usage.rs b/src/structs/buffercache_usage.rs index a08b632..09a2127 100644 --- a/src/structs/buffercache_usage.rs +++ b/src/structs/buffercache_usage.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; pub struct BuffercacheUsage { diff --git a/src/structs/cache_hit.rs b/src/structs/cache_hit.rs index 159f966..1e1b55f 100644 --- a/src/structs/cache_hit.rs +++ b/src/structs/cache_hit.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; use rust_decimal::prelude::*; use rust_decimal_macros::dec; diff --git a/src/structs/calls_legacy.rs b/src/structs/calls_legacy.rs deleted file mode 100644 index 8b13789..0000000 --- a/src/structs/calls_legacy.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/structs/connections.rs b/src/structs/connections.rs index 50597ce..5d9a194 100644 --- a/src/structs/connections.rs +++ b/src/structs/connections.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; pub struct Connections { diff --git a/src/structs/db_settings.rs b/src/structs/db_settings.rs index 2c30b78..6c68942 100644 --- a/src/structs/db_settings.rs +++ b/src/structs/db_settings.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; pub struct DbSetting { diff --git a/src/structs/duplicate_indexes.rs b/src/structs/duplicate_indexes.rs index 419ee1a..a4c6dce 100644 --- a/src/structs/duplicate_indexes.rs +++ b/src/structs/duplicate_indexes.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; pub struct DuplicateIndexes { diff --git a/src/structs/extensions.rs b/src/structs/extensions.rs index 21994d1..82f10e0 100644 --- a/src/structs/extensions.rs +++ b/src/structs/extensions.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; pub struct Extensions { diff --git a/src/structs/index_cache_hit.rs b/src/structs/index_cache_hit.rs index c6220d4..f2a76a4 100644 --- a/src/structs/index_cache_hit.rs +++ b/src/structs/index_cache_hit.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; pub struct IndexCacheHit { diff --git a/src/structs/index_scans.rs b/src/structs/index_scans.rs index e9679ae..863b7ac 100644 --- a/src/structs/index_scans.rs +++ b/src/structs/index_scans.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; pub struct IndexScans { diff --git a/src/structs/index_size.rs b/src/structs/index_size.rs index 9eed5ec..dcb3e7f 100644 --- a/src/structs/index_size.rs +++ b/src/structs/index_size.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; pub struct IndexSize { diff --git a/src/structs/index_usage.rs b/src/structs/index_usage.rs index 784fbcf..d07b1cf 100644 --- a/src/structs/index_usage.rs +++ b/src/structs/index_usage.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; pub struct IndexUsage { diff --git a/src/structs/indexes.rs b/src/structs/indexes.rs index a8e0da6..e690a4b 100644 --- a/src/structs/indexes.rs +++ b/src/structs/indexes.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; pub struct Indexes { diff --git a/src/structs/mandelbrot.rs b/src/structs/mandelbrot.rs index 7872b8e..5c3d2a9 100644 --- a/src/structs/mandelbrot.rs +++ b/src/structs/mandelbrot.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; pub struct Mandelbrot { diff --git a/src/structs/mod.rs b/src/structs/mod.rs index 2499941..d464d1c 100644 --- a/src/structs/mod.rs +++ b/src/structs/mod.rs @@ -5,7 +5,6 @@ pub mod buffercache_stats; pub mod buffercache_usage; pub mod cache_hit; pub mod calls; -pub mod calls_legacy; pub mod connections; pub mod db_settings; pub mod duplicate_indexes; @@ -20,7 +19,6 @@ pub mod long_running_queries; pub mod mandelbrot; pub mod null_indexes; pub mod outliers; -pub mod outliers_legacy; pub mod records_rank; pub mod seq_scans; pub mod shared; diff --git a/src/structs/null_indexes.rs b/src/structs/null_indexes.rs index 4410c24..431c296 100644 --- a/src/structs/null_indexes.rs +++ b/src/structs/null_indexes.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; pub struct NullIndexes { diff --git a/src/structs/outliers_legacy.rs b/src/structs/outliers_legacy.rs deleted file mode 100644 index 8b13789..0000000 --- a/src/structs/outliers_legacy.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/structs/records_rank.rs b/src/structs/records_rank.rs index a37bbff..da1d40c 100644 --- a/src/structs/records_rank.rs +++ b/src/structs/records_rank.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; pub struct RecordsRank { diff --git a/src/structs/seq_scans.rs b/src/structs/seq_scans.rs index 89fcadb..cf2ece2 100644 --- a/src/structs/seq_scans.rs +++ b/src/structs/seq_scans.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; pub struct SeqScans { diff --git a/src/structs/shared.rs b/src/structs/shared.rs index b7dea4d..dd75caf 100644 --- a/src/structs/shared.rs +++ b/src/structs/shared.rs @@ -1,5 +1,5 @@ use pg_interval::Interval; -use postgres::Row; +use postgres::{Client, NoTls, Row}; use std::env; pub trait Tabular { @@ -16,3 +16,14 @@ pub fn get_default_interval() -> Interval { pub fn get_default_schema() -> String { env::var("PG_EXTRAS_SCHEMA").unwrap_or("public".to_string()) } + +pub fn get_rows(query: &str) -> Vec { + connection() + .query(query, &[]) + .unwrap_or_else(|_| Vec::new()) +} + +fn connection() -> Client { + let database_url = env::var("DATABASE_URL").expect("$DATABASE_URL is not set"); + Client::connect(&database_url, NoTls).unwrap() +} diff --git a/src/structs/ssl_used.rs b/src/structs/ssl_used.rs index 6b3a039..4e5889f 100644 --- a/src/structs/ssl_used.rs +++ b/src/structs/ssl_used.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; pub struct SslUsed { diff --git a/src/structs/table_cache_hit.rs b/src/structs/table_cache_hit.rs index b86a303..5bf931c 100644 --- a/src/structs/table_cache_hit.rs +++ b/src/structs/table_cache_hit.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; pub struct TableCacheHit { diff --git a/src/structs/table_index_scans.rs b/src/structs/table_index_scans.rs index 2fb0bfa..e7301f9 100644 --- a/src/structs/table_index_scans.rs +++ b/src/structs/table_index_scans.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; pub struct TableIndexScans { diff --git a/src/structs/table_indexes_size.rs b/src/structs/table_indexes_size.rs index 53619bc..2226690 100644 --- a/src/structs/table_indexes_size.rs +++ b/src/structs/table_indexes_size.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; pub struct TableIndexesSize { diff --git a/src/structs/table_size.rs b/src/structs/table_size.rs index 5c35b38..e4ccca8 100644 --- a/src/structs/table_size.rs +++ b/src/structs/table_size.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; pub struct TableSize { diff --git a/src/structs/tables.rs b/src/structs/tables.rs index 5caa9e5..d7b2b4a 100644 --- a/src/structs/tables.rs +++ b/src/structs/tables.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; pub struct Tables { diff --git a/src/structs/total_index_size.rs b/src/structs/total_index_size.rs index 7b381b8..1c150cd 100644 --- a/src/structs/total_index_size.rs +++ b/src/structs/total_index_size.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; pub struct TotalIndexSize { diff --git a/src/structs/total_table_size.rs b/src/structs/total_table_size.rs index 99ef233..d0f3278 100644 --- a/src/structs/total_table_size.rs +++ b/src/structs/total_table_size.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; pub struct TotalTableSize { diff --git a/src/structs/unused_indexes.rs b/src/structs/unused_indexes.rs index b53d630..36610cf 100644 --- a/src/structs/unused_indexes.rs +++ b/src/structs/unused_indexes.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; pub struct UnusedIndexes { diff --git a/src/structs/vacuum_stats.rs b/src/structs/vacuum_stats.rs index f825dac..404a27e 100644 --- a/src/structs/vacuum_stats.rs +++ b/src/structs/vacuum_stats.rs @@ -1,4 +1,4 @@ -use crate::Tabular; +use crate::structs::shared::Tabular; use postgres::Row; pub struct VacuumStats {