-
Notifications
You must be signed in to change notification settings - Fork 594
/
RelationGraph.tsx
437 lines (385 loc) · 12.4 KB
/
RelationGraph.tsx
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
* Copyright 2024 RisingWave Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import { theme } from "@chakra-ui/react"
import * as d3 from "d3"
import * as dagre from "dagre"
import { useCallback, useEffect, useRef } from "react"
import {
Relation,
relationIsStreamingJob,
relationType,
relationTypeTitleCase,
} from "../lib/api/streaming"
import {
Edge,
Enter,
Position,
RelationPoint,
RelationPointPosition,
} from "../lib/layout"
import { RelationStats } from "../proto/gen/monitor_service"
import { CatalogModal, useCatalogModal } from "./CatalogModal"
import {
backPressureColor,
backPressureWidth,
epochToUnixMillis,
latencyToColor,
} from "./utils/backPressure"
// Size of each relation box in pixels
export const boxWidth = 150
export const boxHeight = 45
// Radius of the icon circle in pixels
const iconRadius = 12
// Horizontal spacing between layers in the graph in pixels
const layerMargin = 80
// Vertical spacing between rows in the graph in pixels
const rowMargin = 30
// Margin around the entire graph layout in pixels
const layoutMargin = 30
function boundBox(relationPosition: RelationPointPosition[]): {
width: number
height: number
} {
let width = 0
let height = 0
for (const { x, y } of relationPosition) {
width = Math.max(width, x + boxWidth)
height = Math.max(height, y + boxHeight)
}
return { width, height }
}
export default function RelationGraph({
nodes,
selectedId,
setSelectedId,
backPressures,
relationStats,
}: {
nodes: RelationPoint[] // rename to RelationNode
selectedId: string | undefined
setSelectedId: (id: string) => void
backPressures?: Map<string, number> // relationId-relationId->back_pressure_rate})
relationStats: { [relationId: number]: RelationStats } | undefined
}) {
const [modalData, setModalId] = useCatalogModal(nodes.map((n) => n.relation))
const svgRef = useRef<SVGSVGElement>(null)
const layoutMapCallback = useCallback(() => {
// Create a new directed graph
const g = new dagre.graphlib.Graph()
// Set graph direction and spacing
g.setGraph({
rankdir: "LR",
nodesep: rowMargin,
ranksep: layerMargin,
marginx: layoutMargin,
marginy: layoutMargin,
})
// Default to assigning empty object as edge label
g.setDefaultEdgeLabel(() => ({}))
// Add nodes
nodes.forEach((node) => {
g.setNode(node.id, node)
})
// Add edges
nodes.forEach((node) => {
node.parentIds?.forEach((parentId) => {
g.setEdge(parentId, node.id) // Here the "parent" means the upstream relation
})
})
// Perform layout
dagre.layout(g)
// Convert to expected format
const layoutMap = g.nodes().map((id) => {
const node = g.node(id)
return {
...node,
x: node.x - boxWidth / 2, // Adjust for center-based coordinates
y: node.y - boxHeight / 2,
} as RelationPointPosition
})
const links = g.edges().map((e) => {
const edge = g.edge(e)
return {
source: e.v,
target: e.w,
points: edge.points || [],
}
})
// Calculate bounds
const { width, height } = boundBox(layoutMap)
return {
layoutMap,
links,
width,
height,
}
}, [nodes])
const { layoutMap, links, width, height } = layoutMapCallback()
useEffect(() => {
const now_ms = Date.now()
const svgNode = svgRef.current
const svgSelection = d3.select(svgNode)
const curveStyle = d3.curveBasis
const line = d3
.line<Position>()
.curve(curveStyle)
.x(({ x }) => x)
.y(({ y }) => y)
const edgeSelection = svgSelection
.select(".edges")
.selectAll<SVGPathElement, null>(".edge")
.data(links)
type EdgeSelection = typeof edgeSelection
const isSelected = (id: string) => id === selectedId
const applyEdge = (sel: EdgeSelection) => {
const color = (d: Edge) => {
if (backPressures) {
let value = backPressures.get(`${d.source}_${d.target}`)
if (value) {
return backPressureColor(value)
}
}
return theme.colors.gray["300"]
}
const width = (d: Edge) => {
if (backPressures) {
let value = backPressures.get(`${d.source}_${d.target}`)
if (value) {
return backPressureWidth(value, 15)
}
}
return 2
}
sel
.attr("d", ({ points }) => line(points))
.attr("fill", "none")
.attr("stroke-width", width)
.attr("stroke", color)
.attr("opacity", (d) =>
isSelected(d.source) || isSelected(d.target) ? 1 : 0.5
)
sel
.on("mouseover", (event, d) => {
// Remove existing tooltip if any
d3.selectAll(".tooltip").remove()
// Create new tooltip
const bpValue = backPressures?.get(`${d.source}_${d.target}`)
const tooltipText = `<b>Relation ${d.source} → ${
d.target
}</b><br>Backpressure: ${
bpValue != null ? `${(bpValue * 100).toFixed(2)}%` : "N/A"
}`
d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("position", "absolute")
.style("background", "white")
.style("padding", "10px")
.style("border", "1px solid #ddd")
.style("border-radius", "4px")
.style("pointer-events", "none")
.style("left", event.pageX + 10 + "px")
.style("top", event.pageY + 10 + "px")
.style("font-size", "12px")
.html(tooltipText)
})
.on("mousemove", (event) => {
d3.select(".tooltip")
.style("left", event.pageX + 10 + "px")
.style("top", event.pageY + 10 + "px")
})
.on("mouseout", () => {
d3.selectAll(".tooltip").remove()
})
return sel
}
const createEdge = (sel: Enter<EdgeSelection>) =>
sel.append("path").attr("class", "edge").call(applyEdge)
edgeSelection.exit().remove()
edgeSelection.enter().call(createEdge)
edgeSelection.call(applyEdge)
const applyNode = (g: NodeSelection) => {
g.attr("transform", ({ x, y }) => `translate(${x},${y})`)
// Rectangle box of relation
let rect = g.select<SVGRectElement>("rect")
if (rect.empty()) {
rect = g.append("rect")
}
rect
.attr("width", boxWidth)
.attr("height", boxHeight)
.attr("rx", 6) // rounded corners
.attr("ry", 6)
.attr("fill", "white")
.attr("stroke", ({ id }) =>
isSelected(id) ? theme.colors.blue["500"] : theme.colors.gray["200"]
)
.attr("stroke-width", 2)
// Icon circle of relation type
let circle = g.select<SVGCircleElement>("circle")
if (circle.empty()) {
circle = g.append("circle")
}
circle
.attr("cx", iconRadius + 10) // position circle in left part of box
.attr("cy", boxHeight / 2)
.attr("r", iconRadius)
.attr("fill", ({ id, relation }) => {
const weight = relationIsStreamingJob(relation) ? "500" : "400"
const baseColor = isSelected(id)
? theme.colors.blue[weight]
: theme.colors.gray[weight]
if (relationStats) {
const relationId = parseInt(id)
if (!isNaN(relationId) && relationStats[relationId]) {
const currentMs = epochToUnixMillis(
relationStats[relationId].currentEpoch
)
return latencyToColor(now_ms - currentMs, baseColor)
}
}
return baseColor
})
// Type letter in circle
let typeText = g.select<SVGTextElement>(".type")
if (typeText.empty()) {
typeText = g.append("text").attr("class", "type")
}
function relationTypeAbbr(relation: Relation) {
const type = relationType(relation)
if (type === "SINK") {
return "K"
} else {
return type.charAt(0)
}
}
// Add a clipPath to contain the text within the box
let clipPath = g.select<SVGClipPathElement>(".clip-path")
if (clipPath.empty()) {
clipPath = g
.append("clipPath")
.attr("class", "clip-path")
.attr("id", (d) => `clip-${d.id}`)
clipPath.append("rect")
}
clipPath
.select("rect")
.attr("width", boxWidth - (iconRadius * 2 + 20)) // Leave space for icon
.attr("height", boxHeight)
.attr("x", iconRadius * 2 + 15)
.attr("y", 0)
typeText
.attr("fill", "white")
.text(({ relation }) => `${relationTypeAbbr(relation)}`)
.attr("font-family", "inherit")
.attr("text-anchor", "middle")
.attr("x", iconRadius + 10)
.attr("y", boxHeight / 2)
.attr("dy", "0.35em") // vertical alignment
.attr("font-size", 16)
.attr("font-weight", "bold")
// Relation name
let text = g.select<SVGTextElement>(".text")
if (text.empty()) {
text = g.append("text").attr("class", "text")
}
text
.attr("fill", "black")
.text(({ name }) => name)
.attr("font-family", "inherit")
.attr("x", iconRadius * 2 + 15) // position text right of circle
.attr("y", boxHeight / 2)
.attr("dy", "0.35em")
.attr("font-size", 14)
.attr("clip-path", (d) => `url(#clip-${d.id})`) // Apply clipPath
// Tooltip for relation
const getTooltipContent = (relation: Relation, id: string) => {
const relationId = parseInt(id)
const stats = relationStats?.[relationId]
const latencySeconds = stats
? (
(Date.now() - epochToUnixMillis(stats.currentEpoch)) /
1000
).toFixed(2)
: "N/A"
const epoch = stats?.currentEpoch ?? "N/A"
return `<b>${relation.name} (${relationTypeTitleCase(
relation
)})</b><br>Epoch: ${epoch}<br>Latency: ${latencySeconds} seconds`
}
g.on("mouseover", (event, { relation, id }) => {
// Remove existing tooltip if any
d3.selectAll(".tooltip").remove()
// Create new tooltip
d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("position", "absolute")
.style("background", "white")
.style("padding", "10px")
.style("border", "1px solid #ddd")
.style("border-radius", "4px")
.style("pointer-events", "none")
.style("left", event.pageX + 10 + "px")
.style("top", event.pageY + 10 + "px")
.style("font-size", "12px")
.html(getTooltipContent(relation, id))
})
.on("mousemove", (event) => {
d3.select(".tooltip")
.style("left", event.pageX + 10 + "px")
.style("top", event.pageY + 10 + "px")
})
.on("mouseout", () => {
d3.selectAll(".tooltip").remove()
})
// Relation modal
g.style("cursor", "pointer").on("click", (_, { relation, id }) => {
setSelectedId(id)
setModalId(relation.id)
})
return g
}
const createNode = (sel: Enter<NodeSelection>) =>
sel.append("g").attr("class", "node").call(applyNode)
const g = svgSelection.select(".boxes")
const nodeSelection = g
.selectAll<SVGGElement, null>(".node")
.data(layoutMap)
type NodeSelection = typeof nodeSelection
nodeSelection.enter().call(createNode)
nodeSelection.call(applyNode)
nodeSelection.exit().remove()
}, [
layoutMap,
links,
selectedId,
setModalId,
setSelectedId,
backPressures,
relationStats,
])
return (
<>
<svg ref={svgRef} width={`${width}px`} height={`${height}px`}>
<g className="edges" />
<g className="boxes" />
</svg>
<CatalogModal modalData={modalData} onClose={() => setModalId(null)} />
</>
)
}