Skip to content

Commit

Permalink
Switch to new elasticsearch client for Vega (elastic#85280)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
alexwizp and kibanamachine committed Dec 10, 2020
1 parent 207fa22 commit 552ed9b
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 20 deletions.
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

0 comments on commit 552ed9b

Please sign in to comment.