aboutsummaryrefslogtreecommitdiff
path: root/src/core/data/counters.rs
blob: 485a5907ad982828fb76aa7203ee5d33ab9cdd5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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))
}