diff options
| author | UMTS at Teleco <crt@teleco.ch> | 2025-12-13 02:51:15 +0100 |
|---|---|---|
| committer | UMTS at Teleco <crt@teleco.ch> | 2025-12-13 02:51:15 +0100 |
| commit | 8323fdd73272a2882781aba3c499ba0be3dff2a6 (patch) | |
| tree | ffbf86473933e69cfaeef30d5c6ea7e5b494856c /src/core/data/counters.rs | |
Diffstat (limited to 'src/core/data/counters.rs')
| -rw-r--r-- | src/core/data/counters.rs | 43 |
1 files changed, 43 insertions, 0 deletions
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<Value>, +) -> Result<i32> { + 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)) +} |
