Skip to content

Commit 46aa628

Browse files
committed
Add sorting params to list endpoints; allow sorting on agent config and package config tables; normalize casing of 'desc' and 'asc'
1 parent 5fcf803 commit 46aa628

File tree

15 files changed

+82
-46
lines changed

15 files changed

+82
-46
lines changed

x-pack/plugins/ingest_manager/common/types/rest_spec/common.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
*/
66

77
export interface ListWithKuery {
8-
page: number;
9-
perPage: number;
8+
page?: number;
9+
perPage?: number;
10+
sortField?: string;
11+
sortOrder?: 'desc' | 'asc';
1012
kuery?: string;
1113
}

x-pack/plugins/ingest_manager/public/applications/ingest_manager/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export { useLink } from './use_link';
1313
export { useKibanaLink } from './use_kibana_link';
1414
export { usePackageIconType, UsePackageIconType } from './use_package_icon_type';
1515
export { usePagination, Pagination } from './use_pagination';
16+
export { useSorting } from './use_sorting';
1617
export { useDebounce } from './use_debounce';
1718
export * from './use_request';
1819
export * from './use_input';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
import { useState } from 'react';
7+
import { CriteriaWithPagination } from '@elastic/eui/src/components/basic_table/basic_table';
8+
9+
export function useSorting<T>(defaultSorting: CriteriaWithPagination<T>['sort']) {
10+
const [sorting, setSorting] = useState<CriteriaWithPagination<T>['sort']>(defaultSorting);
11+
12+
return {
13+
sorting,
14+
setSorting,
15+
};
16+
}

x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/create_package_config_page/step_select_config.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ export const StepSelectConfig: React.FunctionComponent<{
3131
data: agentConfigsData,
3232
error: agentConfigsError,
3333
isLoading: isAgentConfigsLoading,
34-
} = useGetAgentConfigs();
34+
} = useGetAgentConfigs({
35+
page: 1,
36+
perPage: 1000,
37+
sortField: 'name',
38+
sortOrder: 'asc',
39+
});
3540
const agentConfigs = agentConfigsData?.items || [];
3641
const agentConfigsById = agentConfigs.reduce(
3742
(acc: { [key: string]: GetAgentConfigsResponseItem }, config) => {

x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/details_page/components/package_configs/package_configs_table.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export const PackageConfigsTable: React.FunctionComponent<Props> = ({
118118
(): EuiInMemoryTableProps<InMemoryPackageConfig>['columns'] => [
119119
{
120120
field: 'name',
121+
sortable: true,
121122
name: i18n.translate(
122123
'xpack.ingestManager.configDetails.packageConfigsTable.nameColumnTitle',
123124
{
@@ -137,6 +138,7 @@ export const PackageConfigsTable: React.FunctionComponent<Props> = ({
137138
},
138139
{
139140
field: 'packageTitle',
141+
sortable: true,
140142
name: i18n.translate(
141143
'xpack.ingestManager.configDetails.packageConfigsTable.packageNameColumnTitle',
142144
{

x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/agent_config/list_page/index.tsx

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
EuiTableFieldDataColumnType,
1818
EuiTextColor,
1919
} from '@elastic/eui';
20+
import { CriteriaWithPagination } from '@elastic/eui/src/components/basic_table/basic_table';
2021
import { i18n } from '@kbn/i18n';
2122
import { FormattedMessage, FormattedDate } from '@kbn/i18n/react';
2223
import { useHistory } from 'react-router-dom';
@@ -27,6 +28,7 @@ import {
2728
useCapabilities,
2829
useGetAgentConfigs,
2930
usePagination,
31+
useSorting,
3032
useLink,
3133
useConfig,
3234
useUrlParams,
@@ -84,6 +86,10 @@ export const AgentConfigListPage: React.FunctionComponent<{}> = () => {
8486
: urlParams.kuery ?? ''
8587
);
8688
const { pagination, pageSizeOptions, setPagination } = usePagination();
89+
const { sorting, setSorting } = useSorting<AgentConfig>({
90+
field: 'updated_at',
91+
direction: 'desc',
92+
});
8793
const history = useHistory();
8894
const isCreateAgentConfigFlyoutOpen = 'create' in urlParams;
8995
const setIsCreateAgentConfigFlyoutOpen = useCallback(
@@ -106,6 +112,8 @@ export const AgentConfigListPage: React.FunctionComponent<{}> = () => {
106112
const { isLoading, data: agentConfigData, sendRequest } = useGetAgentConfigs({
107113
page: pagination.currentPage,
108114
perPage: pagination.pageSize,
115+
sortField: sorting?.field,
116+
sortOrder: sorting?.direction,
109117
kuery: search,
110118
});
111119

@@ -116,6 +124,7 @@ export const AgentConfigListPage: React.FunctionComponent<{}> = () => {
116124
> = [
117125
{
118126
field: 'name',
127+
sortable: true,
119128
name: i18n.translate('xpack.ingestManager.agentConfigList.nameColumnTitle', {
120129
defaultMessage: 'Name',
121130
}),
@@ -158,6 +167,7 @@ export const AgentConfigListPage: React.FunctionComponent<{}> = () => {
158167
},
159168
{
160169
field: 'updated_at',
170+
sortable: true,
161171
name: i18n.translate('xpack.ingestManager.agentConfigList.updatedOnColumnTitle', {
162172
defaultMessage: 'Last updated on',
163173
}),
@@ -240,6 +250,16 @@ export const AgentConfigListPage: React.FunctionComponent<{}> = () => {
240250
[createAgentConfigButton]
241251
);
242252

253+
const onTableChange = (criteria: CriteriaWithPagination<AgentConfig>) => {
254+
const newPagination = {
255+
...pagination,
256+
currentPage: criteria.page.index + 1,
257+
pageSize: criteria.page.size,
258+
};
259+
setPagination(newPagination);
260+
setSorting(criteria.sort);
261+
};
262+
243263
return (
244264
<AgentConfigListPageLayout>
245265
{isCreateAgentConfigFlyoutOpen ? (
@@ -276,7 +296,7 @@ export const AgentConfigListPage: React.FunctionComponent<{}> = () => {
276296
</EuiFlexGroup>
277297

278298
<EuiSpacer size="m" />
279-
<EuiBasicTable
299+
<EuiBasicTable<AgentConfig>
280300
loading={isLoading}
281301
hasActions={true}
282302
noItemsMessage={
@@ -314,14 +334,8 @@ export const AgentConfigListPage: React.FunctionComponent<{}> = () => {
314334
totalItemCount: agentConfigData ? agentConfigData.total : 0,
315335
pageSizeOptions,
316336
}}
317-
onChange={({ page }: { page: { index: number; size: number } }) => {
318-
const newPagination = {
319-
...pagination,
320-
currentPage: page.index + 1,
321-
pageSize: page.size,
322-
};
323-
setPagination(newPagination);
324-
}}
337+
sorting={{ sort: sorting }}
338+
onChange={onTableChange}
325339
/>
326340
</AgentConfigListPageLayout>
327341
);

x-pack/plugins/ingest_manager/public/applications/ingest_manager/sections/fleet/components/agent_reassign_config_flyout/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ export const AgentReassignConfigFlyout: React.FunctionComponent<Props> = ({ onCl
3636
agent.config_id
3737
);
3838

39-
const agentConfigsRequest = useGetAgentConfigs();
39+
const agentConfigsRequest = useGetAgentConfigs({
40+
page: 1,
41+
perPage: 1000,
42+
});
4043
const agentConfigs = agentConfigsRequest.data ? agentConfigsRequest.data.items : [];
4144

4245
const [isSubmitting, setIsSubmitting] = useState(false);

x-pack/plugins/ingest_manager/server/saved_objects/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ const savedObjectTypes: { [key: string]: SavedObjectsType } = {
119119
},
120120
mappings: {
121121
properties: {
122-
id: { type: 'keyword' },
123-
name: { type: 'text' },
122+
name: { type: 'keyword' },
124123
description: { type: 'text' },
125124
namespace: { type: 'keyword' },
126125
is_default: { type: 'boolean' },

x-pack/plugins/ingest_manager/server/services/agent_config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,12 @@ class AgentConfigService {
143143
soClient: SavedObjectsClientContract,
144144
options: ListWithKuery
145145
): Promise<{ items: AgentConfig[]; total: number; page: number; perPage: number }> {
146-
const { page = 1, perPage = 20, kuery } = options;
146+
const { page = 1, perPage = 20, sortField = 'updated_at', sortOrder = 'desc', kuery } = options;
147147

148148
const agentConfigs = await soClient.find<AgentConfigSOAttributes>({
149149
type: SAVED_OBJECT_TYPE,
150+
sortField,
151+
sortOrder,
150152
page,
151153
perPage,
152154
// To ensure users don't need to know about SO data structure...

x-pack/plugins/ingest_manager/server/services/agents/crud.ts

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,24 @@ import {
1212
AGENT_TYPE_EPHEMERAL,
1313
AGENT_POLLING_THRESHOLD_MS,
1414
} from '../../constants';
15-
import { AgentSOAttributes, Agent, AgentEventSOAttributes } from '../../types';
15+
import { AgentSOAttributes, Agent, AgentEventSOAttributes, ListWithKuery } from '../../types';
1616
import { savedObjectToAgent } from './saved_objects';
1717
import { escapeSearchQueryPhrase } from '../saved_object';
1818

1919
export async function listAgents(
2020
soClient: SavedObjectsClientContract,
21-
options: {
22-
page: number;
23-
perPage: number;
24-
kuery?: string;
21+
options: ListWithKuery & {
2522
showInactive: boolean;
2623
}
2724
) {
28-
const { page, perPage, kuery, showInactive = false } = options;
25+
const {
26+
page = 1,
27+
perPage = 20,
28+
sortField = 'enrolled_at',
29+
sortOrder = 'desc',
30+
kuery,
31+
showInactive = false,
32+
} = options;
2933

3034
const filters = [];
3135

@@ -49,10 +53,11 @@ export async function listAgents(
4953

5054
const { saved_objects, total } = await soClient.find<AgentSOAttributes>({
5155
type: AGENT_SAVED_OBJECT_TYPE,
56+
sortField,
57+
sortOrder,
5258
page,
5359
perPage,
5460
filter: _joinFilters(filters),
55-
..._getSortFields(),
5661
});
5762

5863
const agents: Agent[] = saved_objects.map(savedObjectToAgent);
@@ -137,23 +142,6 @@ export async function deleteAgent(soClient: SavedObjectsClientContract, agentId:
137142
});
138143
}
139144

140-
function _getSortFields(sortOption?: string) {
141-
switch (sortOption) {
142-
case 'ASC':
143-
return {
144-
sortField: 'enrolled_at',
145-
sortOrder: 'ASC',
146-
};
147-
148-
case 'DESC':
149-
default:
150-
return {
151-
sortField: 'enrolled_at',
152-
sortOrder: 'DESC',
153-
};
154-
}
155-
}
156-
157145
function _joinFilters(filters: string[], operator = 'AND') {
158146
return filters.reduce((acc: string | undefined, filter) => {
159147
if (acc) {

0 commit comments

Comments
 (0)