From 9b910c3cbff5875dbe5515def4cc7c1a30e2707b Mon Sep 17 00:00:00 2001 From: grabowski Date: Sat, 17 Oct 2026 23:15:43 +0700 Subject: [PATCH] chore(rust): gap-fill progress lines; slow-statement warning at 30 s --- rust/Cargo.lock | 1 + rust/Cargo.toml | 1 + rust/collector/Cargo.toml | 1 + rust/collector/src/db.rs | 9 ++++++++- rust/collector/src/rid.rs | 18 ++++++++++++++++-- 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index f4ca375..e558f30 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -1163,6 +1163,7 @@ dependencies = [ "anyhow", "chrono", "clap", + "log", "reqwest", "serde", "serde_json", diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 0766fa4..612c2e9 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -15,6 +15,7 @@ repository = "https://git.b4l.co.th/B4L/Northern-Thailand-Ping-River-Monitor" anyhow = "1" chrono = { version = "0.4", default-features = false, features = ["std", "clock", "serde"] } clap = { version = "4", features = ["derive"] } +log = "0.4" # native-tls (OpenSSL on Linux, SChannel on Windows), not rustls: app.rid.go.th (dams) # offers only TLS 1.2 CBC suites (ECDHE-RSA-AES256-SHA384), which rustls refuses. reqwest = { version = "0.12", default-features = false, features = ["native-tls", "json"] } diff --git a/rust/collector/Cargo.toml b/rust/collector/Cargo.toml index f6bef2d..729af76 100644 --- a/rust/collector/Cargo.toml +++ b/rust/collector/Cargo.toml @@ -12,6 +12,7 @@ repository.workspace = true anyhow.workspace = true chrono.workspace = true clap.workspace = true +log.workspace = true reqwest.workspace = true serde.workspace = true serde_json.workspace = true diff --git a/rust/collector/src/db.rs b/rust/collector/src/db.rs index 4974ad6..90ccbfc 100644 --- a/rust/collector/src/db.rs +++ b/rust/collector/src/db.rs @@ -15,10 +15,17 @@ use sqlx::postgres::PgPoolOptions; use std::collections::{BTreeMap, HashMap, HashSet}; pub async fn connect(url: &str) -> Result { + use sqlx::ConnectOptions; + use std::str::FromStr; + // sqlx warns about any statement over 1 s; the whole-history gap scan + // legitimately takes several seconds on ~1M rows. + let options = sqlx::postgres::PgConnectOptions::from_str(url) + .context("invalid database URL")? + .log_slow_statements(log::LevelFilter::Warn, std::time::Duration::from_secs(30)); PgPoolOptions::new() .max_connections(4) .acquire_timeout(std::time::Duration::from_secs(30)) - .connect(url) + .connect_with(options) .await .context("connecting to PostgreSQL") } diff --git a/rust/collector/src/rid.rs b/rust/collector/src/rid.rs index ec64830..8c29a53 100644 --- a/rust/collector/src/rid.rs +++ b/rust/collector/src/rid.rs @@ -301,6 +301,7 @@ pub async fn fill_gaps( dates.len() ); let mut filled = 0; + let mut empty = 0; for (i, d) in dates.iter().enumerate() { if i > 0 { tokio::time::sleep(std::time::Duration::from_secs(1)).await; // be polite @@ -310,11 +311,24 @@ pub async fn fill_gaps( db::save_rid(pool, &readings, stations).await?; filled += readings.len(); } - Ok(_) => tracing::info!("gap fill {d}: RID has no data"), + Ok(_) => { + empty += 1; + tracing::debug!("gap fill {d}: RID has no data"); + } Err(e) => tracing::warn!("gap fill {d}: {e:#}"), } + if (i + 1) % 50 == 0 && i + 1 < dates.len() { + tracing::info!( + "gap fill: {}/{} reports, at {d}, {filled} readings re-saved", + i + 1, + dates.len() + ); + } } - tracing::info!("gap fill done: {filled} readings re-saved"); + tracing::info!( + "gap fill done: {filled} readings re-saved from {} report(s); {empty} had no data at RID", + dates.len() - empty + ); Ok(filled) }