mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Table: Adds adhoc filtering (#25467)
* Table: Adds adhoc filtering * Refactor: changes after PR comments * Refactor: hides filtering for data sources that do not support modifyQuery in Explore * Refactor: fixes strict null error * Changed tooltip position to above icon Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
This commit is contained in:
@@ -20,6 +20,7 @@ import { PromOptions, PromQuery } from './types';
|
||||
import templateSrv from 'app/features/templating/template_srv';
|
||||
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
||||
import { VariableHide } from '../../../features/variables/types';
|
||||
import { describe } from '../../../../test/lib/common';
|
||||
|
||||
const datasourceRequestMock = jest.fn().mockResolvedValue(createDefaultPromResponse());
|
||||
|
||||
@@ -1886,6 +1887,68 @@ describe('prepareTargets', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('modifyQuery', () => {
|
||||
describe('when called with ADD_FILTER', () => {
|
||||
describe('and query has no labels', () => {
|
||||
it('then the correct label should be added', () => {
|
||||
const query: PromQuery = { refId: 'A', expr: 'go_goroutines' };
|
||||
const action = { key: 'cluster', value: 'us-cluster', type: 'ADD_FILTER' };
|
||||
const instanceSettings = ({ jsonData: {} } as unknown) as DataSourceInstanceSettings<PromOptions>;
|
||||
const ds = new PrometheusDatasource(instanceSettings);
|
||||
|
||||
const result = ds.modifyQuery(query, action);
|
||||
|
||||
expect(result.refId).toEqual('A');
|
||||
expect(result.expr).toEqual('go_goroutines{cluster="us-cluster"}');
|
||||
});
|
||||
});
|
||||
|
||||
describe('and query has labels', () => {
|
||||
it('then the correct label should be added', () => {
|
||||
const query: PromQuery = { refId: 'A', expr: 'go_goroutines{cluster="us-cluster"}' };
|
||||
const action = { key: 'pod', value: 'pod-123', type: 'ADD_FILTER' };
|
||||
const instanceSettings = ({ jsonData: {} } as unknown) as DataSourceInstanceSettings<PromOptions>;
|
||||
const ds = new PrometheusDatasource(instanceSettings);
|
||||
|
||||
const result = ds.modifyQuery(query, action);
|
||||
|
||||
expect(result.refId).toEqual('A');
|
||||
expect(result.expr).toEqual('go_goroutines{cluster="us-cluster",pod="pod-123"}');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when called with ADD_FILTER_OUT', () => {
|
||||
describe('and query has no labels', () => {
|
||||
it('then the correct label should be added', () => {
|
||||
const query: PromQuery = { refId: 'A', expr: 'go_goroutines' };
|
||||
const action = { key: 'cluster', value: 'us-cluster', type: 'ADD_FILTER_OUT' };
|
||||
const instanceSettings = ({ jsonData: {} } as unknown) as DataSourceInstanceSettings<PromOptions>;
|
||||
const ds = new PrometheusDatasource(instanceSettings);
|
||||
|
||||
const result = ds.modifyQuery(query, action);
|
||||
|
||||
expect(result.refId).toEqual('A');
|
||||
expect(result.expr).toEqual('go_goroutines{cluster!="us-cluster"}');
|
||||
});
|
||||
});
|
||||
|
||||
describe('and query has labels', () => {
|
||||
it('then the correct label should be added', () => {
|
||||
const query: PromQuery = { refId: 'A', expr: 'go_goroutines{cluster="us-cluster"}' };
|
||||
const action = { key: 'pod', value: 'pod-123', type: 'ADD_FILTER_OUT' };
|
||||
const instanceSettings = ({ jsonData: {} } as unknown) as DataSourceInstanceSettings<PromOptions>;
|
||||
const ds = new PrometheusDatasource(instanceSettings);
|
||||
|
||||
const result = ds.modifyQuery(query, action);
|
||||
|
||||
expect(result.refId).toEqual('A');
|
||||
expect(result.expr).toEqual('go_goroutines{cluster="us-cluster",pod!="pod-123"}');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function createDataRequest(targets: any[], overrides?: Partial<DataQueryRequest>): DataQueryRequest<PromQuery> {
|
||||
const defaults = {
|
||||
app: CoreApp.Dashboard,
|
||||
|
||||
@@ -693,6 +693,10 @@ export class PrometheusDatasource extends DataSourceApi<PromQuery, PromOptions>
|
||||
expression = addLabelToQuery(expression, action.key, action.value);
|
||||
break;
|
||||
}
|
||||
case 'ADD_FILTER_OUT': {
|
||||
expression = addLabelToQuery(expression, action.key, action.value, '!=');
|
||||
break;
|
||||
}
|
||||
case 'ADD_HISTOGRAM_QUANTILE': {
|
||||
expression = `histogram_quantile(0.95, sum(rate(${expression}[5m])) by (le))`;
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user