Skip to content

Commit c66124e

Browse files
[Lens] Make Lens intervals default value adapt to histogram:maxBars Advanced Setting changes (#89305)
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
1 parent 3255f90 commit c66124e

File tree

11 files changed

+75
-60
lines changed

11 files changed

+75
-60
lines changed

x-pack/plugins/lens/public/indexpattern_datasource/indexpattern.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export function getIndexPatternDatasource({
167167
});
168168
},
169169

170-
toExpression,
170+
toExpression: (state, layerId) => toExpression(state, layerId, uiSettings),
171171

172172
renderDataPanel(
173173
domElement: Element,

x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/date_histogram.test.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,11 @@ const indexPattern2: IndexPattern = {
8383
]),
8484
};
8585

86+
const uiSettingsMock = {} as IUiSettingsClient;
87+
8688
const defaultOptions = {
8789
storage: {} as IStorageWrapper,
88-
uiSettings: {} as IUiSettingsClient,
90+
uiSettings: uiSettingsMock,
8991
savedObjectsClient: {} as SavedObjectsClientContract,
9092
dateRange: {
9193
fromDate: 'now-1y',
@@ -200,7 +202,8 @@ describe('date_histogram', () => {
200202
layer.columns.col1 as DateHistogramIndexPatternColumn,
201203
'col1',
202204
indexPattern1,
203-
layer
205+
layer,
206+
uiSettingsMock
204207
);
205208
expect(esAggsFn).toEqual(
206209
expect.objectContaining({
@@ -252,7 +255,8 @@ describe('date_histogram', () => {
252255
},
253256
]),
254257
},
255-
layer
258+
layer,
259+
uiSettingsMock
256260
);
257261
expect(esAggsFn).toEqual(
258262
expect.objectContaining({

x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/filters/filters.test.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ import type { IndexPatternLayer } from '../../../types';
1616
import { createMockedIndexPattern } from '../../../mocks';
1717
import { FilterPopover } from './filter_popover';
1818

19+
const uiSettingsMock = {} as IUiSettingsClient;
20+
1921
const defaultProps = {
2022
storage: {} as IStorageWrapper,
21-
uiSettings: {} as IUiSettingsClient,
23+
uiSettings: uiSettingsMock,
2224
savedObjectsClient: {} as SavedObjectsClientContract,
2325
dateRange: { fromDate: 'now-1d', toDate: 'now' },
2426
data: dataPluginMock.createStartContract(),
@@ -84,7 +86,8 @@ describe('filters', () => {
8486
layer.columns.col1 as FiltersIndexPatternColumn,
8587
'col1',
8688
createMockedIndexPattern(),
87-
layer
89+
layer,
90+
uiSettingsMock
8891
);
8992
expect(esAggsFn).toEqual(
9093
expect.objectContaining({

x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ interface FieldlessOperationDefinition<C extends BaseIndexPatternColumn> {
239239
column: C,
240240
columnId: string,
241241
indexPattern: IndexPattern,
242-
layer: IndexPatternLayer
242+
layer: IndexPatternLayer,
243+
uiSettings: IUiSettingsClient
243244
) => ExpressionAstFunction;
244245
}
245246

@@ -283,7 +284,8 @@ interface FieldBasedOperationDefinition<C extends BaseIndexPatternColumn> {
283284
column: C,
284285
columnId: string,
285286
indexPattern: IndexPattern,
286-
layer: IndexPatternLayer
287+
layer: IndexPatternLayer,
288+
uiSettings: IUiSettingsClient
287289
) => ExpressionAstFunction;
288290
/**
289291
* Optional function to return the suffix used for ES bucket paths and esaggs column id.

x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/last_value.test.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ import { LastValueIndexPatternColumn } from './last_value';
1515
import { lastValueOperation } from './index';
1616
import type { IndexPattern, IndexPatternLayer } from '../../types';
1717

18+
const uiSettingsMock = {} as IUiSettingsClient;
19+
1820
const defaultProps = {
1921
storage: {} as IStorageWrapper,
20-
uiSettings: {} as IUiSettingsClient,
22+
uiSettings: uiSettingsMock,
2123
savedObjectsClient: {} as SavedObjectsClientContract,
2224
dateRange: { fromDate: 'now-1d', toDate: 'now' },
2325
data: dataPluginMock.createStartContract(),
@@ -70,7 +72,8 @@ describe('last_value', () => {
7072
{ ...lastValueColumn, params: { ...lastValueColumn.params } },
7173
'col1',
7274
{} as IndexPattern,
73-
layer
75+
layer,
76+
uiSettingsMock
7477
);
7578
expect(esAggsFn).toEqual(
7679
expect.objectContaining({

x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/percentile.test.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ import { EuiFieldNumber } from '@elastic/eui';
1717
import { act } from 'react-dom/test-utils';
1818
import { EuiFormRow } from '@elastic/eui';
1919

20+
const uiSettingsMock = {} as IUiSettingsClient;
21+
2022
const defaultProps = {
2123
storage: {} as IStorageWrapper,
22-
uiSettings: {} as IUiSettingsClient,
24+
uiSettings: uiSettingsMock,
2325
savedObjectsClient: {} as SavedObjectsClientContract,
2426
dateRange: { fromDate: 'now-1d', toDate: 'now' },
2527
data: dataPluginMock.createStartContract(),
@@ -72,7 +74,8 @@ describe('percentile', () => {
7274
percentileColumn,
7375
'col1',
7476
{} as IndexPattern,
75-
layer
77+
layer,
78+
uiSettingsMock
7679
);
7780
expect(esAggsFn).toEqual(
7881
expect.objectContaining({

x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/ranges/range_editor.tsx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import React, { useEffect, useState } from 'react';
7+
import React, { useState } from 'react';
88
import { i18n } from '@kbn/i18n';
99
import { FormattedMessage } from '@kbn/i18n/react';
1010
import {
@@ -190,15 +190,6 @@ export const RangeEditor = ({
190190
}) => {
191191
const [isAdvancedEditor, toggleAdvancedEditor] = useState(params.type === MODES.Range);
192192

193-
// if the maxBars in the params is set to auto refresh it with the default value only on bootstrap
194-
useEffect(() => {
195-
if (!isAdvancedEditor) {
196-
if (params.maxBars !== maxBars) {
197-
setParam('maxBars', maxBars);
198-
}
199-
}
200-
}, [maxBars, params.maxBars, setParam, isAdvancedEditor]);
201-
202193
if (isAdvancedEditor) {
203194
return (
204195
<AdvancedRangeEditor

x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/ranges/ranges.test.tsx

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@ dataPluginMockValue.fieldFormats.deserialize = jest.fn().mockImplementation(({ p
5151
type ReactMouseEvent = React.MouseEvent<HTMLAnchorElement, MouseEvent> &
5252
React.MouseEvent<HTMLButtonElement, MouseEvent>;
5353

54+
// need this for MAX_HISTOGRAM value
55+
const uiSettingsMock = ({
56+
get: jest.fn().mockReturnValue(100),
57+
} as unknown) as IUiSettingsClient;
58+
5459
const sourceField = 'MyField';
5560
const defaultOptions = {
5661
storage: {} as IStorageWrapper,
57-
// need this for MAX_HISTOGRAM value
58-
uiSettings: ({
59-
get: () => 100,
60-
} as unknown) as IUiSettingsClient,
62+
uiSettings: uiSettingsMock,
6163
savedObjectsClient: {} as SavedObjectsClientContract,
6264
dateRange: {
6365
fromDate: 'now-1y',
@@ -143,7 +145,8 @@ describe('ranges', () => {
143145
layer.columns.col1 as RangeIndexPatternColumn,
144146
'col1',
145147
{} as IndexPattern,
146-
layer
148+
layer,
149+
uiSettingsMock
147150
);
148151
expect(esAggsFn).toMatchInlineSnapshot(`
149152
Object {
@@ -166,6 +169,9 @@ describe('ranges', () => {
166169
"interval": Array [
167170
"auto",
168171
],
172+
"maxBars": Array [
173+
49.5,
174+
],
169175
"min_doc_count": Array [
170176
false,
171177
],
@@ -186,7 +192,8 @@ describe('ranges', () => {
186192
layer.columns.col1 as RangeIndexPatternColumn,
187193
'col1',
188194
{} as IndexPattern,
189-
layer
195+
layer,
196+
uiSettingsMock
190197
);
191198

192199
expect(esAggsFn).toEqual(
@@ -206,7 +213,8 @@ describe('ranges', () => {
206213
layer.columns.col1 as RangeIndexPatternColumn,
207214
'col1',
208215
{} as IndexPattern,
209-
layer
216+
layer,
217+
uiSettingsMock
210218
);
211219

212220
expect(esAggsFn).toEqual(
@@ -226,7 +234,8 @@ describe('ranges', () => {
226234
layer.columns.col1 as RangeIndexPatternColumn,
227235
'col1',
228236
{} as IndexPattern,
229-
layer
237+
layer,
238+
uiSettingsMock
230239
);
231240

232241
expect((esAggsFn as { arguments: unknown }).arguments).toEqual(
@@ -275,7 +284,7 @@ describe('ranges', () => {
275284

276285
it('should start update the state with the default maxBars value', () => {
277286
const updateLayerSpy = jest.fn();
278-
mount(
287+
const instance = mount(
279288
<InlineOptions
280289
{...defaultOptions}
281290
layer={layer}
@@ -285,19 +294,7 @@ describe('ranges', () => {
285294
/>
286295
);
287296

288-
expect(updateLayerSpy).toHaveBeenCalledWith({
289-
...layer,
290-
columns: {
291-
...layer.columns,
292-
col1: {
293-
...layer.columns.col1,
294-
params: {
295-
...layer.columns.col1.params,
296-
maxBars: GRANULARITY_DEFAULT_VALUE,
297-
},
298-
},
299-
},
300-
});
297+
expect(instance.find(EuiRange).prop('value')).toEqual(String(GRANULARITY_DEFAULT_VALUE));
301298
});
302299

303300
it('should update state when changing Max bars number', () => {
@@ -313,8 +310,6 @@ describe('ranges', () => {
313310
/>
314311
);
315312

316-
// There's a useEffect in the component that updates the value on bootstrap
317-
// because there's a debouncer, wait a bit before calling onChange
318313
act(() => {
319314
jest.advanceTimersByTime(TYPING_DEBOUNCE_TIME * 4);
320315

@@ -358,8 +353,6 @@ describe('ranges', () => {
358353
/>
359354
);
360355

361-
// There's a useEffect in the component that updates the value on bootstrap
362-
// because there's a debouncer, wait a bit before calling onChange
363356
act(() => {
364357
jest.advanceTimersByTime(TYPING_DEBOUNCE_TIME * 4);
365358
// minus button
@@ -368,6 +361,7 @@ describe('ranges', () => {
368361
.find('button')
369362
.prop('onClick')!({} as ReactMouseEvent);
370363
jest.advanceTimersByTime(TYPING_DEBOUNCE_TIME * 4);
364+
instance.update();
371365
});
372366

373367
expect(updateLayerSpy).toHaveBeenCalledWith({
@@ -391,6 +385,7 @@ describe('ranges', () => {
391385
.find('button')
392386
.prop('onClick')!({} as ReactMouseEvent);
393387
jest.advanceTimersByTime(TYPING_DEBOUNCE_TIME * 4);
388+
instance.update();
394389
});
395390

396391
expect(updateLayerSpy).toHaveBeenCalledWith({
@@ -788,7 +783,7 @@ describe('ranges', () => {
788783
instance.find(EuiLink).first().prop('onClick')!({} as ReactMouseEvent);
789784
});
790785

791-
expect(updateLayerSpy.mock.calls[1][0].columns.col1.params.format).toEqual({
786+
expect(updateLayerSpy.mock.calls[0][0].columns.col1.params.format).toEqual({
792787
id: 'custom',
793788
params: { decimals: 3 },
794789
});

x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/ranges/ranges.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export const rangeOperation: OperationDefinition<RangeIndexPatternColumn, 'field
132132
sourceField: field.name,
133133
};
134134
},
135-
toEsAggsFn: (column, columnId) => {
135+
toEsAggsFn: (column, columnId, indexPattern, layer, uiSettings) => {
136136
const { sourceField, params } = column;
137137
if (params.type === MODES.Range) {
138138
return buildExpressionFunction<AggFunctionsMapping['aggRange']>('aggRange', {
@@ -158,13 +158,15 @@ export const rangeOperation: OperationDefinition<RangeIndexPatternColumn, 'field
158158
),
159159
}).toAst();
160160
}
161+
const maxBarsDefaultValue =
162+
(uiSettings.get(UI_SETTINGS.HISTOGRAM_MAX_BARS) - MIN_HISTOGRAM_BARS) / 2;
163+
161164
return buildExpressionFunction<AggFunctionsMapping['aggHistogram']>('aggHistogram', {
162165
id: columnId,
163166
enabled: true,
164167
schema: 'segment',
165168
field: sourceField,
166-
// fallback to 0 in case of empty string
167-
maxBars: params.maxBars === AUTO_BARS ? undefined : params.maxBars,
169+
maxBars: params.maxBars === AUTO_BARS ? maxBarsDefaultValue : params.maxBars,
168170
interval: 'auto',
169171
has_extended_bounds: false,
170172
min_doc_count: false,

x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/terms/terms.test.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ import type { TermsIndexPatternColumn } from '.';
1717
import { termsOperation } from '../index';
1818
import { IndexPattern, IndexPatternLayer } from '../../../types';
1919

20+
const uiSettingsMock = {} as IUiSettingsClient;
21+
2022
const defaultProps = {
2123
storage: {} as IStorageWrapper,
22-
uiSettings: {} as IUiSettingsClient,
24+
uiSettings: uiSettingsMock,
2325
savedObjectsClient: {} as SavedObjectsClientContract,
2426
dateRange: { fromDate: 'now-1d', toDate: 'now' },
2527
data: dataPluginMock.createStartContract(),
@@ -66,7 +68,8 @@ describe('terms', () => {
6668
{ ...termsColumn, params: { ...termsColumn.params, otherBucket: true } },
6769
'col1',
6870
{} as IndexPattern,
69-
layer
71+
layer,
72+
uiSettingsMock
7073
);
7174
expect(esAggsFn).toEqual(
7275
expect.objectContaining({
@@ -89,7 +92,8 @@ describe('terms', () => {
8992
},
9093
'col1',
9194
{} as IndexPattern,
92-
layer
95+
layer,
96+
uiSettingsMock
9397
);
9498
expect(esAggsFn).toEqual(
9599
expect.objectContaining({
@@ -129,7 +133,8 @@ describe('terms', () => {
129133
},
130134
},
131135
},
132-
}
136+
},
137+
uiSettingsMock
133138
);
134139
expect(esAggsFn).toEqual(
135140
expect.objectContaining({

0 commit comments

Comments
 (0)