diff --git a/public/app/plugins/datasource/graphite/graphite_query.ts b/public/app/plugins/datasource/graphite/graphite_query.ts index 9a1cb6fd013..1780ce23832 100644 --- a/public/app/plugins/datasource/graphite/graphite_query.ts +++ b/public/app/plugins/datasource/graphite/graphite_query.ts @@ -29,6 +29,7 @@ export default class GraphiteQuery { this.functions = []; this.segments = []; this.tags = []; + this.seriesByTagUsed = false; this.error = null; if (this.target.textEditor) { @@ -57,17 +58,6 @@ export default class GraphiteQuery { } 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) { @@ -98,6 +88,14 @@ export default class GraphiteQuery { innerFunc.updateText(); 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; case 'series-ref': if (this.segments.length > 0 || this.getSeriesByTagFuncIndex() >= 0) { @@ -112,7 +110,7 @@ export default class GraphiteQuery { this.addFunctionParameter(func, astNode.value); break; case 'metric': - if (this.segments.length > 0) { + if (this.segments.length || this.tags.length) { this.addFunctionParameter(func, _.join(_.map(astNode.segments, 'value'), '.')); } else { this.segments = astNode.segments; diff --git a/public/app/plugins/datasource/graphite/specs/graphite_query.test.ts b/public/app/plugins/datasource/graphite/specs/graphite_query.test.ts index 241a21ab40d..1e0da604160 100644 --- a/public/app/plugins/datasource/graphite/specs/graphite_query.test.ts +++ b/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'); }); }); + + 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'); + }); + }); });