diff --git a/x-pack/plugins/infra/public/pages/metrics/hosts/hooks/use_host_count.ts b/x-pack/plugins/infra/public/pages/metrics/hosts/hooks/use_host_count.ts index d261b4ec2e45d..134e651ed7a64 100644 --- a/x-pack/plugins/infra/public/pages/metrics/hosts/hooks/use_host_count.ts +++ b/x-pack/plugins/infra/public/pages/metrics/hosts/hooks/use_host_count.ts @@ -7,10 +7,11 @@ import * as rt from 'io-ts'; import { ES_SEARCH_STRATEGY, IKibanaSearchResponse } from '@kbn/data-plugin/common'; -import { useCallback, useEffect } from 'react'; -import { catchError, map, Observable, of, startWith } from 'rxjs'; +import { useCallback, useEffect, useMemo } from 'react'; +import { catchError, map, Observable, of, startWith, tap } from 'rxjs'; import createContainer from 'constate'; import type { QueryDslQueryContainer, SearchResponse } from '@elastic/elasticsearch/lib/api/types'; +import type { ITelemetryClient } from '../../../../services/telemetry'; import { useKibanaContextForPlugin } from '../../../../hooks/use_kibana'; import { decodeOrThrow } from '../../../../../common/runtime_types'; import { useDataSearch, useLatestPartialDataSearchResponse } from '../../../../utils/data_search'; @@ -79,7 +80,23 @@ export const useHostCount = () => { searchCriteria.dateRange.from, searchCriteria.dateRange.to, ]), - parseResponses: normalizeDataSearchResponse, + parseResponses: useMemo( + () => + normalizeDataSearchResponse({ + telemetry, + telemetryData: { + withQuery: !!searchCriteria.query.query, + withFilters: + searchCriteria.filters.length > 0 || searchCriteria.panelFilters.length > 0, + }, + }), + [ + searchCriteria.filters.length, + searchCriteria.panelFilters.length, + searchCriteria.query.query, + telemetry, + ] + ), }); const { isRequestRunning, isResponsePartial, latestResponseData, latestResponseErrors } = @@ -89,14 +106,6 @@ export const useHostCount = () => { fetchHostCount(); }, [fetchHostCount]); - useEffect(() => { - if (latestResponseData) { - telemetry.reportHostsViewTotalHostCountRetrieved({ - total: latestResponseData.count.value, - }); - } - }, [latestResponseData, telemetry]); - return { errors: latestResponseErrors, isRequestRunning, @@ -116,27 +125,42 @@ const INITIAL_STATE = { loaded: 0, total: undefined, }; -const normalizeDataSearchResponse = ( - response$: Observable>>> -) => - response$.pipe( - map((response) => ({ - data: decodeOrThrow(HostCountResponseRT)(response.rawResponse.aggregations), - errors: [], - isPartial: response.isPartial ?? false, - isRunning: response.isRunning ?? false, - loaded: response.loaded, - total: response.total, - })), - startWith(INITIAL_STATE), - catchError((error) => - of({ - ...INITIAL_STATE, - errors: [error.message ?? error], - isRunning: false, - }) - ) - ); + +const normalizeDataSearchResponse = + ({ + telemetry, + telemetryData, + }: { + telemetry: ITelemetryClient; + telemetryData: { withQuery: boolean; withFilters: boolean }; + }) => + (response$: Observable>>>) => { + return response$.pipe( + map((response) => ({ + data: decodeOrThrow(HostCountResponseRT)(response.rawResponse.aggregations), + errors: [], + isPartial: response.isPartial ?? false, + isRunning: response.isRunning ?? false, + loaded: response.loaded, + total: response.total, + })), + tap(({ data }) => { + telemetry.reportHostsViewTotalHostCountRetrieved({ + total: data.count.value, + with_query: telemetryData.withQuery, + with_filters: telemetryData.withFilters, + }); + }), + startWith(INITIAL_STATE), + catchError((error) => + of({ + ...INITIAL_STATE, + errors: [error.message ?? error], + isRunning: false, + }) + ) + ); + }; const HostCountResponseRT = rt.type({ count: rt.type({ diff --git a/x-pack/plugins/infra/public/services/telemetry/telemetry_events.ts b/x-pack/plugins/infra/public/services/telemetry/telemetry_events.ts index 6ce2d2b827623..b83cbfe262e63 100644 --- a/x-pack/plugins/infra/public/services/telemetry/telemetry_events.ts +++ b/x-pack/plugins/infra/public/services/telemetry/telemetry_events.ts @@ -109,6 +109,20 @@ const hostViewTotalHostCountRetrieved: InfraTelemetryEvent = { optional: false, }, }, + with_query: { + type: 'boolean', + _meta: { + description: 'Has KQL query', + optional: false, + }, + }, + with_filters: { + type: 'boolean', + _meta: { + description: 'Has filters', + optional: false, + }, + }, }, }; diff --git a/x-pack/plugins/infra/public/services/telemetry/telemetry_service.test.ts b/x-pack/plugins/infra/public/services/telemetry/telemetry_service.test.ts index ac450df7dd162..3fa8a9b447111 100644 --- a/x-pack/plugins/infra/public/services/telemetry/telemetry_service.test.ts +++ b/x-pack/plugins/infra/public/services/telemetry/telemetry_service.test.ts @@ -172,6 +172,8 @@ describe('TelemetryService', () => { telemetry.reportHostsViewTotalHostCountRetrieved({ total: 300, + with_filters: true, + with_query: false, }); expect(setupParams.analytics.reportEvent).toHaveBeenCalledTimes(1); @@ -179,6 +181,8 @@ describe('TelemetryService', () => { InfraTelemetryEventTypes.HOST_VIEW_TOTAL_HOST_COUNT_RETRIEVED, { total: 300, + with_filters: true, + with_query: false, } ); }); diff --git a/x-pack/plugins/infra/public/services/telemetry/types.ts b/x-pack/plugins/infra/public/services/telemetry/types.ts index 3b1665078ee3a..769cc303def50 100644 --- a/x-pack/plugins/infra/public/services/telemetry/types.ts +++ b/x-pack/plugins/infra/public/services/telemetry/types.ts @@ -41,6 +41,8 @@ export interface HostFlyoutFilterActionParams { export interface HostsViewQueryHostsCountRetrievedParams { total: number; + with_query: boolean; + with_filters: boolean; } export interface AssetDetailsFlyoutViewedParams {