-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Lens] Add specific IP and Range/Interval sorting to datatable #87006
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2693006
:heavy_plus_sign: Add new IP parser dependency
dej611 a552300
:sparkles: Implemented ip and ranges sorting into Lens datatable
dej611 ea7e910
:white_check_mark: Add more tests for strings and booleans
dej611 e768eb7
:sparkles: Implement always last logic for external values
dej611 5fafb2c
:recycle: Refactor a bit variables to reduce type casting around
dej611 e7c610a
Merge branch 'master' into feature/lens/table-ip-sorting
kibanamachine ca5c970
Merge branch 'master' into feature/lens/table-ip-sorting
kibanamachine c079b89
Merge branch 'master' into feature/lens/table-ip-sorting
kibanamachine 8d97bbe
Merge remote-tracking branch 'upstream/master' into feature/lens/tabl…
dej611 9e6a262
:ok_hand: Remove optional chaining
dej611 251a444
Merge branch 'feature/lens/table-ip-sorting' of github.com:dej611/kib…
dej611 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
187 changes: 187 additions & 0 deletions
187
x-pack/plugins/lens/public/datatable_visualization/sorting.test.tsx
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,187 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { getSortingCriteria } from './sorting'; | ||
import { FieldFormat } from 'src/plugins/data/public'; | ||
import { DatatableColumnType } from 'src/plugins/expressions'; | ||
|
||
function getMockFormatter() { | ||
return { convert: (v: unknown) => `${v as string}` } as FieldFormat; | ||
} | ||
|
||
function testSorting({ | ||
input, | ||
output, | ||
direction, | ||
type, | ||
keepLast, | ||
}: { | ||
input: unknown[]; | ||
output: unknown[]; | ||
direction: 'asc' | 'desc'; | ||
type: DatatableColumnType | 'range'; | ||
keepLast?: boolean; // special flag to handle values that should always be last no matter the direction | ||
}) { | ||
const datatable = input.map((v) => ({ | ||
a: v, | ||
})); | ||
const sorted = output.map((v) => ({ a: v })); | ||
if (direction === 'desc') { | ||
sorted.reverse(); | ||
if (keepLast) { | ||
// Cycle shift of the first element | ||
const firstEl = sorted.shift()!; | ||
sorted.push(firstEl); | ||
} | ||
} | ||
const criteria = getSortingCriteria(type, 'a', getMockFormatter(), direction); | ||
expect(datatable.sort(criteria)).toEqual(sorted); | ||
} | ||
|
||
describe('Data sorting criteria', () => { | ||
describe('Numeric values', () => { | ||
for (const direction of ['asc', 'desc'] as const) { | ||
it(`should provide the number criteria of numeric values (${direction})`, () => { | ||
testSorting({ | ||
input: [7, 6, 5, -Infinity, Infinity], | ||
output: [-Infinity, 5, 6, 7, Infinity], | ||
direction, | ||
type: 'number', | ||
}); | ||
}); | ||
|
||
it(`should provide the number criteria for date values (${direction})`, () => { | ||
const now = Date.now(); | ||
testSorting({ | ||
input: [now, 0, now - 150000], | ||
output: [0, now - 150000, now], | ||
direction, | ||
type: 'date', | ||
}); | ||
}); | ||
} | ||
}); | ||
|
||
describe('String or anything else as string', () => { | ||
for (const direction of ['asc', 'desc'] as const) { | ||
it(`should provide the string criteria for terms values (${direction})`, () => { | ||
testSorting({ | ||
input: ['a', 'b', 'c', 'd', '12'], | ||
output: ['12', 'a', 'b', 'c', 'd'], | ||
direction, | ||
type: 'string', | ||
}); | ||
}); | ||
|
||
it(`should provide the string criteria for other types of values (${direction})`, () => { | ||
testSorting({ | ||
input: [true, false, false], | ||
output: [false, false, true], | ||
direction, | ||
type: 'boolean', | ||
}); | ||
}); | ||
} | ||
}); | ||
|
||
describe('IP sorting', () => { | ||
for (const direction of ['asc', 'desc'] as const) { | ||
it(`should provide the IP criteria for IP values (IPv4 only values) - ${direction}`, () => { | ||
testSorting({ | ||
input: ['127.0.0.1', '192.168.1.50', '200.100.100.10', '10.0.1.76', '8.8.8.8'], | ||
output: ['8.8.8.8', '10.0.1.76', '127.0.0.1', '192.168.1.50', '200.100.100.10'], | ||
direction, | ||
type: 'ip', | ||
}); | ||
}); | ||
|
||
it(`should provide the IP criteria for IP values (IPv6 only values) - ${direction}`, () => { | ||
testSorting({ | ||
input: [ | ||
'fc00::123', | ||
'::1', | ||
'2001:0db8:85a3:0000:0000:8a2e:0370:7334', | ||
'2001:db8:1234:0000:0000:0000:0000:0000', | ||
'2001:db8:1234::', // equivalent to the above | ||
], | ||
output: [ | ||
'::1', | ||
'2001:db8:1234::', | ||
'2001:db8:1234:0000:0000:0000:0000:0000', | ||
'2001:0db8:85a3:0000:0000:8a2e:0370:7334', | ||
'fc00::123', | ||
], | ||
direction, | ||
type: 'ip', | ||
}); | ||
}); | ||
|
||
it(`should provide the IP criteria for IP values (mixed values) - ${direction}`, () => { | ||
// A mix of IPv4, IPv6, IPv4 mapped to IPv6 | ||
testSorting({ | ||
input: [ | ||
'fc00::123', | ||
'192.168.1.50', | ||
'::FFFF:192.168.1.50', // equivalent to the above with the IPv6 mapping | ||
'10.0.1.76', | ||
'8.8.8.8', | ||
'::1', | ||
], | ||
output: [ | ||
'::1', | ||
'8.8.8.8', | ||
'10.0.1.76', | ||
'192.168.1.50', | ||
'::FFFF:192.168.1.50', | ||
'fc00::123', | ||
], | ||
direction, | ||
type: 'ip', | ||
}); | ||
}); | ||
|
||
it(`should provide the IP criteria for 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'], | ||
output: ['::1', '8.8.8.8', '10.0.1.76', '192.168.1.50', 'fc00::123', 'Other'], | ||
direction, | ||
type: 'ip', | ||
keepLast: true, | ||
}); | ||
}); | ||
} | ||
}); | ||
|
||
describe('Range sorting', () => { | ||
for (const direction of ['asc', 'desc'] as const) { | ||
it(`should sort closed ranges - ${direction}`, () => { | ||
testSorting({ | ||
input: [ | ||
{ gte: 1, lt: 5 }, | ||
{ gte: 0, lt: 5 }, | ||
{ gte: 0, lt: 1 }, | ||
], | ||
output: [ | ||
{ gte: 0, lt: 1 }, | ||
{ gte: 0, lt: 5 }, | ||
{ gte: 1, lt: 5 }, | ||
], | ||
direction, | ||
type: 'range', | ||
}); | ||
}); | ||
|
||
it(`should sort open ranges - ${direction}`, () => { | ||
testSorting({ | ||
input: [{ gte: 1, lt: 5 }, { gte: 0, lt: 5 }, { gte: 0 }], | ||
output: [{ gte: 0, lt: 5 }, { gte: 0 }, { gte: 1, lt: 5 }], | ||
direction, | ||
type: 'range', | ||
}); | ||
}); | ||
} | ||
}); | ||
}); |
91 changes: 91 additions & 0 deletions
91
x-pack/plugins/lens/public/datatable_visualization/sorting.tsx
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,91 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import ipaddr from 'ipaddr.js'; | ||
import type { IPv4, IPv6 } from 'ipaddr.js'; | ||
import { FieldFormat } from 'src/plugins/data/public'; | ||
|
||
function isIPv6Address(ip: IPv4 | IPv6): ip is IPv6 { | ||
return ip.kind() === 'ipv6'; | ||
} | ||
|
||
function getSafeIpAddress(ip: string, directionFactor: number) { | ||
if (!ipaddr.isValid(ip)) { | ||
// for non valid IPs have the same behaviour as for now (we assume it's only the "Other" string) | ||
// create a mock object which has all a special value to keep them always at the bottom of the list | ||
return { parts: Array(8).fill(directionFactor * Infinity) }; | ||
} | ||
const parsedIp = ipaddr.parse(ip); | ||
return isIPv6Address(parsedIp) ? parsedIp : parsedIp.toIPv4MappedAddress(); | ||
} | ||
|
||
function getIPCriteria(sortBy: string, directionFactor: number) { | ||
// Create a set of 8 function to sort based on the 8 IPv6 slots of an address | ||
// For IPv4 bring them to the IPv6 "mapped" format and then sort | ||
return (rowA: Record<string, unknown>, rowB: Record<string, unknown>) => { | ||
const ipAString = rowA[sortBy] as string; | ||
const ipBString = rowB[sortBy] as string; | ||
const ipA = getSafeIpAddress(ipAString, directionFactor); | ||
const ipB = getSafeIpAddress(ipBString, directionFactor); | ||
|
||
// Now compare each part of the IPv6 address and exit when a value != 0 is found | ||
let i = 0; | ||
let diff = ipA.parts[i] - ipB.parts[i]; | ||
while (!diff && i < 7) { | ||
i++; | ||
diff = ipA.parts[i] - ipB.parts[i]; | ||
} | ||
|
||
// in case of same address but written in different styles, sort by string length | ||
if (diff === 0) { | ||
return directionFactor * (ipAString.length - ipBString.length); | ||
} | ||
return directionFactor * diff; | ||
}; | ||
} | ||
|
||
function getRangeCriteria(sortBy: string, directionFactor: number) { | ||
// fill missing fields with these open bounds to perform number sorting | ||
const openRange = { gte: -Infinity, lt: Infinity }; | ||
return (rowA: Record<string, unknown>, rowB: Record<string, unknown>) => { | ||
const rangeA = { ...openRange, ...(rowA[sortBy] as Omit<Range, 'type'>) }; | ||
const rangeB = { ...openRange, ...(rowB[sortBy] as Omit<Range, 'type'>) }; | ||
|
||
const fromComparison = rangeA.gte - rangeB.gte; | ||
const toComparison = rangeA.lt - rangeB.lt; | ||
|
||
return directionFactor * (fromComparison || toComparison); | ||
}; | ||
} | ||
|
||
export function getSortingCriteria( | ||
type: string | undefined, | ||
sortBy: string, | ||
formatter: FieldFormat, | ||
direction: string | ||
) { | ||
// handle the direction with a multiply factor. | ||
const directionFactor = direction === 'asc' ? 1 : -1; | ||
|
||
if (['number', 'date'].includes(type || '')) { | ||
return (rowA: Record<string, unknown>, rowB: Record<string, unknown>) => | ||
directionFactor * ((rowA[sortBy] as number) - (rowB[sortBy] as number)); | ||
} | ||
// this is a custom type, and can safely assume the gte and lt fields are all numbers or undefined | ||
if (type === 'range') { | ||
return getRangeCriteria(sortBy, directionFactor); | ||
} | ||
// IP have a special sorting | ||
if (type === 'ip') { | ||
return getIPCriteria(sortBy, directionFactor); | ||
} | ||
// use a string sorter for the rest | ||
return (rowA: Record<string, unknown>, rowB: Record<string, unknown>) => { | ||
const aString = formatter.convert(rowA[sortBy]); | ||
const bString = formatter.convert(rowB[sortBy]); | ||
return directionFactor * aString.localeCompare(bString); | ||
}; | ||
} |
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 |
---|---|---|
|
@@ -16449,6 +16449,11 @@ [email protected], ipaddr.js@^1.9.0: | |
resolved "https://registry.yarnpkg.com/ipaddr.js/-/ipaddr.js-1.9.0.tgz#37df74e430a0e47550fe54a2defe30d8acd95f65" | ||
integrity sha512-M4Sjn6N/+O6/IXSJseKqHoFc+5FdGJ22sXqnjTpdZweHK64MzEPAyQZyEU3R/KRv2GLoa7nNtg/C2Ev6m7z+eA== | ||
|
||
[email protected]: | ||
version "2.0.0" | ||
resolved "https://registry.yarnpkg.com/ipaddr.js/-/ipaddr.js-2.0.0.tgz#77ccccc8063ae71ab65c55f21b090698e763fc6e" | ||
integrity sha512-S54H9mIj0rbxRIyrDMEuuER86LdlgUg9FSeZ8duQb6CUG2iRrA36MYVQBSprTF/ZeAwvyQ5mDGuNvIPM0BIl3w== | ||
|
||
[email protected]: | ||
version "5.0.6" | ||
resolved "https://registry.yarnpkg.com/iron/-/iron-5.0.6.tgz#7121d4a6e3ac2f65e4d02971646fea1995434744" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!