forked from feathr-ai/feathr
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.ts
More file actions
198 lines (166 loc) · 4.62 KB
/
Copy pathutils.ts
File metadata and controls
198 lines (166 loc) · 4.62 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import dagre from "dagre";
import {
ArrowHeadType,
Edge,
Elements,
isNode,
Node,
Position,
} from "react-flow-renderer";
import { FeatureLineage } from "../../models/model";
const DEFAULT_WIDTH = 172;
const DEFAULT_HEIGHT = 36;
type getLayoutElementsRet = {
layoutedElements: Elements;
elementMapping: Record<string, number>;
};
const getElements = (
lineageData: FeatureLineage,
featureType: string | null
) => {
if (lineageData.guidEntityMap === null && lineageData.relations === null) {
return;
}
const elements: Elements = [];
const elementObj: Record<string, string> = {};
for (
let index = 0;
index < Object.values(lineageData.guidEntityMap).length;
index++
) {
const currentNode: any = Object.values(lineageData.guidEntityMap)[index];
if (currentNode.typeName === "feathr_workspace_v1") {
continue; // Open issue: should project node get displayed as well?
}
const nodeId = currentNode.guid;
// If toggled feature type exists, skip other types
if (
featureType &&
featureType !== "all_nodes" &&
currentNode.typeName !== featureType
) {
continue;
}
const node = generateNode({
index,
nodeId,
currentNode,
});
elementObj[nodeId] = index?.toString();
elements.push(node);
}
for (let index = 0; index < lineageData.relations.length; index++) {
var {
fromEntityId: from,
toEntityId: to,
relationshipType,
} = lineageData.relations[index];
if (relationshipType === "Consumes") [from, to] = [to, from];
const edge = generateEdge({ obj: elementObj, from, to });
if (edge?.source && edge?.target) {
if (relationshipType === "Consumes" || relationshipType === "Produces") {
elements.push(edge);
}
}
}
return elements;
};
const getLayoutedElements = (
elements: Elements,
direction = "LR"
): getLayoutElementsRet => {
const dagreGraph = new dagre.graphlib.Graph();
dagreGraph.setDefaultEdgeLabel(() => ({}));
dagreGraph.setGraph({ rankdir: direction });
const isHorizontal = direction === "LR";
for (let index = 0; index < elements.length; index++) {
const element: Node | Edge = elements[index];
if (isNode(element)) {
dagreGraph.setNode(element.id, {
width: DEFAULT_WIDTH,
height: DEFAULT_HEIGHT,
});
} else {
dagreGraph.setEdge(element.source, element.target);
}
}
dagre.layout(dagreGraph);
const newElements = [];
const elementsObj: Record<string, number> = {};
for (let index = 0; index < elements.length; index++) {
const element = elements[index] as Node;
if (isNode(element)) {
elementsObj[element.data?.id] = index;
const nodeWithPosition = dagreGraph.node(element.id);
element.targetPosition = isHorizontal ? Position.Left : Position.Top;
element.sourcePosition = isHorizontal ? Position.Right : Position.Bottom;
element.position = {
x: nodeWithPosition.x - DEFAULT_WIDTH / 2,
y: nodeWithPosition.y - DEFAULT_HEIGHT / 2,
};
}
newElements.push(element);
}
return {
layoutedElements: newElements,
elementMapping: elementsObj,
};
};
const featureTypeColors: Record<string, string> = {
feathr_source_v1: "hsl(315, 100%, 50%)",
feathr_anchor_v1: "hsl(270, 100%, 50%)",
feathr_anchor_feature_v1: "hsl(225, 100%, 50%)",
feathr_derived_feature_v1: "hsl(135, 100%, 50%)",
};
const generateNode = ({
nodeId,
index,
currentNode,
}: // eslint-disable-next-line @typescript-eslint/no-explicit-any
any): any => ({
key: nodeId,
id: index?.toString(),
type: "custom-node",
label: currentNode.displayText,
shape: "box",
color: {
background: featureTypeColors[currentNode.typeName],
},
data: {
id: nodeId,
title: currentNode.displayText,
subtitle: currentNode.typeName,
featureId: currentNode.guid,
version: currentNode.version,
borderColor: featureTypeColors[currentNode.typeName],
},
});
type GenerateEdgeProps = {
obj: Record<string, string>;
from: string;
to: string;
};
const generateEdge = ({ obj, from, to }: GenerateEdgeProps): Edge => {
const source = obj?.[from];
const target = obj?.[to];
const id = `e${source}-${target}`;
return {
id,
source,
target,
arrowHeadType: ArrowHeadType.ArrowClosed,
};
};
export { generateEdge, generateNode, getLayoutedElements, getElements };
export const findNodeInElement = (
nodeId: string | null,
elements: Elements
): Node | null => {
if (nodeId) {
const node = elements.find(
(element) => isNode(element) && element.data.id === nodeId
);
return node as Node;
}
return null;
};