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

[Discover] account for hidden time column in default sort #129659

Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -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
): 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);
}

Comment on lines +216 to +221
Copy link
Member

Choose a reason for hiding this comment

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

nice cleanup ❤️

Comment on lines +216 to +221
Copy link
Member

Choose a reason for hiding this comment

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

nice cleanup ❤️

Copy link
Member

Choose a reason for hiding this comment

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

well, seems I clicked to often, or GitHub has issues?

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