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

[ML] Fix race condition when updating data grid row count #149518

Merged
merged 3 commits into from
Jan 27, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import { RuntimeMappings } from '../../../../common/types/fields';
import { isRuntimeMappings } from '../../../../common/util/runtime_field_utils';

export const INIT_MAX_COLUMNS = 10;
export const COLUMN_CHART_DEFAULT_VISIBILITY_ROWS_THRESHOLED = 10000;
export const COLUMN_CHART_DEFAULT_VISIBILITY_ROWS_THRESHOLD = 10000;

export const euiDataGridStyle: EuiDataGridStyle = {
border: 'all',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export type DataGridItem = Record<string, any>;
export type ChartsVisible = boolean | undefined;
export type RowCountRelation = estypes.SearchTotalHitsRelation | undefined;

export interface RowCountInfo {
rowCount: number;
rowCountRelation: RowCountRelation;
}

export type IndexPagination = Pick<EuiDataGridPaginationProps, 'pageIndex' | 'pageSize'>;

export type OnChangeItemsPerPage = (pageSize: any) => void;
Expand Down Expand Up @@ -105,8 +110,7 @@ export interface UseDataGridReturnType {
setErrorMessage: Dispatch<SetStateAction<string>>;
setNoDataMessage: Dispatch<SetStateAction<string>>;
setPagination: Dispatch<SetStateAction<IndexPagination>>;
setRowCount: Dispatch<SetStateAction<number>>;
setRowCountRelation: Dispatch<SetStateAction<RowCountRelation>>;
setRowCountInfo: Dispatch<SetStateAction<RowCountInfo>>;
setSortingColumns: Dispatch<SetStateAction<EuiDataGridSorting['columns']>>;
setStatus: Dispatch<SetStateAction<INDEX_STATUS>>;
setTableItems: Dispatch<SetStateAction<DataGridItem[]>>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ChartData } from '../../../../common/types/field_histograms';
import { INDEX_STATUS } from '../../data_frame_analytics/common';

import { ColumnChart } from './column_chart';
import { COLUMN_CHART_DEFAULT_VISIBILITY_ROWS_THRESHOLED, INIT_MAX_COLUMNS } from './common';
import { COLUMN_CHART_DEFAULT_VISIBILITY_ROWS_THRESHOLD, INIT_MAX_COLUMNS } from './common';
import {
ChartsVisible,
ColumnId,
Expand All @@ -24,10 +24,15 @@ import {
OnChangeItemsPerPage,
OnChangePage,
OnSort,
RowCountRelation,
RowCountInfo,
UseDataGridReturnType,
} from './types';

const rowCountDefault: RowCountInfo = {
rowCount: 0,
rowCountRelation: undefined,
};

export const useDataGrid = (
columns: EuiDataGridColumn[],
defaultPageSize = 5,
Expand All @@ -40,14 +45,15 @@ export const useDataGrid = (
const [noDataMessage, setNoDataMessage] = useState('');
const [errorMessage, setErrorMessage] = useState('');
const [status, setStatus] = useState(INDEX_STATUS.UNUSED);
const [rowCount, setRowCount] = useState(0);
const [rowCountRelation, setRowCountRelation] = useState<RowCountRelation>(undefined);
const [rowCountInfo, setRowCountInfo] = useState<RowCountInfo>(rowCountDefault);
const [columnCharts, setColumnCharts] = useState<ChartData[]>([]);
const [tableItems, setTableItems] = useState<DataGridItem[]>([]);
const [pagination, setPagination] = useState(defaultPagination);
const [sortingColumns, setSortingColumns] = useState<EuiDataGridSorting['columns']>([]);
const [chartsVisible, setChartsVisible] = useState<ChartsVisible>(undefined);

const { rowCount, rowCountRelation } = rowCountInfo;

const toggleChartVisibility = () => {
if (chartsVisible !== undefined) {
setChartsVisible(!chartsVisible);
Expand Down Expand Up @@ -148,7 +154,7 @@ export const useDataGrid = (
useEffect(() => {
if (chartsVisible === undefined && rowCount > 0 && rowCountRelation !== undefined) {
setChartsVisible(
rowCount <= COLUMN_CHART_DEFAULT_VISIBILITY_ROWS_THRESHOLED &&
rowCount <= COLUMN_CHART_DEFAULT_VISIBILITY_ROWS_THRESHOLD &&
rowCountRelation !== ES_CLIENT_TOTAL_HITS_RELATION.GTE
);
}
Expand All @@ -174,8 +180,7 @@ export const useDataGrid = (
setErrorMessage,
setNoDataMessage,
setPagination,
setRowCount,
setRowCountRelation,
setRowCountInfo,
setSortingColumns,
setStatus,
setTableItems,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export const getIndexData = async (
const {
pagination,
setErrorMessage,
setRowCount,
setRowCountRelation,
setRowCountInfo,
setStatus,
setTableItems,
sortingColumns,
Expand Down Expand Up @@ -64,12 +63,13 @@ export const getIndexData = async (
});

if (!options.didCancel) {
setRowCount(typeof resp.hits.total === 'number' ? resp.hits.total : resp.hits.total!.value);
setRowCountRelation(
typeof resp.hits.total === 'number'
? ('eq' as estypes.SearchTotalHitsRelation)
: resp.hits.total!.relation
);
setRowCountInfo({
rowCount: typeof resp.hits.total === 'number' ? resp.hits.total : resp.hits.total!.value,
rowCountRelation:
typeof resp.hits.total === 'number'
? ('eq' as estypes.SearchTotalHitsRelation)
: resp.hits.total!.relation,
});
setTableItems(
resp.hits.hits.map((d) =>
getProcessedFields(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ export const useIndexData = (
pagination,
resetPagination,
setErrorMessage,
setRowCount,
setRowCountRelation,
setRowCountInfo,
setStatus,
setTableItems,
sortingColumns,
Expand Down Expand Up @@ -199,12 +198,13 @@ export const useIndexData = (
const resp: IndexSearchResponse = await ml.esSearch(esSearchRequest);
const docs = resp.hits.hits.map((d) => getProcessedFields(d.fields ?? {}));

setRowCount(typeof resp.hits.total === 'number' ? resp.hits.total : resp.hits.total!.value);
setRowCountRelation(
typeof resp.hits.total === 'number'
? ('eq' as estypes.SearchTotalHitsRelation)
: resp.hits.total!.relation
);
setRowCountInfo({
rowCount: typeof resp.hits.total === 'number' ? resp.hits.total : resp.hits.total!.value,
rowCountRelation:
typeof resp.hits.total === 'number'
? ('eq' as estypes.SearchTotalHitsRelation)
: resp.hits.total!.relation,
});
setTableItems(docs);
setStatus(INDEX_STATUS.LOADED);
} catch (e) {
Expand Down
16 changes: 8 additions & 8 deletions x-pack/plugins/transform/public/app/hooks/use_index_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ export const useIndexData = (
setColumnCharts,
setCcsWarning,
setErrorMessage,
setRowCount,
setRowCountRelation,
setRowCountInfo,
setStatus,
setTableItems,
sortingColumns,
Expand Down Expand Up @@ -206,12 +205,13 @@ export const useIndexData = (
const docs = resp.hits.hits.map((d) => getProcessedFields(d.fields ?? {}));

setCcsWarning(isCrossClusterSearch && isMissingFields);
setRowCount(typeof resp.hits.total === 'number' ? resp.hits.total : resp.hits.total!.value);
setRowCountRelation(
typeof resp.hits.total === 'number'
? ('eq' as estypes.SearchTotalHitsRelation)
: resp.hits.total!.relation
);
setRowCountInfo({
rowCount: typeof resp.hits.total === 'number' ? resp.hits.total : resp.hits.total!.value,
rowCountRelation:
typeof resp.hits.total === 'number'
? ('eq' as estypes.SearchTotalHitsRelation)
: resp.hits.total!.relation,
});
setTableItems(docs);
setStatus(INDEX_STATUS.LOADED);
};
Expand Down
21 changes: 13 additions & 8 deletions x-pack/plugins/transform/public/app/hooks/use_pivot_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ export const usePivotData = (
resetPagination,
setErrorMessage,
setNoDataMessage,
setRowCount,
setRowCountRelation,
setRowCountInfo,
setStatus,
setTableItems,
sortingColumns,
Expand All @@ -154,8 +153,10 @@ export const usePivotData = (
const getPreviewData = async () => {
if (!validationStatus.isValid) {
setTableItems([]);
setRowCount(0);
setRowCountRelation(ES_CLIENT_TOTAL_HITS_RELATION.EQ);
setRowCountInfo({
rowCount: 0,
rowCountRelation: ES_CLIENT_TOTAL_HITS_RELATION.EQ,
});
setNoDataMessage(validationStatus.errorMessage!);
return;
}
Expand All @@ -175,8 +176,10 @@ export const usePivotData = (
if (!isPostTransformsPreviewResponseSchema(resp)) {
setErrorMessage(getErrorMessage(resp));
setTableItems([]);
setRowCount(0);
setRowCountRelation(ES_CLIENT_TOTAL_HITS_RELATION.EQ);
setRowCountInfo({
rowCount: 0,
rowCountRelation: ES_CLIENT_TOTAL_HITS_RELATION.EQ,
});
setPreviewMappingsProperties({});
setStatus(INDEX_STATUS.ERROR);
return;
Expand Down Expand Up @@ -208,8 +211,10 @@ export const usePivotData = (
populatedProperties = getCombinedProperties(populatedProperties, docs);

setTableItems(docs);
setRowCount(docs.length);
setRowCountRelation(ES_CLIENT_TOTAL_HITS_RELATION.EQ);
setRowCountInfo({
rowCount: docs.length,
rowCountRelation: ES_CLIENT_TOTAL_HITS_RELATION.EQ,
});
setPreviewMappingsProperties(populatedProperties);
setStatus(INDEX_STATUS.LOADED);

Expand Down