better merge algorithm (#107)

This commit is contained in:
Michael Quigley 2022-12-21 15:08:24 -05:00
parent f6b36f3ca7
commit b05733b602
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
3 changed files with 14 additions and 16 deletions

View File

@ -11,7 +11,7 @@ const Network = (props) => {
useEffect(() => {
const fg = targetRef.current;
fg.d3Force('collide', d3.forceCollide(32));
fg.d3Force('collide', d3.forceCollide(22));
}, []);
const paintNode = (node, ctx) => {

View File

@ -8,7 +8,7 @@ const Visualizer = (props) => {
useEffect(() => {
setNetworkGraph(mergeGraph(networkGraph, props.overview));
}, [props.overview]);
}, [props]);
// fgRef to access force graph controls from this component
let fgRef = () => { };

View File

@ -1,10 +1,10 @@
export const mergeGraph = (oldGraph, newOverview) => {
let graph = {
let newGraph = {
nodes: [],
links: []
}
newOverview.forEach(env => {
graph.nodes.push({
newGraph.nodes.push({
id: env.environment.zId,
label: env.environment.description,
type: "environment"
@ -15,13 +15,13 @@ export const mergeGraph = (oldGraph, newOverview) => {
if(svc.backendProxyEndpoint !== "") {
svcLabel = svc.backendProxyEndpoint;
}
graph.nodes.push({
newGraph.nodes.push({
id: svc.token,
label: svcLabel,
type: "service",
val: 10
});
graph.links.push({
newGraph.links.push({
target: env.environment.zId,
source: svc.token,
color: "#777"
@ -29,14 +29,12 @@ export const mergeGraph = (oldGraph, newOverview) => {
});
}
});
graph.nodes.forEach(newNode => {
let found = oldGraph.nodes.find(oldNode => oldNode.id === newNode.id);
if(found) {
newNode.vx = found.vx;
newNode.vy = found.vy;
newNode.x = found.x;
newNode.y = found.y;
}
})
return graph;
// we want to preserve nodes that exist in the new graph, and remove those that don't.
oldGraph.nodes = oldGraph.nodes.filter(oldNode => newGraph.nodes.find(newNode => newNode.id === oldNode.id));
// and then do the opposite; add any nodes that are in newGraph that are missing from oldGraph.
oldGraph.nodes.push(...newGraph.nodes.filter(newNode => !oldGraph.nodes.find(oldNode => oldNode.id === newNode.id)));
return {
nodes: oldGraph.nodes,
links: newGraph.links,
};
};