Skip to content

Commit

Permalink
Including missing agent results is optional & off by default
Browse files Browse the repository at this point in the history
  • Loading branch information
John Schulz committed Mar 16, 2021
1 parent cdd501f commit f02ef35
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 29 deletions.
11 changes: 4 additions & 7 deletions x-pack/plugins/fleet/server/routes/agent/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type { TypeOf } from '@kbn/config-schema';
import { AbortController } from 'abort-controller';

import type {
Agent,
GetAgentsResponse,
GetOneAgentResponse,
GetOneAgentEventsResponse,
Expand Down Expand Up @@ -268,12 +267,10 @@ export const getAgentsHandler: RequestHandler<
: 0;

const body: GetAgentsResponse = {
list: agents
.filter((agent): agent is Agent => !!agent)
.map((agent) => ({
...agent,
status: AgentService.getAgentStatus(agent),
})),
list: agents.map((agent) => ({
...agent,
status: AgentService.getAgentStatus(agent),
})),
total,
totalInactive,
page,
Expand Down
27 changes: 13 additions & 14 deletions x-pack/plugins/fleet/server/services/agents/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export async function getAgentsByKuery(
showInactive: boolean;
}
): Promise<{
agents: Array<Agent | undefined>;
agents: Agent[];
total: number;
page: number;
perPage: number;
Expand Down Expand Up @@ -137,7 +137,7 @@ export async function getAgentsByKuery(
}

return {
agents: agents || [],
agents,
total: agents.length,
page,
perPage,
Expand All @@ -156,7 +156,7 @@ export async function getAllAgentsByKuery(
const res = await getAgentsByKuery(esClient, { ...options, page: 1, perPage: SO_SEARCH_LIMIT });

return {
agents: res.agents.filter((agent): agent is Agent => !!agent),
agents: res.agents,
total: res.total,
};
}
Expand Down Expand Up @@ -220,17 +220,16 @@ async function getAgentDocuments(

export async function getAgentsById(
esClient: ElasticsearchClient,
agentIds: string[]
): Promise<
Agent[]
// | GetResponse<FleetServerAgent>
> {
const agentDocs = await getAgentDocuments(esClient, agentIds);
const results = agentDocs
.map((doc) => searchHitToAgent(doc))
.filter((agent): agent is Agent => !!agent);

return results;
agentIds: string[],
options: { includeMissing?: boolean } = { includeMissing: false }
): Promise<Agent[]> {
const allDocs = await getAgentDocuments(esClient, agentIds);
const agentDocs = options.includeMissing
? allDocs
: allDocs.filter((res) => res._id && res._source);
const agents = agentDocs.map((doc) => searchHitToAgent(doc));

return agents;
}

export async function getAgentByAccessAPIKeyId(
Expand Down
9 changes: 1 addition & 8 deletions x-pack/plugins/fleet/server/services/agents/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@ type FleetServerAgentESResponse =
| GetResponse<FleetServerAgent>
| SearchResponse<FleetServerAgent>['hits']['hits'][0];

export function searchHitToAgent(hit: FleetServerAgentESResponse): Agent | undefined {
if (!(hit._id && hit._source)) {
return;
}
if ('found' in hit && hit.found === false) {
return;
}

export function searchHitToAgent(hit: FleetServerAgentESResponse): Agent {
return {
id: hit._id,
...hit._source,
Expand Down

0 comments on commit f02ef35

Please sign in to comment.