spacedb-console 0.7.2

SpaceDB operator console — the read-model behind the developer/ops dashboard. Turns raw fleet, access, audit and settlement observations into the four boards an operator runs a SpaceDB business from: fleet health, alerts, economics, and access. Native-testable core; a Dioxus/WASM shell binds it to a UI. Phase 1 SpaceDB Mission, M8.
Documentation
//! The dashboard — the four boards assembled from one snapshot of observations.
//!
//! A Dioxus/WASM shell binds [`Dashboard`] fields to components; [`render_text`]
//! produces the same view as an operator snapshot, so the model is demonstrable
//! (and tested) with no browser.

use std::fmt::Write as _;

use serde::{Deserialize, Serialize};

use crate::access::{rollup_access, AccessOverview};
use crate::alerts::{derive_alerts, Alert, AlertThresholds, Severity};
use crate::economics::{rollup_economics, Economics};
use crate::fleet::{assess_fleet, FleetHealth, HealthStatus};
use crate::observe::{
    AgentBudgetObs, AuditObs, CapabilityObs, HomeObs, LagObs, SettledObs, ShardObs, StrongObs,
};

/// One snapshot of everything the console observes.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct Observations {
    pub homes: Vec<HomeObs>,
    pub shards: Vec<ShardObs>,
    pub strong: Vec<StrongObs>,
    pub lags: Vec<LagObs>,
    pub capabilities: Vec<CapabilityObs>,
    pub audit: Vec<AuditObs>,
    pub settled: Vec<SettledObs>,
    pub budgets: Vec<AgentBudgetObs>,
    pub unsettled_claims: usize,
}

/// Operator-tunable thresholds for the whole dashboard.
#[derive(Clone, Copy, Debug)]
pub struct Config {
    pub now: u64,
    pub alerts: AlertThresholds,
    pub expiry_window: u64,
}

impl Config {
    pub fn at(now: u64) -> Self {
        Self {
            now,
            alerts: AlertThresholds::default(),
            expiry_window: 7 * 86_400, // a week
        }
    }
}

/// The four boards.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Dashboard {
    pub health: FleetHealth,
    pub alerts: Vec<Alert>,
    pub economics: Economics,
    pub access: AccessOverview,
}

impl Dashboard {
    /// Compute the whole dashboard from a snapshot.
    pub fn assemble(obs: &Observations, config: &Config) -> Dashboard {
        let health = assess_fleet(
            &obs.homes,
            &obs.shards,
            &obs.strong,
            &obs.lags,
            config.alerts.lag_ops_warn,
        );
        let alerts = derive_alerts(
            &obs.homes,
            &obs.shards,
            &obs.strong,
            &obs.lags,
            &obs.budgets,
            &config.alerts,
        );
        let economics = rollup_economics(
            &obs.settled,
            &obs.budgets,
            obs.unsettled_claims,
            config.alerts.budget_low_pct,
        );
        let access = rollup_access(&obs.capabilities, &obs.audit, config.now, config.expiry_window);
        Dashboard {
            health,
            alerts,
            economics,
            access,
        }
    }

    /// The number of alerts at or above `Critical` — i.e. the page-me count.
    pub fn critical_count(&self) -> usize {
        self.alerts
            .iter()
            .filter(|a| a.severity == Severity::Critical)
            .count()
    }

    /// A plain-text operator snapshot (what the Dioxus shell renders graphically).
    pub fn render_text(&self) -> String {
        let h = &self.health;
        let badge = match h.status {
            HealthStatus::Green => "GREEN",
            HealthStatus::Amber => "AMBER",
            HealthStatus::Red => "RED",
        };
        // Written straight into `out`: `push_str(&format!(..))` built and
        // dropped a String per line. Writing to a String cannot fail.
        let mut out = String::new();
        let _ = writeln!(out, "SpaceDB Operator Console — fleet {badge}");
        let _ = writeln!(
            out,
            "Fleet:    {}/{} homes online · {} shards ({} under-replicated, {} at-risk, {} lost, {} over-replicated) · {} GiB",
            h.homes_online,
            h.homes_total,
            h.shards_total,
            h.shards_under_replicated,
            h.shards_at_risk,
            h.shards_lost,
            h.shards_over_replicated,
            h.bytes_stored / (1 << 30),
        );
        let _ = writeln!(
            out,
            "Strong:   {}/{} collections with quorum · worst lag {} ops",
            h.strong_collections - h.strong_without_quorum,
            h.strong_collections,
            h.worst_lag_ops,
        );
        let _ = writeln!(
            out,
            "Access:   {} human + {} agent caps · {} expiring soon · {} revoked · {} recent denials",
            self.access.active_human,
            self.access.active_agent,
            self.access.expiring_soon,
            self.access.revoked,
            self.access.denied_recent,
        );
        let _ = writeln!(
            out,
            "Economics: {} micro-$MATA revenue (S {} / C {} / T {}) · {} unsettled claims",
            self.economics.revenue_micro_mata,
            self.economics.per_rail.storage,
            self.economics.per_rail.compute,
            self.economics.per_rail.transit,
            self.economics.unsettled_claims,
        );
        let _ = writeln!(out, "Alerts:   {} ({} critical)", self.alerts.len(), self.critical_count());
        for a in &self.alerts {
            let _ = writeln!(out, "  [{:?}] {:?} {} — {}", a.severity, a.kind, a.subject, a.detail);
        }
        out
    }
}