Skip to content

Commit

Permalink
[Discover] account for hidden time column in default sort (#129659)
Browse files Browse the repository at this point in the history
(cherry picked from commit ae68c1c)
  • Loading branch information
drewdaemon committed Apr 7, 2022
1 parent 9e7b53e commit e94612d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { cloneDeep, isEqual } from 'lodash';
import { IUiSettingsClient } from 'kibana/public';
import {
DEFAULT_COLUMNS_SETTING,
DOC_HIDE_TIME_COLUMN_SETTING,
SEARCH_FIELDS_FROM_SOURCE,
SORT_DEFAULT_ORDER_SETTING,
} from '../../../../common';
Expand Down Expand Up @@ -53,7 +54,11 @@ export function getStateDefaults({
const defaultState = {
query,
sort: !sort.length
? getDefaultSort(indexPattern, config.get(SORT_DEFAULT_ORDER_SETTING, 'desc'))
? getDefaultSort(
indexPattern,
config.get(SORT_DEFAULT_ORDER_SETTING, 'desc'),
config.get(DOC_HIDE_TIME_COLUMN_SETTING, false)
)
: sort,
columns,
index: indexPattern?.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ export function TableHeader({
customLabel={indexPattern.getFieldByName(col.name)?.customLabel}
isTimeColumn={indexPattern.timeFieldName === col.name}
sortOrder={
sortOrder.length ? sortOrder : getDefaultSort(indexPattern, defaultSortOrder)
sortOrder.length
? sortOrder
: getDefaultSort(indexPattern, defaultSortOrder, hideTimeColumn)
}
onMoveColumn={onMoveColumn}
onRemoveColumn={onRemoveColumn}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ describe('getDefaultSort function', function () {
});

test('should return default sort for an index pattern with timeFieldName', function () {
expect(getDefaultSort(stubDataView, 'desc')).toEqual([['@timestamp', 'desc']]);
expect(getDefaultSort(stubDataView, 'asc')).toEqual([['@timestamp', 'asc']]);
expect(getDefaultSort(stubDataView, 'desc', false)).toEqual([['@timestamp', 'desc']]);
expect(getDefaultSort(stubDataView, 'asc', false)).toEqual([['@timestamp', 'asc']]);
});

test('should return default sort for an index pattern without timeFieldName', function () {
expect(getDefaultSort(stubDataViewWithoutTimeField, 'desc')).toEqual([]);
expect(getDefaultSort(stubDataViewWithoutTimeField, 'asc')).toEqual([]);
expect(getDefaultSort(stubDataViewWithoutTimeField, 'desc', false)).toEqual([]);
expect(getDefaultSort(stubDataViewWithoutTimeField, 'asc', false)).toEqual([]);
});

test('should return empty sort for data view when time column is hidden', function () {
expect(getDefaultSort(stubDataView, 'desc', true)).toEqual([]);
expect(getDefaultSort(stubDataView, 'asc', true)).toEqual([]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ import { SortOrder } from '../components/table_header/helpers';
*/
export function getDefaultSort(
indexPattern: DataView | undefined,
defaultSortOrder: string = 'desc'
defaultSortOrder: string = 'desc',
hidingTimeColumn: boolean = false
): SortOrder[] {
if (indexPattern?.timeFieldName && isSortable(indexPattern.timeFieldName, indexPattern)) {
if (
indexPattern?.timeFieldName &&
isSortable(indexPattern.timeFieldName, indexPattern) &&
!hidingTimeColumn
) {
return [[indexPattern.timeFieldName, defaultSortOrder]];
} else {
return [];
Expand Down
21 changes: 9 additions & 12 deletions src/plugins/discover/public/embeddable/saved_search_embeddable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ 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 initializeSearchEmbeddableProps() {
const { searchSource } = this.savedSearch;

Expand All @@ -223,20 +229,14 @@ export class SavedSearchEmbeddable
}

if (!this.savedSearch.sort || !this.savedSearch.sort.length) {
this.savedSearch.sort = getDefaultSort(
indexPattern,
this.services.uiSettings.get(SORT_DEFAULT_ORDER_SETTING, 'desc')
);
this.savedSearch.sort = this.getDefaultSort(indexPattern);
}

const props: SearchProps = {
columns: this.savedSearch.columns,
indexPattern,
isLoading: false,
sort: getDefaultSort(
indexPattern,
this.services.uiSettings.get(SORT_DEFAULT_ORDER_SETTING, 'desc')
),
sort: this.getDefaultSort(indexPattern),
rows: [],
searchDescription: this.savedSearch.description,
description: this.savedSearch.description,
Expand Down Expand Up @@ -344,10 +344,7 @@ export class SavedSearchEmbeddable
const savedSearchSort =
this.savedSearch.sort && this.savedSearch.sort.length
? this.savedSearch.sort
: getDefaultSort(
this.searchProps?.indexPattern,
this.services.uiSettings.get(SORT_DEFAULT_ORDER_SETTING, 'desc')
);
: this.getDefaultSort(this.searchProps?.indexPattern);
searchProps.sort = this.input.sort || savedSearchSort;
searchProps.sharedItemTitle = this.panelTitle;
searchProps.rowHeightState = this.input.rowHeight || this.savedSearch.rowHeight;
Expand Down

0 comments on commit e94612d

Please sign in to comment.