Skip to content

Commit

Permalink
Migrates elasticsearch client in the kibana_usage_collector (#86406)
Browse files Browse the repository at this point in the history
  • Loading branch information
TinaHeiligers authored Dec 18, 2020
1 parent 0155974 commit 6a51741
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,25 @@
* specific language governing permissions and limitations
* under the License.
*/

import { elasticsearchServiceMock } from '../../../../../../src/core/server/mocks';
import { getSavedObjectsCounts } from './get_saved_object_counts';

export function mockGetSavedObjectsCounts(params: any) {
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
esClient.search.mockResolvedValue(
// @ts-ignore we only care about the response body
{
body: { ...params },
}
);
return esClient;
}

describe('getSavedObjectsCounts', () => {
test('Get all the saved objects equal to 0 because no results were found', async () => {
const callCluster = jest.fn(() => ({}));
const esClient = mockGetSavedObjectsCounts({});

const results = await getSavedObjectsCounts(callCluster as any, '.kibana');
const results = await getSavedObjectsCounts(esClient, '.kibana');
expect(results).toStrictEqual({
dashboard: { total: 0 },
visualization: { total: 0 },
Expand All @@ -35,7 +46,7 @@ describe('getSavedObjectsCounts', () => {
});

test('Merge the zeros with the results', async () => {
const callCluster = jest.fn(() => ({
const esClient = mockGetSavedObjectsCounts({
aggregations: {
types: {
buckets: [
Expand All @@ -46,9 +57,9 @@ describe('getSavedObjectsCounts', () => {
],
},
},
}));
});

const results = await getSavedObjectsCounts(callCluster as any, '.kibana');
const results = await getSavedObjectsCounts(esClient, '.kibana');
expect(results).toStrictEqual({
dashboard: { total: 1 },
visualization: { total: 0 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/

import { snakeCase } from 'lodash';
import { LegacyAPICaller } from 'kibana/server';
import { ElasticsearchClient } from 'src/core/server';

const TYPES = [
'dashboard',
Expand All @@ -48,7 +48,7 @@ export interface KibanaSavedObjectCounts {
}

export async function getSavedObjectsCounts(
callCluster: LegacyAPICaller,
esClient: ElasticsearchClient,
kibanaIndex: string // Typically '.kibana'. We might need a way to obtain it from the SavedObjects client (or the SavedObjects client to provide a way to run aggregations?)
): Promise<KibanaSavedObjectCounts> {
const savedObjectCountSearchParams = {
Expand All @@ -67,9 +67,9 @@ export async function getSavedObjectsCounts(
},
},
};
const resp = await callCluster('search', savedObjectCountSearchParams);
const { body } = await esClient.search(savedObjectCountSearchParams);
const buckets: Array<{ key: string; doc_count: number }> =
resp.aggregations?.types?.buckets || [];
body.aggregations?.types?.buckets || [];

// Initialise the object with all zeros for all the types
const allZeros: KibanaSavedObjectCounts = TYPES.reduce(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
import {
loggingSystemMock,
pluginInitializerContextConfigMock,
elasticsearchServiceMock,
} from '../../../../../core/server/mocks';
import {
Collector,
createCollectorFetchContextMock,
createUsageCollectionSetupMock,
} from '../../../../usage_collection/server/usage_collection.mock';
import { createCollectorFetchContextMock } from '../../../../usage_collection/server/mocks';
import { registerKibanaUsageCollector } from './';

const logger = loggingSystemMock.createLogger();
Expand All @@ -43,7 +44,9 @@ describe('telemetry_kibana', () => {

const getMockFetchClients = (hits?: unknown[]) => {
const fetchParamsMock = createCollectorFetchContextMock();
fetchParamsMock.callCluster.mockResolvedValue({ hits: { hits } });
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
esClient.search.mockResolvedValue({ body: { hits: { hits } } } as any);
fetchParamsMock.esClient = esClient;
return fetchParamsMock;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ export function getKibanaUsageCollector(
graph_workspace: { total: { type: 'long' } },
timelion_sheet: { total: { type: 'long' } },
},
async fetch({ callCluster }) {
async fetch({ esClient }) {
const {
kibana: { index },
} = await legacyConfig$.pipe(take(1)).toPromise();
return {
index,
...(await getSavedObjectsCounts(callCluster, index)),
...(await getSavedObjectsCounts(esClient, index)),
};
},
});
Expand Down

0 comments on commit 6a51741

Please sign in to comment.