Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to new elasticsearch client for Visualizations #85245

Merged
merged 3 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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