|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# Copyright 2022 Atlan Pte. Ltd. |
| 3 | +# Based on original code from https://github.com/apache/atlas (under Apache-2.0 license) |
| 4 | +from collections import deque |
| 5 | +from typing import TYPE_CHECKING, Any, Optional |
| 6 | + |
| 7 | +from pydantic import Field |
| 8 | + |
| 9 | +if TYPE_CHECKING: |
| 10 | + from dataclasses import dataclass |
| 11 | +else: |
| 12 | + from pydantic.dataclasses import dataclass |
| 13 | + |
| 14 | +from pyatlan.error import InvalidRequestError |
| 15 | +from pyatlan.model.assets import Asset |
| 16 | +from pyatlan.model.core import AtlanObject |
| 17 | +from pyatlan.model.enums import LineageDirection |
| 18 | + |
| 19 | + |
| 20 | +class LineageRelation(AtlanObject): |
| 21 | + from_entity_id: Optional[str] |
| 22 | + to_entity_id: Optional[str] |
| 23 | + process_id: Optional[str] |
| 24 | + relationship_id: Optional[str] |
| 25 | + |
| 26 | + @property |
| 27 | + def is_full_link(self): |
| 28 | + return self.process_id is not None |
| 29 | + |
| 30 | + |
| 31 | +@dataclass(frozen=True) |
| 32 | +class DirectedPair: |
| 33 | + process_guid: str |
| 34 | + target_guid: str |
| 35 | + |
| 36 | + |
| 37 | +@dataclass(frozen=True) |
| 38 | +class LineageGraph: |
| 39 | + downstream_list: dict[str, dict[DirectedPair, None]] |
| 40 | + upstream_list: dict[str, dict[DirectedPair, None]] |
| 41 | + |
| 42 | + @classmethod |
| 43 | + def create(cls, relations: list[LineageRelation]) -> "LineageGraph": |
| 44 | + downstream_list: dict[str, dict[DirectedPair, None]] = {} |
| 45 | + upstream_list: dict[str, dict[DirectedPair, None]] = {} |
| 46 | + |
| 47 | + def add_relation(relation: LineageRelation): |
| 48 | + if ( |
| 49 | + relation.from_entity_id |
| 50 | + and relation.process_id |
| 51 | + and relation.to_entity_id |
| 52 | + ): |
| 53 | + add_edges( |
| 54 | + relation.from_entity_id, relation.process_id, relation.to_entity_id |
| 55 | + ) |
| 56 | + |
| 57 | + def add_edges(source_guid: str, process_guid: str, target_guid: str): |
| 58 | + if source_guid not in downstream_list: |
| 59 | + downstream_list[source_guid] = {} |
| 60 | + if target_guid not in upstream_list: |
| 61 | + upstream_list[target_guid] = {} |
| 62 | + downstream_list[source_guid][ |
| 63 | + DirectedPair(process_guid=process_guid, target_guid=target_guid) |
| 64 | + ] = None |
| 65 | + upstream_list[target_guid][ |
| 66 | + DirectedPair(process_guid=process_guid, target_guid=source_guid) |
| 67 | + ] = None |
| 68 | + |
| 69 | + for relation in relations: |
| 70 | + if relation.is_full_link: |
| 71 | + add_relation(relation) |
| 72 | + else: |
| 73 | + raise InvalidRequestError( |
| 74 | + param="", |
| 75 | + code="ATLAN-JAVA-400-013", |
| 76 | + message="Lineage was retrieved using hideProces=false. " |
| 77 | + "We do not provide a graph view in this case.", |
| 78 | + ) |
| 79 | + return cls(downstream_list=downstream_list, upstream_list=upstream_list) |
| 80 | + |
| 81 | + @staticmethod |
| 82 | + def get_asset_guids( |
| 83 | + guid: str, guids: dict[str, dict[DirectedPair, None]] |
| 84 | + ) -> list[str]: |
| 85 | + if guid in guids: |
| 86 | + return list({pair.target_guid: None for pair in guids[guid].keys()}.keys()) |
| 87 | + return [] |
| 88 | + |
| 89 | + @staticmethod |
| 90 | + def get_process_guids( |
| 91 | + guid: str, guids: dict[str, dict[DirectedPair, None]] |
| 92 | + ) -> list[str]: |
| 93 | + if guid in guids: |
| 94 | + return list({pair.process_guid: None for pair in guids[guid].keys()}.keys()) |
| 95 | + return [] |
| 96 | + |
| 97 | + def get_downstream_asset_guids(self, guid: str) -> list[str]: |
| 98 | + return LineageGraph.get_asset_guids(guid, self.downstream_list) |
| 99 | + |
| 100 | + def get_downstream_process_guids(self, guid: str) -> list[str]: |
| 101 | + return LineageGraph.get_process_guids(guid, self.downstream_list) |
| 102 | + |
| 103 | + def get_upstream_asset_guids(self, guid: str) -> list[str]: |
| 104 | + return LineageGraph.get_asset_guids(guid, self.upstream_list) |
| 105 | + |
| 106 | + def get_upstream_process_guids(self, guid: str) -> list[str]: |
| 107 | + return LineageGraph.get_process_guids(guid, self.upstream_list) |
| 108 | + |
| 109 | + def get_all_downstream_asset_guids_dfs(self, guid: str) -> list[str]: |
| 110 | + visited: dict[str, None] = {} |
| 111 | + stack: deque[str] = deque() |
| 112 | + stack.append(guid) |
| 113 | + while len(stack) > 0: |
| 114 | + to_traverse = stack.pop() |
| 115 | + if to_traverse not in visited: |
| 116 | + visited[to_traverse] = None |
| 117 | + for downstream_guid in self.get_downstream_asset_guids(to_traverse): |
| 118 | + if downstream_guid not in visited: |
| 119 | + stack.append(downstream_guid) |
| 120 | + return list(visited.keys()) |
| 121 | + |
| 122 | + def get_all_upstream_asset_guids_dfs(self, guid: str) -> list[str]: |
| 123 | + visited: dict[str, None] = {} |
| 124 | + stack: deque[str] = deque() |
| 125 | + stack.append(guid) |
| 126 | + while len(stack) > 0: |
| 127 | + to_traverse = stack.pop() |
| 128 | + if to_traverse not in visited: |
| 129 | + visited[to_traverse] = None |
| 130 | + for upstream_guid in self.get_upstream_asset_guids(to_traverse): |
| 131 | + if upstream_guid not in visited: |
| 132 | + stack.append(upstream_guid) |
| 133 | + return list(visited.keys()) |
| 134 | + |
| 135 | + |
| 136 | +class LineageResponse(AtlanObject): |
| 137 | + base_entity_guid: str |
| 138 | + lineage_direction: LineageDirection |
| 139 | + lineage_depth: int |
| 140 | + limit: int |
| 141 | + offset: int |
| 142 | + has_more_upstream_vertices: bool |
| 143 | + has_more_downstream_vertices: bool |
| 144 | + guid_entity_map: dict[str, Asset] |
| 145 | + relations: list[LineageRelation] |
| 146 | + vertex_children_info: Optional[dict[str, Any]] |
| 147 | + graph: Optional[LineageGraph] = None |
| 148 | + |
| 149 | + def get_graph(self): |
| 150 | + if self.graph is None: |
| 151 | + self.graph = LineageGraph.create(self.relations) |
| 152 | + return self.graph |
| 153 | + |
| 154 | + def get_all_downstream_asset_guids_dfs( |
| 155 | + self, guid: Optional[str] = None |
| 156 | + ) -> list[str]: |
| 157 | + return self.get_graph().get_all_downstream_asset_guids_dfs( |
| 158 | + guid if guid else self.base_entity_guid |
| 159 | + ) |
| 160 | + |
| 161 | + def get_all_downstream_assets_dfs(self, guid: Optional[str] = None) -> list[Asset]: |
| 162 | + return [ |
| 163 | + self.guid_entity_map[guid] |
| 164 | + for guid in self.get_graph().get_all_downstream_asset_guids_dfs( |
| 165 | + guid if guid else self.base_entity_guid |
| 166 | + ) |
| 167 | + ] |
| 168 | + |
| 169 | + def get_all_upstream_asset_guids_dfs(self, guid: Optional[str] = None) -> list[str]: |
| 170 | + return self.get_graph().get_all_upstream_asset_guids_dfs( |
| 171 | + guid if guid else self.base_entity_guid |
| 172 | + ) |
| 173 | + |
| 174 | + def get_all_upstream_assets_dfs(self, guid: Optional[str] = None) -> list[Asset]: |
| 175 | + return [ |
| 176 | + self.guid_entity_map[guid] |
| 177 | + for guid in self.get_graph().get_all_upstream_asset_guids_dfs( |
| 178 | + guid if guid else self.base_entity_guid |
| 179 | + ) |
| 180 | + ] |
| 181 | + |
| 182 | + def get_downstream_asset_guids(self, guid: Optional[str] = None) -> list[str]: |
| 183 | + return self.get_graph().get_downstream_asset_guids( |
| 184 | + guid if guid else self.base_entity_guid |
| 185 | + ) |
| 186 | + |
| 187 | + def get_downstream_assets(self, guid: Optional[str] = None) -> list[Asset]: |
| 188 | + return [ |
| 189 | + self.guid_entity_map[guid] |
| 190 | + for guid in self.get_graph().get_downstream_asset_guids( |
| 191 | + guid if guid else self.base_entity_guid |
| 192 | + ) |
| 193 | + ] |
| 194 | + |
| 195 | + def get_downstream_process_guids(self, guid: Optional[str] = None) -> list[str]: |
| 196 | + return self.get_graph().get_downstream_process_guids( |
| 197 | + guid if guid else self.base_entity_guid |
| 198 | + ) |
| 199 | + |
| 200 | + def get_upstream_asset_guids(self, guid: Optional[str] = None) -> list[str]: |
| 201 | + return self.get_graph().get_upstream_asset_guids( |
| 202 | + guid if guid else self.base_entity_guid |
| 203 | + ) |
| 204 | + |
| 205 | + def get_upstream_assets(self, guid: Optional[str] = None) -> list[Asset]: |
| 206 | + return [ |
| 207 | + self.guid_entity_map[guid] |
| 208 | + for guid in self.get_graph().get_upstream_asset_guids( |
| 209 | + guid if guid else self.base_entity_guid |
| 210 | + ) |
| 211 | + ] |
| 212 | + |
| 213 | + def get_upstream_process_guids(self, guid: Optional[str] = None) -> list[str]: |
| 214 | + return self.get_graph().get_upstream_process_guids( |
| 215 | + guid if guid else self.base_entity_guid |
| 216 | + ) |
| 217 | + |
| 218 | + |
| 219 | +class LineageRequest(AtlanObject): |
| 220 | + guid: str |
| 221 | + depth: int = Field(default=0) |
| 222 | + direction: LineageDirection = Field(default=LineageDirection.BOTH) |
| 223 | + hide_process: bool = Field(default=True) |
| 224 | + allow_deleted_process: bool = Field(default=False) |
0 commit comments