aboutsummaryrefslogtreecommitdiff
path: root/src/core/data/counters.rs
diff options
context:
space:
mode:
authorUMTS at Teleco <crt@teleco.ch>2025-12-13 02:51:15 +0100
committerUMTS at Teleco <crt@teleco.ch>2025-12-13 02:51:15 +0100
commit8323fdd73272a2882781aba3c499ba0be3dff2a6 (patch)
treeffbf86473933e69cfaeef30d5c6ea7e5b494856c /src/core/data/counters.rs
committing to insanityHEADmaster
Diffstat (limited to 'src/core/data/counters.rs')
-rw-r--r--src/core/data/counters.rs43
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))
+}