From a8579f2a8b84090093ffe52fe5647a98aecc68b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yulia=20=C4=8Cech?= <6585477+yuliacech@users.noreply.github.com> Date: Mon, 5 Feb 2024 18:19:37 +0000 Subject: [PATCH] [Index Management] Add sorting for index columns added via extensions service (#176163) ## Summary Fixes https://github.com/elastic/kibana/issues/175140 Follow up for https://github.com/elastic/kibana/pull/174785 This PR fixes the sorting in the indices table for columns added via the extensions service. To test this change, add an ILM phase column as the example in this [commit](https://github.com/elastic/kibana/pull/176163/commits/90bdf4788f5e3396519225ab8a3f412865f855b0) and follow the instructions in the ILM plugin [readme](https://github.com/elastic/kibana/blob/04415ce2fc3b302efdace7f391c07047839fb0f1/x-pack/plugins/index_lifecycle_management/README.md#quick-steps-for-testing-ilm-in-index-management). ### Screenshot Screenshot 2024-02-02 at 18 03 34 ### 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 - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [ ] 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) --- .../application/services/sort_table.test.ts | 210 ++++++++++++++++++ .../public/application/services/sort_table.ts | 80 ++++--- .../application/store/selectors/index.js | 3 +- .../public/services/extensions_service.ts | 2 + 4 files changed, 264 insertions(+), 31 deletions(-) create mode 100644 x-pack/plugins/index_management/public/application/services/sort_table.test.ts diff --git a/x-pack/plugins/index_management/public/application/services/sort_table.test.ts b/x-pack/plugins/index_management/public/application/services/sort_table.test.ts new file mode 100644 index 00000000000000..07e969190bd420 --- /dev/null +++ b/x-pack/plugins/index_management/public/application/services/sort_table.test.ts @@ -0,0 +1,210 @@ +/* + * 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; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { Index } from '../../../common'; +import { ExtensionsService } from '../../services/extensions_service'; +import { sortTable } from './sort_table'; +describe('sortTable', () => { + describe('sorts by name', () => { + const indices = [{ name: 'test1' }, { name: 'test2' }] as Index[]; + it('ascending', () => { + const sorted = sortTable(indices, 'name', true); + expect(sorted).toEqual([{ name: 'test1' }, { name: 'test2' }]); + }); + it('descending', () => { + const sorted = sortTable(indices, 'name', false); + expect(sorted).toEqual([{ name: 'test2' }, { name: 'test1' }]); + }); + }); + + describe('sorts by status', () => { + const indices = [{ status: 'open' }, { status: 'close' }] as Index[]; + it('ascending', () => { + const sorted = sortTable(indices, 'status', true); + expect(sorted).toEqual([{ status: 'close' }, { status: 'open' }]); + }); + it('descending', () => { + const sorted = sortTable(indices, 'status', false); + expect(sorted).toEqual([{ status: 'open' }, { status: 'close' }]); + }); + }); + + describe('sorts by health', () => { + const indices = [{ health: 'green' }, { health: 'yellow' }, { health: 'red' }] as Index[]; + it('ascending', () => { + const sorted = sortTable(indices, 'health', true); + expect(sorted).toEqual([{ health: 'green' }, { health: 'red' }, { health: 'yellow' }]); + }); + it('descending', () => { + const sorted = sortTable(indices, 'health', false); + expect(sorted).toEqual([{ health: 'yellow' }, { health: 'red' }, { health: 'green' }]); + }); + }); + + describe('sorts by primary', () => { + const indices = [{ primary: '1' }, { primary: '12' }, { primary: '2' }] as Index[]; + it('ascending', () => { + const sorted = sortTable(indices, 'primary', true); + expect(sorted).toEqual([{ primary: '1' }, { primary: '2' }, { primary: '12' }]); + }); + it('descending', () => { + const sorted = sortTable(indices, 'primary', false); + expect(sorted).toEqual([{ primary: '12' }, { primary: '2' }, { primary: '1' }]); + }); + }); + + describe('sorts by replica', () => { + const indices = [{ replica: '1' }, { replica: '12' }, { replica: '2' }] as Index[]; + it('ascending', () => { + const sorted = sortTable(indices, 'replica', true); + expect(sorted).toEqual([{ replica: '1' }, { replica: '2' }, { replica: '12' }]); + }); + it('descending', () => { + const sorted = sortTable(indices, 'replica', false); + expect(sorted).toEqual([{ replica: '12' }, { replica: '2' }, { replica: '1' }]); + }); + }); + + describe('sorts by documents', () => { + const indices = [{ documents: 1 }, { documents: 12 }, { documents: 2 }] as Index[]; + it('ascending', () => { + const sorted = sortTable(indices, 'documents', true); + expect(sorted).toEqual([{ documents: 1 }, { documents: 2 }, { documents: 12 }]); + }); + it('descending', () => { + const sorted = sortTable(indices, 'documents', false); + expect(sorted).toEqual([{ documents: 12 }, { documents: 2 }, { documents: 1 }]); + }); + }); + + describe('sorts by size', () => { + const indices = [{ size: '248b' }, { size: '2.35mb' }, { size: '6.36kb' }] as Index[]; + it('ascending', () => { + const sorted = sortTable(indices, 'size', true); + expect(sorted).toEqual([{ size: '248b' }, { size: '6.36kb' }, { size: '2.35mb' }]); + }); + it('descending', () => { + const sorted = sortTable(indices, 'size', false); + expect(sorted).toEqual([{ size: '2.35mb' }, { size: '6.36kb' }, { size: '248b' }]); + }); + }); + + describe('sorts by primary_size', () => { + const indices = [ + { primary_size: '248b' }, + { primary_size: '2.35mb' }, + { primary_size: '6.36kb' }, + ] as Index[]; + it('ascending', () => { + const sorted = sortTable(indices, 'primary_size', true); + expect(sorted).toEqual([ + { primary_size: '248b' }, + { primary_size: '6.36kb' }, + { primary_size: '2.35mb' }, + ]); + }); + it('descending', () => { + const sorted = sortTable(indices, 'primary_size', false); + expect(sorted).toEqual([ + { primary_size: '2.35mb' }, + { primary_size: '6.36kb' }, + { primary_size: '248b' }, + ]); + }); + }); + + describe('sorts by data_stream', () => { + const indices = [ + { data_stream: 'test1' }, + { data_stream: undefined }, + { data_stream: 'test2' }, + ] as Index[]; + it('ascending', () => { + const sorted = sortTable(indices, 'data_stream', true); + expect(sorted).toEqual([ + { data_stream: 'test1' }, + { data_stream: 'test2' }, + { data_stream: undefined }, + ]); + }); + it('descending', () => { + const sorted = sortTable(indices, 'data_stream', false); + expect(sorted).toEqual([ + { data_stream: undefined }, + { data_stream: 'test2' }, + { data_stream: 'test1' }, + ]); + }); + }); + + describe('sorts by a column added via extensions service', () => { + const indices = [ + { ilm: { phase: 'hot' } }, + { ilm: { phase: 'warm' } }, + { ilm: { phase: undefined } }, + ] as Index[]; + const extensionsService = { + columns: [ + { + fieldName: 'ilm.phase', + label: 'ILM phase', + order: 10, + render: (index: Index) => (index.ilm?.managed ? index.ilm.phase : ''), + sort: (index: Index) => (index.ilm?.managed ? index.ilm.phase : ''), + }, + ], + } as ExtensionsService; + it('ascending', () => { + const sorted = sortTable(indices, 'ilm.phase', true, extensionsService); + expect(sorted).toEqual([ + { ilm: { phase: 'hot' } }, + { ilm: { phase: 'warm' } }, + { ilm: { phase: undefined } }, + ]); + }); + it('descending', () => { + const sorted = sortTable(indices, 'ilm.phase', false, extensionsService); + expect(sorted).toEqual([ + { ilm: { phase: undefined } }, + { ilm: { phase: 'warm' } }, + { ilm: { phase: 'hot' } }, + ]); + }); + }); + describe('sorting by a column without a sorter', () => { + const indices = [ + { test: 'test1' }, + { test: 'test2' }, + { test: undefined }, + { test: 'test5' }, + { test: 'test3' }, + { test: undefined }, + ] as unknown as Index[]; + it('ascending', () => { + const sorted = sortTable(indices, 'test', true); + expect(sorted).toEqual([ + { test: 'test1' }, + { test: 'test2' }, + { test: 'test3' }, + { test: 'test5' }, + { test: undefined }, + { test: undefined }, + ]); + }); + it('descending', () => { + const sorted = sortTable(indices, 'test', false); + expect(sorted).toEqual([ + { test: undefined }, + { test: undefined }, + { test: 'test5' }, + { test: 'test3' }, + { test: 'test2' }, + { test: 'test1' }, + ]); + }); + }); +}); diff --git a/x-pack/plugins/index_management/public/application/services/sort_table.ts b/x-pack/plugins/index_management/public/application/services/sort_table.ts index 92257391e335a5..f06cd1535838d6 100644 --- a/x-pack/plugins/index_management/public/application/services/sort_table.ts +++ b/x-pack/plugins/index_management/public/application/services/sort_table.ts @@ -5,7 +5,9 @@ * 2.0. */ -import { sortBy } from 'lodash'; +import { sortBy, get } from 'lodash'; +import { Index } from '../../../common'; +import type { ExtensionsService } from '../../services'; type SortField = | 'name' @@ -28,39 +30,57 @@ const unitMagnitude = { pb: 5, }; -const stringSort = (fieldName: SortField) => (item: { [key: string]: any }) => { - return item[fieldName]; -}; +type SortFunction = (index: Index) => any; -const numericSort = (fieldName: SortField) => (item: { [key: string]: any }) => +item[fieldName]; +const numericSort = + (fieldName: SortField): SortFunction => + (item) => + Number(item[fieldName]); -const byteSort = (fieldName: SortField) => (item: { [key: string]: any }) => { - const rawValue = item[fieldName]; - // raw value can be missing if index is closed - if (!rawValue) { - return 0; - } - const matchResult = rawValue.match(/(.*)([kmgtp]b)/); - if (!matchResult) { - return 0; - } - const [, number, unit]: [string, string, Unit] = matchResult; - return +number * Math.pow(1024, unitMagnitude[unit]); -}; +const byteSort = + (fieldName: SortField): SortFunction => + (item) => { + const rawValue = String(item[fieldName]); + // raw value can be missing if index is closed + if (!rawValue) { + return 0; + } + const matchResult = rawValue.match(/(.*)([kmgtp]b)/); + if (!matchResult) { + return 0; + } + const [, number, unit] = matchResult; + return +number * Math.pow(1024, unitMagnitude[unit as Unit]); + }; -const sorters = { - name: stringSort('name'), - status: stringSort('status'), - health: stringSort('health'), - primary: numericSort('primary'), - replica: numericSort('replica'), - documents: numericSort('documents'), - size: byteSort('size'), - primary_size: byteSort('primary_size'), - data_stream: stringSort('data_stream'), +const getSorters = (extensionsService?: ExtensionsService) => { + const sorters = { + primary: numericSort('primary'), + replica: numericSort('replica'), + documents: numericSort('documents'), + size: byteSort('size'), + primary_size: byteSort('primary_size'), + } as any; + const columns = extensionsService?.columns ?? []; + for (const column of columns) { + if (column.sort) { + sorters[column.fieldName] = column.sort; + } + } + return sorters; }; -export const sortTable = (array = [], sortField: SortField, isSortAscending: boolean) => { - const sorted = sortBy(array, sorters[sortField]); +export const sortTable = ( + array: Index[] = [], + sortField: SortField | string, + isSortAscending: boolean, + extensionsService?: ExtensionsService +) => { + const sorters = getSorters(extensionsService); + let sorter = sorters[sortField]; + if (!sorter) { + sorter = (index: Index) => get(index, sortField); + } + const sorted = sortBy(array, sorter); return isSortAscending ? sorted : sorted.reverse(); }; diff --git a/x-pack/plugins/index_management/public/application/store/selectors/index.js b/x-pack/plugins/index_management/public/application/store/selectors/index.js index 6ed09d8234fa44..2e1e01f3122aef 100644 --- a/x-pack/plugins/index_management/public/application/store/selectors/index.js +++ b/x-pack/plugins/index_management/public/application/store/selectors/index.js @@ -94,7 +94,8 @@ export const getPageOfIndices = createSelector( const sortedIndexes = sortTable( filteredIndices, tableState.sortField, - tableState.isSortAscending + tableState.isSortAscending, + extensionsService ); const { firstItemIndex, lastItemIndex } = pager; const pagedIndexes = sortedIndexes.slice(firstItemIndex, lastItemIndex + 1); diff --git a/x-pack/plugins/index_management/public/services/extensions_service.ts b/x-pack/plugins/index_management/public/services/extensions_service.ts index 3e8f6118bf44e0..e7cb4aef2282de 100644 --- a/x-pack/plugins/index_management/public/services/extensions_service.ts +++ b/x-pack/plugins/index_management/public/services/extensions_service.ts @@ -44,6 +44,8 @@ export interface IndicesListColumn { label: string; order: number; render?: (index: Index) => ReactNode; + // return a value used for sorting (only if the value is different from the original value at index[fieldName]) + sort?: (index: Index) => any; } export interface ExtensionsSetup {