clm_rust_core/layer2/
workflow_lineage.rs1use serde::{Deserialize, Serialize};
7use std::collections::HashMap;
8
9#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11pub struct LineageNode {
12 pub id: String,
13 #[serde(rename = "type")]
14 pub node_type: String,
15 pub label: String,
16 pub hash: String,
17 #[serde(default)]
18 pub metadata: HashMap<String, serde_json::Value>,
19}
20
21#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
23pub struct LineageEdge {
24 pub source: String,
25 pub target: String,
26 pub relation: String,
27}
28
29#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
31pub struct LineageDAG {
32 pub target_uri: String,
33 pub nodes: HashMap<String, LineageNode>,
34 pub edges: Vec<LineageEdge>,
35}
36
37#[derive(Debug, Default, Clone)]
39pub struct LineageEngine {
40 nodes: HashMap<String, LineageNode>,
41 edges: Vec<LineageEdge>,
42}
43
44impl LineageEngine {
45 pub fn new() -> Self {
46 Self::default()
47 }
48
49 pub fn add_node(&mut self, node: LineageNode) {
50 self.nodes.insert(node.id.clone(), node);
51 }
52
53 pub fn add_edge(&mut self, source: &str, target: &str, relation: &str) {
54 self.edges.push(LineageEdge {
55 source: source.to_string(),
56 target: target.to_string(),
57 relation: relation.to_string(),
58 });
59 }
60
61 pub fn get_mcard_lineage(&self, target_uri: &str) -> LineageDAG {
62 let (visited_ids, reachable_edges) = crate::layer2::traversal::reachable_from_with_edges(
63 [target_uri.to_string()],
64 |curr| {
65 self.edges
66 .iter()
67 .filter(|edge| edge.target == *curr)
68 .map(|edge| (edge.source.clone(), edge.clone()))
69 .collect::<Vec<_>>()
70 },
71 );
72 let mut reachable_nodes = HashMap::new();
73 for id in visited_ids {
74 if let Some(node) = self.nodes.get(&id) {
75 reachable_nodes.insert(id, node.clone());
76 }
77 }
78
79 LineageDAG {
80 target_uri: target_uri.to_string(),
81 nodes: reachable_nodes,
82 edges: reachable_edges,
83 }
84 }
85
86 pub fn to_mermaid(&self, dag: &LineageDAG) -> String {
87 let mut lines = vec!["flowchart TD".to_string()];
88 for (id, node) in &dag.nodes {
89 let short_hash = if node.hash.len() > 8 {
90 &node.hash[..8]
91 } else {
92 &node.hash
93 };
94 lines.push(format!(" {}[\"{}\\n({}) \"]", id, node.label, short_hash));
95 }
96 for edge in &dag.edges {
97 lines.push(format!(" {} -->|{}| {}", edge.source, edge.relation, edge.target));
98 }
99 lines.join("\n")
100 }
101
102 pub fn to_dot(&self, dag: &LineageDAG) -> String {
103 let mut lines = vec!["digraph Lineage {".to_string(), " rankdir=LR;".to_string()];
104 for (id, node) in &dag.nodes {
105 let short_hash = if node.hash.len() > 8 {
106 &node.hash[..8]
107 } else {
108 &node.hash
109 };
110 lines.push(format!(" \"{}\" [label=\"{}\\n{}\"];", id, node.label, short_hash));
111 }
112 for edge in &dag.edges {
113 lines.push(format!(" \"{}\" -> \"{}\" [label=\"{}\"];", edge.source, edge.target, edge.relation));
114 }
115 lines.push("}".to_string());
116 lines.join("\n")
117 }
118}