From 8323fdd73272a2882781aba3c499ba0be3dff2a6 Mon Sep 17 00:00:00 2001 From: UMTS at Teleco Date: Sat, 13 Dec 2025 02:51:15 +0100 Subject: committing to insanity --- src/core/data/counters.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/core/data/counters.rs (limited to 'src/core/data/counters.rs') diff --git a/src/core/data/counters.rs b/src/core/data/counters.rs new file mode 100644 index 0000000..485a590 --- /dev/null +++ b/src/core/data/counters.rs @@ -0,0 +1,43 @@ +use crate::api::ApiClient; +use anyhow::Result; +use serde_json::Value; + +/// Generic counter function - can count anything from any table with any conditions +/// +/// # Examples +/// ``` +/// // Count all assets +/// count_entities(api, "assets", None)?; +/// +/// // Count available assets +/// count_entities(api, "assets", Some(json!({"lending_status": "Available"})))?; +/// +/// // Count with multiple conditions +/// count_entities(api, "assets", Some(json!({ +/// "lendable": true, +/// "lending_status": "Available" +/// })))?; +/// ``` +pub fn count_entities( + api_client: &ApiClient, + table: &str, + where_conditions: Option, +) -> Result { + log::debug!("Counting {} with conditions: {:?}", table, where_conditions); + let response = api_client.count(table, where_conditions)?; + log::debug!( + "Count response: success={}, data={:?}", + response.success, + response.data + ); + + // Check for database timeout errors + if !response.success { + if crate::api::ApiClient::is_database_timeout_error(&response.error) { + log::warn!("Database timeout detected while counting {}", table); + } + anyhow::bail!("API error: {:?}", response.error); + } + + Ok(response.data.unwrap_or(0)) +} -- cgit v1.2.3-70-g09d2