Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auto interval to histogram AggConfig #76001

Merged
merged 21 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
import { i18n } from '@kbn/i18n';
import { IBucketAggConfig } from './bucket_agg_type';

export const autoInterval = 'auto';
alexwizp marked this conversation as resolved.
Show resolved Hide resolved
export const isAutoInterval = (value: unknown) => value === autoInterval;

export const intervalOptions = [
{
display: i18n.translate('data.search.aggs.buckets.intervalOptions.autoDisplayName', {
defaultMessage: 'Auto',
}),
val: 'auto',
val: autoInterval,
enabled(agg: IBucketAggConfig) {
// not only do we need a time field, but the selected field needs
// to be the time field. (see #3028)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import moment from 'moment';
import { createFilterDateHistogram } from './date_histogram';
import { intervalOptions } from '../_interval_options';
import { intervalOptions, autoInterval } from '../_interval_options';
import { AggConfigs } from '../../agg_configs';
import { mockAggTypesRegistry } from '../../test_helpers';
import { IBucketDateHistogramAggConfig } from '../date_histogram';
Expand All @@ -33,7 +33,10 @@ describe('AggConfig Filters', () => {
let bucketStart: any;
let field: any;

const init = (interval: string = 'auto', duration: any = moment.duration(15, 'minutes')) => {
const init = (
interval: string = autoInterval,
duration: any = moment.duration(15, 'minutes')
) => {
field = {
name: 'date',
};
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/data/common/search/aggs/buckets/date_histogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { i18n } from '@kbn/i18n';

import { KBN_FIELD_TYPES, TimeRange, TimeRangeBounds, UI_SETTINGS } from '../../../../common';

import { intervalOptions } from './_interval_options';
import { intervalOptions, autoInterval, isAutoInterval } from './_interval_options';
import { createFilterDateHistogram } from './create_filter/date_histogram';
import { BucketAggType, IBucketAggConfig } from './bucket_agg_type';
import { BUCKET_TYPES } from './bucket_agg_types';
Expand All @@ -44,7 +44,7 @@ const updateTimeBuckets = (
customBuckets?: IBucketDateHistogramAggConfig['buckets']
) => {
const bounds =
agg.params.timeRange && (agg.fieldIsTimeField() || agg.params.interval === 'auto')
agg.params.timeRange && (agg.fieldIsTimeField() || isAutoInterval(agg.params.interval))
? calculateBounds(agg.params.timeRange)
: undefined;
const buckets = customBuckets || agg.buckets;
Expand Down Expand Up @@ -149,7 +149,7 @@ export const getDateHistogramBucketAgg = ({
return agg.getIndexPattern().timeFieldName;
},
onChange(agg: IBucketDateHistogramAggConfig) {
if (get(agg, 'params.interval') === 'auto' && !agg.fieldIsTimeField()) {
if (isAutoInterval(get(agg, 'params.interval')) && !agg.fieldIsTimeField()) {
delete agg.params.interval;
}
},
Expand Down Expand Up @@ -187,7 +187,7 @@ export const getDateHistogramBucketAgg = ({
}
return state;
},
default: 'auto',
default: autoInterval,
options: intervalOptions,
write(agg, output, aggs) {
updateTimeBuckets(agg, calculateBounds);
Expand Down
21 changes: 21 additions & 0 deletions src/plugins/data/common/search/aggs/buckets/histogram.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,28 @@ describe('Histogram Agg', () => {
});
});

describe('maxBars', () => {
test('should not be written to the DSL', () => {
const aggConfigs = getAggConfigs({
maxBars: 50,
field: {
name: 'field',
},
});
const { [BUCKET_TYPES.HISTOGRAM]: params } = aggConfigs.aggs[0].toDsl();

expect(params).not.toHaveProperty('maxBars');
});
});

describe('interval', () => {
test('accepts "auto" value', () => {
const params = getParams({
interval: 'auto',
});

expect(params).toHaveProperty('interval', 1);
});
test('accepts a whole number', () => {
const params = getParams({
interval: 100,
Expand Down
65 changes: 25 additions & 40 deletions src/plugins/data/common/search/aggs/buckets/histogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import { BucketAggType, IBucketAggConfig } from './bucket_agg_type';
import { createFilterHistogram } from './create_filter/histogram';
import { BUCKET_TYPES } from './bucket_agg_types';
import { ExtendedBounds } from './lib/extended_bounds';
import { isAutoInterval, autoInterval } from './_interval_options';
import { calculateHistogramInterval } from './lib/histogram_calculate_interval';

export interface AutoBounds {
min: number;
Expand All @@ -47,6 +49,7 @@ export interface IBucketHistogramAggConfig extends IBucketAggConfig {
export interface AggParamsHistogram extends BaseAggParams {
field: string;
interval: string;
maxBars?: number;
intervalBase?: number;
min_doc_count?: boolean;
has_extended_bounds?: boolean;
Expand Down Expand Up @@ -102,6 +105,7 @@ export const getHistogramBucketAgg = ({
},
{
name: 'interval',
default: autoInterval,
modifyAggConfigOnSearchRequestStart(
aggConfig: IBucketHistogramAggConfig,
searchSource: any,
Expand All @@ -127,9 +131,12 @@ export const getHistogramBucketAgg = ({
return childSearchSource
.fetch(options)
.then((resp: any) => {
const min = get(resp, 'aggregations.minAgg.value', 0);
const max = get(resp, 'aggregations.maxAgg.value', 0);
alexwizp marked this conversation as resolved.
Show resolved Hide resolved

aggConfig.setAutoBounds({
min: get(resp, 'aggregations.minAgg.value'),
max: get(resp, 'aggregations.maxAgg.value'),
min,
max,
});
})
.catch((e: Error) => {
Expand All @@ -143,46 +150,24 @@ export const getHistogramBucketAgg = ({
});
},
write(aggConfig, output) {
let interval = parseFloat(aggConfig.params.interval);
if (interval <= 0) {
interval = 1;
}
const autoBounds = aggConfig.getAutoBounds();

// ensure interval does not create too many buckets and crash browser
if (autoBounds) {
const range = autoBounds.max - autoBounds.min;
const bars = range / interval;

if (bars > getConfig(UI_SETTINGS.HISTOGRAM_MAX_BARS)) {
const minInterval = range / getConfig(UI_SETTINGS.HISTOGRAM_MAX_BARS);

// Round interval by order of magnitude to provide clean intervals
// Always round interval up so there will always be less buckets than histogram:maxBars
const orderOfMagnitude = Math.pow(10, Math.floor(Math.log10(minInterval)));
let roundInterval = orderOfMagnitude;

while (roundInterval < minInterval) {
roundInterval += orderOfMagnitude;
}
interval = roundInterval;
}
}
const base = aggConfig.params.intervalBase;

if (base) {
if (interval < base) {
// In case the specified interval is below the base, just increase it to it's base
interval = base;
} else if (interval % base !== 0) {
// In case the interval is not a multiple of the base round it to the next base
interval = Math.round(interval / base) * base;
}
}

output.params.interval = interval;
const values = aggConfig.getAutoBounds();

output.params.interval = calculateHistogramInterval({
values,
interval: aggConfig.params.interval,
maxBucketsUiSettings: getConfig(UI_SETTINGS.HISTOGRAM_MAX_BARS),
maxBucketsUserInput: aggConfig.params.maxBars,
intervalBase: aggConfig.params.intervalBase,
});
},
},
{
name: 'maxBars',
shouldShow(agg) {
return isAutoInterval(get(agg, 'params.interval'));
},
write: () => {},
},
{
name: 'min_doc_count',
default: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe('agg_expression_functions', () => {
"interval": "10",
"intervalBase": undefined,
"json": undefined,
"maxBars": undefined,
"min_doc_count": undefined,
},
"schema": undefined,
Expand All @@ -55,8 +56,9 @@ describe('agg_expression_functions', () => {
test('includes optional params when they are provided', () => {
const actual = fn({
field: 'field',
interval: '10',
interval: 'auto',
intervalBase: 1,
maxBars: 25,
min_doc_count: false,
has_extended_bounds: false,
extended_bounds: JSON.stringify({
Expand All @@ -77,9 +79,10 @@ describe('agg_expression_functions', () => {
},
"field": "field",
"has_extended_bounds": false,
"interval": "10",
"interval": "auto",
"intervalBase": 1,
"json": undefined,
"maxBars": 25,
"min_doc_count": false,
},
"schema": undefined,
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/data/common/search/aggs/buckets/histogram_fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ export const aggHistogram = (): FunctionDefinition => ({
defaultMessage: 'Specifies whether to use min_doc_count for this aggregation',
}),
},
maxBars: {
types: ['number'],
help: i18n.translate('data.search.aggs.buckets.histogram.maxBars.help', {
defaultMessage: 'Calculate interval to get approximately this many bars',
}),
},
has_extended_bounds: {
types: ['boolean'],
help: i18n.translate('data.search.aggs.buckets.histogram.hasExtendedBounds.help', {
Expand Down
Loading