use crate::ExecutionError;
use crate::node::{ContextMode, NodeId};
use std::sync::Arc;
use std::time::Duration;
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum GraphEvent {
GraphStart {
node_count: usize,
node_map: Vec<(NodeId, &'static str)>,
},
GraphComplete {
nodes_executed: usize,
duration: Duration,
},
GraphFailure {
error: ExecutionError,
},
SystemStart {
node_id: NodeId,
node_name: &'static str,
},
SystemComplete {
node_id: NodeId,
node_name: &'static str,
duration: Duration,
},
SystemError {
node_id: NodeId,
node_name: &'static str,
error: Arc<str>,
},
DecisionStart {
node_id: NodeId,
node_name: &'static str,
},
DecisionComplete {
node_id: NodeId,
node_name: &'static str,
selected_branch: &'static str,
},
SwitchStart {
node_id: NodeId,
node_name: &'static str,
case_count: usize,
has_default: bool,
},
SwitchComplete {
node_id: NodeId,
node_name: &'static str,
selected_case: &'static str,
used_default: bool,
},
LoopStart {
node_id: NodeId,
node_name: &'static str,
max_iterations: usize,
},
LoopIteration {
node_id: NodeId,
node_name: &'static str,
iteration: usize,
},
LoopEnd {
node_id: NodeId,
node_name: &'static str,
iterations: usize,
nodes_executed: usize,
duration: Duration,
},
ParallelStart {
node_id: NodeId,
node_name: &'static str,
branch_count: usize,
},
ParallelComplete {
node_id: NodeId,
node_name: &'static str,
branch_count: usize,
total_nodes_executed: usize,
duration: Duration,
},
ScopeStart {
node_id: NodeId,
node_name: &'static str,
context_mode: ContextMode,
inner_node_count: usize,
},
ScopeComplete {
node_id: NodeId,
node_name: &'static str,
context_mode: ContextMode,
nodes_executed: usize,
duration: Duration,
},
}
impl GraphEvent {
#[must_use]
pub fn schedule_name(&self) -> &'static str {
match self {
GraphEvent::GraphStart { .. } => "OnGraphStart",
GraphEvent::GraphComplete { .. } => "OnGraphComplete",
GraphEvent::GraphFailure { .. } => "OnGraphFailure",
GraphEvent::SystemStart { .. } => "OnSystemStart",
GraphEvent::SystemComplete { .. } => "OnSystemComplete",
GraphEvent::SystemError { .. } => "OnSystemError",
GraphEvent::DecisionStart { .. } => "OnDecisionStart",
GraphEvent::DecisionComplete { .. } => "OnDecisionComplete",
GraphEvent::SwitchStart { .. } => "OnSwitchStart",
GraphEvent::SwitchComplete { .. } => "OnSwitchComplete",
GraphEvent::LoopStart { .. } => "OnLoopStart",
GraphEvent::LoopIteration { .. } => "OnLoopIteration",
GraphEvent::LoopEnd { .. } => "OnLoopEnd",
GraphEvent::ParallelStart { .. } => "OnParallelStart",
GraphEvent::ParallelComplete { .. } => "OnParallelComplete",
GraphEvent::ScopeStart { .. } => "OnScopeStart",
GraphEvent::ScopeComplete { .. } => "OnScopeComplete",
}
}
#[must_use]
pub fn node_id(&self) -> Option<NodeId> {
match self {
GraphEvent::GraphStart { .. }
| GraphEvent::GraphComplete { .. }
| GraphEvent::GraphFailure { .. } => None,
GraphEvent::SystemStart { node_id, .. }
| GraphEvent::SystemComplete { node_id, .. }
| GraphEvent::SystemError { node_id, .. }
| GraphEvent::DecisionStart { node_id, .. }
| GraphEvent::DecisionComplete { node_id, .. }
| GraphEvent::SwitchStart { node_id, .. }
| GraphEvent::SwitchComplete { node_id, .. }
| GraphEvent::LoopStart { node_id, .. }
| GraphEvent::LoopIteration { node_id, .. }
| GraphEvent::LoopEnd { node_id, .. }
| GraphEvent::ParallelStart { node_id, .. }
| GraphEvent::ParallelComplete { node_id, .. }
| GraphEvent::ScopeStart { node_id, .. }
| GraphEvent::ScopeComplete { node_id, .. } => Some(node_id.clone()),
}
}
}
impl std::fmt::Display for GraphEvent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
GraphEvent::GraphStart {
node_count,
node_map,
} => {
write!(f, "GraphStart(nodes: {})", node_count)?;
if !node_map.is_empty() {
writeln!(f, ", node_map:")?;
for (id, name) in node_map {
writeln!(f, " {:20} {}", name, id)?;
}
};
Ok(())
}
GraphEvent::GraphComplete {
nodes_executed,
duration,
} => {
write!(
f,
"GraphComplete(executed: {}, duration: {:?})",
nodes_executed, duration
)
}
GraphEvent::GraphFailure { error } => {
write!(f, "GraphFailure(error: {})", error)
}
GraphEvent::SystemStart {
node_id,
node_name: system_name,
} => {
write!(f, "SystemStart({} @ {:?})", system_name, node_id)
}
GraphEvent::SystemComplete {
node_id,
node_name: system_name,
duration,
} => {
write!(
f,
"SystemComplete({} @ {:?}, duration: {:?})",
system_name, node_id, duration
)
}
GraphEvent::SystemError {
node_id,
node_name: system_name,
error,
} => {
write!(
f,
"SystemError({} @ {:?}, error: {})",
system_name, node_id, error
)
}
GraphEvent::DecisionStart { node_id, node_name } => {
write!(f, "DecisionStart({} @ {:?})", node_name, node_id)
}
GraphEvent::DecisionComplete {
node_id,
node_name,
selected_branch,
} => {
write!(
f,
"DecisionComplete({} @ {:?}, branch: {})",
node_name, node_id, selected_branch
)
}
GraphEvent::SwitchStart {
node_id,
node_name,
case_count,
has_default,
} => {
write!(
f,
"SwitchStart({} @ {:?}, cases: {}, default: {})",
node_name, node_id, case_count, has_default
)
}
GraphEvent::SwitchComplete {
node_id,
node_name,
selected_case,
used_default,
} => {
write!(
f,
"SwitchComplete({} @ {:?}, case: {}, used_default: {})",
node_name, node_id, selected_case, used_default
)
}
GraphEvent::LoopStart {
node_id,
node_name: loop_name,
max_iterations,
} => {
write!(
f,
"LoopStart({} @ {:?}, max_iterations: {})",
loop_name, node_id, max_iterations
)
}
GraphEvent::LoopIteration {
node_id,
node_name: loop_name,
iteration,
} => {
write!(
f,
"LoopIteration({} @ {:?}, iteration: {})",
loop_name, node_id, iteration
)
}
GraphEvent::LoopEnd {
node_id,
node_name: loop_name,
iterations,
nodes_executed,
duration,
} => {
write!(
f,
"LoopEnd({} @ {:?}, iterations: {}, executed: {}, duration: {:?})",
loop_name, node_id, iterations, nodes_executed, duration
)
}
GraphEvent::ParallelStart {
node_id,
node_name,
branch_count,
} => {
write!(
f,
"ParallelStart({} @ {:?}, branches: {})",
node_name, node_id, branch_count
)
}
GraphEvent::ParallelComplete {
node_id,
node_name,
branch_count,
total_nodes_executed,
duration,
} => {
write!(
f,
"ParallelComplete({} @ {:?}, branches: {}, executed: {}, duration: {:?})",
node_name, node_id, branch_count, total_nodes_executed, duration
)
}
GraphEvent::ScopeStart {
node_id,
node_name,
context_mode,
inner_node_count,
} => {
write!(
f,
"ScopeStart({} @ {:?}, mode: {}, inner_nodes: {})",
node_name, node_id, context_mode, inner_node_count
)
}
GraphEvent::ScopeComplete {
node_id,
node_name,
context_mode,
nodes_executed,
duration,
} => {
write!(
f,
"ScopeComplete({} @ {:?}, mode: {}, executed: {}, duration: {:?})",
node_name, node_id, context_mode, nodes_executed, duration
)
}
}
}
}