Skip to content

Commit e990c47

Browse files
committed
EMT-146: review comments and refactor the code
1 parent 62eba60 commit e990c47

File tree

7 files changed

+25
-39
lines changed

7 files changed

+25
-39
lines changed

x-pack/plugins/endpoint/server/mocks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const createMockMetadataIndexPatternRetriever = () => {
3636
*/
3737
export const createMockAgentService = (): jest.Mocked<AgentService> => {
3838
return {
39-
getAgentStatus: jest.fn(),
39+
getAgentStatusById: jest.fn(),
4040
};
4141
};
4242

x-pack/plugins/endpoint/server/routes/metadata/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ async function enrichHostMetadata(
167167
): Promise<HostInfo> {
168168
let hostStatus = HostStatus.ERROR;
169169
try {
170-
const status = await metadataRequestContext.endpointAppContext.agentService.getAgentStatus(
171-
metadataRequestContext.requestHandlerContext,
170+
const status = await metadataRequestContext.endpointAppContext.agentService.getAgentStatusById(
171+
metadataRequestContext.requestHandlerContext.core.savedObjects.client,
172172
hostMetadata.elastic.agent.id
173173
);
174174
hostStatus = HOST_STATUS_MAPPING.get(status) || HostStatus.ERROR;

x-pack/plugins/endpoint/server/routes/metadata/metadata.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describe('test endpoint route', () => {
8888
[routeConfig, routeHandler] = routerMock.post.mock.calls.find(([{ path }]) =>
8989
path.startsWith('/api/endpoint/metadata')
9090
)!;
91-
mockAgentService.getAgentStatus = jest.fn().mockReturnValue('error');
91+
mockAgentService.getAgentStatusById = jest.fn().mockReturnValue('error');
9292
await routeHandler(
9393
createRouteHandlerContext(mockScopedClient, mockSavedObjectClient),
9494
mockRequest,
@@ -119,7 +119,7 @@ describe('test endpoint route', () => {
119119
},
120120
});
121121

122-
mockAgentService.getAgentStatus = jest.fn().mockReturnValue('error');
122+
mockAgentService.getAgentStatusById = jest.fn().mockReturnValue('error');
123123
mockScopedClient.callAsCurrentUser.mockImplementationOnce(() =>
124124
Promise.resolve((data as unknown) as SearchResponse<HostMetadata>)
125125
);
@@ -162,7 +162,7 @@ describe('test endpoint route', () => {
162162
},
163163
});
164164

165-
mockAgentService.getAgentStatus = jest.fn().mockReturnValue('error');
165+
mockAgentService.getAgentStatusById = jest.fn().mockReturnValue('error');
166166
mockScopedClient.callAsCurrentUser.mockImplementationOnce(() =>
167167
Promise.resolve((data as unknown) as SearchResponse<HostMetadata>)
168168
);
@@ -225,7 +225,7 @@ describe('test endpoint route', () => {
225225
},
226226
})
227227
);
228-
mockAgentService.getAgentStatus = jest.fn().mockReturnValue('error');
228+
mockAgentService.getAgentStatusById = jest.fn().mockReturnValue('error');
229229
[routeConfig, routeHandler] = routerMock.get.mock.calls.find(([{ path }]) =>
230230
path.startsWith('/api/endpoint/metadata')
231231
)!;
@@ -249,7 +249,7 @@ describe('test endpoint route', () => {
249249
const response: SearchResponse<HostMetadata> = (data as unknown) as SearchResponse<
250250
HostMetadata
251251
>;
252-
mockAgentService.getAgentStatus = jest.fn().mockReturnValue('online');
252+
mockAgentService.getAgentStatusById = jest.fn().mockReturnValue('online');
253253
mockScopedClient.callAsCurrentUser.mockImplementationOnce(() => Promise.resolve(response));
254254
[routeConfig, routeHandler] = routerMock.get.mock.calls.find(([{ path }]) =>
255255
path.startsWith('/api/endpoint/metadata')
@@ -276,7 +276,7 @@ describe('test endpoint route', () => {
276276
const response: SearchResponse<HostMetadata> = (data as unknown) as SearchResponse<
277277
HostMetadata
278278
>;
279-
mockAgentService.getAgentStatus = jest.fn().mockImplementation(() => {
279+
mockAgentService.getAgentStatusById = jest.fn().mockImplementation(() => {
280280
throw Boom.notFound('Agent not found');
281281
});
282282
mockScopedClient.callAsCurrentUser.mockImplementationOnce(() => Promise.resolve(response));
@@ -304,7 +304,7 @@ describe('test endpoint route', () => {
304304
const response: SearchResponse<HostMetadata> = (data as unknown) as SearchResponse<
305305
HostMetadata
306306
>;
307-
mockAgentService.getAgentStatus = jest.fn().mockReturnValue('warning');
307+
mockAgentService.getAgentStatusById = jest.fn().mockReturnValue('warning');
308308
mockScopedClient.callAsCurrentUser.mockImplementationOnce(() => Promise.resolve(response));
309309
[routeConfig, routeHandler] = routerMock.get.mock.calls.find(([{ path }]) =>
310310
path.startsWith('/api/endpoint/metadata')

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@
33
* or more contributor license agreements. Licensed under the Elastic License;
44
* you may not use this file except in compliance with the Elastic License.
55
*/
6-
import { RequestHandlerContext } from 'kibana/server';
6+
import { SavedObjectsClientContract } from 'kibana/server';
77
import { AgentStatus } from './models';
88

99
export * from './models';
1010
export * from './rest_spec';
1111

1212
export interface AgentService {
13-
getAgentStatus(
14-
requestHandlerContext: RequestHandlerContext,
15-
agentId: string
16-
): Promise<AgentStatus>;
13+
getAgentStatusById(soClient: SavedObjectsClientContract, agentId: string): Promise<AgentStatus>;
1714
}
1815

1916
export interface IngestManagerConfigType {

x-pack/plugins/ingest_manager/server/plugin.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import {
4545
ESIndexPatternService,
4646
ESIndexPatternSavedObjectService,
4747
} from './services';
48-
import { createAgentService } from './services/agent_service';
48+
import { getAgentStatusById } from './services/agents';
4949

5050
/**
5151
* Describes public IngestManager plugin contract returned at the `setup` stage.
@@ -150,7 +150,9 @@ export class IngestManagerPlugin implements Plugin<IngestManagerSetupContract> {
150150
}
151151
return deepFreeze({
152152
esIndexPatternService: new ESIndexPatternSavedObjectService(),
153-
agentService: createAgentService(),
153+
agentService: {
154+
getAgentStatusById,
155+
},
154156
});
155157
}
156158

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

Lines changed: 0 additions & 21 deletions
This file was deleted.

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

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

77
import { SavedObjectsClientContract } from 'src/core/server';
8-
import { listAgents } from './crud';
8+
import { getAgent, listAgents } from './crud';
99
import { AGENT_EVENT_SAVED_OBJECT_TYPE } from '../../constants';
1010
import { AgentStatus, Agent } from '../../types';
1111

@@ -17,6 +17,14 @@ import {
1717
} from '../../constants';
1818
import { AgentStatusKueryHelper } from '../../../common/services';
1919

20+
export async function getAgentStatusById(
21+
soClient: SavedObjectsClientContract,
22+
agentId: string
23+
): Promise<AgentStatus> {
24+
const agent = await getAgent(soClient, agentId);
25+
return getAgentStatus(agent);
26+
}
27+
2028
export function getAgentStatus(agent: Agent, now: number = Date.now()): AgentStatus {
2129
const { type, last_checkin: lastCheckIn } = agent;
2230
const msLastCheckIn = new Date(lastCheckIn || 0).getTime();

0 commit comments

Comments
 (0)