Skip to content

Commit

Permalink
Enhance query visualization by preventing duplicate node additions an…
Browse files Browse the repository at this point in the history
…d improving edge connections in flowchart generation (#15025)
  • Loading branch information
gortiz authored Feb 11, 2025
1 parent 1d79a9d commit 5d89aa2
Showing 1 changed file with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,27 +128,53 @@ const layoutNodesAndEdges = (nodes, edges, direction = "TB") => {
* Recursively generates nodes and edges for the flowchart from a hierarchical data structure.
*/
const generateFlowElements = (stats) => {
const stageRoots: Map<Number, Node> = new Map();
const nodes: Node[] = [];
const edges: Edge[] = [];

/**
* Traverses the hierarchy and builds nodes and edges.
*/
const traverseTree = (node, level, index, parentId) => {
const { children, ...data } = node;
const createFlowNode = (data, level, index, parentId) => {

const id = `${level}-${index}`; // Unique ID for the node
const { width, height } = calculateNodeDimensions(data);

// Add the node
nodes.push({ id, type: "customNode", data, position: { x: 0, y: 0 }, width, height });
const flowNode: Node = { id, type: "customNode", data, position: { x: 0, y: 0 }, width, height };
nodes.push(flowNode);

// Add an edge if this node has a parent
if (parentId) {
edges.push({ id: `edge-${parentId}-${id}`, source: parentId, target: id });
}
return flowNode;
}

/**
* Traverses the hierarchy and builds nodes and edges.
*
* Nodes that have been already added to the graph are not added again.
*/
const traverseTree = (node, level, index, parentId) => {
const { children, ...data } = node;

const stageId = data["stage"];
if (stageId) {
const oldFlowNode = stageRoots.get(stageId);
if (oldFlowNode) {
// Add an edge if this node has a parent
if (parentId) {
const id = oldFlowNode.id;
edges.push({ id: `edge-${parentId}-${id}`, source: parentId, target: id });
return;
}
}
}

const newFlowNode = createFlowNode(data, level, index, parentId);
if (stageId) {
stageRoots.set(stageId, newFlowNode);
}
// Recursively process children
children?.forEach((child, idx) => traverseTree(child, level + 1, index + idx, id));
children?.forEach((child, idx) => traverseTree(child, level + 1, index + idx, newFlowNode.id));
};

traverseTree(stats, 0, 0, null); // Start traversal from the root node
Expand Down

0 comments on commit 5d89aa2

Please sign in to comment.