|
| 1 | +import { Edge, NodeProps, Node, MarkerType, Position } from "@xyflow/react"; |
| 2 | +import Dagre from "@dagrejs/dagre"; |
| 3 | + |
| 4 | +export interface ExplanationMysql { |
| 5 | + query_block: { |
| 6 | + id: string; |
| 7 | + cost_info: { |
| 8 | + query_cost: number; |
| 9 | + prefix_cost: number; |
| 10 | + }; |
| 11 | + table?: { |
| 12 | + id: string; |
| 13 | + table_name: string; |
| 14 | + cost_info: { |
| 15 | + query_cost: number; |
| 16 | + prefix_cost: number; |
| 17 | + }; |
| 18 | + }; |
| 19 | + nested_loop?: { |
| 20 | + table: { |
| 21 | + id: string; |
| 22 | + table_name: string; |
| 23 | + cost_info: { |
| 24 | + query_cost: number; |
| 25 | + prefix_cost: number; |
| 26 | + }; |
| 27 | + rows_produced_per_join: string; |
| 28 | + }; |
| 29 | + }[]; |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +export interface ExplainNodeProps extends NodeProps { |
| 34 | + data: { |
| 35 | + id: string; |
| 36 | + cost_info: { |
| 37 | + query_cost: number; |
| 38 | + prefix_cost: number; |
| 39 | + read_cost: number; |
| 40 | + eval_cost: number; |
| 41 | + }; |
| 42 | + key: string; |
| 43 | + select_id: number; |
| 44 | + label: string; |
| 45 | + target?: string; |
| 46 | + rows_examined_per_scan: string; |
| 47 | + rows_produced_per_join: string; |
| 48 | + access_type: string; |
| 49 | + table_name: string; |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +const dagreGraph = new Dagre.graphlib.Graph().setDefaultEdgeLabel(() => ({})); |
| 54 | +const nodeWidth = 172; |
| 55 | +const nodeHeight = 36; |
| 56 | +const position = { x: 0, y: 0 }; |
| 57 | + |
| 58 | +export function formatCost(cost: number) { |
| 59 | + return parseFloat(String(cost)).toLocaleString("en-US", { |
| 60 | + maximumFractionDigits: 2, |
| 61 | + notation: "compact", |
| 62 | + compactDisplay: "short", |
| 63 | + }); |
| 64 | +} |
| 65 | + |
| 66 | +function getLayoutedExplanationElements( |
| 67 | + nodes: Node[], |
| 68 | + edges: Edge[], |
| 69 | + direction = "TB" |
| 70 | +) { |
| 71 | + const isHorizontal = direction === "LR"; |
| 72 | + |
| 73 | + dagreGraph.setGraph({ rankdir: direction }); |
| 74 | + |
| 75 | + nodes.forEach((node) => { |
| 76 | + dagreGraph.setNode(node.id, { |
| 77 | + width: node.measured?.width || nodeWidth, |
| 78 | + height: node.measured?.height || nodeHeight, |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + edges.forEach((edge) => { |
| 83 | + dagreGraph.setEdge(edge.source, edge.target); |
| 84 | + }); |
| 85 | + |
| 86 | + Dagre.layout(dagreGraph); |
| 87 | + |
| 88 | + const newNodes = nodes.map((node) => { |
| 89 | + const nodeWithPosition = dagreGraph.node(node.id); |
| 90 | + const newNode = { |
| 91 | + ...node, |
| 92 | + targetPosition: isHorizontal ? "left" : "top", |
| 93 | + sourcePosition: isHorizontal ? "right" : "bottom", |
| 94 | + // We are shifting the dagre node position (anchor=center center) to the top left |
| 95 | + // so it matches the React Flow node anchor point (top left). |
| 96 | + position: { |
| 97 | + x: nodeWithPosition.x - (node.measured?.width || nodeWidth) / 2, |
| 98 | + y: nodeWithPosition.y - (node.measured?.height || nodeHeight) / 2, |
| 99 | + }, |
| 100 | + }; |
| 101 | + |
| 102 | + return newNode; |
| 103 | + }); |
| 104 | + |
| 105 | + return { nodes: newNodes, edges }; |
| 106 | +} |
| 107 | + |
| 108 | +export function buildQueryExplanationFlow(item: ExplanationMysql) { |
| 109 | + const nodes: Node[] = []; |
| 110 | + const edges: Edge[] = []; |
| 111 | + // Keep all tables |
| 112 | + const nodesTables = new Set(); |
| 113 | + const edgesTable = new Set(); |
| 114 | + |
| 115 | + const table = item.query_block.table || null; |
| 116 | + const nested_loop = item.query_block.nested_loop || []; |
| 117 | + const nested_reverse = (nested_loop || []).reverse(); |
| 118 | + |
| 119 | + if (item.query_block) { |
| 120 | + nodes.push({ |
| 121 | + id: "query_block", |
| 122 | + data: { ...item.query_block }, |
| 123 | + type: "QUERY_BLOCK", |
| 124 | + position, |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + if (table) { |
| 129 | + nodes.push({ |
| 130 | + id: table.table_name, |
| 131 | + data: table, |
| 132 | + type: "TABLE", |
| 133 | + position, |
| 134 | + }); |
| 135 | + |
| 136 | + edges.push({ |
| 137 | + id: `query_block-${table.table_name}`, |
| 138 | + source: "query_block", |
| 139 | + target: table.table_name, |
| 140 | + sourceHandle: "query_block", |
| 141 | + targetHandle: table.table_name, |
| 142 | + type: "smoothstep", |
| 143 | + markerStart: { |
| 144 | + type: MarkerType.ArrowClosed, |
| 145 | + width: 14, |
| 146 | + height: 14, |
| 147 | + }, |
| 148 | + style: { |
| 149 | + strokeWidth: 2, |
| 150 | + }, |
| 151 | + animated: true, |
| 152 | + }); |
| 153 | + } |
| 154 | + |
| 155 | + if (!table && nested_reverse.length > 0) { |
| 156 | + for (const [index, value] of nested_reverse.entries()) { |
| 157 | + nodes.push({ |
| 158 | + id: "nested_loop_" + index, |
| 159 | + data: { |
| 160 | + label: "nested loop", |
| 161 | + cost_info: { |
| 162 | + prefix_cost: value.table.cost_info.prefix_cost, |
| 163 | + }, |
| 164 | + target: `nested_loop_${index}-${value.table.table_name}`, |
| 165 | + rows_produced_per_join: value.table.rows_produced_per_join, |
| 166 | + }, |
| 167 | + type: "NESTED_LOOP", |
| 168 | + position, |
| 169 | + measured: { |
| 170 | + width: 100, |
| 171 | + height: 50, |
| 172 | + }, |
| 173 | + }); |
| 174 | + edges.push({ |
| 175 | + id: |
| 176 | + index === 0 |
| 177 | + ? "query_block-nested_loop_0" |
| 178 | + : `nested_loop_${index - 1}-nested_loop_${index}`, |
| 179 | + source: index === 0 ? "query_block" : "nested_loop_" + (index - 1), |
| 180 | + target: index === 0 ? "nested_loop_0" : "nested_loop_" + index, |
| 181 | + sourceHandle: index === 0 ? "query_block" : "left", |
| 182 | + targetHandle: index === 0 ? "nested_loop_0" : "nested_loop_" + index, |
| 183 | + type: "smoothstep", |
| 184 | + markerStart: { |
| 185 | + type: MarkerType.ArrowClosed, |
| 186 | + width: 14, |
| 187 | + height: 14, |
| 188 | + }, |
| 189 | + style: { |
| 190 | + strokeWidth: 2, |
| 191 | + }, |
| 192 | + animated: true, |
| 193 | + label: formatCost(Number(value.table.rows_produced_per_join)) + " rows", |
| 194 | + labelShowBg: false, |
| 195 | + labelStyle: { |
| 196 | + fill: "#AAAAAA", |
| 197 | + color: "#AAAAAA", |
| 198 | + transform: "translate(-5%, -5%)", |
| 199 | + }, |
| 200 | + }); |
| 201 | + } |
| 202 | + |
| 203 | + const layout = getLayoutedExplanationElements(nodes, edges, "RL"); |
| 204 | + |
| 205 | + for (const [index, value] of nested_reverse.entries()) { |
| 206 | + const key = index === nested_reverse.length - 1 ? index - 1 : index; |
| 207 | + const nested = layout.nodes.find((f) => f.id === `nested_loop_${index}`); |
| 208 | + nodesTables.add({ |
| 209 | + id: value.table.table_name, |
| 210 | + data: { |
| 211 | + ...value.table, |
| 212 | + }, |
| 213 | + type: "TABLE", |
| 214 | + position: { |
| 215 | + x: (nested?.position.x || 0) + 50, |
| 216 | + y: (nested?.position.y || 0) + (nested?.measured?.height || 0) + 100, |
| 217 | + }, |
| 218 | + targetPosition: Position.Top, |
| 219 | + sourcePosition: Position.Bottom, |
| 220 | + }); |
| 221 | + edgesTable.add({ |
| 222 | + id: `nested_loop_${key}-${value.table.table_name}`, |
| 223 | + source: "nested_loop_" + key, |
| 224 | + target: value.table.table_name, |
| 225 | + sourceHandle: index === nested_reverse.length - 1 ? "left" : "bottom", |
| 226 | + targetHandle: value.table.table_name, |
| 227 | + type: "smoothstep", |
| 228 | + markerStart: { |
| 229 | + type: MarkerType.ArrowClosed, |
| 230 | + width: 14, |
| 231 | + height: 14, |
| 232 | + }, |
| 233 | + style: { |
| 234 | + strokeWidth: 2, |
| 235 | + }, |
| 236 | + animated: true, |
| 237 | + }); |
| 238 | + } |
| 239 | + return { |
| 240 | + nodes: [ |
| 241 | + ...layout.nodes.filter((_, i) => i < layout.nodes.length - 1), |
| 242 | + ...nodesTables, |
| 243 | + ], |
| 244 | + edges: [...layout.edges, ...edgesTable], |
| 245 | + }; |
| 246 | + } |
| 247 | + |
| 248 | + return { |
| 249 | + nodes: [], |
| 250 | + edges: [], |
| 251 | + }; |
| 252 | +} |
0 commit comments