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

[7.x] Switch to new elasticsearch client for Vega (#85280) #85533

Merged
merged 1 commit 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 @@ -19,6 +19,7 @@

export const mockStats = { somestat: 1 };
export const mockGetStats = jest.fn().mockResolvedValue(mockStats);

jest.doMock('./get_usage_collector', () => ({
getStats: mockGetStats,
}));
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ const mockedSavedObjects = [

const getMockCollectorFetchContext = (hits?: unknown[]) => {
const fetchParamsMock = createCollectorFetchContextMock();
fetchParamsMock.callCluster.mockResolvedValue({ hits: { hits } });

fetchParamsMock.esClient.search = jest.fn().mockResolvedValue({ body: { hits: { hits } } });
return fetchParamsMock;
};

Expand Down Expand Up @@ -104,17 +105,13 @@ describe('Vega visualization usage collector', () => {
};

test('Returns undefined when no results found (undefined)', async () => {
const result = await getStats(getMockCollectorFetchContext().callCluster, mockIndex, mockDeps);
const result = await getStats(getMockCollectorFetchContext().esClient, mockIndex, mockDeps);

expect(result).toBeUndefined();
});

test('Returns undefined when no results found (0 results)', async () => {
const result = await getStats(
getMockCollectorFetchContext([]).callCluster,
mockIndex,
mockDeps
);
const result = await getStats(getMockCollectorFetchContext([]).esClient, mockIndex, mockDeps);

expect(result).toBeUndefined();
});
Expand All @@ -129,7 +126,7 @@ describe('Vega visualization usage collector', () => {
},
},
]);
const result = await getStats(mockCollectorFetchContext.callCluster, mockIndex, mockDeps);
const result = await getStats(mockCollectorFetchContext.esClient, mockIndex, mockDeps);

expect(result).toBeUndefined();
});
Expand All @@ -153,14 +150,14 @@ describe('Vega visualization usage collector', () => {
},
]);

const result = await getStats(mockCollectorFetchContext.callCluster, mockIndex, mockDeps);
const result = await getStats(mockCollectorFetchContext.esClient, mockIndex, mockDeps);

expect(result).toBeUndefined();
});

test('Summarizes visualizations response data', async () => {
const mockCollectorFetchContext = getMockCollectorFetchContext(mockedSavedObjects);
const result = await getStats(mockCollectorFetchContext.callCluster, mockIndex, mockDeps);
const result = await getStats(mockCollectorFetchContext.esClient, mockIndex, mockDeps);

expect(result).toMatchObject({
vega_lib_specs_total: 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
import { parse } from 'hjson';
import { SearchResponse } from 'elasticsearch';
import { LegacyAPICaller, SavedObject } from 'src/core/server';
import { ElasticsearchClient, SavedObject } from 'src/core/server';

import { VegaSavedObjectAttributes, VisTypeVegaPluginSetupDependencies } from '../types';

Expand Down Expand Up @@ -64,7 +64,7 @@ export interface VegaUsage {
}

export const getStats = async (
callCluster: LegacyAPICaller,
esClient: ElasticsearchClient,
index: string,
{ home }: UsageCollectorDependencies
): Promise<VegaUsage | undefined> => {
Expand All @@ -90,7 +90,7 @@ export const getStats = async (
},
};

const esResponse: ESResponse = await callCluster('search', searchParams);
const { body: esResponse } = await esClient.search<ESResponse>(searchParams);
const size = esResponse?.hits?.hits?.length ?? 0;

if (!size) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ describe('registerVegaUsageCollector', () => {
const mockedCollectorFetchContext = createCollectorFetchContextMock();
const fetchResult = await usageCollector.fetch(mockedCollectorFetchContext);
expect(mockGetStats).toBeCalledTimes(1);
expect(mockGetStats).toBeCalledWith(
mockedCollectorFetchContext.callCluster,
mockIndex,
mockDeps
);
expect(mockGetStats).toBeCalledWith(mockedCollectorFetchContext.esClient, mockIndex, mockDeps);
expect(fetchResult).toBe(mockStats);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ export function registerVegaUsageCollector(
vega_lite_lib_specs_total: { type: 'long' },
vega_use_map_total: { type: 'long' },
},
fetch: async ({ callCluster }) => {
fetch: async ({ esClient }) => {
const { index } = (await config.pipe(first()).toPromise()).kibana;

return await getStats(callCluster, index, dependencies);
return await getStats(esClient, index, dependencies);
},
});

Expand Down