use crate::aggregators::StateAggregator;
use crate::model::{NodeType, StateEdge, StateLayer, StateNode};
use async_trait::async_trait;
use mockforge_data::persona_lifecycle::PersonaLifecycle;
use std::collections::HashMap;
use std::sync::{Arc, RwLock as StdRwLock};
pub struct LifecycleAggregator {
lifecycles: Option<Arc<StdRwLock<HashMap<String, PersonaLifecycle>>>>,
}
impl LifecycleAggregator {
pub fn new(lifecycles: Option<Arc<StdRwLock<HashMap<String, PersonaLifecycle>>>>) -> Self {
Self { lifecycles }
}
}
#[async_trait]
impl StateAggregator for LifecycleAggregator {
async fn aggregate(&self) -> anyhow::Result<(Vec<StateNode>, Vec<StateEdge>)> {
let mut nodes = Vec::new();
let edges = Vec::new();
if let Some(ref lifecycles) = self.lifecycles {
let lifecycles = lifecycles.read().unwrap();
for (entity_id, lifecycle) in lifecycles.iter() {
let mut node = StateNode::new(
format!("lifecycle:{}", entity_id),
format!("Lifecycle: {}", entity_id),
NodeType::Entity,
StateLayer::Lifecycle,
);
node.set_state(format!("{:?}", lifecycle.current_state));
node.set_property(
"current_state".to_string(),
serde_json::json!(format!("{:?}", lifecycle.current_state)),
);
node.set_property("entity_id".to_string(), serde_json::json!(entity_id));
node.set_property(
"entered_at".to_string(),
serde_json::json!(lifecycle.state_entered_at.to_rfc3339()),
);
nodes.push(node);
}
}
Ok((nodes, edges))
}
fn layer(&self) -> StateLayer {
StateLayer::Lifecycle
}
}