@kinvolk/headlamp-plugin
Version:
The needed infrastructure for building Headlamp plugins.
51 lines (50 loc) • 1.64 kB
JavaScript
/*
* Copyright 2025 The Kubernetes Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* 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(nodes, edges) {
const nodeMap = new Map();
nodes.forEach(n => {
nodeMap.set(n.id, n);
});
// Create map for incoming and outgoing edges where key is node ID
const outgoingEdges = new Map();
const incomingEdges = new Map();
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);
},
};
}