-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelationship_lineage.py
More file actions
151 lines (131 loc) · 5.03 KB
/
Copy pathrelationship_lineage.py
File metadata and controls
151 lines (131 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python3
"""Directed upstream/downstream lineage traversal for Data Relationship Map."""
from __future__ import annotations
import argparse
import json
from collections import defaultdict, deque
from typing import Any
from relationship_map import load_model
def _nodes(model: dict[str, Any]) -> dict[str, dict[str, Any]]:
return {str(node.get("id")): node for node in model.get("nodes", []) if str(node.get("id", "")).strip()}
def _adjacency(model: dict[str, Any], direction: str) -> dict[str, list[tuple[str, dict[str, Any]]]]:
nodes = _nodes(model)
adjacency: dict[str, list[tuple[str, dict[str, Any]]]] = defaultdict(list)
for rel in model.get("relationships", []):
source = str(rel.get("from", ""))
target = str(rel.get("to", ""))
if source not in nodes or target not in nodes:
continue
if direction == "downstream":
adjacency[source].append((target, rel))
else:
adjacency[target].append((source, rel))
for key in adjacency:
adjacency[key].sort(key=lambda item: (item[0], str(item[1].get("type", ""))))
return adjacency
def traverse(
model: dict[str, Any],
start: str,
direction: str = "downstream",
max_depth: int | None = None,
stop_systems: set[str] | None = None,
stop_objects: set[str] | None = None,
) -> dict[str, Any]:
if direction not in {"downstream", "upstream"}:
raise ValueError("direction must be downstream or upstream")
if max_depth is not None and max_depth < 0:
raise ValueError("max_depth must be >= 0")
nodes = _nodes(model)
if start not in nodes:
return {
"valid_start": False,
"start": start,
"direction": direction,
"reached": [],
"paths": {},
"edges": [],
"boundaries": [],
}
stop_systems = stop_systems or set()
stop_objects = stop_objects or set()
adjacency = _adjacency(model, direction)
queue = deque([(start, 0)])
visited = {start}
paths: dict[str, list[str]] = {start: [start]}
edge_keys: set[tuple[str, str, str]] = set()
edges: list[dict[str, Any]] = []
boundaries: list[dict[str, Any]] = []
while queue:
current, depth = queue.popleft()
current_node = nodes[current]
if current != start and (
str(current_node.get("system", "")) in stop_systems
or str(current_node.get("object", "")) in stop_objects
):
boundaries.append({
"id": current,
"system": current_node.get("system"),
"object": current_node.get("object"),
"depth": depth,
})
continue
if max_depth is not None and depth >= max_depth:
continue
for neighbor, rel in adjacency.get(current, []):
relation_type = str(rel.get("type", "related_to"))
if direction == "downstream":
edge_from, edge_to = current, neighbor
else:
edge_from, edge_to = neighbor, current
key = (edge_from, relation_type, edge_to)
if key not in edge_keys:
edges.append({
"from": edge_from,
"to": edge_to,
"type": relation_type,
"provenance": rel.get("provenance"),
})
edge_keys.add(key)
if neighbor not in visited:
visited.add(neighbor)
paths[neighbor] = paths[current] + [neighbor]
queue.append((neighbor, depth + 1))
reached = []
for node_id in sorted(visited, key=lambda item: (len(paths[item]), item)):
node = nodes[node_id]
reached.append({
"id": node_id,
"depth": len(paths[node_id]) - 1,
"system": node.get("system"),
"object": node.get("object"),
})
return {
"valid_start": True,
"start": start,
"direction": direction,
"reached": reached,
"paths": {node_id: paths[node_id] for node_id in sorted(paths)},
"edges": edges,
"boundaries": sorted(boundaries, key=lambda item: (item["depth"], item["id"])),
}
def main() -> int:
parser = argparse.ArgumentParser(description="Traverse directed enterprise relationship lineage")
parser.add_argument("model")
parser.add_argument("start")
parser.add_argument("--direction", choices=["downstream", "upstream"], default="downstream")
parser.add_argument("--max-depth", type=int)
parser.add_argument("--stop-system", action="append", default=[])
parser.add_argument("--stop-object", action="append", default=[])
args = parser.parse_args()
result = traverse(
load_model(args.model),
args.start,
args.direction,
args.max_depth,
set(args.stop_system),
set(args.stop_object),
)
print(json.dumps(result, indent=2))
return 0 if result["valid_start"] else 2
if __name__ == "__main__":
raise SystemExit(main())