From b5169330dc9865e8b0f49fd02a9e89c3b31a0381 Mon Sep 17 00:00:00 2001 From: Mansi Shinde Date: Tue, 12 Dec 2023 18:18:56 -0500 Subject: [PATCH] sort by managed policy feature (#950) * sort by managed policy feature Signed-off-by: Mansi Shinde * add customSort function and remove comment Signed-off-by: Mansi Shinde * update managed by policy sorting logic Signed-off-by: Mansi Shinde --------- Signed-off-by: Mansi Shinde --- .../BackingIndices/BackingIndices.tsx | 2 +- .../__snapshots__/Indices.test.tsx.snap | 20 +++++++--- public/pages/Indices/utils/constants.tsx | 2 +- server/services/IndexService.ts | 38 ++++++++++++------- 4 files changed, 40 insertions(+), 22 deletions(-) diff --git a/public/pages/CreateDataStream/containers/BackingIndices/BackingIndices.tsx b/public/pages/CreateDataStream/containers/BackingIndices/BackingIndices.tsx index b7f327a83..9f941c933 100644 --- a/public/pages/CreateDataStream/containers/BackingIndices/BackingIndices.tsx +++ b/public/pages/CreateDataStream/containers/BackingIndices/BackingIndices.tsx @@ -103,7 +103,7 @@ export default function BackingIndices(props: SubDetailProps) { { field: "managed", name: "Managed by policy", - sortable: false, + sortable: true, truncateText: true, textOnly: true, render: renderNumber, diff --git a/public/pages/Indices/containers/Indices/__snapshots__/Indices.test.tsx.snap b/public/pages/Indices/containers/Indices/__snapshots__/Indices.test.tsx.snap index 6eba9f596..191b4b655 100644 --- a/public/pages/Indices/containers/Indices/__snapshots__/Indices.test.tsx.snap +++ b/public/pages/Indices/containers/Indices/__snapshots__/Indices.test.tsx.snap @@ -349,21 +349,29 @@ exports[` spec renders the component 1`] = ` - - Managed by policy + + Managed by policy + - + { + function customSort(array, key, sortDirection) { + return array.sort((a, b) => { let flag; - const aStatus = a.extraStatus as string; - const bStatus = b.extraStatus as string; + const aValue = a[key] as string; + const bValue = b[key] as string; + if (sortDirection === "asc") { - flag = aStatus < bStatus; + flag = aValue < bValue; } else { - flag = aStatus > bStatus; + flag = aValue > bValue; } return flag ? -1 : 1; }); } + if (sortField === "status") { + // add new more status to status field so we need to sort + customSort(indicesResponse, "extraStatus", sortDirection); + } + // Filtering out indices that belong to a data stream. This must be done before pagination. const filteredIndices = showDataStreams ? indicesResponse : indicesResponse.filter((index) => index.data_stream === null); @@ -169,17 +177,19 @@ export default class IndexService { const managedStatus = await this._getManagedStatus(request, indexNames); + const allIndices = paginatedIndices.map((catIndex: CatIndex) => ({ + ...catIndex, + managed: managedStatus[catIndex.index] ? "Yes" : "No", + managedPolicy: managedStatus[catIndex.index], + })); + // NOTE: Cannot use response.ok due to typescript type checking return response.custom({ statusCode: 200, body: { ok: true, response: { - indices: paginatedIndices.map((catIndex: CatIndex) => ({ - ...catIndex, - managed: managedStatus[catIndex.index] ? "Yes" : "No", - managedPolicy: managedStatus[catIndex.index], - })), + indices: sortField === "managed" ? customSort(allIndices, "managed", sortDirection) : allIndices, totalIndices: filteredIndices.length, }, },