Skip to content

Commit

Permalink
move over kql telemetry and scripts api
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Dec 10, 2019
1 parent b4a2751 commit a2b96f2
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 78 deletions.
4 changes: 0 additions & 4 deletions src/legacy/core_plugins/kibana/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down

This file was deleted.

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 { KqlTelemetryService } from './kql_telemetry_service';
35 changes: 35 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,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<void> {
constructor(private initializerContext: PluginInitializerContext) {}

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

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 } });
}
);
}
8 changes: 8 additions & 0 deletions src/plugins/data/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DataPluginSetup> {
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),
};
Expand Down
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 { ScriptsService } from './scripts_service';
31 changes: 31 additions & 0 deletions src/plugins/data/server/scripts/route.ts
Original file line number Diff line number Diff line change
@@ -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'],
});
}
);
}
29 changes: 29 additions & 0 deletions src/plugins/data/server/scripts/scripts_service.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
public setup({ http }: CoreSetup) {
registerScriptsRoute(http.createRouter());
}

public start() {}
}

0 comments on commit a2b96f2

Please sign in to comment.