forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Lens] Datatable improvements (elastic#174994)
## Summary Fixes elastic#160719 and elastic#164413 This PR contains some work about Lens datatable, here's a short list: * moved out the sorting logic of the datatable into an independent package: `@kbn/sort-predicates` * leverage the EUI Datagrid `schemaDetectors` for the table rows sorting * apply datatable columns sorting also to the CSV exporter ### Checklist - [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 --------- Co-authored-by: Stratoula Kalafateli <[email protected]> Co-authored-by: kibanamachine <[email protected]>
- Loading branch information
1 parent
450c384
commit 73e5a96
Showing
27 changed files
with
509 additions
and
98 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# @kbn/sort-predicates | ||
|
||
This package contains a flexible sorting function who supports the following types: | ||
|
||
* string | ||
* number | ||
* version | ||
* ip addresses (both IPv4 and IPv6) - handles `Others`/strings correcly in this case | ||
* dates | ||
* ranges open and closed (number type only for now) | ||
* null and undefined (always sorted as last entries, no matter the direction) | ||
* any multi-value version of the types above (version excluded) | ||
|
||
The function is intended to use with objects and to simplify the usage with sorting by a specific column/field. | ||
The functions has been extracted from Lens datatable where it was originally used. | ||
|
||
### How to use it | ||
|
||
Basic usage with an array of objects: | ||
|
||
```js | ||
import { getSortingCriteria } from '@kbn/sorting-predicates'; | ||
|
||
... | ||
const predicate = getSortingCriteria( typeHint, columnId, formatterFn ); | ||
|
||
const orderedRows = [{a: 1, b: 2}, {a: 3, b: 4}] | ||
.sort( (rowA, rowB) => predicate(rowA, rowB, 'asc' /* or 'desc' */)); | ||
``` | ||
|
||
Basic usage with EUI DataGrid schemaDetector: | ||
|
||
```tsx | ||
const [data, setData] = useState(table); | ||
const dataGridColumns: EuiDataGridColumn[] = data.columns.map( (column) => ({ | ||
... | ||
schema: getColumnType(column) | ||
})); | ||
const [sortingColumns, setSortingColumns] = useState([ | ||
{ id: 'custom', direction: 'asc' }, | ||
]); | ||
|
||
const schemaDetectors = dataGridColumns.map((column) => { | ||
const sortingHint = getColumnType(column); | ||
const sortingCriteria = getSortingCriteria( | ||
sortingHint, | ||
column.id, | ||
(val: unknwon) => String(val) | ||
); | ||
return { | ||
sortTextAsc: 'asc' | ||
sortTextDesc: 'desc', | ||
icon: 'starFilled', | ||
type: sortingHint || '', | ||
detector: () => 1, | ||
// This is the actual logic that is used to sort the table | ||
comparator: (_a, _b, direction, { aIndex, bIndex }) => | ||
sortingCriteria(data.rows[aIndex], data.rows[bIndex], direction) as 0 | 1 | -1 | ||
}; | ||
}); | ||
|
||
return <EuiDataGrid | ||
... | ||
inMemory={{ level: 'sorting' }} | ||
columns={dataGridColumns} | ||
schemaDetectors={schemaDetectors} | ||
sorting={{ | ||
columns: sortingColumns, | ||
// this is called only for those columns not covered by the schema detector | ||
// and can use the sorting predica as well, manually applied to the data rows | ||
onSort: () => { ... } | ||
}} | ||
/>; | ||
``` |
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,9 @@ | ||
/* | ||
* 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 { getSortingCriteria } from './src/sorting'; |
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,13 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
module.exports = { | ||
preset: '@kbn/test', | ||
rootDir: '../..', | ||
roots: ['<rootDir>/packages/kbn-sort-predicates'], | ||
}; |
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,5 @@ | ||
{ | ||
"type": "shared-common", | ||
"id": "@kbn/sort-predicates", | ||
"owner": "@elastic/kibana-visualizations" | ||
} |
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,7 @@ | ||
{ | ||
"name": "@kbn/sort-predicates", | ||
"private": true, | ||
"version": "1.0.0", | ||
"license": "SSPL-1.0 OR Elastic License 2.0", | ||
"sideEffects": false | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"outDir": "target/types", | ||
"types": [ | ||
"jest", | ||
"node", | ||
] | ||
}, | ||
"include": [ | ||
"**/*.ts" | ||
], | ||
"exclude": [ | ||
"target/**/*" | ||
], | ||
"kbn_references": [ | ||
"@kbn/field-formats-plugin", | ||
"@kbn/expressions-plugin" | ||
] | ||
} |
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.