use drasi_core::models::{
Element, ElementMetadata, ElementPropertyMap, ElementReference, ElementValue,
};
use std::sync::Arc;
use super::component_graph_source::COMPONENT_GRAPH_SOURCE_ID;
use crate::channels::ComponentStatus;
pub(crate) fn make_node(element_id: &str, labels: &[&str], props: ElementPropertyMap) -> Element {
Element::Node {
metadata: ElementMetadata {
reference: ElementReference::new(COMPONENT_GRAPH_SOURCE_ID, element_id),
labels: labels
.iter()
.map(|l| Arc::from(*l))
.collect::<Vec<_>>()
.into(),
effective_from: now_ms(),
},
properties: props,
}
}
pub(crate) fn make_relation(
element_id: &str,
labels: &[&str],
in_node_id: &str,
out_node_id: &str,
props: ElementPropertyMap,
) -> Element {
Element::Relation {
metadata: ElementMetadata {
reference: ElementReference::new(COMPONENT_GRAPH_SOURCE_ID, element_id),
labels: labels
.iter()
.map(|l| Arc::from(*l))
.collect::<Vec<_>>()
.into(),
effective_from: now_ms(),
},
in_node: ElementReference::new(COMPONENT_GRAPH_SOURCE_ID, in_node_id),
out_node: ElementReference::new(COMPONENT_GRAPH_SOURCE_ID, out_node_id),
properties: props,
}
}
pub(crate) fn make_delete_metadata(element_id: &str, labels: &[&str]) -> ElementMetadata {
ElementMetadata {
reference: ElementReference::new(COMPONENT_GRAPH_SOURCE_ID, element_id),
labels: labels
.iter()
.map(|l| Arc::from(*l))
.collect::<Vec<_>>()
.into(),
effective_from: now_ms(),
}
}
pub(crate) fn now_ms() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as u64
}
pub(crate) fn status_str(status: &ComponentStatus) -> &'static str {
match status {
ComponentStatus::Added => "Added",
ComponentStatus::Stopped => "Stopped",
ComponentStatus::Starting => "Starting",
ComponentStatus::Running => "Running",
ComponentStatus::Stopping => "Stopping",
ComponentStatus::Removed => "Removed",
ComponentStatus::Reconfiguring => "Reconfiguring",
ComponentStatus::Error => "Error",
}
}
pub(crate) fn build_props(pairs: &[(&str, &str)]) -> ElementPropertyMap {
let mut props = ElementPropertyMap::new();
for (k, v) in pairs {
props.insert(k, ElementValue::String(Arc::from(*v)));
}
props
}