-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2538 from headlamp-k8s/map-perf
frontend: Improve Map performance
- Loading branch information
Showing
7 changed files
with
185 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
frontend/src/components/resourceMap/graph/graphLookup.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { makeGraphLookup } from './graphLookup'; | ||
import { GraphEdge, GraphNode } from './graphModel'; | ||
|
||
describe('GraphLookup', () => { | ||
const nodes: GraphNode[] = [ | ||
{ id: '1', type: 'kubeObject', data: {} as any }, | ||
{ id: '2', type: 'kubeObject', data: {} as any }, | ||
{ id: '3', type: 'kubeObject', data: {} as any }, | ||
]; | ||
|
||
const edges: GraphEdge[] = [ | ||
{ id: 'e1', source: '1', target: '2', type: 'typeA' }, | ||
{ id: 'e2', source: '2', target: '3', type: 'typeB' }, | ||
{ id: 'e3', source: '1', target: '3', type: 'typeC' }, | ||
]; | ||
|
||
const graphLookup = makeGraphLookup(nodes, edges); | ||
|
||
it('should get outgoing edges for a node', () => { | ||
const outgoingEdges = graphLookup.getOutgoingEdges('1'); | ||
expect(outgoingEdges).toEqual([ | ||
{ id: 'e1', source: '1', target: '2', type: 'typeA' }, | ||
{ id: 'e3', source: '1', target: '3', type: 'typeC' }, | ||
]); | ||
}); | ||
|
||
it('should get incoming edges for a node', () => { | ||
const incomingEdges = graphLookup.getIncomingEdges('3'); | ||
expect(incomingEdges).toEqual([ | ||
{ id: 'e2', source: '2', target: '3', type: 'typeB' }, | ||
{ id: 'e3', source: '1', target: '3', type: 'typeC' }, | ||
]); | ||
}); | ||
|
||
it('should get a node by its ID', () => { | ||
const node = graphLookup.getNode('2'); | ||
expect(node).toEqual({ id: '2', type: 'kubeObject', data: {} }); | ||
}); | ||
|
||
it('should return undefined for non-existent node ID', () => { | ||
const node = graphLookup.getNode('non-existent'); | ||
expect(node).toBeUndefined(); | ||
}); | ||
|
||
it('should return undefined for outgoing edges of non-existent node ID', () => { | ||
const outgoingEdges = graphLookup.getOutgoingEdges('non-existent'); | ||
expect(outgoingEdges).toBeUndefined(); | ||
}); | ||
|
||
it('should return undefined for incoming edges of non-existent node ID', () => { | ||
const incomingEdges = graphLookup.getIncomingEdges('non-existent'); | ||
expect(incomingEdges).toBeUndefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { GraphEdge, GraphNode } from './graphModel'; | ||
|
||
/** | ||
* Constant time lookup of graph elements | ||
*/ | ||
export interface GraphLookup<N, E> { | ||
/** Get list of outgoing edges from the given node */ | ||
getOutgoingEdges(nodeId: string): E[] | undefined; | ||
/** Get list of incoming edges to the given node */ | ||
getIncomingEdges(nodeId: string): E[] | undefined; | ||
/** Get Node by its' ID */ | ||
getNode(nodeId: string): N | undefined; | ||
} | ||
|
||
/** | ||
* Creates a utility for constant time lookup of graph elements | ||
* | ||
* @param nodes - list of graph Nodes | ||
* @param edges - list of graph Edges | ||
* @returns lookup {@link GraphLookup} | ||
*/ | ||
export function makeGraphLookup<N extends GraphNode, E extends GraphEdge>( | ||
nodes: N[], | ||
edges: E[] | ||
): GraphLookup<N, E> { | ||
const nodeMap = new Map<string, N>(); | ||
nodes.forEach(n => { | ||
nodeMap.set(n.id, n); | ||
}); | ||
|
||
// Create map for incoming and outgoing edges where key is node ID | ||
const outgoingEdges = new Map<string, E[]>(); | ||
const incomingEdges = new Map<string, E[]>(); | ||
|
||
edges.forEach(edge => { | ||
const s = outgoingEdges.get(edge.source) ?? []; | ||
s.push(edge); | ||
outgoingEdges.set(edge.source, s); | ||
|
||
const t = incomingEdges.get(edge.target) ?? []; | ||
t.push(edge); | ||
incomingEdges.set(edge.target, t); | ||
}); | ||
|
||
return { | ||
getOutgoingEdges(nodeId) { | ||
return outgoingEdges.get(nodeId); | ||
}, | ||
getIncomingEdges(nodeId) { | ||
return incomingEdges.get(nodeId); | ||
}, | ||
getNode(nodeId) { | ||
return nodeMap.get(nodeId); | ||
}, | ||
}; | ||
} |
Oops, something went wrong.