From e2014a39923da3265cdeb68d52a71c04158d6973 Mon Sep 17 00:00:00 2001 From: Christiane Heiligers Date: Tue, 22 Dec 2020 09:24:45 -0700 Subject: [PATCH] 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