InfluxDB: Interpolate ad-hoc filters from Dashboard to Explore (#56996)

This commit is contained in:
Beto Muniz 2022-10-17 13:28:54 -03:00 committed by GitHub
parent 5c2b722287
commit 920d2aa599
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 4 deletions

View File

@ -317,11 +317,12 @@ export default class InfluxDatasource extends DataSourceWithBackend<InfluxQuery,
// add global adhoc filters to timeFilter // add global adhoc filters to timeFilter
const adhocFilters = this.templateSrv.getAdhocFilters(this.name); const adhocFilters = this.templateSrv.getAdhocFilters(this.name);
if (adhocFilters.length > 0) { const adhocFiltersFromDashboard = options.targets.flatMap((target: InfluxQuery) => target.adhocFilters ?? []);
if (adhocFilters?.length || adhocFiltersFromDashboard?.length) {
const ahFilters = adhocFilters?.length ? adhocFilters : adhocFiltersFromDashboard;
const tmpQuery = new InfluxQueryModel({ refId: 'A' }, this.templateSrv, scopedVars); const tmpQuery = new InfluxQueryModel({ refId: 'A' }, this.templateSrv, scopedVars);
timeFilter += ' AND ' + tmpQuery.renderAdhocFilters(adhocFilters); timeFilter += ' AND ' + tmpQuery.renderAdhocFilters(ahFilters);
} }
// replace grafana variables // replace grafana variables
scopedVars.timeFilter = { value: timeFilter }; scopedVars.timeFilter = { value: timeFilter };
@ -506,6 +507,7 @@ export default class InfluxDatasource extends DataSourceWithBackend<InfluxQuery,
return { return {
...expandedQuery, ...expandedQuery,
adhocFilters: this.templateSrv.getAdhocFilters(this.name) ?? [],
query: this.templateSrv.replace(query.query ?? '', rest, 'regex'), // The raw query text query: this.templateSrv.replace(query.query ?? '', rest, 'regex'), // The raw query text
alias: this.templateSrv.replace(query.alias ?? '', scopedVars), alias: this.templateSrv.replace(query.alias ?? '', scopedVars),
limit: this.templateSrv.replace(query.limit?.toString() ?? '', scopedVars, 'regex'), limit: this.templateSrv.replace(query.limit?.toString() ?? '', scopedVars, 'regex'),

View File

@ -210,7 +210,18 @@ describe('InfluxDataSource', () => {
$interpolationVar: text, $interpolationVar: text,
$interpolationVar2: text2, $interpolationVar2: text2,
}; };
const adhocFilters = [
{
key: 'adhoc',
operator: '=',
value: 'val',
condition: '',
},
];
const templateSrv: any = { const templateSrv: any = {
getAdhocFilters: jest.fn((name: string) => {
return adhocFilters;
}),
replace: jest.fn((target?: string, scopedVars?: ScopedVars, format?: string | Function): string => { replace: jest.fn((target?: string, scopedVars?: ScopedVars, format?: string | Function): string => {
if (!format) { if (!format) {
return variableMap[target!] || ''; return variableMap[target!] || '';
@ -252,6 +263,7 @@ describe('InfluxDataSource', () => {
}, },
], ],
], ],
adhocFilters,
}; };
function influxChecks(query: any) { function influxChecks(query: any) {
@ -265,6 +277,7 @@ describe('InfluxDataSource', () => {
expect(query.tags![0].value).toBe(textWithFormatRegex); expect(query.tags![0].value).toBe(textWithFormatRegex);
expect(query.groupBy![0].params![0]).toBe(textWithFormatRegex); expect(query.groupBy![0].params![0]).toBe(textWithFormatRegex);
expect(query.select![0][0].params![0]).toBe(textWithFormatRegex); expect(query.select![0][0].params![0]).toBe(textWithFormatRegex);
expect(query.adhocFilters[0].key).toBe(adhocFilters[0].key);
} }
describe('when interpolating query variables for dashboard->explore', () => { describe('when interpolating query variables for dashboard->explore', () => {

View File

@ -1,4 +1,4 @@
import { DataQuery, DataSourceJsonData } from '@grafana/data'; import { DataQuery, DataSourceJsonData, AdHocVariableFilter } from '@grafana/data';
export enum InfluxVersion { export enum InfluxVersion {
InfluxQL = 'InfluxQL', InfluxQL = 'InfluxQL',
@ -70,4 +70,5 @@ export interface InfluxQuery extends DataQuery {
titleColumn?: string; titleColumn?: string;
name?: string; name?: string;
textEditor?: boolean; textEditor?: boolean;
adhocFilters?: AdHocVariableFilter[];
} }