From e009115806d409b0f484fec77e498a003551ec7c Mon Sep 17 00:00:00 2001 From: Jen Huang Date: Wed, 9 Mar 2022 09:28:10 -0800 Subject: [PATCH] [Fleet] Fix ES error during filtering on agent status (#127234) * Remove `body` key from expected ES response, get ES to return hits as integer only, remove expect error annotations * Apply treatment to other areas returning total counts * Lint * Fix tests --- x-pack/plugins/fleet/server/services/agent_policy.ts | 4 ++-- x-pack/plugins/fleet/server/services/agents/crud.test.ts | 2 +- x-pack/plugins/fleet/server/services/agents/crud.ts | 9 ++++++--- .../fleet/server/services/api_keys/enrollment_api_key.ts | 4 ++-- .../fleet/server/services/artifacts/artifacts.test.ts | 4 ++++ .../plugins/fleet/server/services/artifacts/artifacts.ts | 5 +++-- x-pack/plugins/fleet/server/services/artifacts/mocks.ts | 8 +++----- .../plugins/fleet/server/services/fleet_server/index.ts | 6 ++++-- 8 files changed, 25 insertions(+), 17 deletions(-) diff --git a/x-pack/plugins/fleet/server/services/agent_policy.ts b/x-pack/plugins/fleet/server/services/agent_policy.ts index 1784ff190385d..6a191863fc2f7 100644 --- a/x-pack/plugins/fleet/server/services/agent_policy.ts +++ b/x-pack/plugins/fleet/server/services/agent_policy.ts @@ -667,6 +667,7 @@ class AgentPolicyService { const res = await esClient.search({ index: AGENT_POLICY_INDEX, ignore_unavailable: true, + rest_total_hits_as_int: true, body: { query: { term: { @@ -678,8 +679,7 @@ class AgentPolicyService { }, }); - // @ts-expect-error value is number | TotalHits - if (res.body.hits.total.value === 0) { + if ((res.hits.total as number) === 0) { return null; } diff --git a/x-pack/plugins/fleet/server/services/agents/crud.test.ts b/x-pack/plugins/fleet/server/services/agents/crud.test.ts index 6ae8fbd471238..f6988c1c7d280 100644 --- a/x-pack/plugins/fleet/server/services/agents/crud.test.ts +++ b/x-pack/plugins/fleet/server/services/agents/crud.test.ts @@ -30,7 +30,7 @@ describe('Agents CRUD test', () => { function getEsResponse(ids: string[], total: number) { return { hits: { - total: { value: total }, + total, hits: ids.map((id: string) => ({ _id: id, _source: {}, diff --git a/x-pack/plugins/fleet/server/services/agents/crud.ts b/x-pack/plugins/fleet/server/services/agents/crud.ts index 426bddd1bd9b9..03b71ceb496f2 100644 --- a/x-pack/plugins/fleet/server/services/agents/crud.ts +++ b/x-pack/plugins/fleet/server/services/agents/crud.ts @@ -128,6 +128,7 @@ export async function getAgentsByKuery( from, size, track_total_hits: true, + rest_total_hits_as_int: true, ignore_unavailable: true, body: { ...body, @@ -137,7 +138,7 @@ export async function getAgentsByKuery( const res = await queryAgents((page - 1) * perPage, perPage); let agents = res.hits.hits.map(searchHitToAgent); - let total = (res.hits.total as estypes.SearchTotalHits).value; + let total = res.hits.total as number; // filtering for a range on the version string will not work, // nor does filtering on a flattened field (local_metadata), so filter here if (showUpgradeable) { @@ -202,11 +203,13 @@ export async function countInactiveAgents( index: AGENTS_INDEX, size: 0, track_total_hits: true, + rest_total_hits_as_int: true, + filter_path: 'hits.total', ignore_unavailable: true, body, }); - // @ts-expect-error value is number | TotalHits - return res.body.hits.total.value; + + return (res.hits.total as number) || 0; } export async function getAgentById(esClient: ElasticsearchClient, agentId: string) { diff --git a/x-pack/plugins/fleet/server/services/api_keys/enrollment_api_key.ts b/x-pack/plugins/fleet/server/services/api_keys/enrollment_api_key.ts index 03c9e4f979953..ee5d982811e31 100644 --- a/x-pack/plugins/fleet/server/services/api_keys/enrollment_api_key.ts +++ b/x-pack/plugins/fleet/server/services/api_keys/enrollment_api_key.ts @@ -43,6 +43,7 @@ export async function listEnrollmentApiKeys( from: (page - 1) * perPage, size: perPage, track_total_hits: true, + rest_total_hits_as_int: true, ignore_unavailable: true, body: { sort: [{ created_at: { order: 'desc' } }], @@ -55,8 +56,7 @@ export async function listEnrollmentApiKeys( return { items, - // @ts-expect-error value is number | TotalHits - total: res.hits.total.value, + total: res.hits.total as number, page, perPage, }; diff --git a/x-pack/plugins/fleet/server/services/artifacts/artifacts.test.ts b/x-pack/plugins/fleet/server/services/artifacts/artifacts.test.ts index fedb89c1e779b..3f0c5af247eb5 100644 --- a/x-pack/plugins/fleet/server/services/artifacts/artifacts.test.ts +++ b/x-pack/plugins/fleet/server/services/artifacts/artifacts.test.ts @@ -148,6 +148,8 @@ describe('When using the artifacts services', () => { q: '', from: 0, size: 20, + track_total_hits: true, + rest_total_hits_as_int: true, body: { sort: [{ created: { order: 'asc' } }], }, @@ -182,6 +184,8 @@ describe('When using the artifacts services', () => { ignore_unavailable: true, from: 450, size: 50, + track_total_hits: true, + rest_total_hits_as_int: true, body: { sort: [{ identifier: { order: 'desc' } }], }, diff --git a/x-pack/plugins/fleet/server/services/artifacts/artifacts.ts b/x-pack/plugins/fleet/server/services/artifacts/artifacts.ts index 336a03e841266..c6e77301bed25 100644 --- a/x-pack/plugins/fleet/server/services/artifacts/artifacts.ts +++ b/x-pack/plugins/fleet/server/services/artifacts/artifacts.ts @@ -107,6 +107,8 @@ export const listArtifacts = async ( from: (page - 1) * perPage, ignore_unavailable: true, size: perPage, + track_total_hits: true, + rest_total_hits_as_int: true, body: { sort: [{ [sortField]: { order: sortOrder } }], }, @@ -117,8 +119,7 @@ export const listArtifacts = async ( items: searchResult.hits.hits.map((hit) => esSearchHitToArtifact(hit)), page, perPage, - // @ts-expect-error doesn't handle total as number - total: searchResult.hits.total.value, + total: searchResult.hits.total as number, }; } catch (e) { throw new ArtifactsElasticsearchError(e); diff --git a/x-pack/plugins/fleet/server/services/artifacts/mocks.ts b/x-pack/plugins/fleet/server/services/artifacts/mocks.ts index b122d3343dacd..9eee24afe08b2 100644 --- a/x-pack/plugins/fleet/server/services/artifacts/mocks.ts +++ b/x-pack/plugins/fleet/server/services/artifacts/mocks.ts @@ -102,7 +102,8 @@ export const generateArtifactEsGetSingleHitMock = ( export const generateArtifactEsSearchResultHitsMock = (): ESSearchResponse< ArtifactElasticsearchProperties, - {} + {}, + { restTotalHitsAsInt: true } > => { return { took: 0, @@ -114,10 +115,7 @@ export const generateArtifactEsSearchResultHitsMock = (): ESSearchResponse< failed: 0, }, hits: { - total: { - value: 1, - relation: 'eq', - }, + total: 1, max_score: 2, hits: [generateArtifactEsGetSingleHitMock()], }, diff --git a/x-pack/plugins/fleet/server/services/fleet_server/index.ts b/x-pack/plugins/fleet/server/services/fleet_server/index.ts index 0ac47df1bfdaf..43403bf3c9460 100644 --- a/x-pack/plugins/fleet/server/services/fleet_server/index.ts +++ b/x-pack/plugins/fleet/server/services/fleet_server/index.ts @@ -16,8 +16,10 @@ export async function hasFleetServers(esClient: ElasticsearchClient) { const res = await esClient.search<{}, {}>({ index: FLEET_SERVER_SERVERS_INDEX, ignore_unavailable: true, + filter_path: 'hits.total', + track_total_hits: true, + rest_total_hits_as_int: true, }); - // @ts-expect-error value is number | TotalHits - return res.hits.total.value > 0; + return (res.hits.total as number) > 0; }