Home page now shows one clickable summary card per entity type (Networks, Hosts, Applications). Each card displays the total count fetched from the database via a single get_summary() server function, then navigates to the corresponding page on click. Counts are pre-rendered server-side via <Suspense> so the page is useful even before the WASM bundle loads. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
46 lines
1.6 KiB
Rust
46 lines
1.6 KiB
Rust
// 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<Summary, ServerFnError> {
|
|
use sqlx::AnyPool;
|
|
use leptos::prelude::use_context;
|
|
|
|
let pool = use_context::<AnyPool>()
|
|
.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 })
|
|
}
|