Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vis: tree display: refactor monolith tree building code #1596

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 48 additions & 38 deletions packages/ott-vis-panel/src/components/TreeDisplay.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useCallback, useEffect, useRef, useState } from "react";
import * as d3 from "d3";
import type { Monolith, SystemState } from "ott-vis/types";
import type { Monolith, Room, SystemState } from "ott-vis/types";
import { dedupeMonoliths } from "aggregate";
import { useEventBus } from "eventbus";

Expand Down Expand Up @@ -60,31 +60,37 @@ function buildFullTree(systemState: SystemState): TreeNode {

function buildMonolithTrees(monoliths: Monolith[]): TreeNode[] {
return dedupeMonoliths(monoliths).map(monolith => {
const roomNodes: TreeNode[] = monolith.rooms.map(room => {
return {
id: room.name,
region: monolith.region,
group: "room",
children: Array.from({ length: room.clients }, (_, index) => {
return {
id: `${room.name}-${index}`,
region: monolith.region,
group: "client",
children: [],
};
}),
};
});
const monolithNode: TreeNode = {
id: monolith.id,
region: monolith.region,
group: "monolith",
children: roomNodes,
children: buildRoomSubtrees(monolith),
};
return monolithNode;
});
}

function buildRoomSubtree(room: Room, region: string): TreeNode {
const roomNode: TreeNode = {
id: room.name,
region: region,
group: "room",
children: Array.from({ length: room.clients }, (_, index) => {
return {
id: `${room.name}-${index}`,
region: region,
group: "client",
children: [],
};
}),
};
return roomNode;
}

function buildRoomSubtrees(monolith: Monolith): TreeNode[] {
return monolith.rooms.map(room => buildRoomSubtree(room, monolith.region));
}

function buildBalancerRegionTree(systemState: SystemState): TreeNode {
const tree: TreeNode = {
id: "root",
Expand Down Expand Up @@ -210,6 +216,30 @@ function mirrorTree(tree: d3.HierarchyNode<TreeNode>): d3.HierarchyNode<TreeNode
return mirrored;
}

function constructMonolithSubtreesBasic(monolithTree: TreeNode, baseNodeRadius: number) {
const root = d3.hierarchy(monolithTree);
const radius = calcGoodTreeRadius(root, baseNodeRadius);
const treeLayout = d3
.tree<TreeNode>()
.size([Math.PI, radius])
.separation((a, b) => (a.parent === b.parent ? 1 : 2) / a.depth);
treeLayout(root);
// precompute radial coordinates
root.each(node => {
if (node.data.group === "client") {
// @ts-expect-error d3 adds x and y to the node
node.y = radius / 2 + 60;
}
// @ts-expect-error d3 adds x and y to the node
const [x, y] = d3.pointRadial(node.x, node.y);
// @ts-expect-error d3 adds x and y to the node
node.x = x;
// @ts-expect-error d3 adds x and y to the node
node.y = y;
});
return root;
}

interface Node {
id: string;
x: number;
Expand Down Expand Up @@ -269,27 +299,7 @@ const TreeDisplay: React.FC<TreeDisplayProps> = ({
// build all the sub-trees first
const builtMonolithTrees: d3.HierarchyNode<TreeNode>[] = [];
for (const monolithTree of monolithTrees) {
const root = d3.hierarchy(monolithTree);
const radius = calcGoodTreeRadius(root, baseNodeRadius);
const treeLayout = d3
.tree<TreeNode>()
.size([Math.PI, radius])
.separation((a, b) => (a.parent === b.parent ? 1 : 2) / a.depth);
treeLayout(root);
// precompute radial coordinates
root.each(node => {
if (node.data.group === "client") {
// @ts-expect-error d3 adds x and y to the node
node.y = radius / 2 + 60;
}
// @ts-expect-error d3 adds x and y to the node
const [x, y] = d3.pointRadial(node.x, node.y);
// @ts-expect-error d3 adds x and y to the node
node.x = x;
// @ts-expect-error d3 adds x and y to the node
node.y = y;
});

const root = constructMonolithSubtreesBasic(monolithTree, baseNodeRadius);
builtMonolithTrees.push(root);
}

Expand Down
Loading