Skip to content

Commit b516933

Browse files
authored
sort by managed policy feature (#950)
* sort by managed policy feature Signed-off-by: Mansi Shinde <[email protected]> * add customSort function and remove comment Signed-off-by: Mansi Shinde <[email protected]> * update managed by policy sorting logic Signed-off-by: Mansi Shinde <[email protected]> --------- Signed-off-by: Mansi Shinde <[email protected]>
1 parent 44da2ab commit b516933

File tree

4 files changed

+40
-22
lines changed

4 files changed

+40
-22
lines changed

public/pages/CreateDataStream/containers/BackingIndices/BackingIndices.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export default function BackingIndices(props: SubDetailProps) {
103103
{
104104
field: "managed",
105105
name: "Managed by policy",
106-
sortable: false,
106+
sortable: true,
107107
truncateText: true,
108108
textOnly: true,
109109
render: renderNumber,

public/pages/Indices/containers/Indices/__snapshots__/Indices.test.tsx.snap

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,21 +349,29 @@ exports[`<Indices /> spec renders the component 1`] = `
349349
</button>
350350
</th>
351351
<th
352+
aria-live="polite"
353+
aria-sort="none"
352354
class="euiTableHeaderCell"
353355
data-test-subj="tableHeaderCell_managed_2"
354356
role="columnheader"
355357
scope="col"
356358
>
357-
<span
358-
class="euiTableCellContent"
359+
<button
360+
class="euiTableHeaderButton"
361+
data-test-subj="tableHeaderSortButton"
362+
type="button"
359363
>
360364
<span
361-
class="euiTableCellContent__text"
362-
title="Managed by policy"
365+
class="euiTableCellContent"
363366
>
364-
Managed by policy
367+
<span
368+
class="euiTableCellContent__text"
369+
title="Managed by policy"
370+
>
371+
Managed by policy
372+
</span>
365373
</span>
366-
</span>
374+
</button>
367375
</th>
368376
<th
369377
aria-live="polite"

public/pages/Indices/utils/constants.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const getColumns = (props: IColumnOptions): EuiTableFieldDataColumnType<ManagedC
7878
{
7979
field: "managed",
8080
name: "Managed by policy",
81-
sortable: false,
81+
sortable: true,
8282
truncateText: true,
8383
textOnly: true,
8484
render: renderNumber,

server/services/IndexService.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,17 @@ export default class IndexService {
6666
const params: {
6767
index: string;
6868
format: string;
69-
s: string;
69+
s?: string;
7070
expand_wildcards?: string;
7171
} = {
7272
index: getSearchString(terms, indices, dataStreams),
7373
format: "json",
74-
s: `${sortField}:${sortDirection}`,
7574
};
7675

76+
if (sortField !== "managed" && sortField !== "data_stream") {
77+
params.s = `${sortField}:${sortDirection}`;
78+
}
79+
7780
if (expandWildcards) {
7881
params.expand_wildcards = expandWildcards;
7982
}
@@ -142,22 +145,27 @@ export default class IndexService {
142145
}
143146
});
144147

145-
if (sortField === "status") {
146-
// add new more status to status field so we need to sort
147-
indicesResponse.sort((a, b) => {
148+
function customSort(array, key, sortDirection) {
149+
return array.sort((a, b) => {
148150
let flag;
149-
const aStatus = a.extraStatus as string;
150-
const bStatus = b.extraStatus as string;
151+
const aValue = a[key] as string;
152+
const bValue = b[key] as string;
153+
151154
if (sortDirection === "asc") {
152-
flag = aStatus < bStatus;
155+
flag = aValue < bValue;
153156
} else {
154-
flag = aStatus > bStatus;
157+
flag = aValue > bValue;
155158
}
156159

157160
return flag ? -1 : 1;
158161
});
159162
}
160163

164+
if (sortField === "status") {
165+
// add new more status to status field so we need to sort
166+
customSort(indicesResponse, "extraStatus", sortDirection);
167+
}
168+
161169
// Filtering out indices that belong to a data stream. This must be done before pagination.
162170
const filteredIndices = showDataStreams ? indicesResponse : indicesResponse.filter((index) => index.data_stream === null);
163171

@@ -169,17 +177,19 @@ export default class IndexService {
169177

170178
const managedStatus = await this._getManagedStatus(request, indexNames);
171179

180+
const allIndices = paginatedIndices.map((catIndex: CatIndex) => ({
181+
...catIndex,
182+
managed: managedStatus[catIndex.index] ? "Yes" : "No",
183+
managedPolicy: managedStatus[catIndex.index],
184+
}));
185+
172186
// NOTE: Cannot use response.ok due to typescript type checking
173187
return response.custom({
174188
statusCode: 200,
175189
body: {
176190
ok: true,
177191
response: {
178-
indices: paginatedIndices.map((catIndex: CatIndex) => ({
179-
...catIndex,
180-
managed: managedStatus[catIndex.index] ? "Yes" : "No",
181-
managedPolicy: managedStatus[catIndex.index],
182-
})),
192+
indices: sortField === "managed" ? customSort(allIndices, "managed", sortDirection) : allIndices,
183193
totalIndices: filteredIndices.length,
184194
},
185195
},

0 commit comments

Comments
 (0)