1use std::collections::BTreeMap;
2use std::sync::Arc;
3use std::time::Duration;
45use async_trait::async_trait;
6use fedimint_core::PeerId;
7use fedimint_core::core::ModuleKind;
8use fedimint_core::module::ApiAuth;
9use fedimint_core::module::audit::AuditSummary;
1011use crate::{DynServerModule, ServerModule};
1213pub type DynDashboardApi = Arc<dyn IDashboardApi + Send + Sync + 'static>;
1415/// Interface for guardian dashboard API in a running federation
16#[async_trait]
17pub trait IDashboardApi {
18/// Get the guardian's authentication details
19async fn auth(&self) -> ApiAuth;
2021/// Get the guardian ID
22async fn guardian_id(&self) -> PeerId;
2324/// Get a map of peer IDs to guardian names
25async fn guardian_names(&self) -> BTreeMap<PeerId, String>;
2627/// Get the federation name
28async fn federation_name(&self) -> String;
2930/// Get the current active session count
31async fn session_count(&self) -> usize;
3233/// The time it took to order our last proposal in the current session
34async fn consensus_ord_latency(&self) -> Option<Duration>;
3536/// Returns a map of peer ID to estimated round trip time
37async fn p2p_connection_status(&self) -> BTreeMap<PeerId, Option<Duration>>;
3839/// Get the federation invite code to share with users
40async fn federation_invite_code(&self) -> String;
4142/// Get the federation audit summary
43async fn federation_audit(&self) -> AuditSummary;
4445/// Get reference to a server module instance by module kind
46fn get_module_by_kind(&self, kind: ModuleKind) -> Option<&DynServerModule>;
4748/// Create a trait object
49fn into_dyn(self) -> DynDashboardApi
50where
51Self: Sized + Send + Sync + 'static,
52 {
53 Arc::new(self)
54 }
55}
5657/// Extension trait for IDashboardApi providing type-safe module access
58pub trait DashboardApiModuleExt {
59/// Get a typed reference to a server module instance by kind
60fn get_module<M: ServerModule + 'static>(&self) -> Option<&M>;
61}
6263impl DashboardApiModuleExt for DynDashboardApi {
64fn get_module<M: ServerModule + 'static>(&self) -> Option<&M> {
65self.get_module_by_kind(M::module_kind())?
66.as_any()
67 .downcast_ref::<M>()
68 }
69}