Skip to content

Commit

Permalink
Migrates kql_telemetry usage collector es client (#86585)
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
TinaHeiligers and kibanamachine authored Dec 21, 2020
1 parent 7de243e commit 61a0b00
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 46 deletions.
70 changes: 36 additions & 34 deletions src/plugins/data/server/kql_telemetry/usage_collector/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { fetchProvider } from './fetch';
import { LegacyAPICaller } from 'kibana/server';
import { ElasticsearchClient } from 'kibana/server';
import { CollectorFetchContext } from 'src/plugins/usage_collection/server';
import { createCollectorFetchContextMock } from 'src/plugins/usage_collection/server/mocks';

Expand All @@ -30,42 +30,41 @@ jest.mock('../../../common', () => ({
}));

let fetch: ReturnType<typeof fetchProvider>;
let callCluster: LegacyAPICaller;
let esClient: ElasticsearchClient;
let collectorFetchContext: CollectorFetchContext;
const collectorFetchContextMock = createCollectorFetchContextMock();

function setupMockCallCluster(
optCount: { optInCount?: number; optOutCount?: number } | null,
language: string | undefined | null
) {
callCluster = (jest.fn((method, params) => {
if (params && 'id' in params && params.id === 'kql-telemetry:kql-telemetry') {
if (optCount === null) {
return Promise.resolve({
function mockedEsGetMethod() {
if (optCount === null) {
return Promise.resolve({
body: {
_index: '.kibana_1',
_id: 'kql-telemetry:kql-telemetry',
found: false,
});
} else {
return Promise.resolve({
},
});
} else {
return Promise.resolve({
body: {
_source: {
'kql-telemetry': {
...optCount,
},
'kql-telemetry': { ...optCount },
type: 'kql-telemetry',
updated_at: '2018-10-05T20:20:56.258Z',
},
});
}
} else if (params && 'body' in params && params.body.query.term.type === 'config') {
if (language === 'missingConfigDoc') {
return Promise.resolve({
hits: {
hits: [],
},
});
} else {
return Promise.resolve({
},
});
}
}
function mockedEsSearchMethod() {
if (language === 'missingConfigDoc') {
return Promise.resolve({ body: { hits: { hits: [] } } });
} else {
return Promise.resolve({
body: {
hits: {
hits: [
{
Expand All @@ -77,12 +76,15 @@ function setupMockCallCluster(
},
],
},
});
}
},
});
}

throw new Error('invalid call');
}) as unknown) as LegacyAPICaller;
}
const esClientMock = ({
get: jest.fn().mockImplementation(mockedEsGetMethod),
search: jest.fn().mockImplementation(mockedEsSearchMethod),
} as unknown) as ElasticsearchClient;
esClient = esClientMock;
}

describe('makeKQLUsageCollector', () => {
Expand All @@ -95,7 +97,7 @@ describe('makeKQLUsageCollector', () => {
setupMockCallCluster({ optInCount: 1 }, 'kuery');
collectorFetchContext = {
...collectorFetchContextMock,
callCluster,
esClient,
};
const fetchResponse = await fetch(collectorFetchContext);
expect(fetchResponse.optInCount).toBe(1);
Expand All @@ -106,7 +108,7 @@ describe('makeKQLUsageCollector', () => {
setupMockCallCluster({ optInCount: 1 }, 'kuery');
collectorFetchContext = {
...collectorFetchContextMock,
callCluster,
esClient,
};
const fetchResponse = await fetch(collectorFetchContext);
expect(fetchResponse.defaultQueryLanguage).toBe('kuery');
Expand All @@ -117,7 +119,7 @@ describe('makeKQLUsageCollector', () => {
setupMockCallCluster({ optInCount: 1 }, null);
collectorFetchContext = {
...collectorFetchContextMock,
callCluster,
esClient,
};
const fetchResponse = await fetch(collectorFetchContext);
expect(fetchResponse.defaultQueryLanguage).toBe('lucene');
Expand All @@ -127,7 +129,7 @@ describe('makeKQLUsageCollector', () => {
setupMockCallCluster({ optInCount: 1 }, undefined);
collectorFetchContext = {
...collectorFetchContextMock,
callCluster,
esClient,
};
const fetchResponse = await fetch(collectorFetchContext);
expect(fetchResponse.defaultQueryLanguage).toBe('default-lucene');
Expand All @@ -137,7 +139,7 @@ describe('makeKQLUsageCollector', () => {
setupMockCallCluster(null, 'kuery');
collectorFetchContext = {
...collectorFetchContextMock,
callCluster,
esClient,
};
const fetchResponse = await fetch(collectorFetchContext);
expect(fetchResponse.optInCount).toBe(0);
Expand All @@ -148,7 +150,7 @@ describe('makeKQLUsageCollector', () => {
setupMockCallCluster(null, 'missingConfigDoc');
collectorFetchContext = {
...collectorFetchContextMock,
callCluster,
esClient,
};
const fetchResponse = await fetch(collectorFetchContext);
expect(fetchResponse.defaultQueryLanguage).toBe('default-lucene');
Expand Down
28 changes: 16 additions & 12 deletions src/plugins/data/server/kql_telemetry/usage_collector/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,22 @@ export interface Usage {
}

export function fetchProvider(index: string) {
return async ({ callCluster }: CollectorFetchContext): Promise<Usage> => {
const [response, config] = await Promise.all([
callCluster('get', {
index,
id: 'kql-telemetry:kql-telemetry',
ignore: [404],
}),
callCluster('search', {
index,
body: { query: { term: { type: 'config' } } },
ignore: [404],
}),
return async ({ esClient }: CollectorFetchContext): Promise<Usage> => {
const [{ body: response }, { body: config }] = await Promise.all([
esClient.get(
{
index,
id: 'kql-telemetry:kql-telemetry',
},
{ ignore: [404] }
),
esClient.search(
{
index,
body: { query: { term: { type: 'config' } } },
},
{ ignore: [404] }
),
]);

const queryLanguageConfigValue: string | null | undefined = get(
Expand Down

0 comments on commit 61a0b00

Please sign in to comment.