Skip to content
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

sort by managed policy feature #950

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,21 +349,29 @@ exports[`<Indices /> spec renders the component 1`] = `
</button>
</th>
<th
aria-live="polite"
aria-sort="none"
class="euiTableHeaderCell"
data-test-subj="tableHeaderCell_managed_2"
role="columnheader"
scope="col"
>
<span
class="euiTableCellContent"
<button
class="euiTableHeaderButton"
data-test-subj="tableHeaderSortButton"
type="button"
>
<span
class="euiTableCellContent__text"
title="Managed by policy"
class="euiTableCellContent"
>
Managed by policy
<span
class="euiTableCellContent__text"
title="Managed by policy"
>
Managed by policy
</span>
</span>
</span>
</button>
</th>
<th
aria-live="polite"
Expand Down
2 changes: 1 addition & 1 deletion public/pages/Indices/utils/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const getColumns = (props: IColumnOptions): EuiTableFieldDataColumnType<ManagedC
{
field: "managed",
name: "Managed by policy",
sortable: false,
sortable: true,
truncateText: true,
textOnly: true,
render: renderNumber,
Expand Down
38 changes: 24 additions & 14 deletions server/services/IndexService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,17 @@ export default class IndexService {
const params: {
index: string;
format: string;
s: string;
s?: string;
expand_wildcards?: string;
} = {
index: getSearchString(terms, indices, dataStreams),
format: "json",
s: `${sortField}:${sortDirection}`,
};

if (sortField !== "managed" && sortField !== "data_stream") {
params.s = `${sortField}:${sortDirection}`;
}

if (expandWildcards) {
params.expand_wildcards = expandWildcards;
}
Expand Down Expand Up @@ -142,22 +145,27 @@ export default class IndexService {
}
});

if (sortField === "status") {
// add new more status to status field so we need to sort
indicesResponse.sort((a, b) => {
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);

Expand All @@ -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,
},
},
Expand Down
Loading