diff --git a/.betterer.results b/.betterer.results index eab4907dff3..b4bffc4f568 100644 --- a/.betterer.results +++ b/.betterer.results @@ -4219,8 +4219,8 @@ exports[`better eslint`] = { [0, 0, 0, "Unexpected any. Specify a different type.", "49"], [0, 0, 0, "Unexpected any. Specify a different type.", "50"], [0, 0, 0, "Unexpected any. Specify a different type.", "51"], - [0, 0, 0, "Unexpected any. Specify a different type.", "52"], - [0, 0, 0, "Do not use any type assertions.", "53"], + [0, 0, 0, "Do not use any type assertions.", "52"], + [0, 0, 0, "Unexpected any. Specify a different type.", "53"], [0, 0, 0, "Unexpected any. Specify a different type.", "54"], [0, 0, 0, "Unexpected any. Specify a different type.", "55"], [0, 0, 0, "Unexpected any. Specify a different type.", "56"], diff --git a/public/app/plugins/datasource/opentsdb/datasource.ts b/public/app/plugins/datasource/opentsdb/datasource.ts index da0ea1ae059..e24677cae61 100644 --- a/public/app/plugins/datasource/opentsdb/datasource.ts +++ b/public/app/plugins/datasource/opentsdb/datasource.ts @@ -497,14 +497,7 @@ export default class OpenTsDatasource extends DataSourceApi 0) { - query.filters = cloneDeep(target.filters); - - if (query.filters) { - this.interpolateVariablesInFilters(query, options); - } - } else { - query.tags = cloneDeep(target.tags); - - if (query.tags) { - for (const tagKey in query.tags) { - query.tags[tagKey] = this.templateSrv.replace(query.tags[tagKey], options.scopedVars, 'pipe'); - } - } - } - if (target.explicitTags) { query.explicitTags = true; } @@ -563,11 +540,11 @@ export default class OpenTsDatasource extends DataSourceApi) { + interpolateVariablesInFilters(query: OpenTsdbQuery, scopedVars: ScopedVars) { query.filters = query.filters?.map((filter: OpenTsdbFilter): OpenTsdbFilter => { - filter.tagk = this.templateSrv.replace(filter.tagk, options.scopedVars, 'pipe'); + filter.tagk = this.templateSrv.replace(filter.tagk, scopedVars, 'pipe'); - filter.filter = this.templateSrv.replace(filter.filter, options.scopedVars, 'pipe'); + filter.filter = this.templateSrv.replace(filter.filter, scopedVars, 'pipe'); return filter; }); @@ -602,10 +579,30 @@ export default class OpenTsDatasource extends DataSourceApi ({ - ...query, - metric: this.templateSrv.replace(query.metric, scopedVars), - })); + return queries.map((query) => this.interpolateVariablesInQuery(query, scopedVars)); + } + + interpolateVariablesInQuery(target: OpenTsdbQuery, scopedVars: ScopedVars): any { + const query = cloneDeep(target); + + query.metric = this.templateSrv.replace(target.metric, scopedVars, 'pipe'); + + query.aggregator = 'avg'; + if (target.aggregator) { + query.aggregator = this.templateSrv.replace(target.aggregator); + } + + if (query.filters && query.filters.length > 0) { + this.interpolateVariablesInFilters(query, scopedVars); + } else { + if (query.tags) { + for (const tagKey in query.tags) { + query.tags[tagKey] = this.templateSrv.replace(query.tags[tagKey], scopedVars, 'pipe'); + } + } + } + + return query; } convertToTSDBTime(date: any, roundUp: any, timezone: any) { diff --git a/public/app/plugins/datasource/opentsdb/specs/datasource.test.ts b/public/app/plugins/datasource/opentsdb/specs/datasource.test.ts index 85dc68d61b2..f854e6856e2 100644 --- a/public/app/plugins/datasource/opentsdb/specs/datasource.test.ts +++ b/public/app/plugins/datasource/opentsdb/specs/datasource.test.ts @@ -128,7 +128,7 @@ describe('opentsdb', () => { expect(ds.interpolateVariablesInQueries([], {})).toHaveLength(0); }); - it('should replace metric variable', () => { + it('should replace metric and filter variable', () => { const { ds, templateSrv } = getTestcontext(); const logQuery: OpenTsdbQuery = { refId: 'someRefId', @@ -136,8 +136,8 @@ describe('opentsdb', () => { filters: [ { type: 'type', - tagk: 'someTagk', - filter: 'someTagv', + tagk: '$someTagk', + filter: '$someTagv', groupBy: true, }, ], @@ -145,8 +145,10 @@ describe('opentsdb', () => { ds.interpolateVariablesInQueries([logQuery], {}); - expect(templateSrv.replace).toHaveBeenCalledWith('$someVar', {}); - expect(templateSrv.replace).toHaveBeenCalledTimes(1); + expect(templateSrv.replace).toHaveBeenCalledWith('$someVar', {}, 'pipe'); + expect(templateSrv.replace).toHaveBeenCalledWith('$someTagk', {}, 'pipe'); + expect(templateSrv.replace).toHaveBeenCalledWith('$someTagv', {}, 'pipe'); + expect(templateSrv.replace).toHaveBeenCalledTimes(3); }); it('should replace filter tag key and value', () => { @@ -210,7 +212,7 @@ describe('opentsdb', () => { }, }; - ds.interpolateVariablesInFilters(logQuery, dataQR); + ds.interpolateVariablesInFilters(logQuery, dataQR.scopedVars); expect(templateSrv.replace).toHaveBeenCalledWith('$someTagk', scopedVars, 'pipe'); expect(templateSrv.replace).toHaveBeenCalledWith('$someTagv', scopedVars, 'pipe');