Skip to content

Commit

Permalink
[Lens] Fix datatable sorting for keyword values (#160163)
Browse files Browse the repository at this point in the history
## Summary

Fixes #160139 

~~This PR introduces the `inMemory` prop for the EUI DataGrid table
which makes the sorting by column also for keyword values.~~

The fix was actually specific for `Last value` as the inferred type was
always set to `number` leading to wrong results for any non-numeric data
type. This PR performs a better check and sorting now works better for
any non-numeric value when using `Last value`.
Also, the sorting logic takes into account also array values, often
returned by `Last value` operation.

Added new unit tests for array values and functional tests.


![table_sorting_fix_single](https://github.com/elastic/kibana/assets/924948/0edf89b1-e05e-4f8b-8439-74f7c2172223)

![table_sorting_fix](https://github.com/elastic/kibana/assets/924948/ea165484-ac55-45f0-a7df-c78cd12aa146)



### Checklist

Delete any items that are not applicable to this PR.

- [ ] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [ ] [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
- [ ] Any UI touched in this PR is usable by keyboard only (learn more
about [keyboard accessibility](https://webaim.org/techniques/keyboard/))
- [ ] Any UI touched in this PR does not create any new axe failures
(run axe in browser:
[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),
[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))
- [ ] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [ ] This renders correctly on smaller devices using a responsive
layout. (You can test this [in your
browser](https://www.browserstack.com/guide/responsive-testing-on-local-server))
- [ ] This was checked for [cross-browser
compatibility](https://www.elastic.co/support/matrix#matrix_browsers)


### Risk Matrix

Delete this section if it is not applicable to this PR.

Before closing this PR, invite QA, stakeholders, and other developers to
identify risks that should be tested prior to the change/feature
release.

When forming the risk matrix, consider some of the following examples
and how they may potentially impact the change:

| Risk | Probability | Severity | Mitigation/Notes |

|---------------------------|-------------|----------|-------------------------|
| Multiple Spaces—unexpected behavior in non-default Kibana Space.
| Low | High | Integration tests will verify that all features are still
supported in non-default Kibana Space and when user switches between
spaces. |
| Multiple nodes—Elasticsearch polling might have race conditions
when multiple Kibana nodes are polling for the same tasks. | High | Low
| Tasks are idempotent, so executing them multiple times will not result
in logical error, but will degrade performance. To test for this case we
add plenty of unit tests around this logic and document manual testing
procedure. |
| Code should gracefully handle cases when feature X or plugin Y are
disabled. | Medium | High | Unit tests will verify that any feature flag
or plugin combination still results in our service operational. |
| [See more potential risk
examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) |


### For maintainers

- [ ] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

---------

Co-authored-by: Stratoula Kalafateli <efstratia.kalafateli@elastic.co>
  • Loading branch information
dej611 and stratoula authored Jun 30, 2023
1 parent 5666b14 commit 37b909c
Show file tree
Hide file tree
Showing 5 changed files with 322 additions and 101 deletions.
106 changes: 104 additions & 2 deletions x-pack/plugins/lens/common/expressions/datatable/sorting.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function testSorting({
const datatable = input.map((v) => ({
a: v,
}));
const sorted = output.map((v) => ({ a: v }));
const sorted = [...output];
if (direction === 'desc' && reverseOutput) {
sorted.reverse();
if (keepLast) {
Expand All @@ -41,7 +41,7 @@ function testSorting({
}
}
const criteria = getSortingCriteria(type, 'a', getMockFormatter(), direction);
expect(datatable.sort(criteria)).toEqual(sorted);
expect(datatable.sort(criteria).map((row) => row.a)).toEqual(sorted);
}

describe('Data sorting criteria', () => {
Expand All @@ -65,6 +65,25 @@ describe('Data sorting criteria', () => {
type: 'date',
});
});

it(`should provide the number criteria of array numeric values (${direction})`, () => {
testSorting({
input: [7, [7, 1], [7, 2], [6], -Infinity, Infinity],
output: [-Infinity, [6], 7, [7, 1], [7, 2], Infinity],
direction,
type: 'number',
});
});

it(`should provide the number criteria for array date values (${direction})`, () => {
const now = Date.now();
testSorting({
input: [now, [0, now], [0], now - 150000],
output: [[0], [0, now], now - 150000, now],
direction,
type: 'date',
});
});
}

it(`should sort undefined and null to the end`, () => {
Expand Down Expand Up @@ -116,6 +135,15 @@ describe('Data sorting criteria', () => {
type: 'version',
});
});

it(`should provide the array version criteria for terms values (${direction})`, () => {
testSorting({
input: [['1.21.0'], ['1.1.0', '1.1.1'], ['1.21.1', '1.1.1'], '1.112.0', '1.0.0'],
output: ['1.0.0', ['1.1.0', '1.1.1'], ['1.21.0'], ['1.21.1', '1.1.1'], '1.112.0'],
direction,
type: 'version',
});
});
}

it('should sort non-version stuff to the end', () => {
Expand Down Expand Up @@ -148,6 +176,15 @@ describe('Data sorting criteria', () => {
});
});

it(`should provide the string criteria for array terms values (${direction})`, () => {
testSorting({
input: ['a', 'b', 'c', 'd', '12', ['a', 'b'], ['b', 'c']],
output: ['12', 'a', ['a', 'b'], 'b', ['b', 'c'], 'c', 'd'],
direction,
type: 'string',
});
});

it(`should provide the string criteria for other types of values (${direction})`, () => {
testSorting({
input: [true, false, false],
Expand All @@ -156,6 +193,15 @@ describe('Data sorting criteria', () => {
type: 'boolean',
});
});

it(`should provide the string criteria for other types of array values (${direction})`, () => {
testSorting({
input: [true, false, false, [true, false]],
output: [false, false, true, [true, false]],
direction,
type: 'boolean',
});
});
}

it('should sort undefined and null to the end', () => {
Expand Down Expand Up @@ -257,6 +303,34 @@ describe('Data sorting criteria', () => {
keepLast: true,
});
});

it(`should provide the IP criteria for array IP values (mixed values with invalid "Other" field) - ${direction}`, () => {
testSorting({
input: [
'fc00::123',
'192.168.1.50',
'Other',
'10.0.1.76',
'8.8.8.8',
'::1',
['fc00::123', '192.168.1.50'],
['::1', '10.0.1.76'],
],
output: [
'::1',
['::1', '10.0.1.76'],
'8.8.8.8',
'10.0.1.76',
'192.168.1.50',
'fc00::123',
['fc00::123', '192.168.1.50'],
'Other',
],
direction,
type: 'ip',
keepLast: true,
});
});
}

it('should sort undefined and null to the end', () => {
Expand Down Expand Up @@ -341,6 +415,34 @@ describe('Data sorting criteria', () => {
type: 'range',
});
});

it(`should sort array values for both open and closed ranges - ${direction}`, () => {
testSorting({
input: [
{ gte: 1, lt: 5 },
,
[
{ gte: 0, lt: 5 },
{ gte: 1, lt: 5 },
],
[{ gte: 0 }, { gte: 1, lt: 5 }],
{ gte: 0, lt: 5 },
{ gte: 0 },
],
output: [
{ gte: 0, lt: 5 },
[
{ gte: 0, lt: 5 },
{ gte: 1, lt: 5 },
],
{ gte: 0 },
[{ gte: 0 }, { gte: 1, lt: 5 }],
{ gte: 1, lt: 5 },
],
direction,
type: 'range',
});
});
}

it('should sort undefined and null to the end', () => {
Expand Down
Loading

0 comments on commit 37b909c

Please sign in to comment.