Skip to content

Commit

Permalink
EMT-146: review comments and refactor the code
Browse files Browse the repository at this point in the history
  • Loading branch information
nnamdifrankie committed Apr 20, 2020
1 parent 62eba60 commit e990c47
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 39 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/endpoint/server/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const createMockMetadataIndexPatternRetriever = () => {
*/
export const createMockAgentService = (): jest.Mocked<AgentService> => {
return {
getAgentStatus: jest.fn(),
getAgentStatusById: jest.fn(),
};
};

Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/endpoint/server/routes/metadata/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ async function enrichHostMetadata(
): Promise<HostInfo> {
let hostStatus = HostStatus.ERROR;
try {
const status = await metadataRequestContext.endpointAppContext.agentService.getAgentStatus(
metadataRequestContext.requestHandlerContext,
const status = await metadataRequestContext.endpointAppContext.agentService.getAgentStatusById(
metadataRequestContext.requestHandlerContext.core.savedObjects.client,
hostMetadata.elastic.agent.id
);
hostStatus = HOST_STATUS_MAPPING.get(status) || HostStatus.ERROR;
Expand Down
14 changes: 7 additions & 7 deletions x-pack/plugins/endpoint/server/routes/metadata/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('test endpoint route', () => {
[routeConfig, routeHandler] = routerMock.post.mock.calls.find(([{ path }]) =>
path.startsWith('/api/endpoint/metadata')
)!;
mockAgentService.getAgentStatus = jest.fn().mockReturnValue('error');
mockAgentService.getAgentStatusById = jest.fn().mockReturnValue('error');
await routeHandler(
createRouteHandlerContext(mockScopedClient, mockSavedObjectClient),
mockRequest,
Expand Down Expand Up @@ -119,7 +119,7 @@ describe('test endpoint route', () => {
},
});

mockAgentService.getAgentStatus = jest.fn().mockReturnValue('error');
mockAgentService.getAgentStatusById = jest.fn().mockReturnValue('error');
mockScopedClient.callAsCurrentUser.mockImplementationOnce(() =>
Promise.resolve((data as unknown) as SearchResponse<HostMetadata>)
);
Expand Down Expand Up @@ -162,7 +162,7 @@ describe('test endpoint route', () => {
},
});

mockAgentService.getAgentStatus = jest.fn().mockReturnValue('error');
mockAgentService.getAgentStatusById = jest.fn().mockReturnValue('error');
mockScopedClient.callAsCurrentUser.mockImplementationOnce(() =>
Promise.resolve((data as unknown) as SearchResponse<HostMetadata>)
);
Expand Down Expand Up @@ -225,7 +225,7 @@ describe('test endpoint route', () => {
},
})
);
mockAgentService.getAgentStatus = jest.fn().mockReturnValue('error');
mockAgentService.getAgentStatusById = jest.fn().mockReturnValue('error');
[routeConfig, routeHandler] = routerMock.get.mock.calls.find(([{ path }]) =>
path.startsWith('/api/endpoint/metadata')
)!;
Expand All @@ -249,7 +249,7 @@ describe('test endpoint route', () => {
const response: SearchResponse<HostMetadata> = (data as unknown) as SearchResponse<
HostMetadata
>;
mockAgentService.getAgentStatus = jest.fn().mockReturnValue('online');
mockAgentService.getAgentStatusById = jest.fn().mockReturnValue('online');
mockScopedClient.callAsCurrentUser.mockImplementationOnce(() => Promise.resolve(response));
[routeConfig, routeHandler] = routerMock.get.mock.calls.find(([{ path }]) =>
path.startsWith('/api/endpoint/metadata')
Expand All @@ -276,7 +276,7 @@ describe('test endpoint route', () => {
const response: SearchResponse<HostMetadata> = (data as unknown) as SearchResponse<
HostMetadata
>;
mockAgentService.getAgentStatus = jest.fn().mockImplementation(() => {
mockAgentService.getAgentStatusById = jest.fn().mockImplementation(() => {
throw Boom.notFound('Agent not found');
});
mockScopedClient.callAsCurrentUser.mockImplementationOnce(() => Promise.resolve(response));
Expand Down Expand Up @@ -304,7 +304,7 @@ describe('test endpoint route', () => {
const response: SearchResponse<HostMetadata> = (data as unknown) as SearchResponse<
HostMetadata
>;
mockAgentService.getAgentStatus = jest.fn().mockReturnValue('warning');
mockAgentService.getAgentStatusById = jest.fn().mockReturnValue('warning');
mockScopedClient.callAsCurrentUser.mockImplementationOnce(() => Promise.resolve(response));
[routeConfig, routeHandler] = routerMock.get.mock.calls.find(([{ path }]) =>
path.startsWith('/api/endpoint/metadata')
Expand Down
7 changes: 2 additions & 5 deletions x-pack/plugins/ingest_manager/common/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { RequestHandlerContext } from 'kibana/server';
import { SavedObjectsClientContract } from 'kibana/server';
import { AgentStatus } from './models';

export * from './models';
export * from './rest_spec';

export interface AgentService {
getAgentStatus(
requestHandlerContext: RequestHandlerContext,
agentId: string
): Promise<AgentStatus>;
getAgentStatusById(soClient: SavedObjectsClientContract, agentId: string): Promise<AgentStatus>;
}

export interface IngestManagerConfigType {
Expand Down
6 changes: 4 additions & 2 deletions x-pack/plugins/ingest_manager/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
ESIndexPatternService,
ESIndexPatternSavedObjectService,
} from './services';
import { createAgentService } from './services/agent_service';
import { getAgentStatusById } from './services/agents';

/**
* Describes public IngestManager plugin contract returned at the `setup` stage.
Expand Down Expand Up @@ -150,7 +150,9 @@ export class IngestManagerPlugin implements Plugin<IngestManagerSetupContract> {
}
return deepFreeze({
esIndexPatternService: new ESIndexPatternSavedObjectService(),
agentService: createAgentService(),
agentService: {
getAgentStatusById,
},
});
}

Expand Down
21 changes: 0 additions & 21 deletions x-pack/plugins/ingest_manager/server/services/agent_service.ts

This file was deleted.

10 changes: 9 additions & 1 deletion x-pack/plugins/ingest_manager/server/services/agents/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { SavedObjectsClientContract } from 'src/core/server';
import { listAgents } from './crud';
import { getAgent, listAgents } from './crud';
import { AGENT_EVENT_SAVED_OBJECT_TYPE } from '../../constants';
import { AgentStatus, Agent } from '../../types';

Expand All @@ -17,6 +17,14 @@ import {
} from '../../constants';
import { AgentStatusKueryHelper } from '../../../common/services';

export async function getAgentStatusById(
soClient: SavedObjectsClientContract,
agentId: string
): Promise<AgentStatus> {
const agent = await getAgent(soClient, agentId);
return getAgentStatus(agent);
}

export function getAgentStatus(agent: Agent, now: number = Date.now()): AgentStatus {
const { type, last_checkin: lastCheckIn } = agent;
const msLastCheckIn = new Date(lastCheckIn || 0).getTime();
Expand Down

0 comments on commit e990c47

Please sign in to comment.