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

Migrates elasticsearch client in the kibana_usage_collector #86406

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 @@ -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
TinaHeiligers marked this conversation as resolved.
Show resolved Hide resolved
{
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