Skip to content

Commit

Permalink
[OnWeek][Discover] Allow to change current sample size and save it wi…
Browse files Browse the repository at this point in the history
…th a saved search (elastic#157269)

- Closes elastic#94140
- elastic#11758
- elastic#4060
- elastic#3220
- elastic#23307
- Closes elastic#131130 

## Summary

This PR allows to change current sample size right from Discover page,
no need to modify the global default value.

Saved search panels on Dashboard will also use the saved value to fetch
only the requested sample size. This customisation was requested by many
customers as it will allow to load Dashboards faster.

Current range for the slider: from 10 to 1000 (with a step 10).

<img width="400" alt="Screenshot 2023-10-09 at 11 10 52"
src="https://github.com/elastic/kibana/assets/1415710/74e2e4ad-9929-4a44-8d85-c2baafccbaa6">


### Checklist

- [x] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [x] This renders correctly on smaller devices using a responsive
layout. (You can test this [in your
browser](https://www.browserstack.com/guide/responsive-testing-on-local-server))
- [x] This was checked for [cross-browser
compatibility](https://www.elastic.co/support/matrix#matrix_browsers)
  • Loading branch information
jughosta authored and benakansara committed Oct 22, 2023
1 parent d54437a commit e98658b
Show file tree
Hide file tree
Showing 47 changed files with 964 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ReactWrapper } from 'enzyme';
import {
EuiButton,
EuiCopy,
EuiDataGrid,
EuiDataGridCellValueElementProps,
EuiDataGridCustomBodyProps,
} from '@elastic/eui';
Expand Down Expand Up @@ -52,7 +53,7 @@ function getProps(): UnifiedDataTableProps {
onSetColumns: jest.fn(),
onSort: jest.fn(),
rows: esHitsMock.map((hit) => buildDataTableRecord(hit, dataViewMock)),
sampleSize: 30,
sampleSizeState: 30,
searchDescription: '',
searchTitle: '',
setExpandedDoc: jest.fn(),
Expand Down Expand Up @@ -301,6 +302,74 @@ describe('UnifiedDataTable', () => {
});
});

describe('display settings', () => {
it('should include additional display settings if onUpdateSampleSize is provided', async () => {
const component = await getComponent({
...getProps(),
sampleSizeState: 150,
onUpdateSampleSize: jest.fn(),
onUpdateRowHeight: jest.fn(),
});

expect(component.find(EuiDataGrid).prop('toolbarVisibility')).toMatchInlineSnapshot(`
Object {
"additionalControls": <React.Fragment />,
"showColumnSelector": false,
"showDisplaySelector": Object {
"additionalDisplaySettings": <UnifiedDataTableAdditionalDisplaySettings
onChangeSampleSize={[MockFunction]}
sampleSize={150}
/>,
"allowDensity": false,
"allowResetButton": false,
"allowRowHeight": true,
},
"showFullScreenSelector": true,
"showSortSelector": true,
}
`);
});

it('should not include additional display settings if onUpdateSampleSize is not provided', async () => {
const component = await getComponent({
...getProps(),
sampleSizeState: 200,
onUpdateRowHeight: jest.fn(),
});

expect(component.find(EuiDataGrid).prop('toolbarVisibility')).toMatchInlineSnapshot(`
Object {
"additionalControls": <React.Fragment />,
"showColumnSelector": false,
"showDisplaySelector": Object {
"allowDensity": false,
"allowRowHeight": true,
},
"showFullScreenSelector": true,
"showSortSelector": true,
}
`);
});

it('should hide display settings if no handlers provided', async () => {
const component = await getComponent({
...getProps(),
onUpdateRowHeight: undefined,
onUpdateSampleSize: undefined,
});

expect(component.find(EuiDataGrid).prop('toolbarVisibility')).toMatchInlineSnapshot(`
Object {
"additionalControls": <React.Fragment />,
"showColumnSelector": false,
"showDisplaySelector": undefined,
"showFullScreenSelector": true,
"showSortSelector": true,
}
`);
});
});

describe('externalControlColumns', () => {
it('should render external leading control columns', async () => {
const component = await getComponent({
Expand Down
55 changes: 39 additions & 16 deletions packages/kbn-unified-data-table/src/components/data_table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
EuiDataGridControlColumn,
EuiDataGridCustomBodyProps,
EuiDataGridCellValueElementProps,
EuiDataGridToolBarVisibilityDisplaySelectorOptions,
EuiDataGridStyle,
} from '@elastic/eui';
import type { DataView } from '@kbn/data-views-plugin/public';
Expand Down Expand Up @@ -63,6 +64,7 @@ import {
toolbarVisibility as toolbarVisibilityDefaults,
} from '../constants';
import { UnifiedDataTableFooter } from './data_table_footer';
import { UnifiedDataTableAdditionalDisplaySettings } from './data_table_additional_display_settings';

export type SortOrder = [string, string];

Expand Down Expand Up @@ -137,10 +139,6 @@ export interface UnifiedDataTableProps {
* Array of documents provided by Elasticsearch
*/
rows?: DataTableRecord[];
/**
* The max size of the documents returned by Elasticsearch
*/
sampleSize: number;
/**
* Function to set the expanded document, which is displayed in a flyout
*/
Expand Down Expand Up @@ -205,6 +203,18 @@ export interface UnifiedDataTableProps {
* Update rows per page state
*/
onUpdateRowsPerPage?: (rowsPerPage: number) => void;
/**
* Configuration option to limit sample size slider
*/
maxAllowedSampleSize?: number;
/**
* The max size of the documents returned by Elasticsearch
*/
sampleSizeState: number;
/**
* Update rows per page state
*/
onUpdateSampleSize?: (sampleSize: number) => void;
/**
* Callback to execute on edit runtime field
*/
Expand Down Expand Up @@ -328,7 +338,6 @@ export const UnifiedDataTable = ({
onSetColumns,
onSort,
rows,
sampleSize,
searchDescription,
searchTitle,
settings,
Expand All @@ -342,6 +351,9 @@ export const UnifiedDataTable = ({
className,
rowHeightState,
onUpdateRowHeight,
maxAllowedSampleSize,
sampleSizeState,
onUpdateSampleSize,
isPlainRecord = false,
rowsPerPageState,
onUpdateRowsPerPage,
Expand Down Expand Up @@ -715,16 +727,27 @@ export const UnifiedDataTable = ({
[usedSelectedDocs, isFilterActive, rows, externalAdditionalControls]
);

const showDisplaySelector = useMemo(
() =>
!!onUpdateRowHeight
? {
allowDensity: false,
allowRowHeight: true,
}
: undefined,
[onUpdateRowHeight]
);
const showDisplaySelector = useMemo(() => {
const options: EuiDataGridToolBarVisibilityDisplaySelectorOptions = {};

if (onUpdateRowHeight) {
options.allowDensity = false;
options.allowRowHeight = true;
}

if (onUpdateSampleSize) {
options.allowResetButton = false;
options.additionalDisplaySettings = (
<UnifiedDataTableAdditionalDisplaySettings
maxAllowedSampleSize={maxAllowedSampleSize}
sampleSize={sampleSizeState}
onChangeSampleSize={onUpdateSampleSize}
/>
);
}

return Object.keys(options).length ? options : undefined;
}, [maxAllowedSampleSize, sampleSizeState, onUpdateRowHeight, onUpdateSampleSize]);

const inMemory = useMemo(() => {
return isPlainRecord && columns.length
Expand Down Expand Up @@ -837,7 +860,7 @@ export const UnifiedDataTable = ({
<UnifiedDataTableFooter
isLoadingMore={loadingState === DataLoadingState.loadingMore}
rowCount={rowCount}
sampleSize={sampleSize}
sampleSize={sampleSizeState}
pageCount={pageCount}
pageIndex={paginationObj?.pageIndex}
totalHits={totalHits}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import React from 'react';
import { mountWithIntl } from '@kbn/test-jest-helpers';
import { act } from 'react-dom/test-utils';
import { findTestSubject } from '@elastic/eui/lib/test';
import { UnifiedDataTableAdditionalDisplaySettings } from './data_table_additional_display_settings';
import lodash from 'lodash';

jest.spyOn(lodash, 'debounce').mockImplementation((fn: any) => fn);

describe('UnifiedDataTableAdditionalDisplaySettings', function () {
describe('sampleSize', function () {
it('should work correctly', async () => {
const onChangeSampleSizeMock = jest.fn();

const component = mountWithIntl(
<UnifiedDataTableAdditionalDisplaySettings
sampleSize={10}
onChangeSampleSize={onChangeSampleSizeMock}
/>
);
const input = findTestSubject(component, 'unifiedDataTableSampleSizeInput').last();
expect(input.prop('value')).toBe(10);

await act(async () => {
input.simulate('change', {
target: {
value: 100,
},
});
});

expect(onChangeSampleSizeMock).toHaveBeenCalledWith(100);

await new Promise((resolve) => setTimeout(resolve, 0));
component.update();

expect(
findTestSubject(component, 'unifiedDataTableSampleSizeInput').last().prop('value')
).toBe(100);
});

it('should not execute the callback for an invalid input', async () => {
const invalidValue = 600;
const onChangeSampleSizeMock = jest.fn();

const component = mountWithIntl(
<UnifiedDataTableAdditionalDisplaySettings
maxAllowedSampleSize={500}
sampleSize={50}
onChangeSampleSize={onChangeSampleSizeMock}
/>
);
const input = findTestSubject(component, 'unifiedDataTableSampleSizeInput').last();
expect(input.prop('value')).toBe(50);

await act(async () => {
input.simulate('change', {
target: {
value: invalidValue,
},
});
});

await new Promise((resolve) => setTimeout(resolve, 0));
component.update();

expect(
findTestSubject(component, 'unifiedDataTableSampleSizeInput').last().prop('value')
).toBe(invalidValue);

expect(onChangeSampleSizeMock).not.toHaveBeenCalled();
});

it('should render value changes correctly', async () => {
const onChangeSampleSizeMock = jest.fn();

const component = mountWithIntl(
<UnifiedDataTableAdditionalDisplaySettings
sampleSize={200}
onChangeSampleSize={onChangeSampleSizeMock}
/>
);

expect(
findTestSubject(component, 'unifiedDataTableSampleSizeInput').last().prop('value')
).toBe(200);

component.setProps({
sampleSize: 500,
onChangeSampleSize: onChangeSampleSizeMock,
});

await new Promise((resolve) => setTimeout(resolve, 0));
component.update();

expect(
findTestSubject(component, 'unifiedDataTableSampleSizeInput').last().prop('value')
).toBe(500);

expect(onChangeSampleSizeMock).not.toHaveBeenCalled();
});
});
});
Loading

0 comments on commit e98658b

Please sign in to comment.