use std::collections::BTreeMap;
use crate::ids::ReactorId;
#[allow(unused_imports)] use super::{Agent, Reactor, Report, Run, RunError};
#[derive(Debug, Default)]
pub struct OrchestratorReport {
pub report: BTreeMap<ReactorId, Result<Report, RunError>>,
}
impl OrchestratorReport {
pub fn rejected(
&self,
) -> impl Iterator<Item = (ReactorId, crate::ids::AgentId, &serde_json::Value)>
{
self.report.iter().flat_map(|(&reactor, result)| {
result
.iter()
.flat_map(|report| &report.rejected)
.map(move |(&agent, state)| (reactor, agent, state))
})
}
}
impl IntoIterator for OrchestratorReport {
type Item = (ReactorId, Result<Report, RunError>);
type IntoIter = std::collections::btree_map::IntoIter<
ReactorId,
Result<Report, RunError>,
>;
fn into_iter(self) -> Self::IntoIter {
self.report.into_iter()
}
}
impl<'a> IntoIterator for &'a OrchestratorReport {
type Item = (&'a ReactorId, &'a Result<Report, RunError>);
type IntoIter = std::collections::btree_map::Iter<
'a,
ReactorId,
Result<Report, RunError>,
>;
fn into_iter(self) -> Self::IntoIter {
self.report.iter()
}
}
#[derive(Default)]
pub struct Orchestrator {
reactors: Vec<Box<dyn Run>>,
}
impl Orchestrator {
pub fn new() -> Self {
Self::default()
}
pub fn push(&mut self, reactor: impl Run + 'static) -> &mut Self {
self.reactors.push(Box::new(reactor));
self
}
pub async fn run(&mut self) -> OrchestratorReport {
let results = futures::future::join_all(
self.reactors
.iter_mut()
.map(|r| async { (r.id(), r.run().await) }),
)
.await;
OrchestratorReport {
report: results.into_iter().collect(),
}
}
}