-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathtopo-viz.js
367 lines (317 loc) · 10.9 KB
/
topo-viz.js
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action, set } from '@ember/object';
import { inject as service } from '@ember/service';
import { next } from '@ember/runloop';
import { scaleLinear } from 'd3-scale';
import { extent, deviation, mean } from 'd3-array';
import { line, curveBasis } from 'd3-shape';
import styleStringProperty from '../utils/properties/style-string';
export default class TopoViz extends Component {
@service system;
@tracked element = null;
@tracked topology = { datacenters: [] };
@tracked activeNode = null;
@tracked activeAllocation = null;
@tracked activeEdges = [];
@tracked edgeOffset = { x: 0, y: 0 };
@tracked viewportColumns = 2;
@tracked highlightAllocation = null;
@tracked tooltipProps = {};
@styleStringProperty('tooltipProps') tooltipStyle;
get isSingleColumn() {
if (this.topology.datacenters.length <= 1 || this.viewportColumns === 1)
return true;
// Compute the coefficient of variance to determine if it would be
// better to stack datacenters or place them in columns
const nodeCounts = this.topology.datacenters.map(
(datacenter) => datacenter.nodes.length
);
const variationCoefficient = deviation(nodeCounts) / mean(nodeCounts);
// The point at which the varation is too extreme for a two column layout
const threshold = 0.5;
if (variationCoefficient > threshold) return true;
return false;
}
get datacenterIsSingleColumn() {
// If there are enough nodes, use two columns of nodes within
// a single column layout of datacenters to increase density.
if (this.viewportColumns === 1) return true;
return (
!this.isSingleColumn ||
(this.isSingleColumn && this.args.nodes.length <= 20)
);
}
// Once a cluster is large enough, the exact details of a node are
// typically irrelevant and a waste of space.
get isDense() {
return this.args.nodes.length > 50;
}
dataForNode(node) {
return {
node,
datacenter: node.datacenter,
memory: node.resources.memory,
cpu: node.resources.cpu,
allocations: [],
isSelected: false,
};
}
dataForAllocation(allocation, node) {
const jobId = allocation.belongsTo('job').id();
return {
allocation,
node,
jobId,
groupKey: JSON.stringify([jobId, allocation.taskGroupName]),
memory: allocation.allocatedResources.memory,
cpu: allocation.allocatedResources.cpu,
memoryPercent: allocation.allocatedResources.memory / node.memory,
cpuPercent: allocation.allocatedResources.cpu / node.cpu,
isSelected: false,
};
}
@action
buildTopology() {
const nodes = this.args.nodes;
const allocations = this.args.allocations;
// Nodes may not have a resources property due to having an old Nomad agent version.
const badNodes = [];
// Wrap nodes in a topo viz specific data structure and build an index to speed up allocation assignment
const nodeContainers = [];
const nodeIndex = {};
nodes.forEach((node) => {
if (!node.resources) {
badNodes.push(node);
return;
}
const container = this.dataForNode(node);
nodeContainers.push(container);
nodeIndex[node.id] = container;
});
// Wrap allocations in a topo viz specific data structure, assign allocations to nodes, and build an allocation
// index keyed off of job and task group
const allocationIndex = {};
allocations.forEach((allocation) => {
const nodeId = allocation.belongsTo('node').id();
const nodeContainer = nodeIndex[nodeId];
// Ignore orphaned allocations and allocations on nodes with an old Nomad agent version.
if (!nodeContainer) return;
const allocationContainer = this.dataForAllocation(
allocation,
nodeContainer
);
nodeContainer.allocations.push(allocationContainer);
const key = allocationContainer.groupKey;
if (!allocationIndex[key]) allocationIndex[key] = [];
allocationIndex[key].push(allocationContainer);
});
// Group nodes into datacenters
const datacentersMap = nodeContainers.reduce(
(datacenters, nodeContainer) => {
if (!datacenters[nodeContainer.datacenter])
datacenters[nodeContainer.datacenter] = [];
datacenters[nodeContainer.datacenter].push(nodeContainer);
return datacenters;
},
{}
);
// Turn hash of datacenters into a sorted array
const datacenters = Object.keys(datacentersMap)
.map((key) => ({ name: key, nodes: datacentersMap[key] }))
.sortBy('name');
const topology = {
datacenters,
allocationIndex,
selectedKey: null,
heightScale: scaleLinear()
.range([15, 40])
.domain(extent(nodeContainers.mapBy('memory'))),
};
this.topology = topology;
if (badNodes.length && this.args.onDataError) {
this.args.onDataError([
{
type: 'filtered-nodes',
context: badNodes,
},
]);
}
}
@action
captureElement(element) {
this.element = element;
this.determineViewportColumns();
}
@action
showNodeDetails(node) {
if (this.activeNode) {
set(this.activeNode, 'isSelected', false);
}
this.activeNode = this.activeNode === node ? null : node;
if (this.activeNode) {
set(this.activeNode, 'isSelected', true);
}
if (this.args.onNodeSelect) this.args.onNodeSelect(this.activeNode);
}
@action showTooltip(allocation, element) {
const bbox = element.getBoundingClientRect();
this.highlightAllocation = allocation;
this.tooltipProps = {
left: window.scrollX + bbox.left + bbox.width / 2,
top: window.scrollY + bbox.top,
};
}
@action hideTooltip() {
this.highlightAllocation = null;
}
@action
associateAllocations(allocation) {
if (this.activeAllocation === allocation) {
this.activeAllocation = null;
this.activeEdges = [];
if (this.topology.selectedKey) {
const selectedAllocations =
this.topology.allocationIndex[this.topology.selectedKey];
if (selectedAllocations) {
selectedAllocations.forEach((allocation) => {
set(allocation, 'isSelected', false);
});
}
set(this.topology, 'selectedKey', null);
}
} else {
if (this.activeNode) {
set(this.activeNode, 'isSelected', false);
}
this.activeNode = null;
this.activeAllocation = allocation;
const selectedAllocations =
this.topology.allocationIndex[this.topology.selectedKey];
if (selectedAllocations) {
selectedAllocations.forEach((allocation) => {
set(allocation, 'isSelected', false);
});
}
set(this.topology, 'selectedKey', allocation.groupKey);
const newAllocations =
this.topology.allocationIndex[this.topology.selectedKey];
if (newAllocations) {
newAllocations.forEach((allocation) => {
set(allocation, 'isSelected', true);
});
}
// Only show the lines if the selected allocations are sparse (low count relative to the client count or low count generally).
if (
newAllocations.length < 10 ||
newAllocations.length < this.args.nodes.length * 0.75
) {
this.computedActiveEdges();
} else {
this.activeEdges = [];
}
}
if (this.args.onAllocationSelect)
this.args.onAllocationSelect(
this.activeAllocation && this.activeAllocation.allocation
);
if (this.args.onNodeSelect) this.args.onNodeSelect(this.activeNode);
}
@action
determineViewportColumns() {
this.viewportColumns = this.element.clientWidth < 900 ? 1 : 2;
}
@action
resizeEdges() {
if (this.activeEdges.length > 0) {
this.computedActiveEdges();
}
}
@action
computedActiveEdges() {
// Wait a render cycle
next(() => {
const path = line().curve(curveBasis);
// 1. Get the active element
const allocation = this.activeAllocation.allocation;
const activeEl = this.element.querySelector(
`[data-allocation-id="${allocation.id}"]`
);
const activePoint = centerOfBBox(activeEl.getBoundingClientRect());
// 2. Collect the mem and cpu pairs for all selected allocs
const selectedMem = Array.from(
this.element.querySelectorAll('.memory .bar.is-selected')
);
const selectedPairs = selectedMem.map((mem) => {
const id = mem.closest('[data-allocation-id]').dataset.allocationId;
const cpu = mem
.closest('.topo-viz-node')
.querySelector(`.cpu .bar[data-allocation-id="${id}"]`);
return [mem, cpu];
});
const selectedPoints = selectedPairs.map((pair) => {
return pair.map((el) => centerOfBBox(el.getBoundingClientRect()));
});
// 3. For each pair, compute the midpoint of the truncated triangle of points [Mem, Cpu, Active]
selectedPoints.forEach((points) => {
const d1 = pointBetween(points[0], activePoint, 100, 0.5);
const d2 = pointBetween(points[1], activePoint, 100, 0.5);
points.push(midpoint(d1, d2));
});
// 4. Generate curves for each active->mem and active->cpu pair going through the bisector
const curves = [];
// Steps are used to restrict the range of curves. The closer control points are placed, the less
// curvature the curve generator will generate.
const stepsMain = [0, 0.8, 1.0];
// The second prong the fork does not need to retrace the entire path from the activePoint
const stepsSecondary = [0.8, 1.0];
selectedPoints.forEach((points) => {
curves.push(
curveFromPoints(
...pointsAlongPath(activePoint, points[2], stepsMain),
points[0]
),
curveFromPoints(
...pointsAlongPath(activePoint, points[2], stepsSecondary),
points[1]
)
);
});
this.activeEdges = curves.map((curve) => path(curve));
this.edgeOffset = { x: window.scrollX, y: window.scrollY };
});
}
}
function centerOfBBox(bbox) {
return {
x: bbox.x + bbox.width / 2,
y: bbox.y + bbox.height / 2,
};
}
function dist(p1, p2) {
return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
}
// Return the point between p1 and p2 at len (or pct if len > dist(p1, p2))
function pointBetween(p1, p2, len, pct) {
const d = dist(p1, p2);
const ratio = d < len ? pct : len / d;
return pointBetweenPct(p1, p2, ratio);
}
function pointBetweenPct(p1, p2, pct) {
const dx = p2.x - p1.x;
const dy = p2.y - p1.y;
return { x: p1.x + dx * pct, y: p1.y + dy * pct };
}
function pointsAlongPath(p1, p2, pcts) {
return pcts.map((pct) => pointBetweenPct(p1, p2, pct));
}
function midpoint(p1, p2) {
return pointBetweenPct(p1, p2, 0.5);
}
function curveFromPoints(...points) {
return points.map((p) => [p.x, p.y]);
}