From 4c828c968e39b88e745f7c7ee9650f56c74e38d7 Mon Sep 17 00:00:00 2001 From: prplV Date: Thu, 6 Mar 2025 17:52:57 +0300 Subject: [PATCH] export docs fix --- crates/api-grub/src/export.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/crates/api-grub/src/export.rs b/crates/api-grub/src/export.rs index d649134..9c1a5f9 100644 --- a/crates/api-grub/src/export.rs +++ b/crates/api-grub/src/export.rs @@ -38,6 +38,11 @@ pub struct Exporter { } impl Exporter { + /// Fills `deadpool_postgres::Config` object with values from ENV VARS: + /// - `DB_HOST` + /// - `DB_DBNAME` + /// - `DB_USER` + /// - `DB_PASSWORD` fn config_construct() -> Result { let mut cfg = Config::new(); cfg.host = Some(env::var("DB_HOST")?); @@ -46,6 +51,9 @@ impl Exporter { cfg.password = Some(env::var("DB_PASSWORD")?); Ok(cfg) } + /// Uses `deadpool_postgres::Config` object to create DB connections + /// pool to share between async tasks and to restrict a count of parallel + /// connections fn pool_construct() -> Option { return match Self::config_construct() { Ok(config) => { @@ -61,6 +69,7 @@ impl Exporter { }, } } + /// Checks if DB connections pool is empty #[allow(unused)] pub fn is_no_connection(&self) -> bool { self.pool.is_none() } pub fn init() -> Self { @@ -68,6 +77,9 @@ impl Exporter { pool : Self::pool_construct(), } } + /// Shares a connection `deadpool_postgres::Client as PgClient` + /// + /// Function awaits til the moment it can return `Option` #[allow(unused)] pub async fn get_connection_from_pool(&self) -> Option { if let Some(pool) = &self.pool { @@ -75,12 +87,15 @@ impl Exporter { } None } + /// Exports data in `&str` jsonb format to DB using connection from the pool #[allow(unused)] pub async fn export_data(client: PgClient, metrics: &str) -> Result<()> { let query = client.prepare_cached("INSERT INTO metrics (body) VALUES ($1);").await?; let _ = client.query(&query, &[&metrics]).await?; Ok(()) } + /// Exports metrics in `PrometheusMetrics` format to Exporter defined + /// as env var $EXORPTER_URL pub async fn export_metrics(metrics: PrometheusMetrics) -> Result { let url = env::var("EXPORTER_URL")?; @@ -92,6 +107,8 @@ impl Exporter { req?; Ok(metrics.get_bytes_len()) } + /// Exports metrics in `PrometheusMetricsExtended` format to Exporter defined + /// as env var $EXORPTER_URL pub async fn export_extended_metrics(metrics: PrometheusMetricsExtended) -> Result { let url = env::var("EXPORTER_URL")?; let req = Client::new() @@ -105,6 +122,7 @@ impl Exporter { } impl Drop for Exporter { + // Custom destructor to log deinitializing of the `Exporter` fn drop(&mut self) { info!("Deinitializng Exporter and DB connection pool ...") }