From 43b09c2b95bcf694e75694bbbd60673b081a488c Mon Sep 17 00:00:00 2001 From: Christiane Heiligers Date: Sat, 19 Dec 2020 18:26:13 -0700 Subject: [PATCH 1/2] Migrates search telemetry usage collector es client from legacy to new --- .../data/server/search/collectors/fetch.ts | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/plugins/data/server/search/collectors/fetch.ts b/src/plugins/data/server/search/collectors/fetch.ts index 344bc18c7b4b6..d59ed74edce10 100644 --- a/src/plugins/data/server/search/collectors/fetch.ts +++ b/src/plugins/data/server/search/collectors/fetch.ts @@ -23,24 +23,20 @@ import { SharedGlobalConfig } from 'kibana/server'; import { CollectorFetchContext } from 'src/plugins/usage_collection/server'; import { Usage } from './register'; -interface SearchTelemetrySavedObject { - 'search-telemetry': Usage; -} - export function fetchProvider(config$: Observable) { - return async ({ callCluster }: CollectorFetchContext): Promise => { + return async ({ esClient }: CollectorFetchContext): Promise => { const config = await config$.pipe(first()).toPromise(); - - const response = await callCluster('search', { - index: config.kibana.index, - body: { - query: { term: { type: { value: 'search-telemetry' } } }, + const { body } = await esClient.search( + { + index: config.kibana.index, + body: { + query: { term: { type: { value: 'search-telemetry' } } }, + }, }, - ignore: [404], - }); - - return response.hits.hits.length - ? response.hits.hits[0]._source['search-telemetry'] + { ignore: [404] } + ); + return body.hits?.hits?.length > 0 + ? body.hits.hits[0]._source['search-telemetry'] : { successCount: 0, errorCount: 0, From 7081eff5becc8ec42eb1a604107ecf9990d1e206 Mon Sep 17 00:00:00 2001 From: Christiane Heiligers Date: Sun, 20 Dec 2020 12:56:35 -0700 Subject: [PATCH 2/2] Explicit type --- .../data/server/search/collectors/fetch.ts | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/plugins/data/server/search/collectors/fetch.ts b/src/plugins/data/server/search/collectors/fetch.ts index d59ed74edce10..9d0d431cf4eaf 100644 --- a/src/plugins/data/server/search/collectors/fetch.ts +++ b/src/plugins/data/server/search/collectors/fetch.ts @@ -20,13 +20,18 @@ import { Observable } from 'rxjs'; import { first } from 'rxjs/operators'; import { SharedGlobalConfig } from 'kibana/server'; +import { SearchResponse } from 'elasticsearch'; import { CollectorFetchContext } from 'src/plugins/usage_collection/server'; import { Usage } from './register'; +interface SearchTelemetry { + 'search-telemetry': Usage; +} +type ESResponse = SearchResponse; export function fetchProvider(config$: Observable) { return async ({ esClient }: CollectorFetchContext): Promise => { const config = await config$.pipe(first()).toPromise(); - const { body } = await esClient.search( + const { body: esResponse } = await esClient.search( { index: config.kibana.index, body: { @@ -35,12 +40,14 @@ export function fetchProvider(config$: Observable) { }, { ignore: [404] } ); - return body.hits?.hits?.length > 0 - ? body.hits.hits[0]._source['search-telemetry'] - : { - successCount: 0, - errorCount: 0, - averageDuration: null, - }; + const size = esResponse?.hits?.hits?.length ?? 0; + if (!size) { + return { + successCount: 0, + errorCount: 0, + averageDuration: null, + }; + } + return esResponse.hits.hits[0]._source['search-telemetry']; }; }