/
layout.ts
295 lines (253 loc) · 8.25 KB
/
layout.ts
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import * as dagre from 'dagre';
import groupBy from 'lodash/groupBy';
import {IBounds, IPoint} from '../graph/common';
import {GraphData, GraphNode, GraphId} from './Utils';
export interface AssetLayout {
id: GraphId;
bounds: IBounds; // Overall frame of the box relative to 0,0 on the graph
}
export interface GroupLayout {
id: GraphId;
groupName: string;
repositoryName: string;
repositoryLocationName: string;
repositoryDisambiguationRequired: boolean;
bounds: IBounds; // Overall frame of the box relative to 0,0 on the graph
}
export type AssetLayoutEdge = {
from: IPoint;
fromId: string;
to: IPoint;
toId: string;
};
export type AssetGraphLayout = {
width: number;
height: number;
edges: AssetLayoutEdge[];
nodes: {[id: string]: AssetLayout};
groups: {[name: string]: GroupLayout};
};
// Prefix group nodes in the Dagre layout so that an asset and an asset
// group cannot have the same name.
const GROUP_NODE_PREFIX = 'group__';
const MARGIN = 100;
export type LayoutAssetGraphOptions = {
horizontalDAGs: boolean;
longestPath: boolean;
};
export const layoutAssetGraph = (
graphData: GraphData,
opts: LayoutAssetGraphOptions,
): AssetGraphLayout => {
const g = new dagre.graphlib.Graph({compound: true});
const ranker = opts.longestPath ? 'longest-path' : 'tight-tree';
g.setGraph(
opts.horizontalDAGs
? {
rankdir: 'LR',
marginx: MARGIN,
marginy: MARGIN,
nodesep: -10,
edgesep: 90,
ranksep: 60,
ranker,
}
: {
rankdir: 'TB',
marginx: MARGIN,
marginy: MARGIN,
nodesep: 40,
edgesep: 10,
ranksep: 10,
ranker,
},
);
g.setDefaultEdgeLabel(() => ({}));
const parentNodeIdForNode = (node: GraphNode) =>
[
GROUP_NODE_PREFIX,
node.definition.repository.location.name,
node.definition.repository.name,
node.definition.groupName,
].join('__');
// const shouldRender = (node?: GraphNode) => node && node.definition.opNames.length > 0;
const shouldRender = (node?: GraphNode) => node;
const renderedNodes = Object.values(graphData.nodes).filter(shouldRender);
const groups: {[id: string]: GroupLayout} = {};
for (const node of renderedNodes) {
if (node.definition.groupName) {
const id = parentNodeIdForNode(node);
groups[id] = {
id,
groupName: node.definition.groupName,
repositoryName: node.definition.repository.name,
repositoryLocationName: node.definition.repository.location.name,
repositoryDisambiguationRequired: false,
bounds: {x: 0, y: 0, width: 0, height: 0},
};
}
}
// Add all the group boxes to the graph
const showGroups = Object.keys(groups).length > 1;
if (showGroups) {
Object.keys(groups).forEach((groupId) => g.setNode(groupId, {}));
}
// Add all the nodes to the graph
renderedNodes.forEach((node) => {
g.setNode(node.id, getAssetNodeDimensions(node.definition));
if (showGroups && node.definition.groupName) {
g.setParent(node.id, parentNodeIdForNode(node));
}
});
const linksToAssetsOutsideGraphedSet: {[id: string]: true} = {};
// Add the edges to the graph, and accumulate a set of "foreign nodes" (for which
// we have an inbound/outbound edge, but we don't have the `node` in the graphData).
Object.entries(graphData.downstream).forEach(([upstreamId, graphDataDownstream]) => {
const downstreamIds = Object.keys(graphDataDownstream);
downstreamIds.forEach((downstreamId) => {
if (
!shouldRender(graphData.nodes[downstreamId]) &&
!shouldRender(graphData.nodes[upstreamId])
) {
return;
}
g.setEdge({v: upstreamId, w: downstreamId}, {weight: 1});
if (!shouldRender(graphData.nodes[downstreamId])) {
(linksToAssetsOutsideGraphedSet as any)[downstreamId] = true;
} else if (!shouldRender(graphData.nodes[upstreamId])) {
(linksToAssetsOutsideGraphedSet as any)[upstreamId] = true;
}
});
});
// Add all the link nodes to the graph
Object.keys(linksToAssetsOutsideGraphedSet).forEach((id) => {
const path = JSON.parse(id);
const label = path[path.length - 1] || '';
g.setNode(id, getAssetLinkDimensions(label, opts));
});
dagre.layout(g);
let maxWidth = 0;
let maxHeight = 0;
const nodes: {[id: string]: AssetLayout} = {};
g.nodes().forEach((id) => {
const dagreNode = g.node(id);
if (!dagreNode) {
return;
}
const bounds = {
x: dagreNode.x - dagreNode.width / 2,
y: dagreNode.y - dagreNode.height / 2,
width: dagreNode.width,
height: dagreNode.height,
};
if (!id.startsWith(GROUP_NODE_PREFIX)) {
nodes[id] = {id, bounds};
}
maxWidth = Math.max(maxWidth, dagreNode.x + dagreNode.width / 2);
maxHeight = Math.max(maxHeight, dagreNode.y + dagreNode.height / 2);
});
// Apply bounds to the groups based on the nodes inside them
if (showGroups) {
for (const node of renderedNodes) {
if (node.definition.groupName) {
const groupId = parentNodeIdForNode(node);
const groupForId = groups[groupId]!;
groupForId.bounds =
groupForId.bounds.width === 0
? nodes[node.id]!.bounds
: extendBounds(groupForId.bounds, nodes[node.id]!.bounds);
}
}
for (const group of Object.values(groups)) {
group.bounds = padBounds(group.bounds, {x: 15, top: 70, bottom: -10});
}
}
// Annotate groups that require disambiguation (same group name appears twice)
Object.values(groupBy(Object.values(groups), (g) => g.groupName))
.filter((set) => set.length > 1)
.flat()
.forEach((group) => {
group.bounds.y -= 18;
group.bounds.height += 18;
group.repositoryDisambiguationRequired = true;
});
const edges: AssetLayoutEdge[] = [];
g.edges().forEach((e) => {
const v = g.node(e.v);
const vXInset = !!linksToAssetsOutsideGraphedSet[e.v] ? 16 : 24;
const w = g.node(e.w);
const wXInset = !!linksToAssetsOutsideGraphedSet[e.w] ? 16 : 24;
// Ignore the coordinates from dagre and use the top left + bottom left of the
edges.push(
opts.horizontalDAGs
? {
from: {x: v.x + v.width / 2, y: v.y},
fromId: e.v,
to: {x: w.x - w.width / 2 - 5, y: w.y},
toId: e.w,
}
: {
from: {x: v.x - v.width / 2 + vXInset, y: v.y - 30 + v.height / 2},
fromId: e.v,
to: {x: w.x - w.width / 2 + wXInset, y: w.y + 20 - w.height / 2},
toId: e.w,
},
);
});
return {
nodes,
edges,
width: maxWidth + MARGIN,
height: maxHeight + MARGIN,
groups: showGroups ? groups : {},
};
};
export const ASSET_LINK_NAME_MAX_LENGTH = 10;
export const getAssetLinkDimensions = (label: string, opts: LayoutAssetGraphOptions) => {
return opts.horizontalDAGs
? {width: 32 + 8 * Math.min(ASSET_LINK_NAME_MAX_LENGTH, label.length), height: 90}
: {width: 106, height: 90};
};
export const padBounds = (a: IBounds, padding: {x: number; top: number; bottom: number}) => {
return {
x: a.x - padding.x,
y: a.y - padding.top,
width: a.width + padding.x * 2,
height: a.height + padding.top + padding.bottom,
};
};
export const extendBounds = (a: IBounds, b: IBounds) => {
const xmin = Math.min(a.x, b.x);
const ymin = Math.min(a.y, b.y);
const xmax = Math.max(a.x + a.width, b.x + b.width);
const ymax = Math.max(a.y + a.height, b.y + b.height);
return {x: xmin, y: ymin, width: xmax - xmin, height: ymax - ymin};
};
export const ASSET_NODE_NAME_MAX_LENGTH = 28;
export const getAssetNodeDimensions = (def: {
assetKey: {path: string[]};
opNames: string[];
isSource: boolean;
isObservable: boolean;
isPartitioned: boolean;
graphName: string | null;
description?: string | null;
computeKind: string | null;
}) => {
const width = 265;
if (def.isSource && !def.isObservable) {
return {width, height: 102};
} else {
let height = 100; // top tags area + name + description
if (def.isSource) {
height += 30; // last observed
} else {
height += 26; // status row
if (def.isPartitioned) {
height += 40;
}
}
height += 30; // tags beneath
return {width, height};
}
};