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

[RAC] EuiDataGrid pagination #109269

Merged
merged 12 commits into from
Aug 26, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export function AlertsTableTGrid(props: AlertsTableTGridProps) {
return [
{
id: 'expand',
width: 96,
width: 120,
headerCellRender: () => {
return (
<EventsThContent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ export const FILTER_FOR_VALUE = i18n.translate('xpack.observability.hoverActions
defaultMessage: 'Filter for value',
});

/**
* rowIndex is bigger than `data.length` for pages with page numbers bigger than one.
* For that reason, we must calculate `rowIndex % itemsPerPage`.
*
* Ex:
* Given `rowIndex` is `13` and `itemsPerPage` is `10`.
* It means that the `activePage` is `2` and the `pageRowIndex` is `3`
*
* **Warning**:
* Be careful with array out of bounds. `pageRowIndex` can be bigger or equal to `data.length`
* in the scenario where the user changes the event status (Open, Acknowledged, Closed).
*/
export const getPageRowIndex = (rowIndex: number, itemsPerPage: number) => rowIndex % itemsPerPage;

/** a hook to eliminate the verbose boilerplate required to use common services */
const useKibanaServices = () => {
const { timelines } = useKibana<{ timelines: TimelinesUIStart }>().services;
Expand All @@ -35,11 +49,15 @@ const useKibanaServices = () => {

/** actions common to all cells (e.g. copy to clipboard) */
const commonCellActions: TGridCellAction[] = [
({ data }: { data: TimelineNonEcsData[][] }) => ({ rowIndex, columnId, Component }) => {
({ data, pageSize }: { data: TimelineNonEcsData[][]; pageSize: number }) => ({
rowIndex,
columnId,
Component,
}) => {
const { timelines } = useKibanaServices();

const value = getMappedNonEcsValue({
data: data[rowIndex],
data: data[getPageRowIndex(rowIndex, pageSize)],
fieldName: columnId,
});

Expand All @@ -60,9 +78,13 @@ const commonCellActions: TGridCellAction[] = [

/** actions for adding filters to the search bar */
const buildFilterCellActions = (addToQuery: (value: string) => void): TGridCellAction[] => [
({ data }: { data: TimelineNonEcsData[][] }) => ({ rowIndex, columnId, Component }) => {
({ data, pageSize }: { data: TimelineNonEcsData[][]; pageSize: number }) => ({
rowIndex,
columnId,
Component,
}) => {
const value = getMappedNonEcsValue({
data: data[rowIndex],
data: data[getPageRowIndex(rowIndex, pageSize)],
fieldName: columnId,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ export const LOAD_MORE =
export const SERVER_SIDE_EVENT_COUNT = '[data-test-subj="server-side-event-count"]';

export const EVENTS_VIEWER_PAGINATION =
'[data-test-subj="events-viewer-panel"] [data-test-subj="timeline-pagination"]';
'[data-test-subj="events-viewer-panel"] .euiDataGrid__pagination';
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,36 @@ const useKibanaServices = () => {
return { timelines, filterManager };
};

/**
* rowIndex is bigger than `data.length` for pages with page numbers bigger than one.
* For that reason, we must calculate `rowIndex % itemsPerPage`.
*
* Ex:
* Given `rowIndex` is `13` and `itemsPerPage` is `10`.
* It means that the `activePage` is `2` and the `pageRowIndex` is `3`
*
* **Warning**:
* Be careful with array out of bounds. `pageRowIndex` can be bigger or equal to `data.length`
* in the scenario where the user changes the event status (Open, Acknowledged, Closed).
*/
export const getPageRowIndex = (rowIndex: number, itemsPerPage: number) => rowIndex % itemsPerPage;
Copy link
Member Author

Choose a reason for hiding this comment

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

This function is defined twice (timelines and security solutions). Where can we move it to reuse on both places?

Copy link
Contributor

@stephmilovic stephmilovic Aug 25, 2021

Choose a reason for hiding this comment

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

since this is already importing from the timeslines plugin, i think we can import it from there

Copy link
Contributor

Choose a reason for hiding this comment

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

okay I exported it from timeline public, it is imported by both o11y and securitySol.


/** the default actions shown in `EuiDataGrid` cells */
export const defaultCellActions: TGridCellAction[] = [
({ data }: { data: TimelineNonEcsData[][] }) => ({ rowIndex, columnId, Component }) => {
({ data, pageSize }: { data: TimelineNonEcsData[][]; pageSize: number }) => ({
rowIndex,
columnId,
Component,
}) => {
const { timelines, filterManager } = useKibanaServices();

const pageRowIndex = getPageRowIndex(rowIndex, pageSize);
if (pageRowIndex >= data.length) {
return null;
}

const value = getMappedNonEcsValue({
data: data[rowIndex],
data: data[pageRowIndex],
fieldName: columnId,
});

Expand All @@ -58,11 +81,20 @@ export const defaultCellActions: TGridCellAction[] = [
</>
);
},
({ data }: { data: TimelineNonEcsData[][] }) => ({ rowIndex, columnId, Component }) => {
({ data, pageSize }: { data: TimelineNonEcsData[][]; pageSize: number }) => ({
rowIndex,
columnId,
Component,
}) => {
const { timelines, filterManager } = useKibanaServices();

const pageRowIndex = getPageRowIndex(rowIndex, pageSize);
if (pageRowIndex >= data.length) {
return null;
}

const value = getMappedNonEcsValue({
data: data[rowIndex],
data: data[pageRowIndex],
fieldName: columnId,
});

Expand All @@ -80,11 +112,20 @@ export const defaultCellActions: TGridCellAction[] = [
</>
);
},
({ data }: { data: TimelineNonEcsData[][] }) => ({ rowIndex, columnId, Component }) => {
({ data, pageSize }: { data: TimelineNonEcsData[][]; pageSize: number }) => ({
rowIndex,
columnId,
Component,
}) => {
const { timelines } = useKibanaServices();

const pageRowIndex = getPageRowIndex(rowIndex, pageSize);
if (pageRowIndex >= data.length) {
return null;
}

const value = getMappedNonEcsValue({
data: data[rowIndex],
data: data[pageRowIndex],
fieldName: columnId,
});

Expand Down Expand Up @@ -122,16 +163,23 @@ export const defaultCellActions: TGridCellAction[] = [
browserFields,
data,
timelineId,
pageSize,
}: {
browserFields: BrowserFields;
data: TimelineNonEcsData[][];
timelineId: string;
pageSize: number;
}) => ({ rowIndex, columnId, Component }) => {
const [showTopN, setShowTopN] = useState(false);
const onClick = useCallback(() => setShowTopN(!showTopN), [showTopN]);

const pageRowIndex = getPageRowIndex(rowIndex, pageSize);
if (pageRowIndex >= data.length) {
return null;
}

const value = getMappedNonEcsValue({
data: data[rowIndex],
data: data[pageRowIndex],
fieldName: columnId,
});

Expand Down Expand Up @@ -159,11 +207,20 @@ export const defaultCellActions: TGridCellAction[] = [
</>
);
},
({ data }: { data: TimelineNonEcsData[][] }) => ({ rowIndex, columnId, Component }) => {
({ data, pageSize }: { data: TimelineNonEcsData[][]; pageSize: number }) => ({
rowIndex,
columnId,
Component,
}) => {
const { timelines } = useKibanaServices();

const pageRowIndex = getPageRowIndex(rowIndex, pageSize);
if (pageRowIndex >= data.length) {
return null;
}

const value = getMappedNonEcsValue({
data: data[rowIndex],
data: data[pageRowIndex],
fieldName: columnId,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { esQuery, Filter } from '../../../../../../../src/plugins/data/public';
import { Status } from '../../../../common/detection_engine/schemas/common/schemas';
import { RowRendererId, TimelineIdLiteral } from '../../../../common/types/timeline';
import { StatefulEventsViewer } from '../../../common/components/events_viewer';
import { HeaderSection } from '../../../common/components/header_section';
import {
displayErrorToast,
displaySuccessToast,
Expand Down Expand Up @@ -371,8 +370,7 @@ export const AlertsTableComponent: React.FC<AlertsTableComponentProps> = ({

if (loading || indexPatternsLoading || isEmpty(selectedPatterns)) {
return (
<EuiPanel hasBorder>
<HeaderSection title="" />
<EuiPanel hasBorder={false} hasShadow={false} paddingSize="none">
<EuiLoadingContent data-test-subj="loading-alerts-panel" />
</EuiPanel>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ export type TGridCellAction = ({
browserFields,
data,
timelineId,
pageSize,
}: {
browserFields: BrowserFields;
/** each row of data is represented as one TimelineNonEcsData[] */
data: TimelineNonEcsData[][];
timelineId: string;
pageSize: number;
}) => (props: EuiDataGridColumnCellActionProps) => ReactNode;

/** The specification of a column header */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { useState, useLayoutEffect } from 'react';

// That could be different from security and observability. Get it as parameter?
const INITIAL_DATA_GRID_HEIGHT = 967;

// It will recalculate DataGrid height after this time interval.
const TIME_INTERVAL = 50;

/**
* You are probably asking yourself "Why 3?". But that is the wrong mindset. You should be asking yourself "why not 3?".
Copy link
Contributor

Choose a reason for hiding this comment

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

😂

* 3 (three) is a number, numeral and digit. It is the natural number following 2 and preceding 4, and is the smallest
* odd prime number and the only prime preceding a square number. It has religious or cultural significance in many societies.
*/
const MAGIC_GAP = 3;

/**
* HUGE HACK!!!
* DataGrtid height isn't properly calculated when the grid has horizontal scroll.
* https://github.com/elastic/eui/issues/5030
*
* In order to get around this bug we are calculating `DataGrid` height here and setting it as a prop.
*
* Please delete me and allow DataGrid to calculate its height when the bug is fixed.
*/
export const useDataGridHeightHack = (pageSize: number, rowCount: number) => {
const [height, setHeight] = useState(INITIAL_DATA_GRID_HEIGHT);

useLayoutEffect(() => {
setTimeout(() => {
const gridVirtualized = document.querySelector('#body-data-grid .euiDataGrid__virtualized');

if (
gridVirtualized &&
gridVirtualized.children[0].clientHeight !== gridVirtualized.clientHeight // check if it has vertical scroll
) {
setHeight(
height +
gridVirtualized.children[0].clientHeight -
gridVirtualized.clientHeight +
MAGIC_GAP
);
}
}, TIME_INTERVAL);
}, [pageSize, rowCount, height]);

return height;
};
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ describe('Body', () => {
excludedRowRendererIds: [],
id: 'timeline-test',
isSelectAllChecked: false,
isLoading: false,
itemsPerPageOptions: [],
loadingEventIds: [],
loadPage: jest.fn(),
querySize: 25,
pageSize: 25,
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this the initial amount shown in the table? Would it make sense to have this be at least 50?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hi @mdefazio , Yes the default size is 50 items in security solutions and observability. This is just a test, we are good 👍

renderCellValue: TestCellRenderer,
rowRenderers: [],
selectedEventIds: {},
Expand All @@ -78,7 +79,6 @@ describe('Body', () => {
showCheckboxes: false,
tabType: TimelineTabs.query,
tableView: 'gridView',
totalPages: 1,
totalItems: 1,
leadingControlColumns: [],
trailingControlColumns: [],
Expand Down
Loading