-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Extensions of DiscoverAppLocator for DiscoverServerPlugin (#15…
…0631) ## Summary Adds `DiscoverServerPluginLocatorService`, a set of utilities that allows consumers to extract search info from `DiscoverAppLocatorParams`. Needed for #148775 ## Refactoring changes * Moved some code from `src/plugins/discover/public/utils/sorting` to `common`, which was needed in the server-side context. * Moved the definition of the `SavedSearch` interface from `src/plugins/saved_search/public` to `common` ### Checklist Delete any items that are not applicable to this PR. - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios
- Loading branch information
Showing
37 changed files
with
1,270 additions
and
241 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
74 changes: 74 additions & 0 deletions
74
src/plugins/discover/common/utils/sorting/get_sort.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { getSort, getSortArray } from './get_sort'; | ||
import { | ||
stubDataView, | ||
stubDataViewWithoutTimeField, | ||
} from '@kbn/data-views-plugin/common/data_view.stub'; | ||
|
||
describe('docTable', function () { | ||
describe('getSort function', function () { | ||
test('should be a function', function () { | ||
expect(typeof getSort === 'function').toBeTruthy(); | ||
}); | ||
|
||
test('should return an array of objects', function () { | ||
expect(getSort([['bytes', 'desc']], stubDataView)).toEqual([{ bytes: 'desc' }]); | ||
expect(getSort([['bytes', 'desc']], stubDataViewWithoutTimeField)).toEqual([ | ||
{ bytes: 'desc' }, | ||
]); | ||
}); | ||
|
||
test('should passthrough arrays of objects', () => { | ||
expect(getSort([{ bytes: 'desc' }], stubDataView)).toEqual([{ bytes: 'desc' }]); | ||
}); | ||
|
||
test('should return an empty array when passed an unsortable field', function () { | ||
expect(getSort([['non-sortable', 'asc']], stubDataView)).toEqual([]); | ||
expect(getSort([['lol_nope', 'asc']], stubDataView)).toEqual([]); | ||
|
||
expect(getSort([['non-sortable', 'asc']], stubDataViewWithoutTimeField)).toEqual([]); | ||
}); | ||
|
||
test('should return an empty array ', function () { | ||
expect(getSort([], stubDataView)).toEqual([]); | ||
expect(getSort([['foo', 'bar']], stubDataView)).toEqual([]); | ||
expect(getSort([{ foo: 'bar' }], stubDataView)).toEqual([]); | ||
}); | ||
|
||
test('should convert a legacy sort to an array of objects', function () { | ||
expect(getSort(['foo', 'desc'], stubDataView)).toEqual([{ foo: 'desc' }]); | ||
expect(getSort(['foo', 'asc'], stubDataView)).toEqual([{ foo: 'asc' }]); | ||
}); | ||
}); | ||
describe('getSortArray function', function () { | ||
test('should have an array method', function () { | ||
expect(getSortArray).toBeInstanceOf(Function); | ||
}); | ||
|
||
test('should return an array of arrays for sortable fields', function () { | ||
expect(getSortArray([['bytes', 'desc']], stubDataView)).toEqual([['bytes', 'desc']]); | ||
}); | ||
|
||
test('should return an array of arrays from an array of elasticsearch sort objects', function () { | ||
expect(getSortArray([{ bytes: 'desc' }], stubDataView)).toEqual([['bytes', 'desc']]); | ||
}); | ||
|
||
test('should sort by an empty array when an unsortable field is given', function () { | ||
expect(getSortArray([{ 'non-sortable': 'asc' }], stubDataView)).toEqual([]); | ||
expect(getSortArray([{ lol_nope: 'asc' }], stubDataView)).toEqual([]); | ||
expect(getSortArray([{ 'non-sortable': 'asc' }], stubDataViewWithoutTimeField)).toEqual([]); | ||
}); | ||
|
||
test('should return an empty array when passed an empty sort array', () => { | ||
expect(getSortArray([], stubDataView)).toEqual([]); | ||
expect(getSortArray([], stubDataViewWithoutTimeField)).toEqual([]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { DataView } from '@kbn/data-views-plugin/common'; | ||
import type { SortOrder } from '@kbn/saved-search-plugin/public'; | ||
import { isPlainObject } from 'lodash'; | ||
|
||
export type SortPairObj = Record<string, string>; | ||
export type SortPair = SortOrder | SortPairObj; | ||
export type SortInput = SortPair | SortPair[]; | ||
|
||
export function isSortable(fieldName: string, dataView: DataView): boolean { | ||
const field = dataView.getFieldByName(fieldName); | ||
return !!(field && field.sortable); | ||
} | ||
|
||
function createSortObject(sortPair: SortInput, dataView: DataView): SortPairObj | undefined { | ||
if ( | ||
Array.isArray(sortPair) && | ||
sortPair.length === 2 && | ||
isSortable(String(sortPair[0]), dataView) | ||
) { | ||
const [field, direction] = sortPair as SortOrder; | ||
return { [field]: direction }; | ||
} else if (isPlainObject(sortPair) && isSortable(Object.keys(sortPair)[0], dataView)) { | ||
return sortPair as SortPairObj; | ||
} | ||
} | ||
|
||
export function isLegacySort(sort: SortPair[] | SortPair): sort is SortPair { | ||
return ( | ||
sort.length === 2 && typeof sort[0] === 'string' && (sort[1] === 'desc' || sort[1] === 'asc') | ||
); | ||
} | ||
|
||
/** | ||
* Take a sorting array and make it into an object | ||
* @param {array} sort two dimensional array [[fieldToSort, directionToSort]] | ||
* or an array of objects [{fieldToSort: directionToSort}] | ||
* @param {object} dataView used for determining default sort | ||
* @returns Array<{object}> an array of sort objects | ||
*/ | ||
export function getSort(sort: SortPair[] | SortPair, dataView: DataView): 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)) | ||
.filter((sortPairObj) => typeof sortPairObj === 'object') as SortPairObj[]; | ||
} | ||
return []; | ||
} | ||
|
||
/** | ||
* compared to getSort it doesn't return an array of objects, it returns an array of arrays | ||
* [[fieldToSort: directionToSort]] | ||
*/ | ||
export function getSortArray(sort: SortInput, dataView: DataView): SortOrder[] { | ||
return getSort(sort, dataView).reduce((acc: SortOrder[], sortPair) => { | ||
const entries = Object.entries(sortPair); | ||
if (entries && entries[0]) { | ||
acc.push(entries[0]); | ||
} | ||
return acc; | ||
}, []); | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
export { getDefaultSort } from './get_default_sort'; | ||
export { getSort, getSortArray } from './get_sort'; | ||
export type { SortInput, SortPair } from './get_sort'; | ||
export { getSortForSearchSource } from './get_sort_for_search_source'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.