diff --git a/x-pack/plugins/uptime/server/lib/adapters/telemetry/kibana_telemetry_adapter.ts b/x-pack/plugins/uptime/server/lib/adapters/telemetry/kibana_telemetry_adapter.ts index 3f6c3da2d6af0..8922661ec0504 100644 --- a/x-pack/plugins/uptime/server/lib/adapters/telemetry/kibana_telemetry_adapter.ts +++ b/x-pack/plugins/uptime/server/lib/adapters/telemetry/kibana_telemetry_adapter.ts @@ -5,29 +5,21 @@ */ import moment from 'moment'; -import { - ISavedObjectsRepository, - ILegacyScopedClusterClient, - SavedObjectsClientContract, - ElasticsearchClient, -} from 'kibana/server'; +import { ISavedObjectsRepository, SavedObjectsClientContract } from 'kibana/server'; import { CollectorFetchContext, UsageCollectionSetup } from 'src/plugins/usage_collection/server'; import { PageViewParams, UptimeTelemetry, Usage } from './types'; import { savedObjectsAdapter } from '../../saved_objects'; -import { UptimeESClient } from '../../lib'; +import { UptimeESClient, createUptimeESClient } from '../../lib'; interface UptimeTelemetryCollector { [key: number]: UptimeTelemetry; } - // seconds in an hour const BUCKET_SIZE = 3600; // take buckets in the last day const BUCKET_NUMBER = 24; export class KibanaTelemetryAdapter { - public static callCluster: ILegacyScopedClusterClient['callAsCurrentUser'] | ElasticsearchClient; - public static registerUsageCollector = ( usageCollector: UsageCollectionSetup, getSavedObjectsClient: () => ISavedObjectsRepository | undefined @@ -76,10 +68,11 @@ export class KibanaTelemetryAdapter { }, }, }, - fetch: async ({ callCluster }: CollectorFetchContext) => { + fetch: async ({ esClient }: CollectorFetchContext) => { const savedObjectsClient = getSavedObjectsClient()!; if (savedObjectsClient) { - await this.countNoOfUniqueMonitorAndLocations(callCluster, savedObjectsClient); + const uptimeEsClient = createUptimeESClient({ esClient, savedObjectsClient }); + await this.countNoOfUniqueMonitorAndLocations(uptimeEsClient, savedObjectsClient); } const report = this.getReport(); return { last_24_hours: { hits: { ...report } } }; @@ -132,7 +125,7 @@ export class KibanaTelemetryAdapter { } public static async countNoOfUniqueMonitorAndLocations( - callCluster: ILegacyScopedClusterClient['callAsCurrentUser'] | UptimeESClient, + callCluster: UptimeESClient, savedObjectsClient: ISavedObjectsRepository | SavedObjectsClientContract ) { const dynamicSettings = await savedObjectsAdapter.getUptimeDynamicSettings(savedObjectsClient); @@ -194,15 +187,12 @@ export class KibanaTelemetryAdapter { }, }; - const { body: result } = - typeof callCluster === 'function' - ? await callCluster('search', params) - : await callCluster.search(params); + const { body: result } = await callCluster.search(params); const numberOfUniqueMonitors: number = result?.aggregations?.unique_monitors?.value ?? 0; const numberOfUniqueLocations: number = result?.aggregations?.unique_locations?.value ?? 0; - const monitorNameStats: any = result?.aggregations?.monitor_name; - const locationNameStats: any = result?.aggregations?.observer_loc_name; + const monitorNameStats = result?.aggregations?.monitor_name; + const locationNameStats = result?.aggregations?.observer_loc_name; const uniqueMonitors: any = result?.aggregations?.monitors.buckets; const bucketId = this.getBucketToIncrement(); diff --git a/x-pack/plugins/uptime/server/lib/lib.ts b/x-pack/plugins/uptime/server/lib/lib.ts index ee84cf4463ceb..6515bdce31c69 100644 --- a/x-pack/plugins/uptime/server/lib/lib.ts +++ b/x-pack/plugins/uptime/server/lib/lib.ts @@ -3,7 +3,12 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import { ElasticsearchClient, SavedObjectsClientContract, KibanaRequest } from 'kibana/server'; +import { + ElasticsearchClient, + SavedObjectsClientContract, + KibanaRequest, + ISavedObjectsRepository, +} from 'kibana/server'; import chalk from 'chalk'; import { UMBackendFrameworkAdapter } from './adapters'; import { UMLicenseCheck } from './domains'; @@ -41,7 +46,7 @@ export function createUptimeESClient({ }: { esClient: ElasticsearchClient; request?: KibanaRequest; - savedObjectsClient: SavedObjectsClientContract; + savedObjectsClient: SavedObjectsClientContract | ISavedObjectsRepository; }) { const { _debug = false } = (request?.query as { _debug: boolean }) ?? {}; diff --git a/x-pack/typings/elasticsearch/aggregations.d.ts b/x-pack/typings/elasticsearch/aggregations.d.ts index 12542363fa8e8..9f25920353b2f 100644 --- a/x-pack/typings/elasticsearch/aggregations.d.ts +++ b/x-pack/typings/elasticsearch/aggregations.d.ts @@ -99,6 +99,7 @@ export interface AggregationOptionsByType { extended_stats: { field: string; }; + string_stats: { field: string }; top_hits: { from?: number; size?: number; @@ -274,6 +275,13 @@ interface AggregationResponsePart