Merge pull request #13651 from grafana/13628_fix_dag

Make sure to add all variable nodes to directed acyclic graph (dag) before linking variables
This commit is contained in:
Marcus Efraimsson 2018-10-15 11:42:59 +02:00 committed by GitHub
commit 9f85a7494a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 4 deletions

View File

@ -104,5 +104,17 @@ describe('Directed acyclic graph', () => {
const actual = nodeH.getOptimizedInputEdges(); const actual = nodeH.getOptimizedInputEdges();
expect(actual).toHaveLength(0); expect(actual).toHaveLength(0);
}); });
it('when linking non-existing input node with existing output node should throw error', () => {
expect(() => {
dag.link('non-existing', 'A');
}).toThrowError("cannot link input node named non-existing since it doesn't exist in graph");
});
it('when linking existing input node with non-existing output node should throw error', () => {
expect(() => {
dag.link('A', 'non-existing');
}).toThrowError("cannot link output node named non-existing since it doesn't exist in graph");
});
}); });
}); });

View File

@ -15,6 +15,14 @@ export class Edge {
} }
link(inputNode: Node, outputNode: Node) { link(inputNode: Node, outputNode: Node) {
if (!inputNode) {
throw Error('inputNode is required');
}
if (!outputNode) {
throw Error('outputNode is required');
}
this.unlink(); this.unlink();
this.inputNode = inputNode; this.inputNode = inputNode;
this.outputNode = outputNode; this.outputNode = outputNode;
@ -152,7 +160,11 @@ export class Graph {
for (let n = 0; n < inputArr.length; n++) { for (let n = 0; n < inputArr.length; n++) {
const i = inputArr[n]; const i = inputArr[n];
if (typeof i === 'string') { if (typeof i === 'string') {
inputNodes.push(this.getNode(i)); const n = this.getNode(i);
if (!n) {
throw Error(`cannot link input node named ${i} since it doesn't exist in graph`);
}
inputNodes.push(n);
} else { } else {
inputNodes.push(i); inputNodes.push(i);
} }
@ -161,7 +173,11 @@ export class Graph {
for (let n = 0; n < outputArr.length; n++) { for (let n = 0; n < outputArr.length; n++) {
const i = outputArr[n]; const i = outputArr[n];
if (typeof i === 'string') { if (typeof i === 'string') {
outputNodes.push(this.getNode(i)); const n = this.getNode(i);
if (!n) {
throw Error(`cannot link output node named ${i} since it doesn't exist in graph`);
}
outputNodes.push(n);
} else { } else {
outputNodes.push(i); outputNodes.push(i);
} }

View File

@ -291,9 +291,11 @@ export class VariableSrv {
createGraph() { createGraph() {
const g = new Graph(); const g = new Graph();
this.variables.forEach(v1 => { this.variables.forEach(v => {
g.createNode(v1.name); g.createNode(v.name);
});
this.variables.forEach(v1 => {
this.variables.forEach(v2 => { this.variables.forEach(v2 => {
if (v1 === v2) { if (v1 === v2) {
return; return;