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

Migrate kql telemetry and scripts api #52651

Merged
merged 9 commits into from
Dec 20, 2019
6 changes: 0 additions & 6 deletions src/legacy/core_plugins/kibana/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,12 @@ import { importApi } from './server/routes/api/import';
import { exportApi } from './server/routes/api/export';
import { homeApi } from './server/routes/api/home';
import { managementApi } from './server/routes/api/management';
import { scriptsApi } from './server/routes/api/scripts';
import { registerSuggestionsApi } from './server/routes/api/suggestions';
import { registerKqlTelemetryApi } from './server/routes/api/kql_telemetry';
import { registerFieldFormats } from './server/field_formats/register';
import { registerTutorials } from './server/tutorials/register';
import * as systemApi from './server/lib/system_api';
import mappings from './mappings.json';
import { getUiSettingDefaults } from './ui_setting_defaults';
import { makeKQLUsageCollector } from './server/lib/kql_usage_collector';
import { registerCspCollector } from './server/lib/csp_usage_collector';
import { injectVars } from './inject_vars';
import { i18n } from '@kbn/i18n';
Expand Down Expand Up @@ -327,16 +324,13 @@ export default function (kibana) {
// uuid
await manageUuid(server);
// routes
scriptsApi(server);
importApi(server);
exportApi(server);
homeApi(server);
managementApi(server);
registerSuggestionsApi(server);
registerKqlTelemetryApi(server);
registerFieldFormats(server);
registerTutorials(server);
makeKQLUsageCollector(usageCollection, server);
registerCspCollector(usageCollection, server);
server.expose('systemApi', systemApi);
server.injectUiAppVars('kibana', () => injectVars(server));
Expand Down

This file was deleted.

3 changes: 2 additions & 1 deletion src/legacy/core_plugins/kibana/ui_setting_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import moment from 'moment-timezone';
import numeralLanguages from '@elastic/numeral/languages';
import { i18n } from '@kbn/i18n';
import { DEFAULT_QUERY_LANGUAGE } from '../../../plugins/data/common';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should import from public here if possible, instead of common

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is used on the server, so importing from public won't work. Funny enough as it's not in a server dir, importing from server also doesn't work with the current linting rules. I would like to keep common for now and we can clean it up when migrating this file


export function getUiSettingDefaults() {
const weekdays = moment.weekdays().slice();
Expand Down Expand Up @@ -114,7 +115,7 @@ export function getUiSettingDefaults() {
},
'search:queryLanguage': {
name: queryLanguageSettingName,
value: 'kuery',
value: DEFAULT_QUERY_LANGUAGE,
description: i18n.translate('kbn.advancedSettings.searchQueryLanguageText', {
defaultMessage:
'Query language used by the query bar. KQL is a new language built specifically for Kibana.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,4 @@
* under the License.
*/

import { registerLanguages } from './register_languages';

export function scriptsApi(server) {
registerLanguages(server);
}
export const DEFAULT_QUERY_LANGUAGE = 'kuery';
1 change: 1 addition & 0 deletions src/plugins/data/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export * from './index_patterns';
export * from './es_query';
export * from './utils';
export * from './types';
export * from './constants';
3 changes: 2 additions & 1 deletion src/plugins/data/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"version": "kibana",
"server": true,
"ui": true,
"requiredPlugins": ["uiActions"]
"requiredPlugins": ["uiActions"],
"optionalPlugins": ["usageCollection"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,4 @@
* under the License.
*/

export function registerLanguages(server) {
server.route({
path: '/api/kibana/scripts/languages',
method: 'GET',
handler: function () {
return ['painless', 'expression'];
}
});
}
export { KqlTelemetryService } from './kql_telemetry_service';
48 changes: 48 additions & 0 deletions src/plugins/data/server/kql_telemetry/kql_telemetry_service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { first } from 'rxjs/operators';
import { CoreSetup, Plugin, PluginInitializerContext } from 'kibana/server';
import { registerKqlTelemetryRoute } from './route';
import { UsageCollectionSetup } from '../../../usage_collection/server';
import { makeKQLUsageCollector } from './usage_collector';

export class KqlTelemetryService implements Plugin<void> {
constructor(private initializerContext: PluginInitializerContext) {}

public async setup(
{ http, savedObjects }: CoreSetup,
{ usageCollection }: { usageCollection?: UsageCollectionSetup }
) {
registerKqlTelemetryRoute(
http.createRouter(),
savedObjects,
this.initializerContext.logger.get('data', 'kql-telemetry')
);

if (usageCollection) {
const config = await this.initializerContext.config.legacy.globalConfig$
.pipe(first())
.toPromise();
makeKQLUsageCollector(usageCollection, config.kibana.index);
}
}

public start() {}
}
64 changes: 64 additions & 0 deletions src/plugins/data/server/kql_telemetry/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { CoreSetup, IRouter, Logger } from 'kibana/server';
import { schema } from '@kbn/config-schema';

export function registerKqlTelemetryRoute(
router: IRouter,
savedObjects: CoreSetup['savedObjects'],
logger: Logger
) {
router.post(
{
path: '/api/kibana/kql_opt_in_telemetry',
validate: {
body: schema.object({
opt_in: schema.boolean(),
}),
},
},
async (context, request, response) => {
const internalRepository = savedObjects.createScopedRepository(request);

const {
body: { opt_in: optIn },
} = request;

const counterName = optIn ? 'optInCount' : 'optOutCount';

try {
await internalRepository.incrementCounter('kql-telemetry', 'kql-telemetry', counterName);
} catch (error) {
logger.warn(`Unable to increment counter: ${error}`);
return response.customError({
statusCode: error.status,
body: {
message: 'Something went wrong',
attributes: {
success: false,
},
},
});
}

return response.ok({ body: { success: true } });
}
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@
* under the License.
*/

jest.mock('../../../ui_setting_defaults', () => ({
getUiSettingDefaults: () => ({ 'search:queryLanguage': { value: 'lucene' } }),
}));

import { fetchProvider } from './fetch';
import { APICaller } from 'kibana/server';

jest.mock('../../../common', () => ({
DEFAULT_QUERY_LANGUAGE: 'lucene',
}));

let fetch;
let callCluster;
let fetch: ReturnType<typeof fetchProvider>;
let callCluster: APICaller;

function setupMockCallCluster(optCount, language) {
callCluster = jest.fn((method, params) => {
if ('id' in params && params.id === 'kql-telemetry:kql-telemetry') {
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({
_index: '.kibana_1',
Expand All @@ -46,9 +50,9 @@ function setupMockCallCluster(optCount, language) {
},
});
}
} else if ('body' in params && params.body.query.term.type === 'config') {
} else if (params && 'body' in params && params.body.query.term.type === 'config') {
if (language === 'missingConfigDoc') {
Promise.resolve({
return Promise.resolve({
hits: {
hits: [],
},
Expand All @@ -69,7 +73,9 @@ function setupMockCallCluster(optCount, language) {
});
}
}
});

throw new Error('invalid call');
}) as unknown) as APICaller;
}

describe('makeKQLUsageCollector', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
* under the License.
*/

import { getUiSettingDefaults } from '../../../ui_setting_defaults';
import { get } from 'lodash';
import { APICaller } from 'kibana/server';
import { DEFAULT_QUERY_LANGUAGE } from '../../../common';

const uiSettingDefaults = getUiSettingDefaults();
const defaultSearchQueryLanguageSetting = uiSettingDefaults['search:queryLanguage'].value;
const defaultSearchQueryLanguageSetting = DEFAULT_QUERY_LANGUAGE;

export function fetchProvider(index) {
return async callCluster => {
export function fetchProvider(index: string) {
return async (callCluster: APICaller) => {
const [response, config] = await Promise.all([
callCluster('get', {
index,
Expand All @@ -38,7 +38,10 @@ export function fetchProvider(index) {
}),
]);

const queryLanguageConfigValue = get(config, 'hits.hits[0]._source.config.search:queryLanguage');
const queryLanguageConfigValue = get(
config,
'hits.hits[0]._source.config.search:queryLanguage'
);

// search:queryLanguage can potentially be in four states in the .kibana index:
// 1. undefined: this means the user has never touched this setting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,26 @@
*/

import { makeKQLUsageCollector } from './make_kql_usage_collector';
import { UsageCollectionSetup } from '../../../../usage_collection/server';

describe('makeKQLUsageCollector', () => {
let server;
let makeUsageCollectorStub;
let registerStub;
let usageCollection;
let usageCollectionMock: jest.Mocked<UsageCollectionSetup>;

beforeEach(() => {
makeUsageCollectorStub = jest.fn();
registerStub = jest.fn();
usageCollection = {
makeUsageCollector: makeUsageCollectorStub,
registerCollector: registerStub,
};
server = {
config: () => ({ get: () => '.kibana' })
};
usageCollectionMock = ({
makeUsageCollector: jest.fn(),
registerCollector: jest.fn(),
} as unknown) as jest.Mocked<UsageCollectionSetup>;
});

it('should call registerCollector', () => {
makeKQLUsageCollector(usageCollection, server);
expect(registerStub).toHaveBeenCalledTimes(1);
makeKQLUsageCollector(usageCollectionMock, '.kibana');
expect(usageCollectionMock.registerCollector).toHaveBeenCalledTimes(1);
});

it('should call makeUsageCollector with type = kql', () => {
makeKQLUsageCollector(usageCollection, server);
expect(makeUsageCollectorStub).toHaveBeenCalledTimes(1);
expect(makeUsageCollectorStub.mock.calls[0][0].type).toBe('kql');
makeKQLUsageCollector(usageCollectionMock, '.kibana');
expect(usageCollectionMock.makeUsageCollector).toHaveBeenCalledTimes(1);
expect(usageCollectionMock.makeUsageCollector.mock.calls[0][0].type).toBe('kql');
});
});
Loading