forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Infra] Change host count KPI query (elastic#188950)
Closes elastic#188757 ## Summary This PR adds an endpoint to get the hosts (monitored by the system integration) count. Currently, it supports only hosts but it can be extended to other asset types (if/when needed). So the endpoint ( **POST /api/infra/{assetType}/count** ) supports only 'host' as `{assetType}`.⚠️ This PR adds only the endpoint - it is not used yet! To avoid having different host counts and results shown on the UI this PR is not updating the hook responsible for the request because currently the hosts shown in the table are not filtered by the system integration (showing the filtered result of this endpoint can lead to inconsistencies between the count and the results shown in the table) Once [elastic#188756](elastic#188756) and [elastic#188752](elastic#188752) are done we can use this endpoint. ## Testing It can't be tested in the UI so we can test it: <details> <summary>Using curl:</summary> ```bash curl --location -u elastic:changeme 'http://0.0.0.0:5601/ftw/api/infra/host/count' \ --header 'kbn-xsrf: xxxx' \ --header 'Content-Type: application/json' \ --data '{ "query": { "bool": { "must": [], "filter": [], "should": [], "must_not": [] } }, "from": "2024-07-23T11:34:11.640Z", "to": "2024-07-23T11:49:11.640Z", "sourceId": "default" }' ``` </details> In case of testing with oblt replace the `elastic:changeme` with your user and password
- Loading branch information
1 parent
bd3032b
commit fd8b7f6
Showing
9 changed files
with
336 additions
and
12 deletions.
There are no files selected for viewing
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
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
64 changes: 64 additions & 0 deletions
64
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,64 @@ | ||
/* | ||
* 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, EVENT_MODULE, METRICSET_MODULE } from '../../../../../common/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.