aboutsummaryrefslogtreecommitdiff
path: root/src/core/components/stats.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/components/stats.rs')
-rw-r--r--src/core/components/stats.rs57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/core/components/stats.rs b/src/core/components/stats.rs
new file mode 100644
index 0000000..356d458
--- /dev/null
+++ b/src/core/components/stats.rs
@@ -0,0 +1,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)
+}