added secondary sort if default sort agents (#135548)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Julia Bardi 2022-07-04 14:26:15 +02:00 committed by GitHub
parent 8e607ccdbc
commit ffaa9e4a6b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 7 deletions

View file

@ -193,6 +193,29 @@ describe('Agents CRUD test', () => {
total: 7,
});
});
it('should pass secondary sort for default sort', async () => {
searchMock.mockImplementationOnce(() => Promise.resolve(getEsResponse(['1', '2'], 2)));
await getAgentsByKuery(esClientMock, {
showInactive: false,
});
expect(searchMock.mock.calls[searchMock.mock.calls.length - 1][0].body.sort).toEqual([
{ enrolled_at: { order: 'desc' } },
{ 'local_metadata.host.hostname.keyword': { order: 'asc' } },
]);
});
it('should not pass secondary sort for non-default sort', async () => {
searchMock.mockImplementationOnce(() => Promise.resolve(getEsResponse(['1', '2'], 2)));
await getAgentsByKuery(esClientMock, {
showInactive: false,
sortField: 'policy_id',
});
expect(searchMock.mock.calls[searchMock.mock.calls.length - 1][0].body.sort).toEqual([
{ policy_id: { order: 'desc' } },
]);
});
});
describe('processAgentsInBatches', () => {

View file

@ -233,6 +233,11 @@ export async function getAgentsByKuery(
const kueryNode = _joinFilters(filters);
const body = kueryNode ? { query: toElasticsearchQuery(kueryNode) } : {};
const isDefaultSort = sortField === 'enrolled_at' && sortOrder === 'desc';
// if using default sorting (enrolled_at), adding a secondary sort on hostname, so that the results are not changing randomly in case many agents were enrolled at the same time
const secondarySort: estypes.Sort = isDefaultSort
? [{ 'local_metadata.host.hostname.keyword': { order: 'asc' } }]
: [];
const queryAgents = async (from: number, size: number) =>
esClient.search<FleetServerAgent, {}>({
index: AGENTS_INDEX,
@ -243,7 +248,7 @@ export async function getAgentsByKuery(
ignore_unavailable: true,
body: {
...body,
sort: [{ [sortField]: { order: sortOrder } }],
sort: [{ [sortField]: { order: sortOrder } }, ...secondarySort],
},
});
const res = await queryAgents((page - 1) * perPage, perPage);

View file

@ -83,5 +83,23 @@ export default function ({ getService }: FtrProviderContext) {
'agent1',
]);
});
it('should return agents in enrolled_at and hostname order when default sort options and same enrollment time', async () => {
let { body: apiResponse } = await supertest.get(`/api/fleet/agents`).expect(200);
expect(apiResponse.items.map((agent: { id: string }) => agent.id)).to.eql([
'agent4',
'agent1',
'agent2',
'agent3',
]);
({ body: apiResponse } = await supertest.get(`/api/fleet/agents`).expect(200));
expect(apiResponse.items.map((agent: { id: string }) => agent.id)).to.eql([
'agent4',
'agent1',
'agent2',
'agent3',
]);
});
});
}

View file

@ -8,7 +8,7 @@
"active": true,
"policy_id": "policy1",
"type": "PERMANENT",
"local_metadata": {},
"local_metadata": { "host": {"hostname": "host1"}},
"user_provided_metadata": {},
"enrolled_at": "2022-06-21T12:14:25Z",
"last_checkin": "2022-06-27T12:26:29Z",
@ -27,9 +27,9 @@
"active": true,
"policy_id": "policy1",
"type": "PERMANENT",
"local_metadata": {},
"local_metadata": { "host": {"hostname": "host2"}},
"user_provided_metadata": {},
"enrolled_at": "2022-06-21T12:15:25Z",
"enrolled_at": "2022-06-21T12:14:25Z",
"last_checkin": "2022-06-27T12:27:29Z",
"tags": ["existingTag"]
}
@ -46,9 +46,10 @@
"active": true,
"policy_id": "policy1",
"type": "PERMANENT",
"local_metadata": {},
"local_metadata": { "host": {"hostname": "host3"}},
"user_provided_metadata": {},
"enrolled_at": "2022-06-21T12:16:25Z",
"enrolled_at": "2022-06-21T12:14:25Z",
"last_checkin": "2022-06-27T12:28:29Z",
"last_checkin": "2022-06-27T12:28:29Z",
"tags": ["tag1"]
}
@ -65,7 +66,7 @@
"active": true,
"policy_id": "policy1",
"type": "PERMANENT",
"local_metadata": {},
"local_metadata": { "host": {"hostname": "host4"}},
"user_provided_metadata": {},
"enrolled_at": "2022-06-21T12:17:25Z",
"last_checkin": "2022-06-27T12:29:29Z"