Browse Source

make sure to add all variable nodes to dag before linking variables

Marcus Efraimsson 7 years ago
parent
commit
4b1a2d3b11

+ 12 - 0
public/app/core/utils/dag.test.ts

@@ -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");
+    });
   });
   });
 });
 });

+ 18 - 2
public/app/core/utils/dag.ts

@@ -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);
       }
       }

+ 4 - 2
public/app/features/templating/variable_srv.ts

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