// api/summary.rs — Dashboard summary server function // // Returns the count of each main entity in a single server round-trip. // The home page uses this to display a quick-glance dashboard without // loading the full list of each entity. use leptos::prelude::*; use serde::{Deserialize, Serialize}; // Counts for every top-level entity shown on the dashboard. // Add a field here when a new entity type is introduced. #[derive(Clone, Debug, Serialize, Deserialize, Default)] pub struct Summary { pub network_count: i64, pub host_count: i64, pub application_count: i64, } #[server] pub async fn get_summary() -> Result { use sqlx::AnyPool; use leptos::prelude::use_context; let pool = use_context::() .ok_or_else(|| ServerFnError::new("Database pool not found in context"))?; // Three lightweight COUNT queries — no full table scans on the payload side. // sqlx returns COUNT(*) as i64 for both SQLite and PostgreSQL. let network_count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM networks") .fetch_one(&pool) .await .map_err(|e| ServerFnError::new(e.to_string()))?; let host_count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM hosts") .fetch_one(&pool) .await .map_err(|e| ServerFnError::new(e.to_string()))?; let application_count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM applications") .fetch_one(&pool) .await .map_err(|e| ServerFnError::new(e.to_string()))?; Ok(Summary { network_count, host_count, application_count }) }