Skip to content

Commit

Permalink
fix auto interval filter building (elastic#102086)
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 authored and kibanamachine committed Jun 17, 2021
1 parent c29ecaa commit 3dfbfae
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,24 @@ import { mockAggTypesRegistry, mockGetFieldFormatsStart } from '../../test_helpe
import { BUCKET_TYPES } from '../bucket_agg_types';
import { IBucketAggConfig } from '../bucket_agg_type';
import { createFilterHistogram } from './histogram';
import { RangeFilter } from '../../../../es_query';

function validateFilter(filter: RangeFilter) {
expect(mockGetFieldFormatsStart().deserialize).toHaveBeenCalledTimes(1);
expect(filter).toHaveProperty('meta');
expect(filter.meta).toHaveProperty('index', '1234');
expect(filter).toHaveProperty('range');
expect(filter.range).toHaveProperty('bytes');
expect(filter.range.bytes).toHaveProperty('gte', 2048);
expect(filter.range.bytes).toHaveProperty('lt', 3072);
expect(filter.meta).toHaveProperty('formattedValue');
}

describe('AggConfig Filters', () => {
describe('histogram', () => {
const getConfig = (() => {}) as FieldFormatsGetConfigFn;
const getAggConfigs = () => {
jest.clearAllMocks();
const field = {
name: 'bytes',
format: new BytesFormat({}, getConfig),
Expand Down Expand Up @@ -56,14 +69,20 @@ describe('AggConfig Filters', () => {
'2048'
);

expect(mockGetFieldFormatsStart().deserialize).toHaveBeenCalledTimes(1);
expect(filter).toHaveProperty('meta');
expect(filter.meta).toHaveProperty('index', '1234');
expect(filter).toHaveProperty('range');
expect(filter.range).toHaveProperty('bytes');
expect(filter.range.bytes).toHaveProperty('gte', 2048);
expect(filter.range.bytes).toHaveProperty('lt', 3072);
expect(filter.meta).toHaveProperty('formattedValue');
validateFilter(filter);
});

test('should work for auto histograms', () => {
const aggConfigs = getAggConfigs();
const histogramAggConfig = aggConfigs.aggs[0];
histogramAggConfig.params.interval = 'auto';
histogramAggConfig.params.used_interval = 1024;

const filter = createFilterHistogram(mockGetFieldFormatsStart)(
histogramAggConfig as IBucketAggConfig,
'2048'
);
validateFilter(filter);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ export const createFilterHistogram = (
return (aggConfig: IBucketAggConfig, key: string) => {
const { deserialize } = getFieldFormatsStart();
const value = parseInt(key, 10);
const params: RangeFilterParams = { gte: value, lt: value + aggConfig.params.interval };
const params: RangeFilterParams = {
gte: value,
lt:
value +
(typeof aggConfig.params.used_interval === 'number'
? aggConfig.params.used_interval
: aggConfig.params.interval),
};

return buildRangeFilter(
aggConfig.params.field,
Expand Down

0 comments on commit 3dfbfae

Please sign in to comment.