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

Improve the perceived performance of Discover #6599

Merged
merged 1 commit into from
Apr 24, 2024
Merged
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
2 changes: 2 additions & 0 deletions changelogs/fragments/6599.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feat:
- Improve the perceived performance of Discover when using the default tabular renderer ([#6599](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6599))
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@
"@types/dedent": "^0.7.0",
"@types/deep-freeze-strict": "^1.1.0",
"@types/delete-empty": "^2.0.0",
"@types/dompurify": "^2.3.3",
"@types/dompurify": "^3.0.5",
"@types/elasticsearch": "^5.0.33",
"@types/enzyme": "^3.10.7",
"@types/eslint": "^6.1.3",
Expand Down Expand Up @@ -372,7 +372,7 @@
"d3-cloud": "1.2.5",
"dedent": "^0.7.0",
"delete-empty": "^2.0.0",
"dompurify": "^2.4.1",
"dompurify": "^3.0.11",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.5",
"enzyme-to-json": "^3.5.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import './_data_grid_table.scss';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does the css get imported twice or is it deduplicated? (not sure if the dedupe option is turned on for the loader)

Given this will be removed, feel free to ignore if this ends up having no real impact.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You are right. The other one needs to go. Will add it to s subsequent PR.


import React, { useMemo, useCallback } from 'react';
import { EuiDataGrid, EuiDataGridSorting } from '@elastic/eui';
import { IndexPattern, getServices } from '../../../opensearch_dashboards_services';
import { fetchTableDataCell } from './data_grid_table_cell_value';
import { buildDataGridColumns, computeVisibleColumns } from './data_grid_table_columns';
import { DocViewInspectButton } from './data_grid_table_docview_inspect_button';
import { OpenSearchSearchHit } from '../../doc_views/doc_views_types';
import { usePagination } from '../utils/use_pagination';
import { buildColumns } from '../../utils/columns';
import { DOC_HIDE_TIME_COLUMN_SETTING, SAMPLE_SIZE_SETTING } from '../../../../common';
import { UI_SETTINGS } from '../../../../../data/common';
import { SortOrder } from '../../../saved_searches/types';
import { useToolbarOptions } from './data_grid_toolbar';

export interface DataGridProps {
columns: string[];
indexPattern: IndexPattern;
onSort: (s: SortOrder[]) => void;
rows: OpenSearchSearchHit[];
onSetColumns: (columns: string[]) => void;
sort: SortOrder[];
isToolbarVisible?: boolean;
isContextView?: boolean;
}

const DataGridUI = ({
columns,
indexPattern,
onSetColumns,
onSort,
sort,
rows,
isToolbarVisible = true,
isContextView = false,
}: DataGridProps) => {
const services = getServices();
const rowCount = useMemo(() => (rows ? rows.length : 0), [rows]);
AMoo-Miki marked this conversation as resolved.
Show resolved Hide resolved
const { toolbarOptions, lineCount } = useToolbarOptions();
const [pageSizeLimit, isShortDots] = useMemo(() => {
return [
services.uiSettings.get(SAMPLE_SIZE_SETTING),
services.uiSettings.get(UI_SETTINGS.SHORT_DOTS_ENABLE),
];
}, [services.uiSettings]);
const displayTimeColumn = useMemo(
() =>
!services.uiSettings.get(DOC_HIDE_TIME_COLUMN_SETTING, false) && indexPattern?.isTimeBased(),
[indexPattern, services.uiSettings]
);
const pagination = usePagination({ rowCount, pageSizeLimit });

let adjustedColumns = buildColumns(columns);
AMoo-Miki marked this conversation as resolved.
Show resolved Hide resolved
// handle case where the user removes selected filed and leaves only time column
if (
adjustedColumns.length === 1 &&
indexPattern &&
adjustedColumns[0] === indexPattern.timeFieldName
) {
adjustedColumns = [...adjustedColumns, '_source'];
}

const includeSourceInColumns = adjustedColumns.includes('_source');
const sortingColumns = useMemo(() => sort.map(([id, direction]) => ({ id, direction })), [sort]);
const rowHeightsOptions = useMemo(
() => ({
defaultHeight: {
lineCount: lineCount || (includeSourceInColumns ? 3 : 1),
},
}),
[includeSourceInColumns, lineCount]
);

const onColumnSort = useCallback(
(cols: EuiDataGridSorting['columns']) => {
onSort(cols.map(({ id, direction }) => [id, direction]));
},
[onSort]
);

const renderCellValue = useMemo(() => fetchTableDataCell(indexPattern, rows, isShortDots), [
indexPattern,
isShortDots,
rows,
]);

const displayedTableColumns = useMemo(
() =>
buildDataGridColumns(
adjustedColumns,
indexPattern,
displayTimeColumn,
includeSourceInColumns,
isContextView
),
[adjustedColumns, indexPattern, displayTimeColumn, includeSourceInColumns, isContextView]
);

const dataGridTableColumnsVisibility = useMemo(
() => ({
visibleColumns: computeVisibleColumns(
adjustedColumns,
indexPattern,
displayTimeColumn
) as string[],
setVisibleColumns: (cols: string[]) => {
onSetColumns(cols);
},
}),
[adjustedColumns, indexPattern, displayTimeColumn, onSetColumns]
);

const sorting: EuiDataGridSorting = useMemo(
() => ({ columns: sortingColumns, onSort: onColumnSort }),
[sortingColumns, onColumnSort]
);

const leadingControlColumns = useMemo(() => {
return [
{
id: 'inspectCollapseColumn',
headerCellRender: () => null,
rowCellRender: DocViewInspectButton,
width: 40,
},
];
}, []);
AMoo-Miki marked this conversation as resolved.
Show resolved Hide resolved

return (
<EuiDataGrid
aria-labelledby="aria-labelledby"
columns={displayedTableColumns}
columnVisibility={dataGridTableColumnsVisibility}
leadingControlColumns={leadingControlColumns}
data-test-subj="docTable"
pagination={pagination}
renderCellValue={renderCellValue}
rowCount={rowCount}
sorting={sorting}
toolbarVisibility={isToolbarVisible ? toolbarOptions : false}
rowHeightsOptions={rowHeightsOptions}
className="discoverDataGrid"
/>
);
};

export const DataGrid = React.memo(DataGridUI);
Loading
Loading