From 9342b4754032d273e5ae8bdea2142039cfc86f62 Mon Sep 17 00:00:00 2001 From: pawurb Date: Fri, 29 Dec 2023 18:08:22 +0100 Subject: [PATCH] Compile files to binary --- src/lib.rs | 87 +++++++++++++++++++++---------- src/structs/bloat.rs | 2 +- src/structs/buffercache_stats.rs | 2 +- src/structs/buffercache_usage.rs | 2 +- src/structs/cache_hit.rs | 2 +- src/structs/calls_legacy.rs | 1 - src/structs/connections.rs | 2 +- src/structs/db_settings.rs | 2 +- src/structs/duplicate_indexes.rs | 2 +- src/structs/extensions.rs | 2 +- src/structs/index_cache_hit.rs | 2 +- src/structs/index_scans.rs | 2 +- src/structs/index_size.rs | 2 +- src/structs/index_usage.rs | 2 +- src/structs/indexes.rs | 2 +- src/structs/mandelbrot.rs | 2 +- src/structs/mod.rs | 2 - src/structs/null_indexes.rs | 2 +- src/structs/outliers_legacy.rs | 1 - src/structs/records_rank.rs | 2 +- src/structs/seq_scans.rs | 2 +- src/structs/shared.rs | 13 ++++- src/structs/ssl_used.rs | 2 +- src/structs/table_cache_hit.rs | 2 +- src/structs/table_index_scans.rs | 2 +- src/structs/table_indexes_size.rs | 2 +- src/structs/table_size.rs | 2 +- src/structs/tables.rs | 2 +- src/structs/total_index_size.rs | 2 +- src/structs/total_table_size.rs | 2 +- src/structs/unused_indexes.rs | 2 +- src/structs/vacuum_stats.rs | 2 +- 32 files changed, 99 insertions(+), 59 deletions(-) delete mode 100644 src/structs/calls_legacy.rs delete mode 100644 src/structs/outliers_legacy.rs diff --git a/src/lib.rs b/src/lib.rs index c100539..4517157 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; @@ -52,7 +52,7 @@ pub fn render_table(items: Vec) { pub fn bloat() -> Vec { let query = read_file(Bloat::FILE_NAME); - get_rows(&query).iter().map(Bloat::new).collect() + get_rows(query).iter().map(Bloat::new).collect() } pub fn blocking(limit: Option) -> Vec { @@ -69,12 +69,12 @@ pub fn calls(limit: Option) -> Vec { pub fn extensions() -> Vec { let query = read_file(Extensions::FILE_NAME); - get_rows(&query).iter().map(Extensions::new).collect() + get_rows(query).iter().map(Extensions::new).collect() } pub fn table_cache_hit() -> Vec { let query = read_file(TableCacheHit::FILE_NAME); - get_rows(&query).iter().map(TableCacheHit::new).collect() + get_rows(query).iter().map(TableCacheHit::new).collect() } pub fn tables(schema: Option) -> Vec { @@ -91,12 +91,12 @@ pub fn index_cache_hit(schema: Option) -> Vec { pub fn indexes() -> Vec { let query = read_file(Indexes::FILE_NAME); - get_rows(&query).iter().map(Indexes::new).collect() + get_rows(query).iter().map(Indexes::new).collect() } pub fn index_size() -> Vec { let query = read_file(IndexSize::FILE_NAME); - get_rows(&query).iter().map(IndexSize::new).collect() + get_rows(query).iter().map(IndexSize::new).collect() } pub fn index_usage(schema: Option) -> Vec { @@ -120,17 +120,17 @@ pub fn null_indexes(min_relation_size_mb: Option) -> Vec { pub fn locks() -> Vec { let query = read_file(Locks::FILE_NAME); - get_rows(&query).iter().map(Locks::new).collect() + get_rows(query).iter().map(Locks::new).collect() } pub fn all_locks() -> Vec { let query = read_file(AllLocks::FILE_NAME); - get_rows(&query).iter().map(AllLocks::new).collect() + get_rows(query).iter().map(AllLocks::new).collect() } pub fn long_running_queries() -> Vec { let query = read_file(LongRunningQueries::FILE_NAME); - get_rows(&query) + get_rows(query) .iter() .map(LongRunningQueries::new) .collect() @@ -138,12 +138,12 @@ pub fn long_running_queries() -> Vec { pub fn mandelbrot() -> Vec { let query = read_file(Mandelbrot::FILE_NAME); - get_rows(&query).iter().map(Mandelbrot::new).collect() + get_rows(query).iter().map(Mandelbrot::new).collect() } pub fn outliers() -> Vec { let query = read_file(Outliers::FILE_NAME); - get_rows(&query).iter().map(Outliers::new).collect() + get_rows(query).iter().map(Outliers::new).collect() } pub fn records_rank(schema: Option) -> Vec { @@ -172,17 +172,17 @@ pub fn table_indexes_size(schema: Option) -> Vec { pub fn table_size() -> Vec { let query = read_file(TableSize::FILE_NAME); - get_rows(&query).iter().map(TableSize::new).collect() + get_rows(query).iter().map(TableSize::new).collect() } pub fn total_index_size() -> Vec { let query = read_file(TotalIndexSize::FILE_NAME); - get_rows(&query).iter().map(TotalIndexSize::new).collect() + get_rows(query).iter().map(TotalIndexSize::new).collect() } pub fn total_table_size() -> Vec { let query = read_file(TotalTableSize::FILE_NAME); - get_rows(&query).iter().map(TotalTableSize::new).collect() + get_rows(query).iter().map(TotalTableSize::new).collect() } pub fn unused_indexes(schema: Option) -> Vec { @@ -193,32 +193,32 @@ pub fn unused_indexes(schema: Option) -> Vec { pub fn duplicate_indexes() -> Vec { let query = read_file(DuplicateIndexes::FILE_NAME); - get_rows(&query).iter().map(DuplicateIndexes::new).collect() + get_rows(query).iter().map(DuplicateIndexes::new).collect() } pub fn vacuum_stats() -> Vec { let query = read_file(VacuumStats::FILE_NAME); - get_rows(&query).iter().map(VacuumStats::new).collect() + get_rows(query).iter().map(VacuumStats::new).collect() } pub fn buffercache_stats() -> Vec { let query = read_file(BuffercacheStats::FILE_NAME); - get_rows(&query).iter().map(BuffercacheStats::new).collect() + get_rows(query).iter().map(BuffercacheStats::new).collect() } pub fn buffercache_usage() -> Vec { let query = read_file(BuffercacheUsage::FILE_NAME); - get_rows(&query).iter().map(BuffercacheUsage::new).collect() + get_rows(query).iter().map(BuffercacheUsage::new).collect() } pub fn ssl_used() -> Vec { let query = read_file(SslUsed::FILE_NAME); - get_rows(&query).iter().map(SslUsed::new).collect() + get_rows(query).iter().map(SslUsed::new).collect() } pub fn connections() -> Vec { let query = read_file(Connections::FILE_NAME); - get_rows(&query).iter().map(Connections::new).collect() + get_rows(query).iter().map(Connections::new).collect() } pub fn cache_hit(schema: Option) -> Vec { @@ -229,12 +229,45 @@ pub fn cache_hit(schema: Option) -> Vec { pub fn db_settings() -> Vec { let query = read_file("db_settings"); - 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)) + get_rows(query).iter().map(DbSetting::new).collect() +} + +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 {