Skip to content

Commit

Permalink
Switch to new elasticsearch client for Visualizations (#85245)
Browse files Browse the repository at this point in the history
* Switch to new elasticsearch client for Visualizations

* update get_usage_collector.test.ts
  • Loading branch information
alexwizp authored Dec 10, 2020
1 parent be0372f commit b3d5dce
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import moment from 'moment';
import { LegacyAPICaller } from 'src/core/server';
import { ElasticsearchClient } from 'src/core/server';
import { getStats } from './get_usage_collector';

const defaultMockSavedObjects = [
Expand Down Expand Up @@ -120,8 +120,11 @@ const enlargedMockSavedObjects = [

describe('Visualizations usage collector', () => {
const mockIndex = '';

const getMockCallCluster = (hits: unknown[]) =>
(() => Promise.resolve({ hits: { hits } }) as unknown) as LegacyAPICaller;
({
search: () => Promise.resolve({ body: { hits: { hits } } }) as unknown,
} as ElasticsearchClient);

test('Returns undefined when no results found (undefined)', async () => {
const result = await getStats(getMockCallCluster(undefined as any), mockIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
*/

import { countBy, get, groupBy, mapValues, max, min, values } from 'lodash';
import { ElasticsearchClient } from 'kibana/server';
import { SearchResponse } from 'elasticsearch';

import { LegacyAPICaller } from 'src/core/server';
import { getPastDays } from './get_past_days';

type ESResponse = SearchResponse<{ visualization: { visState: string } }>;

interface VisSummary {
Expand All @@ -47,7 +46,7 @@ export interface VisualizationUsage {
* Parse the response data into telemetry payload
*/
export async function getStats(
callCluster: LegacyAPICaller,
esClient: ElasticsearchClient,
index: string
): Promise<VisualizationUsage | undefined> {
const searchParams = {
Expand All @@ -65,7 +64,7 @@ export async function getStats(
},
},
};
const esResponse: ESResponse = await callCluster('search', searchParams);
const { body: esResponse } = await esClient.search<ESResponse>(searchParams);
const size = get(esResponse, 'hits.hits.length', 0);
if (size < 1) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('registerVisualizationsCollector', () => {
const mockCollectorFetchContext = createCollectorFetchContextMock();
const fetchResult = await usageCollector.fetch(mockCollectorFetchContext);
expect(mockGetStats).toBeCalledTimes(1);
expect(mockGetStats).toBeCalledWith(mockCollectorFetchContext.callCluster, mockIndex);
expect(mockGetStats).toBeCalledWith(mockCollectorFetchContext.esClient, mockIndex);
expect(fetchResult).toBe(mockStats);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ export function registerVisualizationsCollector(
saved_90_days_total: { type: 'long' },
},
},
fetch: async ({ callCluster }) => {
fetch: async ({ esClient }) => {
const index = (await config.pipe(first()).toPromise()).kibana.index;
return await getStats(callCluster, index);
return await getStats(esClient, index);
},
});
collectorSet.registerCollector(collector);
Expand Down

0 comments on commit b3d5dce

Please sign in to comment.