mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Elasticsearch: Fix adding of adhoc filters when jumping to explore (#60691)
* Elasticsearch: Fix adding of adhoc filters when jumping to explore * Change NOT operator to - as it is preffered solution
This commit is contained in:
@@ -81,6 +81,7 @@ interface TestContext {
|
||||
jsonData?: Partial<ElasticsearchOptions>;
|
||||
database?: string;
|
||||
fetchMockImplementation?: (options: BackendSrvRequest) => Observable<FetchResponse>;
|
||||
templateSrvMock?: TemplateSrv;
|
||||
}
|
||||
|
||||
interface Data {
|
||||
@@ -92,7 +93,8 @@ function getTestContext({
|
||||
from = 'now-5m',
|
||||
jsonData,
|
||||
database = '[test-]YYYY.MM.DD',
|
||||
fetchMockImplementation = undefined,
|
||||
fetchMockImplementation,
|
||||
templateSrvMock,
|
||||
}: TestContext = {}) {
|
||||
const defaultMock = (options: BackendSrvRequest) => of(createFetchResponse(data));
|
||||
|
||||
@@ -113,16 +115,18 @@ function getTestContext({
|
||||
const settings: Partial<DataSourceInstanceSettings<ElasticsearchOptions>> = { url: ELASTICSEARCH_MOCK_URL };
|
||||
settings.jsonData = jsonData as ElasticsearchOptions;
|
||||
|
||||
const templateSrv = {
|
||||
replace: (text?: string) => {
|
||||
if (text?.startsWith('$')) {
|
||||
return `resolvedVariable`;
|
||||
} else {
|
||||
return text;
|
||||
}
|
||||
},
|
||||
getAdhocFilters: () => [],
|
||||
} as unknown as TemplateSrv;
|
||||
const templateSrv =
|
||||
templateSrvMock ??
|
||||
({
|
||||
replace: (text?: string) => {
|
||||
if (text?.startsWith('$')) {
|
||||
return `resolvedVariable`;
|
||||
} else {
|
||||
return text;
|
||||
}
|
||||
},
|
||||
getAdhocFilters: () => [],
|
||||
} as unknown as TemplateSrv);
|
||||
|
||||
const ds = createElasticDatasource(settings, templateSrv);
|
||||
|
||||
@@ -889,6 +893,24 @@ describe('ElasticDatasource', () => {
|
||||
expect((interpolatedQuery.bucketAggs![0] as Filters).settings!.filters![0].query).toBe('resolvedVariable');
|
||||
});
|
||||
|
||||
it('should correctly add ad hoc filters when interpolating variables in query', () => {
|
||||
const templateSrvMock = {
|
||||
replace: (text?: string) => text,
|
||||
getAdhocFilters: () => [{ key: 'bar', operator: '=', value: 'test' }],
|
||||
} as unknown as TemplateSrv;
|
||||
const { ds } = getTestContext({ templateSrvMock });
|
||||
const query: ElasticsearchQuery = {
|
||||
refId: 'A',
|
||||
bucketAggs: [{ type: 'filters', settings: { filters: [{ query: '$var', label: '' }] }, id: '1' }],
|
||||
metrics: [{ type: 'count', id: '1' }],
|
||||
query: 'foo:"bar"',
|
||||
};
|
||||
|
||||
const interpolatedQuery = ds.interpolateVariablesInQueries([query], {})[0];
|
||||
|
||||
expect(interpolatedQuery.query).toBe('foo:"bar" AND bar:"test"');
|
||||
});
|
||||
|
||||
it('should correctly handle empty query strings in filters bucket aggregation', () => {
|
||||
const { ds } = getTestContext();
|
||||
const query: ElasticsearchQuery = {
|
||||
@@ -1062,6 +1084,81 @@ describe('modifyQuery', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('addAdhocFilters', () => {
|
||||
describe('with invalid filters', () => {
|
||||
it('should filter out ad hoc filter without key', () => {
|
||||
const templateSrvMock = {
|
||||
getAdhocFilters: () => [{ key: '', operator: '=', value: 'a' }],
|
||||
} as unknown as TemplateSrv;
|
||||
const { ds } = getTestContext({ templateSrvMock });
|
||||
|
||||
const query = ds.addAdHocFilters('foo:"bar"');
|
||||
expect(query).toBe('foo:"bar"');
|
||||
});
|
||||
|
||||
it('should filter out ad hoc filter without value', () => {
|
||||
const templateSrvMock = {
|
||||
getAdhocFilters: () => [{ key: 'a', operator: '=', value: '' }],
|
||||
} as unknown as TemplateSrv;
|
||||
const { ds } = getTestContext({ templateSrvMock });
|
||||
|
||||
const query = ds.addAdHocFilters('foo:"bar"');
|
||||
expect(query).toBe('foo:"bar"');
|
||||
});
|
||||
|
||||
it('should filter out filter ad hoc filter with invalid operator', () => {
|
||||
const templateSrvMock = {
|
||||
getAdhocFilters: () => [{ key: 'a', operator: 'A', value: '' }],
|
||||
} as unknown as TemplateSrv;
|
||||
const { ds } = getTestContext({ templateSrvMock });
|
||||
|
||||
const query = ds.addAdHocFilters('foo:"bar"');
|
||||
expect(query).toBe('foo:"bar"');
|
||||
});
|
||||
});
|
||||
|
||||
describe('with 1 ad hoc filter', () => {
|
||||
const templateSrvMock = {
|
||||
getAdhocFilters: () => [{ key: 'test', operator: '=', value: 'test1' }],
|
||||
} as unknown as TemplateSrv;
|
||||
const { ds } = getTestContext({ templateSrvMock });
|
||||
|
||||
it('should correctly add 1 ad hoc filter when query is not empty', () => {
|
||||
const query = ds.addAdHocFilters('foo:"bar"');
|
||||
expect(query).toBe('foo:"bar" AND test:"test1"');
|
||||
});
|
||||
|
||||
it('should correctly add 1 ad hoc filter when query is empty', () => {
|
||||
const query = ds.addAdHocFilters('');
|
||||
expect(query).toBe('test:"test1"');
|
||||
});
|
||||
});
|
||||
|
||||
describe('with multiple ad hoc filters', () => {
|
||||
const templateSrvMock = {
|
||||
getAdhocFilters: () => [
|
||||
{ key: 'bar', operator: '=', value: 'baz' },
|
||||
{ key: 'job', operator: '!=', value: 'grafana' },
|
||||
{ key: 'service', operator: '=~', value: 'service' },
|
||||
{ key: 'count', operator: '>', value: '1' },
|
||||
],
|
||||
} as unknown as TemplateSrv;
|
||||
const { ds } = getTestContext({ templateSrvMock });
|
||||
|
||||
it('should correctly add ad hoc filters when query is not empty', () => {
|
||||
const query = ds.addAdHocFilters('foo:"bar" AND test:"test1"');
|
||||
expect(query).toBe(
|
||||
'foo:"bar" AND test:"test1" AND bar:"baz" AND -job:"grafana" AND service:/service/ AND count:>1'
|
||||
);
|
||||
});
|
||||
|
||||
it('should correctly add ad hoc filters when query is empty', () => {
|
||||
const query = ds.addAdHocFilters('');
|
||||
expect(query).toBe('bar:"baz" AND -job:"grafana" AND service:/service/ AND count:>1');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
const createElasticQuery = (): DataQueryRequest<ElasticsearchQuery> => {
|
||||
return {
|
||||
requestId: '',
|
||||
|
||||
Reference in New Issue
Block a user