Plugins: InfluxDB variable interpolation fix (#51917)

* Don't use regex on flux mode while applying template variables
This commit is contained in:
ismail simsek
2022-07-07 17:16:35 +02:00
committed by GitHub
parent ca80865bf1
commit 885c517983
3 changed files with 15 additions and 18 deletions

View File

@@ -10404,9 +10404,6 @@ exports[`no undocumented stories`] = {
"packages/grafana-ui/src/components/QueryField/QueryField.story.tsx:5381": [ "packages/grafana-ui/src/components/QueryField/QueryField.story.tsx:5381": [
[0, 0, 0, "No undocumented stories are allowed, please add an .mdx file with some documentation", "5381"] [0, 0, 0, "No undocumented stories are allowed, please add an .mdx file with some documentation", "5381"]
], ],
"packages/grafana-ui/src/components/RefreshPicker/RefreshPicker.story.tsx:5381": [
[0, 0, 0, "No undocumented stories are allowed, please add an .mdx file with some documentation", "5381"]
],
"packages/grafana-ui/src/components/SecretTextArea/SecretTextArea.story.tsx:5381": [ "packages/grafana-ui/src/components/SecretTextArea/SecretTextArea.story.tsx:5381": [
[0, 0, 0, "No undocumented stories are allowed, please add an .mdx file with some documentation", "5381"] [0, 0, 0, "No undocumented stories are allowed, please add an .mdx file with some documentation", "5381"]
], ],

View File

@@ -240,7 +240,7 @@ export default class InfluxDatasource extends DataSourceWithBackend<InfluxQuery,
if (this.isFlux) { if (this.isFlux) {
return { return {
...query, ...query,
query: this.templateSrv.replace(query.query ?? '', rest, 'regex'), // The raw query text query: this.templateSrv.replace(query.query ?? '', rest), // The raw query text
}; };
} }

View File

@@ -83,7 +83,7 @@ describe('InfluxDataSource', () => {
}); });
describe('When getting error on 200 after issuing a query', () => { describe('When getting error on 200 after issuing a query', () => {
const queryOptions: any = { const queryOptions = {
range: { range: {
from: '2018-01-01T00:00:00Z', from: '2018-01-01T00:00:00Z',
to: '2018-01-02T00:00:00Z', to: '2018-01-02T00:00:00Z',
@@ -102,7 +102,7 @@ describe('InfluxDataSource', () => {
}; };
it('throws an error', async () => { it('throws an error', async () => {
fetchMock.mockImplementation((req: any) => { fetchMock.mockImplementation(() => {
return of({ return of({
data: { data: {
results: [ results: [
@@ -202,11 +202,6 @@ describe('InfluxDataSource', () => {
}; };
const ds = new InfluxDatasource(instanceSettings, templateSrv); const ds = new InfluxDatasource(instanceSettings, templateSrv);
const fluxQuery = {
refId: 'x',
query: '$interpolationVar,$interpolationVar2',
};
const influxQuery = { const influxQuery = {
refId: 'x', refId: 'x',
alias: '$interpolationVar', alias: '$interpolationVar',
@@ -238,11 +233,6 @@ describe('InfluxDataSource', () => {
], ],
}; };
function fluxChecks(query: any) {
expect(templateSrv.replace).toBeCalledTimes(1);
expect(query).toBe(textWithFormatRegex);
}
function influxChecks(query: any) { function influxChecks(query: any) {
expect(templateSrv.replace).toBeCalledTimes(10); expect(templateSrv.replace).toBeCalledTimes(10);
expect(query.alias).toBe(text); expect(query.alias).toBe(text);
@@ -259,11 +249,16 @@ describe('InfluxDataSource', () => {
describe('when interpolating query variables for dashboard->explore', () => { describe('when interpolating query variables for dashboard->explore', () => {
it('should interpolate all variables with Flux mode', () => { it('should interpolate all variables with Flux mode', () => {
ds.isFlux = true; ds.isFlux = true;
const fluxQuery = {
refId: 'x',
query: '$interpolationVar,$interpolationVar2',
};
const queries = ds.interpolateVariablesInQueries([fluxQuery], { const queries = ds.interpolateVariablesInQueries([fluxQuery], {
interpolationVar: { text: text, value: text }, interpolationVar: { text: text, value: text },
interpolationVar2: { text: text2, value: text2 }, 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', () => { it('should interpolate all variables with InfluxQL mode', () => {
@@ -279,13 +274,18 @@ describe('InfluxDataSource', () => {
describe('when interpolating template variables', () => { describe('when interpolating template variables', () => {
it('should apply all template variables with Flux mode', () => { it('should apply all template variables with Flux mode', () => {
ds.isFlux = true; ds.isFlux = true;
const fluxQuery = {
refId: 'x',
query: '$interpolationVar',
};
const query = ds.applyTemplateVariables(fluxQuery, { const query = ds.applyTemplateVariables(fluxQuery, {
interpolationVar: { interpolationVar: {
text: text, text: text,
value: 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', () => { it('should apply all template variables with InfluxQL mode', () => {