浏览代码

improve handling of query references

Dan Cech 8 年之前
父节点
当前提交
3a1700cbee

+ 12 - 11
public/app/plugins/datasource/graphite/graphite_query.ts

@@ -91,27 +91,25 @@ export default class GraphiteQuery {
         this.functions.push(innerFunc);
         break;
       case 'series-ref':
-        this.addFunctionParameter(func, astNode.value, index, this.segments.length > 0);
+        if (this.segments.length > 0) {
+          this.addFunctionParameter(func, astNode.value, index, true);
+        } else {
+          this.segments.push(astNode);
+        }
         break;
       case 'bool':
       case 'string':
       case 'number':
-        if ((index-1) >= func.def.params.length) {
-          throw { message: 'invalid number of parameters to method ' + func.def.name };
-        }
         var shiftBack = this.isShiftParamsBack(func);
         this.addFunctionParameter(func, astNode.value, index, shiftBack);
-      break;
+        break;
       case 'metric':
         if (this.segments.length > 0) {
-        if (astNode.segments.length !== 1) {
-          throw { message: 'Multiple metric params not supported, use text editor.' };
+          this.addFunctionParameter(func, _.join(_.map(astNode.segments, 'value'), '.'), index, true);
+        } else {
+          this.segments = astNode.segments;
         }
-        this.addFunctionParameter(func, astNode.segments[0].value, index, true);
         break;
-      }
-
-      this.segments = astNode.segments;
     }
   }
 
@@ -149,6 +147,9 @@ export default class GraphiteQuery {
     if (shiftBack) {
       index = Math.max(index - 1, 0);
     }
+    if (index > func.def.params.length) {
+      throw { message: 'too many parameters for function ' + func.def.name };
+    }
     func.params[index] = value;
   }
 

+ 20 - 1
public/app/plugins/datasource/graphite/query_ctrl.ts

@@ -62,6 +62,10 @@ export class GraphiteQueryCtrl extends QueryCtrl {
   }
 
   checkOtherSegments(fromIndex) {
+    if (this.queryModel.segments.length === 1 && this.queryModel.segments[0].type === 'series-ref') {
+      return;
+    }
+
     if (fromIndex === 0) {
       this.addSelectMetricSegment();
       return;
@@ -108,8 +112,23 @@ export class GraphiteQueryCtrl extends QueryCtrl {
 
       if (altSegments.length === 0) { return altSegments; }
 
+      // add query references
+      if (index === 0) {
+        _.eachRight(this.panelCtrl.panel.targets, target => {
+          if (target.refId === this.queryModel.target.refId) {
+            return;
+          }
+
+          altSegments.unshift(this.uiSegmentSrv.newSegment({
+            type: 'series-ref',
+            value: '#' + target.refId,
+            expandable: false,
+          }));
+        });
+      }
+
       // add template variables
-      _.each(this.templateSrv.variables, variable => {
+      _.eachRight(this.templateSrv.variables, variable => {
         altSegments.unshift(this.uiSegmentSrv.newSegment({
           type: 'template',
           value: '$' + variable.name,

+ 3 - 3
public/app/plugins/datasource/graphite/specs/query_ctrl_specs.ts

@@ -95,11 +95,11 @@ describe('GraphiteQueryCtrl', function() {
     });
 
     it('should not add select metric segment', function() {
-      expect(ctx.ctrl.segments.length).to.be(0);
+      expect(ctx.ctrl.segments.length).to.be(1);
     });
 
-    it('should add both series refs as params', function() {
-      expect(ctx.ctrl.queryModel.functions[0].params.length).to.be(2);
+    it('should add second series ref as param', function() {
+      expect(ctx.ctrl.queryModel.functions[0].params.length).to.be(1);
     });
   });