use crate::{
events::ConsensusEventBus, scope::ConsensusScope, service::ConsensusService,
session::ConsensusState, signing::ConsensusSignatureScheme, storage::ConsensusStorage,
};
#[derive(Debug, Clone)]
pub struct ConsensusStats {
pub total_sessions: usize,
pub active_sessions: usize,
pub failed_sessions: usize,
pub consensus_reached: usize,
}
impl<Scope, Storage, Event, Signer> ConsensusService<Scope, Storage, Event, Signer>
where
Scope: ConsensusScope,
Storage: ConsensusStorage<Scope>,
Event: ConsensusEventBus<Scope>,
Signer: ConsensusSignatureScheme,
{
pub async fn get_scope_stats(&self, scope: &Scope) -> ConsensusStats {
self.list_scope_sessions(scope)
.await
.map(|scope_sessions| {
let total_sessions = scope_sessions.len();
let active_sessions = scope_sessions.iter().filter(|s| s.is_active()).count();
let consensus_reached = scope_sessions
.iter()
.filter(|s| matches!(s.state, ConsensusState::ConsensusReached(_)))
.count();
let failed_sessions = scope_sessions
.iter()
.filter(|s| matches!(s.state, ConsensusState::Failed))
.count();
ConsensusStats {
total_sessions,
active_sessions,
consensus_reached,
failed_sessions,
}
})
.unwrap_or(ConsensusStats {
total_sessions: 0,
active_sessions: 0,
consensus_reached: 0,
failed_sessions: 0,
})
}
}