Skip to content

Commit

Permalink
Fixes #59513 by hiding one of the symmetric edges rather than omiting…
Browse files Browse the repository at this point in the history
… it (#59514)

in cytoscape graph. Also selects root nodes with no incoming edges
rather than just unconnected nodes.

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
ogupte and elasticmachine authored Mar 10, 2020
1 parent a663f65 commit 35bcb36
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ function getLayoutOptions(
};
}

function selectRoots(elements: cytoscape.ElementDefinition[]): string[] {
const nodes = cytoscape({ elements }).nodes();
const unconnectedNodes = nodes.roots().intersection(nodes.leaves());
function selectRoots(cy: cytoscape.Core): string[] {
const nodes = cy.nodes();
const roots = nodes.roots();
const rumNodes = nodes.filter(node => isRumAgentName(node.data('agentName')));
return rumNodes.union(unconnectedNodes).map(node => node.id());
return rumNodes.union(roots).map(node => node.id());
}

export function Cytoscape({
Expand Down Expand Up @@ -118,7 +118,7 @@ export function Cytoscape({
}

if (event.cy.elements().length > 0) {
const selectedRoots = selectRoots(elements);
const selectedRoots = selectRoots(event.cy);
const layout = cy.layout(
getLayoutOptions(selectedRoots, height, width)
);
Expand All @@ -130,7 +130,7 @@ export function Cytoscape({
}
}
},
[cy, serviceName, elements, height, width]
[cy, serviceName, height, width]
);

// Trigger a custom "data" event when data changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,15 @@ export function getCytoscapeElements(

// instead of adding connections in two directions,
// we add a `bidirectional` flag to use in styling
// and hide the inverse edge when rendering
const dedupedConnections = (sortBy(
Object.values(connectionsById),
// make sure that order is stable
'id'
) as ConnectionWithId[]).reduce<
Array<ConnectionWithId & { bidirectional?: boolean }>
Array<
ConnectionWithId & { bidirectional?: boolean; isInverseEdge?: boolean }
>
>((prev, connection) => {
const reversedConnection = prev.find(
c =>
Expand All @@ -151,7 +154,10 @@ export function getCytoscapeElements(

if (reversedConnection) {
reversedConnection.bidirectional = true;
return prev;
return prev.concat({
...connection,
isInverseEdge: true
});
}

return prev.concat(connection);
Expand All @@ -160,6 +166,7 @@ export function getCytoscapeElements(
const cyEdges = dedupedConnections.map(connection => {
return {
group: 'edges' as const,
classes: connection.isInverseEdge ? 'invisible' : undefined,
data: {
id: connection.id,
source: connection.source.id,
Expand Down

0 comments on commit 35bcb36

Please sign in to comment.