Skip to content

Commit

Permalink
Fix adhoc filters with number fields
Browse files Browse the repository at this point in the history
  • Loading branch information
iwysiu committed Jan 24, 2025
1 parent bdc9612 commit 9c6bbe5
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 9 deletions.
90 changes: 86 additions & 4 deletions src/opensearchDatasource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,10 @@ describe('OpenSearchDatasource', function (this: any) {
key: 'test2',
key_as_string: 'test2_as_string',
},
{
doc_count: 2,
key: 5,
},
],
},
},
Expand All @@ -789,16 +793,17 @@ describe('OpenSearchDatasource', function (this: any) {
results = res;
});

expect(results.length).toEqual(2);
expect(results.length).toEqual(3);
});

it('should get results', () => {
expect(results.length).toEqual(2);
expect(results.length).toEqual(3);
});

it('should use key or key_as_string', () => {
expect(results[0].text).toEqual('test');
expect(results[1].text).toEqual('test2_as_string');
expect(results[2].text).toEqual('5');
});

it('should not set search type to count', () => {
Expand Down Expand Up @@ -842,6 +847,10 @@ describe('OpenSearchDatasource', function (this: any) {
key: 'test2',
key_as_string: 'test2_as_string',
},
{
doc_count: 2,
key: 5,
},
],
},
},
Expand All @@ -866,16 +875,17 @@ describe('OpenSearchDatasource', function (this: any) {
results = res;
});

expect(results.length).toEqual(2);
expect(results.length).toEqual(3);
});

it('should get results', () => {
expect(results.length).toEqual(2);
expect(results.length).toEqual(3);
});

it('should use key or key_as_string', () => {
expect(results[0].text).toEqual('test');
expect(results[1].text).toEqual('test2_as_string');
expect(results[2].text).toEqual('5');
});

it('should not set search type to count', () => {
Expand All @@ -900,6 +910,78 @@ describe('OpenSearchDatasource', function (this: any) {
});
});

describe('When calling getTagValues', () => {
const timeRangeMock = createTimeRange(toUtc([2022, 8, 21, 6, 10, 10]), toUtc([2022, 8, 24, 6, 10, 21]));
let results: MetricFindValue[];
let mockResource = jest.fn().mockResolvedValue({});

beforeEach(() => {
mockResource.mockClear();
// @ts-ignore-next-line
config.featureToggles.openSearchBackendFlowEnabled = true;
createDatasource({
url: OPENSEARCH_MOCK_URL,
jsonData: {
database: 'test',
version: '1.0.0',
timeField: '@timestamp',
} as OpenSearchOptions,
} as DataSourceInstanceSettings<OpenSearchOptions>);

mockResource = jest.fn().mockImplementation((options) => {
return Promise.resolve({
responses: [
{
aggregations: {
'1': {
buckets: [
{ doc_count: 1, key: 'test' },
{
doc_count: 2,
key: 'test2',
key_as_string: 'test2_as_string',
},
{
doc_count: 2,
key: 5,
},
],
},
},
},
],
});
});
ctx.ds.postResource = mockResource;
ctx.ds.getTagValues({ key: 'test', timeRange: timeRangeMock, filters: [] }).then((res) => {
results = res;
});
});

it('should respect the currently selected time range', () => {
expect(mockResource).toHaveBeenCalledTimes(1);
const esQuery = JSON.parse(mockResource.mock.calls[0][1].split('\n')[1]);
const { lte, gte } = esQuery.query.bool.filter[0].range['@timestamp'];

expect(gte).toBe('1663740610000'); // 2022-09-21T06:10:10Z
expect(lte).toBe('1663999821000'); // 2022-09-24T06:10:21Z
});

it('should return numbers as strings', async () => {
expect(mockResource).toHaveBeenCalledTimes(1);

expect(results.length).toBe(3);
expect(results[0].text).toBe('test');
expect(results[0].value).toBe('test');

expect(results[1].text).toBe('test2_as_string');
expect(results[1].value).toBe('test2');

expect(results[2].text).toBe('5');
expect(results[2].value).toBe('5');
});
});

describe('annotationQuery', () => {
describe('results processing', () => {
beforeEach(() => {
Expand Down
11 changes: 6 additions & 5 deletions src/opensearchDatasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export class OpenSearchDatasource
if (query.hide) {
return undefined;
}

let isQuerySuitable = false;

switch (options.type) {
Expand Down Expand Up @@ -1090,7 +1090,7 @@ export class OpenSearchDatasource
});
}

getTerms(queryDef: any, range = getDefaultTimeRange()) {
getTerms(queryDef: any, range = getDefaultTimeRange(), isTagValueQuery = false) {
const searchType = this.flavor === Flavor.Elasticsearch && lt(this.version, '5.0.0') ? 'count' : 'query_then_fetch';
const header = this.getQueryHeader(searchType, range.from, range.to);
let esQuery = JSON.stringify(this.queryBuilder.getTermsQuery(queryDef));
Expand All @@ -1114,9 +1114,10 @@ export class OpenSearchDatasource

const buckets = res.responses[0].aggregations['1'].buckets;
return _.map(buckets, (bucket) => {
const keyString = String(bucket.key);
return {
text: bucket.key_as_string || bucket.key,
value: bucket.key,
text: bucket.key_as_string || keyString,
value: isTagValueQuery ? keyString : bucket.key,
};
});
});
Expand Down Expand Up @@ -1169,7 +1170,7 @@ export class OpenSearchDatasource
}

getTagValues(options: any) {
return this.getTerms({ field: options.key, query: '*' }, options.timeRange);
return this.getTerms({ field: options.key, query: '*' }, options.timeRange, true);
}

targetContainsTemplate(target: any) {
Expand Down

0 comments on commit 9c6bbe5

Please sign in to comment.