-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Infra] Change host count KPI query #188950
Merged
jennypavlova
merged 13 commits into
elastic:main
from
jennypavlova:188757-infra-change-host-count-kpi-query
Jul 25, 2024
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2af9b05
Add endpoint that returns the hosts count
jennypavlova 8f5822d
Rename host to asset type and add param
jennypavlova 1d89159
Update test
jennypavlova 423f5fb
Move and rename endpoint to asset count and add params validation
jennypavlova 0078b7c
Rename to asset count
jennypavlova d093d69
Change quert types
jennypavlova d2e537c
Remove unused kquery filter
jennypavlova c3e0d2c
Update README
jennypavlova 03b1db2
Update README
jennypavlova 9c8e4af
Merge branch 'main' into 188757-infra-change-host-count-kpi-query
jennypavlova 1375ddb
CR comments
jennypavlova 9063842
Merge branch 'main' into 188757-infra-change-host-count-kpi-query
jennypavlova befee95
Move constants
jennypavlova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
x-pack/plugins/observability_solution/infra/common/http_api/asset_count_api.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { dateRt } from '@kbn/io-ts-utils'; | ||
import * as rt from 'io-ts'; | ||
|
||
const AssetTypeRT = rt.type({ | ||
assetType: rt.literal('host'), | ||
}); | ||
|
||
export const GetInfraAssetCountRequestBodyPayloadRT = rt.intersection([ | ||
rt.partial({ | ||
query: rt.UnknownRecord, | ||
}), | ||
rt.type({ | ||
sourceId: rt.string, | ||
from: dateRt, | ||
to: dateRt, | ||
}), | ||
]); | ||
|
||
export const GetInfraAssetCountRequestParamsPayloadRT = AssetTypeRT; | ||
|
||
export const GetInfraAssetCountResponsePayloadRT = rt.intersection([ | ||
AssetTypeRT, | ||
rt.type({ | ||
count: rt.number, | ||
}), | ||
]); | ||
|
||
export type GetInfraAssetCountRequestParamsPayload = rt.TypeOf< | ||
typeof GetInfraAssetCountRequestParamsPayloadRT | ||
>; | ||
export type GetInfraAssetCountRequestBodyPayload = Omit< | ||
rt.TypeOf<typeof GetInfraAssetCountRequestBodyPayloadRT>, | ||
'from' | 'to' | ||
> & { | ||
from: string; | ||
to: string; | ||
}; | ||
|
||
export type GetInfraAssetCountResponsePayload = rt.TypeOf< | ||
typeof GetInfraAssetCountResponsePayloadRT | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
*/ | ||
|
||
export const BUCKET_KEY = 'host.name'; | ||
export const EVENT_MODULE = 'event.module'; | ||
export const METRICSET_MODULE = 'metricset.module'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps these could be in the constants in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, thanks ✅ |
||
export const METADATA_AGGREGATION_NAME = 'metadata'; | ||
export const FILTER_AGGREGATION_SUB_AGG_NAME = 'result'; | ||
|
||
|
65 changes: 65 additions & 0 deletions
65
x-pack/plugins/observability_solution/infra/server/routes/infra/lib/host/get_hosts_count.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { rangeQuery, termQuery } from '@kbn/observability-plugin/server'; | ||
import { BoolQuery } from '@kbn/es-query'; | ||
import { InfraMetricsClient } from '../../../../lib/helpers/get_infra_metrics_client'; | ||
import { HOST_NAME_FIELD } from '../../../../../common/constants'; | ||
import { EVENT_MODULE, METRICSET_MODULE } from '../constants'; | ||
|
||
export async function getHostsCount({ | ||
infraMetricsClient, | ||
query, | ||
from, | ||
to, | ||
}: { | ||
infraMetricsClient: InfraMetricsClient; | ||
query?: BoolQuery; | ||
from: string; | ||
to: string; | ||
}) { | ||
const queryFilter = query?.filter ?? []; | ||
const queryBool = query ?? {}; | ||
|
||
const params = { | ||
allow_no_indices: true, | ||
ignore_unavailable: true, | ||
body: { | ||
size: 0, | ||
track_total_hits: false, | ||
query: { | ||
bool: { | ||
...queryBool, | ||
filter: [ | ||
...queryFilter, | ||
...rangeQuery(new Date(from).getTime(), new Date(to).getTime()), | ||
{ | ||
bool: { | ||
should: [ | ||
...termQuery(EVENT_MODULE, 'system'), | ||
...termQuery(METRICSET_MODULE, 'system'), | ||
], | ||
minimum_should_match: 1, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
aggs: { | ||
count: { | ||
cardinality: { | ||
field: HOST_NAME_FIELD, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const result = await infraMetricsClient.search(params); | ||
|
||
return result.aggregations?.count.value ?? 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we this could be a GET request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to pass the query, filters, time range, etc. so we can even expand more the body if it is a POST. I am not sure how much will those params extend in the future so I was thinking that having the consistency with the other endpoints with similar functions can help here. But I don't have a strong opinion on that. Do you think it's better to have query params instead of the request body and convert to GET?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GET operation is preferable in this case, since nothing is being created. Infra uses
POST
for historical reasons, but most of them should beGET
instead. The complicated part is on how to pass the filters in the query params.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed I created an issue to investigate the option to use GET instead of POST in our APIs #189170 There are some benefits and concerns described there and it would be nice also to keep them consistent and have GET requests where possible ( I totally agree that it will be better but we still need to check if the url limitations affect us) So this change won't be part of this PR