Skip to content

Commit

Permalink
[Fleet] Fix ES error during filtering on agent status (#127234)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jen-huang authored Mar 9, 2022
1 parent 5e8d66e commit e009115
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 17 deletions.
4 changes: 2 additions & 2 deletions x-pack/plugins/fleet/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/server/services/agents/crud.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
Expand Down
9 changes: 6 additions & 3 deletions x-pack/plugins/fleet/server/services/agents/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' } }],
Expand All @@ -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,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' } }],
},
Expand Down Expand Up @@ -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' } }],
},
Expand Down
5 changes: 3 additions & 2 deletions x-pack/plugins/fleet/server/services/artifacts/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }],
},
Expand All @@ -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);
Expand Down
8 changes: 3 additions & 5 deletions x-pack/plugins/fleet/server/services/artifacts/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ export const generateArtifactEsGetSingleHitMock = (

export const generateArtifactEsSearchResultHitsMock = (): ESSearchResponse<
ArtifactElasticsearchProperties,
{}
{},
{ restTotalHitsAsInt: true }
> => {
return {
took: 0,
Expand All @@ -114,10 +115,7 @@ export const generateArtifactEsSearchResultHitsMock = (): ESSearchResponse<
failed: 0,
},
hits: {
total: {
value: 1,
relation: 'eq',
},
total: 1,
max_score: 2,
hits: [generateArtifactEsGetSingleHitMock()],
},
Expand Down
6 changes: 4 additions & 2 deletions x-pack/plugins/fleet/server/services/fleet_server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit e009115

Please sign in to comment.