From 26bb08e1e6db11d5ebb55d9a55f474adaffeae0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Jamr=C3=B3z?= Date: Mon, 13 Sep 2021 20:10:35 +0200 Subject: [PATCH] Graphite: Fix drag and drop queries (#39130) * Do not use "select metric" segment when rendering target * Use better names for tests --- .../datasource/graphite/graphite_query.ts | 2 +- .../graphite/specs/graphite_query.test.ts | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/public/app/plugins/datasource/graphite/graphite_query.ts b/public/app/plugins/datasource/graphite/graphite_query.ts index 8b3d45bb1d2..da76d351e42 100644 --- a/public/app/plugins/datasource/graphite/graphite_query.ts +++ b/public/app/plugins/datasource/graphite/graphite_query.ts @@ -180,7 +180,7 @@ export default class GraphiteQuery { }; if (!this.target.textEditor) { - const metricPath = this.getSegmentPathUpTo(this.segments.length).replace(/\.select metric$/, ''); + const metricPath = this.getSegmentPathUpTo(this.segments.length).replace(/\.?select metric$/, ''); this.target.target = reduce(this.functions, wrapFunction, metricPath); } 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 62e5c08505e..16ab9616b01 100644 --- a/public/app/plugins/datasource/graphite/specs/graphite_query.test.ts +++ b/public/app/plugins/datasource/graphite/specs/graphite_query.test.ts @@ -74,4 +74,25 @@ describe('Graphite query model', () => { expect(ctx.queryModel.functions[1].params[0]).toBe('$limit'); }); }); + + describe('when query is generated from segments', () => { + beforeEach(() => { + ctx.target = { refId: 'A', target: '' }; + ctx.queryModel = new GraphiteQuery(ctx.datasource, ctx.target, ctx.templateSrv); + }); + + it('and no segments are selected then the query is empty', () => { + ctx.queryModel.segments = [{ value: 'select metric' }]; + ctx.queryModel.updateModelTarget(ctx.targets); + + expect(ctx.queryModel.target.target).toBe(''); + }); + + it('and some segments are selected then segments without selected value are omitted', () => { + ctx.queryModel.segments = [{ value: 'foo' }, { value: 'bar' }, { value: 'select metric' }]; + ctx.queryModel.updateModelTarget(ctx.targets); + + expect(ctx.queryModel.target.target).toBe('foo.bar'); + }); + }); });