mirror of
https://github.com/grafana/grafana.git
synced 2024-11-26 02:40:26 -06:00
Chore: Don't apply adhoc filters when promQLScope is enabled (#86454)
* check promQLScope * add tests
This commit is contained in:
parent
dceb83ab42
commit
e528c79689
@ -17,7 +17,7 @@ import {
|
||||
TimeRange,
|
||||
VariableHide,
|
||||
} from '@grafana/data';
|
||||
import { TemplateSrv } from '@grafana/runtime';
|
||||
import { config, TemplateSrv } from '@grafana/runtime';
|
||||
|
||||
import {
|
||||
alignRange,
|
||||
@ -542,6 +542,10 @@ describe('PrometheusDatasource', () => {
|
||||
});
|
||||
|
||||
describe('interpolateVariablesInQueries', () => {
|
||||
afterEach(() => {
|
||||
config.featureToggles.promQLScope = undefined;
|
||||
});
|
||||
|
||||
it('should call replace function 2 times', () => {
|
||||
const query: PromQuery = {
|
||||
expr: 'test{job="testjob"}',
|
||||
@ -568,6 +572,21 @@ describe('PrometheusDatasource', () => {
|
||||
ds.interpolateVariablesInQueries(queries, {});
|
||||
expect(ds.enhanceExprWithAdHocFilters).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not apply adhoc filters when promQLScope is enabled', () => {
|
||||
config.featureToggles.promQLScope = true;
|
||||
ds.enhanceExprWithAdHocFilters = jest.fn();
|
||||
ds.generateScopeFilters = jest.fn();
|
||||
const queries = [
|
||||
{
|
||||
refId: 'A',
|
||||
expr: 'rate({bar="baz", job="foo"} [5m]',
|
||||
},
|
||||
];
|
||||
ds.interpolateVariablesInQueries(queries, {});
|
||||
expect(ds.enhanceExprWithAdHocFilters).not.toHaveBeenCalled();
|
||||
expect(ds.generateScopeFilters).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('applyTemplateVariables', () => {
|
||||
@ -575,6 +594,10 @@ describe('PrometheusDatasource', () => {
|
||||
replaceMock.mockImplementation((a: string, ...rest: unknown[]) => a);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
config.featureToggles.promQLScope = false;
|
||||
});
|
||||
|
||||
it('should call replace function for legendFormat', () => {
|
||||
const query = {
|
||||
expr: 'test{job="bar"}',
|
||||
@ -637,6 +660,45 @@ describe('PrometheusDatasource', () => {
|
||||
expect(result).toMatchObject({ expr: 'test{job="bar", k1="v1", k2!="v2"}' });
|
||||
});
|
||||
|
||||
it('should generate scope filters and **not** apply ad-hoc filters to expr', () => {
|
||||
config.featureToggles.promQLScope = true;
|
||||
replaceMock.mockImplementation((a: string) => a);
|
||||
const filters = [
|
||||
{
|
||||
key: 'k1',
|
||||
operator: '=',
|
||||
value: 'v1',
|
||||
},
|
||||
{
|
||||
key: 'k2',
|
||||
operator: '!=',
|
||||
value: 'v2',
|
||||
},
|
||||
];
|
||||
|
||||
const query = {
|
||||
expr: 'test{job="bar"}',
|
||||
refId: 'A',
|
||||
};
|
||||
|
||||
const expectedScopeFilters: ScopeSpecFilter[] = [
|
||||
{
|
||||
key: 'k1',
|
||||
operator: 'equals',
|
||||
value: 'v1',
|
||||
},
|
||||
{
|
||||
key: 'k2',
|
||||
operator: 'not-equals',
|
||||
value: 'v2',
|
||||
},
|
||||
];
|
||||
|
||||
const result = ds.applyTemplateVariables(query, {}, filters);
|
||||
expect(result.expr).toBe('test{job="bar"}');
|
||||
expect(result.adhocFilters).toEqual(expectedScopeFilters);
|
||||
});
|
||||
|
||||
it('should add ad-hoc filters only to expr', () => {
|
||||
replaceMock.mockImplementation((a: string) => a?.replace('$A', '99') ?? a);
|
||||
const filters = [
|
||||
|
@ -751,15 +751,17 @@ export class PrometheusDatasource
|
||||
if (queries && queries.length) {
|
||||
expandedQueries = queries.map((query) => {
|
||||
const interpolatedQuery = this.templateSrv.replace(query.expr, scopedVars, this.interpolateQueryExpr);
|
||||
const withAdhocFilters = this.enhanceExprWithAdHocFilters(filters, interpolatedQuery);
|
||||
|
||||
const expandedQuery = {
|
||||
...query,
|
||||
...(config.featureToggles.promQLScope ? { adhocFilters: this.generateScopeFilters(filters) } : {}),
|
||||
datasource: this.getRef(),
|
||||
expr: withAdhocFilters,
|
||||
expr: config.featureToggles.promQLScope
|
||||
? interpolatedQuery
|
||||
: this.enhanceExprWithAdHocFilters(filters, interpolatedQuery),
|
||||
interval: this.templateSrv.replace(query.interval, scopedVars),
|
||||
};
|
||||
|
||||
return expandedQuery;
|
||||
});
|
||||
}
|
||||
@ -921,13 +923,10 @@ export class PrometheusDatasource
|
||||
// interpolate expression
|
||||
const expr = this.templateSrv.replace(target.expr, variables, this.interpolateQueryExpr);
|
||||
|
||||
// Add ad hoc filters
|
||||
const exprWithAdHocFilters = this.enhanceExprWithAdHocFilters(filters, expr);
|
||||
|
||||
return {
|
||||
...target,
|
||||
...(config.featureToggles.promQLScope ? { adhocFilters: this.generateScopeFilters(filters) } : {}),
|
||||
expr: exprWithAdHocFilters,
|
||||
expr: config.featureToggles.promQLScope ? expr : this.enhanceExprWithAdHocFilters(filters, expr),
|
||||
interval: this.templateSrv.replace(target.interval, variables),
|
||||
legendFormat: this.templateSrv.replace(target.legendFormat, variables),
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user