[v11.3.x] InfluxDB: Adhoc filters can use template vars as values (#98785)
InfluxDB: adhoc filters can use template vars as values (#98567)
---------
Co-authored-by: Ivan Ortega <ivanortegaalba@gmail.com>
(cherry picked from commit be1505f2fb)
Co-authored-by: Andrew Hackmann <5140848+bossinc@users.noreply.github.com>
This commit is contained in:
co-authored by
Andrew Hackmann
parent
8c94782840
commit
2d8c7ea350
@@ -1,5 +1,6 @@
|
||||
import { lastValueFrom, of } from 'rxjs';
|
||||
|
||||
import { AdHocVariableFilter } from '@grafana/data';
|
||||
import { BackendSrvRequest, TemplateSrv } from '@grafana/runtime';
|
||||
import config from 'app/core/config';
|
||||
|
||||
@@ -10,6 +11,7 @@ import { mockInfluxQueryRequest } from './__mocks__/request';
|
||||
import { mockInfluxFetchResponse, mockMetricFindQueryResponse } from './__mocks__/response';
|
||||
import { BROWSER_MODE_DISABLED_MESSAGE } from './constants';
|
||||
import InfluxDatasource from './datasource';
|
||||
import { InfluxQuery, InfluxVersion } from './types';
|
||||
|
||||
const fetchMock = mockBackendService(mockInfluxFetchResponse());
|
||||
|
||||
@@ -267,7 +269,10 @@ describe('InfluxDataSource Backend Mode [influxdbBackendMigration=true]', () =>
|
||||
});
|
||||
|
||||
describe('interpolateQueryExpr', () => {
|
||||
let ds = getMockInfluxDS(getMockDSInstanceSettings(), {} as TemplateSrv);
|
||||
const templateSrvStub = {
|
||||
replace: jest.fn().mockImplementation((...rest: unknown[]) => 'templateVarReplaced'),
|
||||
} as unknown as TemplateSrv;
|
||||
let ds = getMockInfluxDS(getMockDSInstanceSettings(), templateSrvStub);
|
||||
it('should return the value as it is', () => {
|
||||
const value = 'normalValue';
|
||||
const variableMock = queryBuilder().withId('tempVar').withName('tempVar').withMulti(false).build();
|
||||
@@ -373,4 +378,19 @@ describe('interpolateQueryExpr', () => {
|
||||
const expectation = `1.0`;
|
||||
expect(result).toBe(expectation);
|
||||
});
|
||||
|
||||
it('template var in adhoc', () => {
|
||||
const templateVarName = '$templateVarName';
|
||||
const templateVarValue = 'templateVarValue';
|
||||
const templateSrvStub = {
|
||||
replace: jest
|
||||
.fn()
|
||||
.mockImplementation((target?: string) => (target === templateVarName ? templateVarValue : target)),
|
||||
} as unknown as TemplateSrv;
|
||||
const ds = getMockInfluxDS(getMockDSInstanceSettings(), templateSrvStub);
|
||||
ds.version = InfluxVersion.SQL;
|
||||
const adhocFilter: AdHocVariableFilter[] = [{ key: 'bar', value: templateVarName, operator: '=' }];
|
||||
const result = ds.applyTemplateVariables(mockInfluxQueryRequest() as unknown as InfluxQuery, {}, adhocFilter);
|
||||
expect(result.tags![0].value).toBe(templateVarValue);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -208,6 +208,7 @@ export default class InfluxDatasource extends DataSourceWithBackend<InfluxQuery,
|
||||
if (query.adhocFilters?.length) {
|
||||
const adhocFiltersToTags: InfluxQueryTag[] = (query.adhocFilters ?? []).map((af) => {
|
||||
const { condition, ...asTag } = af;
|
||||
asTag.value = this.templateSrv.replace(asTag.value ?? '', variables);
|
||||
return asTag;
|
||||
});
|
||||
query.tags = [...(query.tags ?? []), ...adhocFiltersToTags];
|
||||
|
||||
Reference in New Issue
Block a user