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(() => { useEffect(() => {
const fg = targetRef.current; const fg = targetRef.current;
fg.d3Force('collide', d3.forceCollide(32)); fg.d3Force('collide', d3.forceCollide(22));
}, []); }, []);
const paintNode = (node, ctx) => { const paintNode = (node, ctx) => {

View File

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

View File

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