Skip to content

Commit

Permalink
[Discover] Fix legacy sort saved search stored in dashboard saved obj…
Browse files Browse the repository at this point in the history
…ects (#137488)

* Refactor and centralize sort functionality
  • Loading branch information
kertal authored Aug 23, 2022
1 parent bdbd227 commit 1a213bd
Show file tree
Hide file tree
Showing 25 changed files with 111 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { FormattedMessage } from '@kbn/i18n-react';
import { EuiHorizontalRule, EuiText } from '@elastic/eui';
import type { DataView } from '@kbn/data-views-plugin/public';
import { SortDirection } from '@kbn/data-plugin/public';
import type { SortOrder } from '@kbn/saved-search-plugin/public';
import { CONTEXT_STEP_SETTING, DOC_HIDE_TIME_COLUMN_SETTING } from '../../../common';
import { LoadingStatus } from './services/context_query_state';
import { ActionBar } from './components/action_bar/action_bar';
Expand All @@ -20,7 +21,6 @@ import { AppState } from './services/context_state';
import { SurrDocType } from './services/context';
import { MAX_CONTEXT_SIZE, MIN_CONTEXT_SIZE } from './services/constants';
import { DocTableContext } from '../../components/doc_table/doc_table_context';
import type { SortPairArr } from '../../components/doc_table/utils/get_sort';
import { useDiscoverServices } from '../../hooks/use_discover_services';
import type { DataTableRecord } from '../../types';

Expand Down Expand Up @@ -151,7 +151,7 @@ export function ContextAppContent({
expandedDoc={expandedDoc}
isLoading={isAnchorLoading}
sampleSize={0}
sort={sort as SortPairArr[]}
sort={sort as SortOrder[]}
isSortEnabled={false}
showTimeCol={showTimeCol}
useNewFieldsApi={useNewFieldsApi}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n-react';
import { DataView } from '@kbn/data-views-plugin/public';
import { SavedSearch } from '@kbn/saved-search-plugin/public';
import { SavedSearch, SortOrder } from '@kbn/saved-search-plugin/public';
import { useDiscoverServices } from '../../../../hooks/use_discover_services';
import { DocViewFilterFn } from '../../../../services/doc_views/doc_views_types';
import { DiscoverGrid } from '../../../../components/discover_grid/discover_grid';
Expand All @@ -32,7 +32,6 @@ import { DataDocuments$, DataDocumentsMsg, RecordRawType } from '../../hooks/use
import { AppState, GetStateReturn } from '../../services/discover_state';
import { useDataState } from '../../hooks/use_data_state';
import { DocTableInfinite } from '../../../../components/doc_table/doc_table_infinite';
import { SortPairArr } from '../../../../components/doc_table/utils/get_sort';
import { DocumentExplorerCallout } from '../document_explorer_callout';
import { DocumentExplorerUpdateCallout } from '../document_explorer_callout/document_explorer_update_callout';
import { DiscoverTourProvider } from '../../../../components/discover_tour';
Expand Down Expand Up @@ -195,7 +194,7 @@ function DiscoverDocumentsComponent({
dataView={dataView}
isLoading={isLoading}
rows={rows}
sort={(state.sort as SortPairArr[]) || []}
sort={(state.sort as SortOrder[]) || []}
sampleSize={sampleSize}
searchDescription={savedSearch.description}
searchTitle={savedSearch.title}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
Query,
} from '@kbn/es-query';
import { SavedSearch, getSavedSearch } from '@kbn/saved-search-plugin/public';
import type { SortOrder } from '@kbn/saved-search-plugin/public';
import { getState } from '../services/discover_state';
import { getStateDefaults } from '../utils/get_state_defaults';
import { DiscoverServices } from '../../../build_services';
Expand All @@ -32,7 +33,6 @@ import { useSearchSession } from './use_search_session';
import { useDataState } from './use_data_state';
import { FetchStatus } from '../../types';
import { getDataViewAppState } from '../utils/get_switch_data_view_app_state';
import { SortPairArr } from '../../../components/doc_table/utils/get_sort';
import { DataTableRecord } from '../../../types';
import { restoreStateFromSavedSearch } from '../../../services/saved_searches/restore_from_saved_search';

Expand Down Expand Up @@ -217,7 +217,7 @@ export function useDiscoverState({
dataView,
nextDataView,
state.columns || [],
(state.sort || []) as SortPairArr[],
(state.sort || []) as SortOrder[],
config.get(MODIFY_COLUMNS_ON_SWITCH),
config.get(SORT_DEFAULT_ORDER_SETTING),
state.query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { IUiSettingsClient } from '@kbn/core/public';
import { DataPublicPluginStart } from '@kbn/data-plugin/public';
import { Storage } from '@kbn/kibana-utils-plugin/public';
import { SavedSearch } from '@kbn/saved-search-plugin/public';
import { getDefaultSort, getSortArray } from '../../../utils/sorting';
import {
DEFAULT_COLUMNS_SETTING,
DOC_HIDE_TIME_COLUMN_SETTING,
Expand All @@ -19,7 +20,6 @@ import {
} from '../../../../common';

import { AppState } from '../services/discover_state';
import { getDefaultSort, getSortArray } from '../../../components/doc_table';
import { CHART_HIDDEN_KEY } from '../components/chart/discover_chart';

function getDefaultColumns(savedSearch: SavedSearch, config: IUiSettingsClient) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
*/
import { isOfAggregateQueryType, Query, AggregateQuery } from '@kbn/es-query';
import type { DataView } from '@kbn/data-views-plugin/public';
import { getSortArray, SortPairArr } from '../../../components/doc_table/utils/get_sort';
import type { SortOrder } from '@kbn/saved-search-plugin/public';
import { getSortArray } from '../../../utils/sorting';

/**
* Helper function to remove or adapt the currently selected columns/sort to be valid with the next
Expand All @@ -17,7 +18,7 @@ export function getDataViewAppState(
currentDataView: DataView,
nextDataView: DataView,
currentColumns: string[],
currentSort: SortPairArr[],
currentSort: SortOrder[],
modifyColumns: boolean = true,
sortDirection: string = 'desc',
query?: Query | AggregateQuery
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import { isOfAggregateQueryType } from '@kbn/es-query';
import { DataView } from '@kbn/data-views-plugin/public';
import { SavedObjectSaveOpts } from '@kbn/saved-objects-plugin/public';
import { SavedSearch } from '@kbn/saved-search-plugin/public';
import type { SortOrder } from '@kbn/saved-search-plugin/public';
import { saveSavedSearch } from '@kbn/saved-search-plugin/public';
import { SavedSearch, SortOrder, saveSavedSearch } from '@kbn/saved-search-plugin/public';
import { updateSearchSource } from './update_search_source';
import { AppState } from '../services/discover_state';
import { DiscoverServices } from '../../../build_services';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { DataViewType, DataView } from '@kbn/data-views-plugin/public';
import type { SortOrder } from '@kbn/saved-search-plugin/public';
import { SORT_DEFAULT_ORDER_SETTING } from '../../../../common';
import { DiscoverServices } from '../../../build_services';
import { getSortForSearchSource } from '../../../components/doc_table';
import { getSortForSearchSource } from '../../../utils/sorting';

/**
* Helper function to update the given searchSource before fetching/sharing/persisting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
EuiLink,
} from '@elastic/eui';
import type { DataView } from '@kbn/data-views-plugin/public';
import type { SortOrder } from '@kbn/saved-search-plugin/public';
import { DocViewFilterFn } from '../../services/doc_views/doc_views_types';
import { getSchemaDetectors } from './discover_grid_schema';
import { DiscoverGridFlyout } from './discover_grid_flyout';
Expand All @@ -43,7 +44,6 @@ import {
SHOW_MULTIFIELDS,
} from '../../../common';
import { DiscoverGridDocumentToolbarBtn } from './discover_grid_document_selection';
import { SortPairArr } from '../doc_table/utils/get_sort';
import { getFieldsToShow } from '../../utils/get_fields_to_show';
import type { DataTableRecord, ValueToStringConverter } from '../../types';
import { useRowHeightsOptions } from '../../hooks/use_row_heights_options';
Expand Down Expand Up @@ -141,7 +141,7 @@ export interface DiscoverGridProps {
/**
* Current sort setting
*/
sort: SortPairArr[];
sort: SortOrder[];
/**
* How the data is fetched
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import { i18n } from '@kbn/i18n';
import type { DataView } from '@kbn/data-views-plugin/public';

export type SortOrder = [string, string];
export interface ColumnProps {
name: string;
displayName: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import React from 'react';
import { mountWithIntl } from '@kbn/test-jest-helpers';
import type { DataView, DataViewField } from '@kbn/data-views-plugin/public';
import type { SortOrder } from '@kbn/saved-search-plugin/public';
import { TableHeader } from './table_header';
import { findTestSubject } from '@elastic/eui/lib/test';
import { SortOrder } from './helpers';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import { DOC_HIDE_TIME_COLUMN_SETTING } from '../../../../../common';
import { FORMATS_UI_SETTINGS } from '@kbn/field-formats-plugin/common';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import React, { useMemo } from 'react';
import type { DataView } from '@kbn/data-views-plugin/public';
import { FORMATS_UI_SETTINGS } from '@kbn/field-formats-plugin/common';
import type { SortOrder } from '@kbn/saved-search-plugin/public';
import { TableHeaderColumn } from './table_header_column';
import { SortOrder, getDisplayedColumns } from './helpers';
import { getDefaultSort } from '../../utils/get_default_sort';
import { getDisplayedColumns } from './helpers';
import { getDefaultSort } from '../../../../utils/sorting';
import { useDiscoverServices } from '../../../../hooks/use_discover_services';
import { DOC_HIDE_TIME_COLUMN_SETTING, SORT_DEFAULT_ORDER_SETTING } from '../../../../../common';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import React from 'react';
import { i18n } from '@kbn/i18n';
import { EuiButtonIcon, EuiToolTip, EuiIconTip } from '@elastic/eui';
import { SortOrder } from './helpers';
import type { SortOrder } from '@kbn/saved-search-plugin/public';
import { DocViewTableScoreSortWarning } from './score_sort_warning';

interface Props {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import React, { forwardRef, useCallback, useMemo } from 'react';
import { EuiIcon, EuiSpacer, EuiText } from '@elastic/eui';
import type { DataView, DataViewField } from '@kbn/data-views-plugin/public';
import type { SortOrder } from '@kbn/saved-search-plugin/public';
import { FormattedMessage } from '@kbn/i18n-react';
import { TableHeader } from './components/table_header/table_header';
import { SHOW_MULTIFIELDS } from '../../../common';
import { SortOrder } from './components/table_header/helpers';
import { TableRow } from './components/table_row';
import { DocViewFilterFn } from '../../services/doc_views/doc_views_types';
import { getFieldsToShow } from '../../utils/get_fields_to_show';
Expand Down
37 changes: 17 additions & 20 deletions src/plugins/discover/public/embeddable/saved_search_embeddable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { I18nProvider } from '@kbn/i18n-react';
import type { KibanaExecutionContext } from '@kbn/core/public';
import { Container, Embeddable, FilterableEmbeddable } from '@kbn/embeddable-plugin/public';
import { Adapters, RequestAdapter } from '@kbn/inspector-plugin/common';
import type { SortOrder } from '@kbn/saved-search-plugin/public';
import {
APPLY_FILTER_TRIGGER,
FilterManager,
Expand All @@ -33,6 +34,7 @@ import { DataView, DataViewField } from '@kbn/data-views-plugin/public';
import { UiActionsStart } from '@kbn/ui-actions-plugin/public';
import { KibanaContextProvider, KibanaThemeProvider } from '@kbn/kibana-react-plugin/public';
import { SavedSearch } from '@kbn/saved-search-plugin/public';
import { getSortForEmbeddable, SortPair } from '../utils/sorting';
import { RecordRawType } from '../application/main/hooks/use_saved_search';
import { buildDataTableRecord } from '../utils/build_data_record';
import { DataTableRecord, EsHitRecord } from '../types';
Expand All @@ -53,8 +55,6 @@ import { handleSourceColumnState } from '../utils/state_helpers';
import { DiscoverGridProps } from '../components/discover_grid/discover_grid';
import { DiscoverGridSettings } from '../components/discover_grid/types';
import { DocTableProps } from '../components/doc_table/doc_table_wrapper';
import { getDefaultSort } from '../components/doc_table';
import { SortOrder } from '../components/doc_table/components/table_header/helpers';
import { VIEW_MODE } from '../components/view_mode_toggle';
import { updateSearchSource } from './utils/update_search_source';
import { FieldStatisticsTable } from '../application/main/components/field_stats_table';
Expand Down Expand Up @@ -103,6 +103,7 @@ export class SavedSearchEmbeddable
private prevTimeRange?: TimeRange;
private prevFilters?: Filter[];
private prevQuery?: Query;
private prevSort?: SortOrder[];
private prevSearchSessionId?: string;
private searchProps?: SearchProps;

Expand Down Expand Up @@ -285,10 +286,8 @@ export class SavedSearchEmbeddable
}
};

private getDefaultSort(dataView?: DataView) {
const defaultSortOrder = this.services.uiSettings.get(SORT_DEFAULT_ORDER_SETTING, 'desc');
const hidingTimeColumn = this.services.uiSettings.get(DOC_HIDE_TIME_COLUMN_SETTING, false);
return getDefaultSort(dataView, defaultSortOrder, hidingTimeColumn);
private getSort(sort: SortPair[] | undefined, dataView?: DataView) {
return getSortForEmbeddable(sort, dataView, this.services.uiSettings);
}

private initializeSearchEmbeddableProps() {
Expand All @@ -299,16 +298,13 @@ export class SavedSearchEmbeddable
if (!dataView) {
return;
}

if (!this.savedSearch.sort || !this.savedSearch.sort.length) {
this.savedSearch.sort = this.getDefaultSort(dataView);
}
const sort = this.getSort(this.savedSearch.sort, dataView);

const props: SearchProps = {
columns: this.savedSearch.columns,
dataView,
isLoading: false,
sort: this.getDefaultSort(dataView),
sort,
rows: [],
searchDescription: this.savedSearch.description,
description: this.savedSearch.description,
Expand Down Expand Up @@ -339,9 +335,9 @@ export class SavedSearchEmbeddable
onSetColumns: (columns: string[]) => {
this.updateInput({ columns });
},
onSort: (sort: string[][]) => {
onSort: (nextSort: string[][]) => {
const sortOrderArr: SortOrder[] = [];
sort.forEach((arr) => {
nextSort.forEach((arr) => {
sortOrderArr.push(arr as SortOrder);
});
this.updateInput({ sort: sortOrderArr });
Expand Down Expand Up @@ -400,14 +396,15 @@ export class SavedSearchEmbeddable
}

private isFetchRequired(searchProps?: SearchProps) {
if (!searchProps) {
if (!searchProps || !searchProps.dataView) {
return false;
}

return (
!onlyDisabledFiltersChanged(this.input.filters, this.prevFilters) ||
!isEqual(this.prevQuery, this.input.query) ||
!isEqual(this.prevTimeRange, this.input.timeRange) ||
!isEqual(searchProps.sort, this.input.sort || this.savedSearch.sort) ||
!isEqual(this.prevSort, this.input.sort) ||
this.prevSearchSessionId !== this.input.searchSessionId
);
}
Expand All @@ -431,12 +428,11 @@ export class SavedSearchEmbeddable
{ columns: this.input.columns || this.savedSearch.columns },
this.services.core.uiSettings
).columns;
searchProps.sort = this.getSort(
this.input.sort || this.savedSearch.sort,
searchProps?.dataView
);

const savedSearchSort =
this.savedSearch.sort && this.savedSearch.sort.length
? this.savedSearch.sort
: this.getDefaultSort(this.searchProps?.dataView);
searchProps.sort = this.input.sort || savedSearchSort;
searchProps.sharedItemTitle = this.panelTitle;
searchProps.rowHeightState = this.input.rowHeight || this.savedSearch.rowHeight;
searchProps.rowsPerPageState = this.input.rowsPerPage || this.savedSearch.rowsPerPage;
Expand All @@ -453,6 +449,7 @@ export class SavedSearchEmbeddable
this.prevQuery = this.input.query;
this.prevTimeRange = this.input.timeRange;
this.prevSearchSessionId = this.input.searchSessionId;
this.prevSort = this.input.sort;
this.searchProps = searchProps;
await this.fetch();
} else if (this.searchProps && this.node) {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/discover/public/embeddable/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import type { Filter, TimeRange, Query } from '@kbn/es-query';
import { DataView } from '@kbn/data-views-plugin/public';
import { SavedSearch } from '@kbn/saved-search-plugin/public';
import { SortOrder } from '../components/doc_table/components/table_header/helpers';
import type { SortOrder } from '@kbn/saved-search-plugin/public';

export interface SearchInput extends EmbeddableInput {
timeRange: TimeRange;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
* Side Public License, v 1.
*/
import type { DataView } from '@kbn/data-views-plugin/public';
import { ISearchSource } from '@kbn/data-plugin/public';
import { getSortForSearchSource } from '../../components/doc_table';
import { SortPairArr } from '../../components/doc_table/utils/get_sort';
import type { ISearchSource } from '@kbn/data-plugin/public';
import type { SortOrder } from '@kbn/saved-search-plugin/public';
import { getSortForSearchSource } from '../../utils/sorting';

export const updateSearchSource = (
searchSource: ISearchSource,
dataView: DataView | undefined,
sort: (SortPairArr[] & string[][]) | undefined,
sort: (SortOrder[] & string[][]) | undefined,
useNewFieldsApi: boolean,
defaults: {
sampleSize: number;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/discover/public/utils/get_sharing_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import type {
} from '@kbn/data-plugin/public';
import type { Filter } from '@kbn/es-query';
import type { SavedSearch, SortOrder } from '@kbn/saved-search-plugin/public';
import { getSortForSearchSource } from './sorting';
import {
DOC_HIDE_TIME_COLUMN_SETTING,
SEARCH_FIELDS_FROM_SOURCE,
SORT_DEFAULT_ORDER_SETTING,
} from '../../common';
import { getSortForSearchSource } from '../components/doc_table';
import { AppState, isEqualFilters } from '../application/main/services/discover_state';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
*/

import type { DataView } from '@kbn/data-views-plugin/public';
import type { SortOrder } from '@kbn/saved-search-plugin/public';
import { isSortable } from './get_sort';
import { SortOrder } from '../components/table_header/helpers';

/**
* use in case the user didn't manually sort.
Expand Down
Loading

0 comments on commit 1a213bd

Please sign in to comment.