aboutsummaryrefslogtreecommitdiff
path: root/src/core/components/stats.rs
blob: 356d4589f7e2b8bfb11fc022f26d238077b82a36 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use crate::api::ApiClient;
use crate::core::counters::count_entities;
use crate::models::DashboardStats;
use anyhow::Result;
use serde_json::json;

/// Fetch all dashboard statistics using the generic counter
pub fn fetch_dashboard_stats(api_client: &ApiClient) -> Result<DashboardStats> {
    log::debug!("Fetching dashboard statistics...");

    let mut stats = DashboardStats::default();

    // 1. Total Assets - count everything
    stats.total_assets = count_entities(api_client, "assets", None).unwrap_or_else(|e| {
        log::error!("Failed to count total assets: {}", e);
        0
    });

    // 2. Okay Items - assets with status "Good"
    stats.okay_items = count_entities(api_client, "assets", Some(json!({"status": "Good"})))
        .unwrap_or_else(|e| {
            log::error!("Failed to count okay items: {}", e);
            0
        });

    // 3. Attention Items - anything that needs attention
    // Count: Faulty, Missing, Attention status + Overdue lending status
    let faulty =
        count_entities(api_client, "assets", Some(json!({"status": "Faulty"}))).unwrap_or(0);

    let missing =
        count_entities(api_client, "assets", Some(json!({"status": "Missing"}))).unwrap_or(0);

    let attention_status =
        count_entities(api_client, "assets", Some(json!({"status": "Attention"}))).unwrap_or(0);

    let scrapped =
        count_entities(api_client, "assets", Some(json!({"status": "Scrapped"}))).unwrap_or(0);

    let overdue = count_entities(
        api_client,
        "assets",
        Some(json!({"lending_status": "Overdue"})),
    )
    .unwrap_or(0);

    stats.attention_items = faulty + missing + attention_status + scrapped + overdue;

    log::info!(
        "Dashboard stats: {} total, {} okay, {} need attention",
        stats.total_assets,
        stats.okay_items,
        stats.attention_items
    );

    Ok(stats)
}