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

Use new form for auto histogram #4

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
21 changes: 21 additions & 0 deletions src/dev/ci_setup/setup_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ installNode=$1

dir="$(pwd)"
cacheDir="$HOME/.kibana"
downloads="$cacheDir/downloads"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure that it's related to that PR


RED='\033[0;31m'
C_RESET='\033[0m' # Reset color
Expand Down Expand Up @@ -133,6 +134,26 @@ export CYPRESS_DOWNLOAD_MIRROR="https://us-central1-elastic-kibana-184716.cloudf

export CHECKS_REPORTER_ACTIVE=false

###
### Download Chrome and install to this shell
###

# Available using the version information search at https://omahaproxy.appspot.com/
chromeVersion=84

mkdir -p "$downloads"

if [ -d $cacheDir/chrome-$chromeVersion/chrome-linux ]; then
echo " -- Chrome already downloaded and extracted"
else
mkdir -p "$cacheDir/chrome-$chromeVersion"

echo " -- Downloading and extracting Chrome"
curl -o "$downloads/chrome.zip" -L "https://us-central1-elastic-kibana-184716.cloudfunctions.net/kibana-ci-proxy-cache/chrome_$chromeVersion.zip"
unzip -o "$downloads/chrome.zip" -d "$cacheDir/chrome-$chromeVersion"
export PATH="$cacheDir/chrome-$chromeVersion/chrome-linux:$PATH"
fi

# This is mainly for release-manager builds, which run in an environment that doesn't have Chrome installed
if [[ "$(which google-chrome-stable)" || "$(which google-chrome)" ]]; then
echo "Chrome detected, setting DETECT_CHROMEDRIVER_VERSION=true"
Expand Down
28 changes: 28 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,6 +103,34 @@ describe('Histogram Agg', () => {
});
});

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

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

describe('maxBars', () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you, I'll add that test

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 a whole number', () => {
const params = getParams({
Expand Down
18 changes: 15 additions & 3 deletions src/plugins/data/common/search/aggs/buckets/histogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { get } from 'lodash';
import { i18n } from '@kbn/i18n';

import { isAutoInterval, KBN_FIELD_TYPES, UI_SETTINGS } from '../../../../common';
import { KBN_FIELD_TYPES, UI_SETTINGS } from '../../../../common';
import { AggTypesDependencies } from '../agg_types';
import { BaseAggParams } from '../types';

Expand All @@ -47,7 +47,9 @@ export interface IBucketHistogramAggConfig extends IBucketAggConfig {

export interface AggParamsHistogram extends BaseAggParams {
field: string;
interval: string;
useAuto: boolean;
maxBars?: number;
interval?: number;
intervalBase?: number;
min_doc_count?: boolean;
has_extended_bounds?: boolean;
Expand Down Expand Up @@ -101,8 +103,16 @@ export const getHistogramBucketAgg = ({
default: null,
write: () => {},
},
{
name: 'useAuto',
default: true,
write: () => {},
},
{
name: 'interval',
shouldShow(agg) {
return !get(agg, 'params.useAuto');
},
modifyAggConfigOnSearchRequestStart(
aggConfig: IBucketHistogramAggConfig,
searchSource: any,
Expand Down Expand Up @@ -150,6 +160,7 @@ export const getHistogramBucketAgg = ({
const values = aggConfig.getAutoBounds();

output.params.interval = calculateHistogramInterval({
useAuto: aggConfig.params.useAuto,
values,
interval: aggConfig.params.interval,
maxBucketsUiSettings: getConfig(UI_SETTINGS.HISTOGRAM_MAX_BARS),
Expand All @@ -159,8 +170,9 @@ export const getHistogramBucketAgg = ({
},
{
name: 'maxBars',
default: getConfig(UI_SETTINGS.HISTOGRAM_MAX_BARS),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will cause a regression for integers values

image

Copy link
Owner

@alexwizp alexwizp Aug 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6 should be the correct interval for that case by default. But unfortunately I cannot calculate it on the default phase.
I didn't find better idea instead of showing 'auto' placeholder and calculate it after getting real min/max

image

shouldShow(agg) {
return isAutoInterval(get(agg, 'params.interval'));
return get(agg, 'params.useAuto');
},
write: () => {},
},
Expand Down
18 changes: 13 additions & 5 deletions src/plugins/data/common/search/aggs/buckets/histogram_fn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ describe('agg_expression_functions', () => {

test('fills in defaults when only required args are provided', () => {
const actual = fn({
useAuto: true,
field: 'field',
interval: '10',
});
expect(actual).toMatchInlineSnapshot(`
Object {
Expand All @@ -40,10 +40,12 @@ describe('agg_expression_functions', () => {
"extended_bounds": undefined,
"field": "field",
"has_extended_bounds": undefined,
"interval": "10",
"interval": undefined,
"intervalBase": undefined,
"json": undefined,
"maxBars": undefined,
"min_doc_count": undefined,
"useAuto": true,
},
"schema": undefined,
"type": "histogram",
Expand All @@ -54,9 +56,11 @@ describe('agg_expression_functions', () => {

test('includes optional params when they are provided', () => {
const actual = fn({
useAuto: false,
field: 'field',
interval: '10',
interval: 10,
intervalBase: 1,
maxBars: 100,
min_doc_count: false,
has_extended_bounds: false,
extended_bounds: JSON.stringify({
Expand All @@ -80,7 +84,9 @@ describe('agg_expression_functions', () => {
"interval": "10",
"intervalBase": 1,
"json": undefined,
"maxBars": 100,
"min_doc_count": false,
"useAuto": false,
},
"schema": undefined,
"type": "histogram",
Expand All @@ -90,17 +96,19 @@ describe('agg_expression_functions', () => {

test('correctly parses json string argument', () => {
const actual = fn({
useAuto: false,
field: 'field',
interval: '10',
interval: 10,
json: '{ "foo": true }',
});

expect(actual.value.params.json).toEqual({ foo: true });

expect(() => {
fn({
useAuto: false,
field: 'field',
interval: '10',
interval: 10,
json: '/// intentionally malformed json ///',
});
}).toThrowErrorMatchingInlineSnapshot(`"Unable to parse json argument string"`);
Expand Down
24 changes: 21 additions & 3 deletions src/plugins/data/common/search/aggs/buckets/histogram_fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ const fnName = 'aggHistogram';
type Input = any;
type AggArgs = AggExpressionFunctionArgs<typeof BUCKET_TYPES.HISTOGRAM>;

type Arguments = Assign<AggArgs, { extended_bounds?: string }>;
type Arguments = Assign<
AggArgs,
{
extended_bounds?: string;
}
>;

type Output = AggExpressionType;
type FunctionDefinition = ExpressionFunctionDefinition<typeof fnName, Input, Arguments, Output>;
Expand Down Expand Up @@ -66,9 +71,16 @@ export const aggHistogram = (): FunctionDefinition => ({
defaultMessage: 'Field to use for this aggregation',
}),
},
interval: {
types: ['string'],
useAuto: {
types: ['boolean'],
required: true,
help: i18n.translate('data.search.aggs.buckets.histogram.useAuto.help', {
defaultMessage:
'Specifies whether to calculate an auto interval or use the provided interval',
}),
},
interval: {
types: ['number'],
help: i18n.translate('data.search.aggs.buckets.histogram.interval.help', {
defaultMessage: 'Interval to use for this aggregation',
}),
Expand All @@ -79,6 +91,12 @@ export const aggHistogram = (): FunctionDefinition => ({
defaultMessage: 'IntervalBase to use 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',
}),
},
min_doc_count: {
types: ['boolean'],
help: i18n.translate('data.search.aggs.buckets.histogram.minDocCount.help', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,14 @@ import {
CalculateHistogramIntervalParams,
} from './histogram_calculate_interval';

const getLargestPossibleInterval = (params: CalculateHistogramIntervalParams) => {
const diff = params.values!.max - params.values!.min;
return diff / params.maxBucketsUiSettings;
};

describe('calculateHistogramInterval', () => {
describe('auto calculating mode', () => {
let params: CalculateHistogramIntervalParams;

beforeEach(() => {
params = {
interval: 'auto',
useAuto: true,
interval: '',
intervalBase: undefined,
maxBucketsUiSettings: 100,
maxBucketsUserInput: undefined,
Expand All @@ -51,13 +47,10 @@ describe('calculateHistogramInterval', () => {
maxBucketsUserInput: 200,
values: {
min: 150,
max: 250,
max: 350,
},
};
const expectedInterval = getLargestPossibleInterval(p);

expect(expectedInterval).toBe(1);
expect(calculateHistogramInterval(p)).toBe(expectedInterval);
expect(calculateHistogramInterval(p)).toBe(2);
});

test('should correctly work for float numbers (small numbers)', () => {
Expand Down Expand Up @@ -96,10 +89,7 @@ describe('calculateHistogramInterval', () => {
max: 100,
},
};
const expectedInterval = getLargestPossibleInterval(p);

expect(expectedInterval).toBe(1);
expect(calculateHistogramInterval(p)).toBe(expectedInterval);
expect(calculateHistogramInterval(p)).toBe(1);
});

test('should set intervals for integer numbers (diff less than maxBucketsUiSettings)', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
* under the License.
*/

import { isAutoInterval } from '../_interval_options';

interface IntervalValuesRange {
min: number;
max: number;
}

export interface CalculateHistogramIntervalParams {
useAuto: boolean;
interval: string;
maxBucketsUiSettings: number;
maxBucketsUserInput?: number;
Expand Down Expand Up @@ -103,17 +102,17 @@ const calculateAutoInterval = (
};

export const calculateHistogramInterval = ({
useAuto,
interval,
maxBucketsUiSettings,
maxBucketsUserInput,
intervalBase,
values,
}: CalculateHistogramIntervalParams) => {
const isAuto = isAutoInterval(interval);
let calculatedInterval = 0;

if (values) {
calculatedInterval = isAuto
calculatedInterval = useAuto
? calculateAutoInterval(values, maxBucketsUiSettings, maxBucketsUserInput)
: calculateForGivenInterval(values, parseFloat(interval), maxBucketsUiSettings);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const buckets = {
[BUCKET_TYPES.HISTOGRAM]: {
interval: controls.NumberIntervalParamEditor,
maxBars: controls.MaxBarsParamEditor,
useAuto: controls.AutoIntervalParamEditor,
min_doc_count: controls.MinDocCountParamEditor,
has_extended_bounds: controls.HasExtendedBoundsParamEditor,
extended_bounds: controls.ExtendedBoundsParamEditor,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import React from 'react';

import { EuiSwitch, EuiFormRow } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { AggParamEditorProps } from '../agg_param_props';

function AutoIntervalParamEditor({ value = false, setValue }: AggParamEditorProps<boolean>) {
const label = i18n.translate('visDefaultEditor.controls.useAutoInterval', {
defaultMessage: 'Use auto interval',
});

return (
<EuiFormRow compressed>
<EuiSwitch
compressed={true}
label={label}
checked={value}
onChange={(ev) => setValue(ev.target.checked)}
/>
</EuiFormRow>
);
}

export { AutoIntervalParamEditor };
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* under the License.
*/

export { AutoIntervalParamEditor } from './auto_switch';
export { AutoPrecisionParamEditor } from './auto_precision';
export { DateRangesParamEditor } from './date_ranges';
export { DropPartialsParamEditor } from './drop_partials';
Expand Down
Loading