From 6231adba04119159793f4ae5c8a6c9b5d12db003 Mon Sep 17 00:00:00 2001 From: Christiane Heiligers Date: Mon, 21 Dec 2020 16:39:04 -0700 Subject: [PATCH 1/5] Describes ts-ignore reason better --- .../telemetry/kibana_telemetry_adapter.ts | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) 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..7ab11a6eaf9a9 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 @@ -7,7 +7,6 @@ import moment from 'moment'; import { ISavedObjectsRepository, - ILegacyScopedClusterClient, SavedObjectsClientContract, ElasticsearchClient, } from 'kibana/server'; @@ -19,15 +18,12 @@ import { UptimeESClient } 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 +72,10 @@ export class KibanaTelemetryAdapter { }, }, }, - fetch: async ({ callCluster }: CollectorFetchContext) => { + fetch: async ({ esClient }: CollectorFetchContext) => { const savedObjectsClient = getSavedObjectsClient()!; if (savedObjectsClient) { - await this.countNoOfUniqueMonitorAndLocations(callCluster, savedObjectsClient); + await this.countNoOfUniqueMonitorAndLocations(esClient, savedObjectsClient); } const report = this.getReport(); return { last_24_hours: { hits: { ...report } } }; @@ -132,7 +128,7 @@ export class KibanaTelemetryAdapter { } public static async countNoOfUniqueMonitorAndLocations( - callCluster: ILegacyScopedClusterClient['callAsCurrentUser'] | UptimeESClient, + callCluster: ElasticsearchClient | UptimeESClient, savedObjectsClient: ISavedObjectsRepository | SavedObjectsClientContract ) { const dynamicSettings = await savedObjectsAdapter.getUptimeDynamicSettings(savedObjectsClient); @@ -193,11 +189,8 @@ export class KibanaTelemetryAdapter { }, }, }; - - const { body: result } = - typeof callCluster === 'function' - ? await callCluster('search', params) - : await callCluster.search(params); + // @ts-ignore: Union types do not have compatible signatures + 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; From e2014a39923da3265cdeb68d52a71c04158d6973 Mon Sep 17 00:00:00 2001 From: Christiane Heiligers Date: Tue, 22 Dec 2020 09:24:45 -0700 Subject: [PATCH 2/5] Refactors to fix types --- .../lib/adapters/telemetry/kibana_telemetry_adapter.ts | 9 ++++++--- x-pack/plugins/uptime/server/lib/lib.ts | 8 ++++++++ x-pack/typings/elasticsearch/aggregations.d.ts | 8 ++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) 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 7ab11a6eaf9a9..66ec57f53a547 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 @@ -11,9 +11,10 @@ import { ElasticsearchClient, } from 'kibana/server'; import { CollectorFetchContext, UsageCollectionSetup } from 'src/plugins/usage_collection/server'; +import { ESSearchResponse } from '../../../../../../typings/elasticsearch'; import { PageViewParams, UptimeTelemetry, Usage } from './types'; import { savedObjectsAdapter } from '../../saved_objects'; -import { UptimeESClient } from '../../lib'; +import { UptimeESClient, isUptimeESClient } from '../../lib'; interface UptimeTelemetryCollector { [key: number]: UptimeTelemetry; @@ -189,8 +190,10 @@ export class KibanaTelemetryAdapter { }, }, }; - // @ts-ignore: Union types do not have compatible signatures - const { body: result } = await callCluster.search(params); + + const { body: result } = isUptimeESClient(callCluster) + ? await callCluster.search(params) + : await callCluster.search>(params); const numberOfUniqueMonitors: number = result?.aggregations?.unique_monitors?.value ?? 0; const numberOfUniqueLocations: number = result?.aggregations?.unique_locations?.value ?? 0; diff --git a/x-pack/plugins/uptime/server/lib/lib.ts b/x-pack/plugins/uptime/server/lib/lib.ts index ee84cf4463ceb..ff5bab3626d93 100644 --- a/x-pack/plugins/uptime/server/lib/lib.ts +++ b/x-pack/plugins/uptime/server/lib/lib.ts @@ -105,6 +105,14 @@ export function createUptimeESClient({ }; } +export function isUptimeESClient(client: any): client is UptimeESClient { + return ( + 'baseESClient' in client && + 'getSavedObjectClient' in client && + 'search' in client && + 'count' in client + ); +} /* eslint-disable no-console */ function formatObj(obj: Record) { 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 Date: Tue, 22 Dec 2020 17:15:16 -0700 Subject: [PATCH 3/5] Improves types --- .../server/lib/adapters/telemetry/kibana_telemetry_adapter.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 66ec57f53a547..30f7e8effc584 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 @@ -197,8 +197,8 @@ export class KibanaTelemetryAdapter { 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(); From 7dc1ed45899f82c17b0614d691b7c5b762f5400d Mon Sep 17 00:00:00 2001 From: Christiane Heiligers Date: Mon, 4 Jan 2021 09:58:12 -0700 Subject: [PATCH 4/5] Use createUptimeEsClient to simplify collector initialization --- .../lib/adapters/telemetry/kibana_telemetry_adapter.ts | 5 +++-- x-pack/plugins/uptime/server/lib/lib.ts | 9 +++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) 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 30f7e8effc584..fd8c4180b469e 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 @@ -14,7 +14,7 @@ import { CollectorFetchContext, UsageCollectionSetup } from 'src/plugins/usage_c import { ESSearchResponse } from '../../../../../../typings/elasticsearch'; import { PageViewParams, UptimeTelemetry, Usage } from './types'; import { savedObjectsAdapter } from '../../saved_objects'; -import { UptimeESClient, isUptimeESClient } from '../../lib'; +import { UptimeESClient, isUptimeESClient, createUptimeESClient } from '../../lib'; interface UptimeTelemetryCollector { [key: number]: UptimeTelemetry; @@ -76,7 +76,8 @@ export class KibanaTelemetryAdapter { fetch: async ({ esClient }: CollectorFetchContext) => { const savedObjectsClient = getSavedObjectsClient()!; if (savedObjectsClient) { - await this.countNoOfUniqueMonitorAndLocations(esClient, savedObjectsClient); + const uptimeEsClient = createUptimeESClient({ esClient, savedObjectsClient }); + await this.countNoOfUniqueMonitorAndLocations(uptimeEsClient, savedObjectsClient); } const report = this.getReport(); return { last_24_hours: { hits: { ...report } } }; diff --git a/x-pack/plugins/uptime/server/lib/lib.ts b/x-pack/plugins/uptime/server/lib/lib.ts index ff5bab3626d93..ad92d882bb69b 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 }) ?? {}; From f63fe37a28464ef58583c71a0bc68d2cb26b0969 Mon Sep 17 00:00:00 2001 From: Christiane Heiligers Date: Mon, 4 Jan 2021 13:17:28 -0700 Subject: [PATCH 5/5] Types cleanup and simplification --- .../telemetry/kibana_telemetry_adapter.ts | 15 ++++----------- x-pack/plugins/uptime/server/lib/lib.ts | 8 -------- 2 files changed, 4 insertions(+), 19 deletions(-) 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 fd8c4180b469e..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,16 +5,11 @@ */ import moment from 'moment'; -import { - ISavedObjectsRepository, - SavedObjectsClientContract, - ElasticsearchClient, -} from 'kibana/server'; +import { ISavedObjectsRepository, SavedObjectsClientContract } from 'kibana/server'; import { CollectorFetchContext, UsageCollectionSetup } from 'src/plugins/usage_collection/server'; -import { ESSearchResponse } from '../../../../../../typings/elasticsearch'; import { PageViewParams, UptimeTelemetry, Usage } from './types'; import { savedObjectsAdapter } from '../../saved_objects'; -import { UptimeESClient, isUptimeESClient, createUptimeESClient } from '../../lib'; +import { UptimeESClient, createUptimeESClient } from '../../lib'; interface UptimeTelemetryCollector { [key: number]: UptimeTelemetry; @@ -130,7 +125,7 @@ export class KibanaTelemetryAdapter { } public static async countNoOfUniqueMonitorAndLocations( - callCluster: ElasticsearchClient | UptimeESClient, + callCluster: UptimeESClient, savedObjectsClient: ISavedObjectsRepository | SavedObjectsClientContract ) { const dynamicSettings = await savedObjectsAdapter.getUptimeDynamicSettings(savedObjectsClient); @@ -192,9 +187,7 @@ export class KibanaTelemetryAdapter { }, }; - const { body: result } = isUptimeESClient(callCluster) - ? 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; diff --git a/x-pack/plugins/uptime/server/lib/lib.ts b/x-pack/plugins/uptime/server/lib/lib.ts index ad92d882bb69b..6515bdce31c69 100644 --- a/x-pack/plugins/uptime/server/lib/lib.ts +++ b/x-pack/plugins/uptime/server/lib/lib.ts @@ -110,14 +110,6 @@ export function createUptimeESClient({ }; } -export function isUptimeESClient(client: any): client is UptimeESClient { - return ( - 'baseESClient' in client && - 'getSavedObjectClient' in client && - 'search' in client && - 'count' in client - ); -} /* eslint-disable no-console */ function formatObj(obj: Record) {