Browse Source

Graphite: Fixes issue with seriesByTag & function with variable param (#17795)

Fixes #17696
Torkel Ödegaard 6 years ago
parent
commit
8fcc370fe9

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

@@ -29,6 +29,7 @@ export default class GraphiteQuery {
     this.functions = [];
     this.functions = [];
     this.segments = [];
     this.segments = [];
     this.tags = [];
     this.tags = [];
+    this.seriesByTagUsed = false;
     this.error = null;
     this.error = null;
 
 
     if (this.target.textEditor) {
     if (this.target.textEditor) {
@@ -57,17 +58,6 @@ export default class GraphiteQuery {
     }
     }
 
 
     this.checkOtherSegmentsIndex = this.segments.length - 1;
     this.checkOtherSegmentsIndex = this.segments.length - 1;
-    this.checkForSeriesByTag();
-  }
-
-  checkForSeriesByTag() {
-    const seriesByTagFunc: any = _.find(this.functions, func => func.def.name === 'seriesByTag');
-    if (seriesByTagFunc) {
-      this.seriesByTagUsed = true;
-      seriesByTagFunc.hidden = true;
-      const tags = this.splitSeriesByTagParams(seriesByTagFunc);
-      this.tags = tags;
-    }
   }
   }
 
 
   getSegmentPathUpTo(index) {
   getSegmentPathUpTo(index) {
@@ -98,6 +88,14 @@ export default class GraphiteQuery {
 
 
         innerFunc.updateText();
         innerFunc.updateText();
         this.functions.push(innerFunc);
         this.functions.push(innerFunc);
+
+        // extract tags from seriesByTag function and hide function
+        if (innerFunc.def.name === 'seriesByTag' && !this.seriesByTagUsed) {
+          this.seriesByTagUsed = true;
+          innerFunc.hidden = true;
+          this.tags = this.splitSeriesByTagParams(innerFunc);
+        }
+
         break;
         break;
       case 'series-ref':
       case 'series-ref':
         if (this.segments.length > 0 || this.getSeriesByTagFuncIndex() >= 0) {
         if (this.segments.length > 0 || this.getSeriesByTagFuncIndex() >= 0) {
@@ -112,7 +110,7 @@ export default class GraphiteQuery {
         this.addFunctionParameter(func, astNode.value);
         this.addFunctionParameter(func, astNode.value);
         break;
         break;
       case 'metric':
       case 'metric':
-        if (this.segments.length > 0) {
+        if (this.segments.length || this.tags.length) {
           this.addFunctionParameter(func, _.join(_.map(astNode.segments, 'value'), '.'));
           this.addFunctionParameter(func, _.join(_.map(astNode.segments, 'value'), '.'));
         } else {
         } else {
           this.segments = astNode.segments;
           this.segments = astNode.segments;

+ 13 - 0
public/app/plugins/datasource/graphite/specs/graphite_query.test.ts

@@ -57,4 +57,17 @@ describe('Graphite query model', () => {
       expect(ctx.queryModel.functions[1].params[0]).toBe('#A');
       expect(ctx.queryModel.functions[1].params[0]).toBe('#A');
     });
     });
   });
   });
+
+  describe('when query has seriesByTag and highestMax with variable param', () => {
+    beforeEach(() => {
+      ctx.target = { refId: 'A', target: `highestMax(seriesByTag('namespace=asd'), $limit)` };
+      ctx.targets = [ctx.target];
+      ctx.queryModel = new GraphiteQuery(ctx.datasource, ctx.target, ctx.templateSrv);
+    });
+
+    it('should add $limit to highestMax function param', () => {
+      expect(ctx.queryModel.segments.length).toBe(0);
+      expect(ctx.queryModel.functions[1].params[0]).toBe('$limit');
+    });
+  });
 });
 });