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

[PoC] One Discover context awareness #8

Closed
Closed
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
10 changes: 7 additions & 3 deletions packages/kbn-discover-utils/src/utils/build_data_record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ export function buildDataTableRecord(
* @param docs Array of documents returned from Elasticsearch
* @param dataView this current data view
*/
export function buildDataTableRecordList(
export function buildDataTableRecordList<T extends DataTableRecord = DataTableRecord>(
docs: EsHitRecord[],
dataView?: DataView
dataView?: DataView,
{ processRecord }: { processRecord?: (record: DataTableRecord) => T } = {}
): DataTableRecord[] {
return docs.map((doc) => buildDataTableRecord(doc, dataView));
return docs.map((doc) => {
const record = buildDataTableRecord(doc, dataView);
return processRecord ? processRecord(record) : record;
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import { DOC_TABLE_LEGACY } from '../constants';

export function isLegacyTableEnabled({
uiSettings,
isTextBasedQueryMode,
isEsqlMode,
}: {
uiSettings: IUiSettingsClient;
isTextBasedQueryMode: boolean;
isEsqlMode: boolean;
}): boolean {
if (isTextBasedQueryMode) {
if (isEsqlMode) {
return false; // only show the new data grid
}

Expand Down
8 changes: 4 additions & 4 deletions src/plugins/discover/common/utils/sorting/get_default_sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ import { isSortable } from './get_sort';

/**
* use in case the user didn't manually sort.
* the default sort is returned depending on the data view or non for text based queries
* the default sort is returned depending on the data view or non for ES|QL queries
*/
export function getDefaultSort(
dataView: DataView | undefined,
defaultSortOrder: string = 'desc',
hidingTimeColumn: boolean = false,
isTextBasedQueryMode: boolean
isEsqlMode: boolean
): SortOrder[] {
if (isTextBasedQueryMode) {
if (isEsqlMode) {
return [];
}

if (
dataView?.timeFieldName &&
isSortable(dataView.timeFieldName, dataView, isTextBasedQueryMode) &&
isSortable(dataView.timeFieldName, dataView, isEsqlMode) &&
!hidingTimeColumn
) {
return [[dataView.timeFieldName, defaultSortOrder]];
Expand Down
28 changes: 12 additions & 16 deletions src/plugins/discover/common/utils/sorting/get_sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,10 @@ export type SortPairObj = Record<string, string>;
export type SortPair = SortOrder | SortPairObj;
export type SortInput = SortPair | SortPair[];

export function isSortable(
fieldName: string,
dataView: DataView,
isTextBasedQueryMode: boolean
): boolean {
if (isTextBasedQueryMode) {
// in-memory sorting is used for text-based queries
// would be great to have a way to determine if a text-based column is sortable
export function isSortable(fieldName: string, dataView: DataView, isEsqlMode: boolean): boolean {
if (isEsqlMode) {
// in-memory sorting is used for ES|QL queries
// would be great to have a way to determine if a ES|QL column is sortable
return fieldName !== '_source';
}
const field = dataView.getFieldByName(fieldName);
Expand All @@ -31,18 +27,18 @@ export function isSortable(
function createSortObject(
sortPair: SortInput,
dataView: DataView,
isTextBasedQueryMode: boolean
isEsqlMode: boolean
): SortPairObj | undefined {
if (
Array.isArray(sortPair) &&
sortPair.length === 2 &&
isSortable(String(sortPair[0]), dataView, isTextBasedQueryMode)
isSortable(String(sortPair[0]), dataView, isEsqlMode)
) {
const [field, direction] = sortPair as SortOrder;
return { [field]: direction };
} else if (
isPlainObject(sortPair) &&
isSortable(Object.keys(sortPair)[0], dataView, isTextBasedQueryMode)
isSortable(Object.keys(sortPair)[0], dataView, isEsqlMode)
) {
return sortPair as SortPairObj;
}
Expand All @@ -59,21 +55,21 @@ export function isLegacySort(sort: SortPair[] | SortPair): sort is SortPair {
* @param {array} sort two dimensional array [[fieldToSort, directionToSort]]
* or an array of objects [{fieldToSort: directionToSort}]
* @param {object} dataView used for determining default sort
* @param {boolean} isTextBasedQueryMode
* @param {boolean} isEsqlMode
* @returns Array<{object}> an array of sort objects
*/
export function getSort(
sort: SortPair[] | SortPair,
dataView: DataView,
isTextBasedQueryMode: boolean
isEsqlMode: boolean
): SortPairObj[] {
if (Array.isArray(sort)) {
if (isLegacySort(sort)) {
// To stay compatible with legacy sort, which just supported a single sort field
return [{ [sort[0]]: sort[1] }];
}
return sort
.map((sortPair: SortPair) => createSortObject(sortPair, dataView, isTextBasedQueryMode))
.map((sortPair: SortPair) => createSortObject(sortPair, dataView, isEsqlMode))
.filter((sortPairObj) => typeof sortPairObj === 'object') as SortPairObj[];
}
return [];
Expand All @@ -86,9 +82,9 @@ export function getSort(
export function getSortArray(
sort: SortInput,
dataView: DataView,
isTextBasedQueryMode: boolean
isEsqlMode: boolean
): SortOrder[] {
return getSort(sort, dataView, isTextBasedQueryMode).reduce((acc: SortOrder[], sortPair) => {
return getSort(sort, dataView, isEsqlMode).reduce((acc: SortOrder[], sortPair) => {
const entries = Object.entries(sortPair);
if (entries && entries[0]) {
acc.push(entries[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function getSortForSearchSource({
}

const { timeFieldName } = dataView;
const sortPairs = getSort(sort, dataView, false); // text based request is not using search source
const sortPairs = getSort(sort, dataView, false); // ES|QL request is not using search source

const sortForSearchSource = sortPairs.map((sortPair: Record<string, string>) => {
if (timeFieldName && sortPair[timeFieldName]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { LocalStorageMock } from '../../../../__mocks__/local_storage_mock';
import { DiscoverServices } from '../../../../build_services';
import { discoverServiceMock } from '../../../../__mocks__/services';
import { DiscoverTourProvider } from '../../../../components/discover_tour';
import { getDiscoverStateMock } from '../../../../__mocks__/discover_state.mock';
import { DiscoverMainProvider } from '../../state_management/discover_state_provider';

const defaultServices = {
...discoverServiceMock,
Expand Down Expand Up @@ -62,9 +64,11 @@ describe('Document Explorer Update callout', () => {
it('should start a tour when the button is clicked', () => {
const result = mountWithIntl(
<KibanaContextProvider services={defaultServices}>
<DiscoverTourProvider isPlainRecord={false}>
<DocumentExplorerUpdateCallout />
</DiscoverTourProvider>
<DiscoverMainProvider value={getDiscoverStateMock({})}>
<DiscoverTourProvider>
<DocumentExplorerUpdateCallout />
</DiscoverTourProvider>
</DiscoverMainProvider>
</KibanaContextProvider>
);

Expand Down
Loading