use std::collections::{HashMap, HashSet};
use std::time::Duration;
use id_effect::{Cause, Exit, HNil};
use id_effect_axum::EffectExecutor;
use id_effect_node::control::TreeNodeView;
use id_effect_process::{CallError, Pid, SupervisorHandle};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct SupervisorsView {
pub roots: Vec<TreeNodeView>,
pub unreachable: Vec<UnreachableSupervisor>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct UnreachableSupervisor {
pub id: String,
pub pid: String,
pub error: String,
}
type Children = Vec<(String, Option<Pid>)>;
fn call_error(cause: Cause<CallError>) -> String {
match cause {
Cause::Fail(e) => format!("{e:?}"),
other => format!("{other:?}"),
}
}
pub(crate) async fn snapshot(
supervisors: &[(String, SupervisorHandle)],
timeout: Duration,
) -> SupervisorsView {
let mut answers: Vec<Option<Children>> = Vec::with_capacity(supervisors.len());
let mut unreachable = Vec::new();
for (id, handle) in supervisors {
let call = handle.which_children::<HNil>(timeout);
match EffectExecutor::global().run_effect(call).await {
Exit::Success(children) => answers.push(Some(children)),
Exit::Failure(cause) => {
answers.push(None);
unreachable.push(UnreachableSupervisor {
id: id.clone(),
pid: handle.pid().to_string(),
error: call_error(cause),
});
}
}
}
SupervisorsView {
roots: nest(supervisors, &answers),
unreachable,
}
}
fn nest(
supervisors: &[(String, SupervisorHandle)],
answers: &[Option<Children>],
) -> Vec<TreeNodeView> {
let by_pid: HashMap<&Pid, usize> = supervisors
.iter()
.enumerate()
.filter(|(i, _)| answers[*i].is_some())
.map(|(i, (_, handle))| (handle.pid(), i))
.collect();
let nested: HashSet<usize> = answers
.iter()
.flatten()
.flatten()
.filter_map(|(_, pid)| pid.as_ref().and_then(|p| by_pid.get(p).copied()))
.collect();
let tree = Tree {
supervisors,
answers,
by_pid,
};
(0..supervisors.len())
.filter(|i| answers[*i].is_some() && !nested.contains(i))
.map(|i| tree.node(i, supervisors[i].0.clone(), &mut HashSet::new()))
.collect()
}
struct Tree<'a> {
supervisors: &'a [(String, SupervisorHandle)],
answers: &'a [Option<Children>],
by_pid: HashMap<&'a Pid, usize>,
}
impl Tree<'_> {
fn node(&self, i: usize, id: String, path: &mut HashSet<usize>) -> TreeNodeView {
path.insert(i);
let children = self.answers[i]
.iter()
.flatten()
.map(|(child, pid)| {
let sup = pid.as_ref().and_then(|p| self.by_pid.get(p).copied());
match sup {
Some(j) if !path.contains(&j) => self.node(j, child.clone(), path),
_ => TreeNodeView {
id: child.clone(),
pid: pid.as_ref().map(ToString::to_string),
children: Vec::new(),
},
}
})
.collect();
path.remove(&i);
TreeNodeView {
id,
pid: Some(self.supervisors[i].1.pid().to_string()),
children,
}
}
}