Skip to content

Commit 0e8deec

Browse files
committed
[Fleet] Fix agent status count
1 parent 0c6bec1 commit 0e8deec

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Boom from '@hapi/boom';
99
import type { SearchResponse, MGetResponse, GetResponse } from 'elasticsearch';
1010
import type { SavedObjectsClientContract, ElasticsearchClient } from 'src/core/server';
1111

12+
import type { ESSearchResponse } from '../../../../../../typings/elasticsearch';
1213
import type { AgentSOAttributes, Agent, ListWithKuery } from '../../types';
1314
import { appContextService, agentPolicyService } from '../../services';
1415
import type { FleetServerAgent } from '../../../common';
@@ -118,7 +119,7 @@ export async function getAgentsByKuery(
118119

119120
const kueryNode = _joinFilters(filters);
120121
const body = kueryNode ? { query: esKuery.toElasticsearchQuery(kueryNode) } : {};
121-
const res = await esClient.search<SearchResponse<FleetServerAgent>>({
122+
const res = await esClient.search<ESSearchResponse<FleetServerAgent, {}>>({
122123
index: AGENTS_INDEX,
123124
from: (page - 1) * perPage,
124125
size: perPage,
@@ -138,7 +139,7 @@ export async function getAgentsByKuery(
138139

139140
return {
140141
agents,
141-
total: agents.length,
142+
total: res.body.hits.total.value,
142143
page,
143144
perPage,
144145
};

x-pack/plugins/fleet/server/services/agents/helpers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
import type { GetResponse, SearchResponse } from 'elasticsearch';
99

10+
import type { ESSearchHit } from '../../../../../../typings/elasticsearch';
1011
import type { Agent, AgentSOAttributes, FleetServerAgent } from '../../types';
1112

1213
type FleetServerAgentESResponse =
1314
| GetResponse<FleetServerAgent>
15+
| ESSearchHit<FleetServerAgent>
1416
| SearchResponse<FleetServerAgent>['hits']['hits'][0];
1517

1618
export function searchHitToAgent(hit: FleetServerAgentESResponse): Agent {
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import expect from '@kbn/expect';
9+
10+
import { FtrProviderContext } from '../../../api_integration/ftr_provider_context';
11+
import { AGENTS_INDEX } from '../../../../plugins/fleet/common';
12+
13+
export default function ({ getService }: FtrProviderContext) {
14+
const es = getService('es');
15+
const esArchiver = getService('esArchiver');
16+
const supertest = getService('supertest');
17+
18+
describe('fleet_agents_status', () => {
19+
before(async () => {
20+
await esArchiver.loadIfNeeded('fleet/agents');
21+
// 2 agents online
22+
await es.update({
23+
id: 'agent1',
24+
refresh: 'wait_for',
25+
index: AGENTS_INDEX,
26+
body: {
27+
doc: {
28+
last_checkin: new Date().toISOString(),
29+
},
30+
},
31+
});
32+
await es.update({
33+
id: 'agent2',
34+
refresh: 'wait_for',
35+
index: AGENTS_INDEX,
36+
body: {
37+
doc: {
38+
last_checkin: new Date().toISOString(),
39+
},
40+
},
41+
});
42+
// 1 agents offline
43+
await es.update({
44+
id: 'agent3',
45+
refresh: 'wait_for',
46+
index: AGENTS_INDEX,
47+
body: {
48+
doc: {
49+
last_checkin: new Date(Date.now() - 1000 * 60 * 60 * 60 * 10).toISOString(),
50+
},
51+
},
52+
});
53+
// 1 agent upgrading
54+
await es.update({
55+
id: 'agent4',
56+
refresh: 'wait_for',
57+
index: AGENTS_INDEX,
58+
body: {
59+
doc: {
60+
last_checkin: new Date().toISOString(),
61+
updgrade_started_at: new Date().toISOString(),
62+
},
63+
},
64+
});
65+
});
66+
after(async () => {
67+
await esArchiver.unload('fleet/agents');
68+
});
69+
70+
it('should return the status of agents', async () => {
71+
const { body: apiResponse } = await supertest.get(`/api/fleet/agent-status`).expect(200);
72+
73+
expect(apiResponse).to.eql({
74+
results: {
75+
events: 0,
76+
total: 4,
77+
online: 2,
78+
error: 0,
79+
offline: 1,
80+
updating: 1,
81+
other: 1,
82+
},
83+
});
84+
});
85+
});
86+
}

x-pack/test/fleet_api_integration/apis/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default function ({ loadTestFile }) {
2525
loadTestFile(require.resolve('./agents/actions'));
2626
loadTestFile(require.resolve('./agents/upgrade'));
2727
loadTestFile(require.resolve('./agents/reassign'));
28+
loadTestFile(require.resolve('./agents/status'));
2829

2930
// Enrollment API keys
3031
loadTestFile(require.resolve('./enrollment_api_keys/crud'));

0 commit comments

Comments
 (0)