From a2b96f2edc94e3be4e2a282b8a8e604609347e02 Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Tue, 10 Dec 2019 17:28:53 +0100 Subject: [PATCH] move over kql telemetry and scripts api --- src/legacy/core_plugins/kibana/index.js | 4 -- .../server/routes/api/kql_telemetry/index.js | 60 ----------------- .../data/server/kql_telemetry/index.ts} | 6 +- .../kql_telemetry/kql_telemetry_service.ts | 35 ++++++++++ .../data/server/kql_telemetry/route.ts | 64 +++++++++++++++++++ src/plugins/data/server/plugin.ts | 8 +++ .../data/server/scripts/index.ts} | 10 +-- src/plugins/data/server/scripts/route.ts | 31 +++++++++ .../data/server/scripts/scripts_service.ts | 29 +++++++++ 9 files changed, 169 insertions(+), 78 deletions(-) delete mode 100644 src/legacy/core_plugins/kibana/server/routes/api/kql_telemetry/index.js rename src/{legacy/core_plugins/kibana/server/routes/api/scripts/index.js => plugins/data/server/kql_telemetry/index.ts} (86%) create mode 100644 src/plugins/data/server/kql_telemetry/kql_telemetry_service.ts create mode 100644 src/plugins/data/server/kql_telemetry/route.ts rename src/{legacy/core_plugins/kibana/server/routes/api/scripts/register_languages.js => plugins/data/server/scripts/index.ts} (79%) create mode 100644 src/plugins/data/server/scripts/route.ts create mode 100644 src/plugins/data/server/scripts/scripts_service.ts diff --git a/src/legacy/core_plugins/kibana/index.js b/src/legacy/core_plugins/kibana/index.js index 659ca36d84090..d8e56e4b5f0fe 100644 --- a/src/legacy/core_plugins/kibana/index.js +++ b/src/legacy/core_plugins/kibana/index.js @@ -27,9 +27,7 @@ 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'; @@ -327,13 +325,11 @@ 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); diff --git a/src/legacy/core_plugins/kibana/server/routes/api/kql_telemetry/index.js b/src/legacy/core_plugins/kibana/server/routes/api/kql_telemetry/index.js deleted file mode 100644 index 04ce8e807ea20..0000000000000 --- a/src/legacy/core_plugins/kibana/server/routes/api/kql_telemetry/index.js +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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 Joi from 'joi'; -import Boom from 'boom'; - -export function registerKqlTelemetryApi(server) { - server.route({ - path: '/api/kibana/kql_opt_in_telemetry', - method: 'POST', - config: { - validate: { - payload: Joi.object({ - opt_in: Joi.bool().required(), - }), - }, - tags: ['api'], - }, - handler: async function (request) { - const { savedObjects: { getSavedObjectsRepository } } = server; - const { callWithInternalUser } = server.plugins.elasticsearch.getCluster('admin'); - const internalRepository = getSavedObjectsRepository(callWithInternalUser); - - const { - payload: { opt_in: optIn }, - } = request; - - const counterName = optIn ? 'optInCount' : 'optOutCount'; - - try { - await internalRepository.incrementCounter( - 'kql-telemetry', - 'kql-telemetry', - counterName, - ); - } - catch (error) { - return new Boom('Something went wrong', { statusCode: error.status, data: { success: false } }); - } - - return { success: true }; - }, - }); -} diff --git a/src/legacy/core_plugins/kibana/server/routes/api/scripts/index.js b/src/plugins/data/server/kql_telemetry/index.ts similarity index 86% rename from src/legacy/core_plugins/kibana/server/routes/api/scripts/index.js rename to src/plugins/data/server/kql_telemetry/index.ts index 441963b02f14f..0191113d95007 100644 --- a/src/legacy/core_plugins/kibana/server/routes/api/scripts/index.js +++ b/src/plugins/data/server/kql_telemetry/index.ts @@ -17,8 +17,4 @@ * under the License. */ -import { registerLanguages } from './register_languages'; - -export function scriptsApi(server) { - registerLanguages(server); -} +export { KqlTelemetryService } from './kql_telemetry_service'; diff --git a/src/plugins/data/server/kql_telemetry/kql_telemetry_service.ts b/src/plugins/data/server/kql_telemetry/kql_telemetry_service.ts new file mode 100644 index 0000000000000..5890341eb41d0 --- /dev/null +++ b/src/plugins/data/server/kql_telemetry/kql_telemetry_service.ts @@ -0,0 +1,35 @@ +/* + * 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, Plugin, PluginInitializerContext } from 'kibana/server'; +import { registerKqlTelemetryRoute } from './route'; + +export class KqlTelemetryService implements Plugin { + constructor(private initializerContext: PluginInitializerContext) {} + + public setup({ http, savedObjects }: CoreSetup) { + registerKqlTelemetryRoute( + http.createRouter(), + savedObjects, + this.initializerContext.logger.get('data', 'kql-telemetry') + ); + } + + public start() {} +} diff --git a/src/plugins/data/server/kql_telemetry/route.ts b/src/plugins/data/server/kql_telemetry/route.ts new file mode 100644 index 0000000000000..3185da22b12b3 --- /dev/null +++ b/src/plugins/data/server/kql_telemetry/route.ts @@ -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 } }); + } + ); +} diff --git a/src/plugins/data/server/plugin.ts b/src/plugins/data/server/plugin.ts index e81250e653ebd..bfd04aec5711f 100644 --- a/src/plugins/data/server/plugin.ts +++ b/src/plugins/data/server/plugin.ts @@ -21,18 +21,26 @@ import { PluginInitializerContext, CoreSetup, CoreStart, Plugin } from '../../.. import { IndexPatternsService } from './index_patterns'; import { ISearchSetup } from './search'; import { SearchService } from './search/search_service'; +import { ScriptsService } from './scripts'; +import { KqlTelemetryService } from './kql_telemetry'; export interface DataPluginSetup { search: ISearchSetup; } export class DataServerPlugin implements Plugin { private readonly searchService: SearchService; + private readonly scriptsService: ScriptsService; + private readonly kqlTelemetryService: KqlTelemetryService; private readonly indexPatterns = new IndexPatternsService(); constructor(initializerContext: PluginInitializerContext) { this.searchService = new SearchService(initializerContext); + this.scriptsService = new ScriptsService(); + this.kqlTelemetryService = new KqlTelemetryService(initializerContext); } public setup(core: CoreSetup) { this.indexPatterns.setup(core); + this.scriptsService.setup(core); + this.kqlTelemetryService.setup(core); return { search: this.searchService.setup(core), }; diff --git a/src/legacy/core_plugins/kibana/server/routes/api/scripts/register_languages.js b/src/plugins/data/server/scripts/index.ts similarity index 79% rename from src/legacy/core_plugins/kibana/server/routes/api/scripts/register_languages.js rename to src/plugins/data/server/scripts/index.ts index 6b27d24dda785..d4a3e70fdbf53 100644 --- a/src/legacy/core_plugins/kibana/server/routes/api/scripts/register_languages.js +++ b/src/plugins/data/server/scripts/index.ts @@ -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 { ScriptsService } from './scripts_service'; diff --git a/src/plugins/data/server/scripts/route.ts b/src/plugins/data/server/scripts/route.ts new file mode 100644 index 0000000000000..3eb77f09b25cd --- /dev/null +++ b/src/plugins/data/server/scripts/route.ts @@ -0,0 +1,31 @@ +/* + * 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 { IRouter } from 'kibana/server'; + +export function registerScriptsRoute(router: IRouter) { + router.get( + { path: '/api/kibana/scripts/languages', validate: false }, + async (context, request, response) => { + return response.ok({ + body: ['painless', 'expression'], + }); + } + ); +} diff --git a/src/plugins/data/server/scripts/scripts_service.ts b/src/plugins/data/server/scripts/scripts_service.ts new file mode 100644 index 0000000000000..9a3a20a64276d --- /dev/null +++ b/src/plugins/data/server/scripts/scripts_service.ts @@ -0,0 +1,29 @@ +/* + * 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, Plugin } from 'kibana/server'; +import { registerScriptsRoute } from './route'; + +export class ScriptsService implements Plugin { + public setup({ http }: CoreSetup) { + registerScriptsRoute(http.createRouter()); + } + + public start() {} +}