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 { 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) }