Graphite: Fix drag and drop queries (#39130)

* Do not use "select metric" segment when rendering target

* Use better names for tests
This commit is contained in:
Piotr Jamróz 2021-09-13 20:10:35 +02:00 committed by GitHub
parent 2cc0788187
commit 26bb08e1e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

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

View File

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