[v9.0.x] Plugins: InfluxDB variable interpolation fix (#51917) (#51922)

* Plugins: InfluxDB variable interpolation fix (#51917)

* Don't use regex on flux mode while applying template variables

(cherry picked from commit 885c517983)
This commit is contained in:
ismail simsek
2022-07-07 11:51:58 -04:00
committed by GitHub
parent a35e7f0bf5
commit f3ddc42358
3 changed files with 16 additions and 19 deletions
+1 -4
View File
@@ -7745,10 +7745,7 @@ exports[`better eslint`] = {
[0, 0, 0, "Unexpected any. Specify a different type.", "13"],
[0, 0, 0, "Unexpected any. Specify a different type.", "14"],
[0, 0, 0, "Unexpected any. Specify a different type.", "15"],
[0, 0, 0, "Unexpected any. Specify a different type.", "16"],
[0, 0, 0, "Unexpected any. Specify a different type.", "17"],
[0, 0, 0, "Unexpected any. Specify a different type.", "18"],
[0, 0, 0, "Unexpected any. Specify a different type.", "19"]
[0, 0, 0, "Unexpected any. Specify a different type.", "16"]
],
"public/app/plugins/datasource/influxdb/specs/influx_query_model.test.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],
@@ -240,7 +240,7 @@ export default class InfluxDatasource extends DataSourceWithBackend<InfluxQuery,
if (this.isFlux) {
return {
...query,
query: this.templateSrv.replace(query.query ?? '', rest, 'regex'), // The raw query text
query: this.templateSrv.replace(query.query ?? '', rest), // The raw query text
};
}
@@ -83,7 +83,7 @@ describe('InfluxDataSource', () => {
});
describe('When getting error on 200 after issuing a query', () => {
const queryOptions: any = {
const queryOptions = {
range: {
from: '2018-01-01T00:00:00Z',
to: '2018-01-02T00:00:00Z',
@@ -102,7 +102,7 @@ describe('InfluxDataSource', () => {
};
it('throws an error', async () => {
fetchMock.mockImplementation((req: any) => {
fetchMock.mockImplementation(() => {
return of({
data: {
results: [
@@ -200,11 +200,6 @@ describe('InfluxDataSource', () => {
};
const ds = new InfluxDatasource(instanceSettings, templateSrv);
const fluxQuery = {
refId: 'x',
query: '$interpolationVar,$interpolationVar2',
};
const influxQuery = {
refId: 'x',
alias: '$interpolationVar',
@@ -236,11 +231,6 @@ describe('InfluxDataSource', () => {
],
};
function fluxChecks(query: any) {
expect(templateSrv.replace).toBeCalledTimes(1);
expect(query).toBe(textWithFormatRegex);
}
function influxChecks(query: any) {
expect(templateSrv.replace).toBeCalledTimes(10);
expect(query.alias).toBe(text);
@@ -257,11 +247,16 @@ describe('InfluxDataSource', () => {
describe('when interpolating query variables for dashboard->explore', () => {
it('should interpolate all variables with Flux mode', () => {
ds.isFlux = true;
const fluxQuery = {
refId: 'x',
query: '$interpolationVar,$interpolationVar2',
};
const queries = ds.interpolateVariablesInQueries([fluxQuery], {
interpolationVar: { text: text, value: text },
interpolationVar2: { text: text2, value: text2 },
});
fluxChecks(queries[0].query);
expect(templateSrv.replace).toBeCalledTimes(1);
expect(queries[0].query).toBe(textWithFormatRegex);
});
it('should interpolate all variables with InfluxQL mode', () => {
@@ -277,13 +272,18 @@ describe('InfluxDataSource', () => {
describe('when interpolating template variables', () => {
it('should apply all template variables with Flux mode', () => {
ds.isFlux = true;
const fluxQuery = {
refId: 'x',
query: '$interpolationVar',
};
const query = ds.applyTemplateVariables(fluxQuery, {
interpolationVar: {
text: text,
value: text,
},
});
fluxChecks(query.query);
expect(templateSrv.replace).toBeCalledTimes(1);
expect(query.query).toBe(text);
});
it('should apply all template variables with InfluxQL mode', () => {