From dc7639354ec6da39a1183d752c366ae0b4b23c4f Mon Sep 17 00:00:00 2001 From: Joey Tawadrous <90795735+joey-grafana@users.noreply.github.com> Date: Thu, 27 Jan 2022 14:00:46 +0000 Subject: [PATCH] InfluxDB: interpolate all variables in queries for dash->explore (for influxql) (#44415) * Moved flux case to its own return * Added missing template replaces for limit, slimit, tz * Added missing template replaces for select * Added missing template replaces for group by * Added tests * PR changes --- .../plugins/datasource/influxdb/datasource.ts | 37 +++++++++++++- .../influxdb/specs/datasource.test.ts | 51 +++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/public/app/plugins/datasource/influxdb/datasource.ts b/public/app/plugins/datasource/influxdb/datasource.ts index 38d0e6a74bb..6f5239f3bb4 100644 --- a/public/app/plugins/datasource/influxdb/datasource.ts +++ b/public/app/plugins/datasource/influxdb/datasource.ts @@ -340,17 +340,52 @@ export default class InfluxDatasource extends DataSourceWithBackend 0) { expandedQueries = queries.map((query) => { + if (this.isFlux) { + return { + ...query, + datasource: this.getRef(), + query: this.templateSrv.replace(query.query ?? '', scopedVars, 'regex'), + }; + } + const expandedQuery = { ...query, datasource: this.getRef(), measurement: this.templateSrv.replace(query.measurement ?? '', scopedVars, 'regex'), policy: this.templateSrv.replace(query.policy ?? '', scopedVars, 'regex'), + limit: this.templateSrv.replace(query.limit?.toString() ?? '', scopedVars, 'regex'), + slimit: this.templateSrv.replace(query.slimit?.toString() ?? '', scopedVars, 'regex'), + tz: this.templateSrv.replace(query.tz ?? '', scopedVars), }; - if (query.rawQuery || this.isFlux) { + if (query.rawQuery) { expandedQuery.query = this.templateSrv.replace(query.query ?? '', scopedVars, 'regex'); } + if (query.groupBy) { + expandedQuery.groupBy = query.groupBy.map((groupBy) => { + return { + ...groupBy, + params: groupBy.params?.map((param) => { + return this.templateSrv.replace(param.toString(), undefined, 'regex'); + }), + }; + }); + } + + if (query.select) { + expandedQuery.select = query.select.map((selects) => { + return selects.map((select: any) => { + return { + ...select, + params: select.params?.map((param: any) => { + return this.templateSrv.replace(param.toString(), undefined, 'regex'); + }), + }; + }); + }); + } + if (query.tags) { expandedQuery.tags = query.tags.map((tag) => { return { diff --git a/public/app/plugins/datasource/influxdb/specs/datasource.test.ts b/public/app/plugins/datasource/influxdb/specs/datasource.test.ts index 796290608bb..384e6f4b7a6 100644 --- a/public/app/plugins/datasource/influxdb/specs/datasource.test.ts +++ b/public/app/plugins/datasource/influxdb/specs/datasource.test.ts @@ -173,4 +173,55 @@ describe('InfluxDataSource', () => { }); }); }); + + describe('Interpolating query variables for dashboard->explore', () => { + const templateSrv: any = { replace: jest.fn() }; + const instanceSettings: any = {}; + const ds = new InfluxDatasource(instanceSettings, templateSrv); + const text = 'interpolationText'; + + it('Should interpolate all variables', () => { + const query = { + refId: 'x', + measurement: '$interpolationVar', + policy: '$interpolationVar', + limit: '$interpolationVar', + slimit: '$interpolationVar', + tz: '$interpolationVar', + tags: [ + { + key: 'cpu', + operator: '=~', + value: '/^$interpolationVar$/', + }, + ], + groupBy: [ + { + params: ['$interpolationVar'], + type: 'tag', + }, + ], + select: [ + [ + { + params: ['$interpolationVar'], + type: 'field', + }, + ], + ], + }; + templateSrv.replace.mockReturnValue(text); + + const queries = ds.interpolateVariablesInQueries([query], { interpolationVar: { text: text, value: text } }); + expect(templateSrv.replace).toBeCalledTimes(8); + expect(queries[0].measurement).toBe(text); + expect(queries[0].policy).toBe(text); + expect(queries[0].limit).toBe(text); + expect(queries[0].slimit).toBe(text); + expect(queries[0].tz).toBe(text); + expect(queries[0].tags![0].value).toBe(text); + expect(queries[0].groupBy![0].params![0]).toBe(text); + expect(queries[0].select![0][0].params![0]).toBe(text); + }); + }); });