Skip to content

Commit

Permalink
[Canvas] Migrate usage collector to NP plugin (elastic#53303)
Browse files Browse the repository at this point in the history
* Move canvas usage collector to NP plugin

* Removing old usage collector fom legacy Canvas plugin

* Adding types placeholder

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
2 people authored and jkelastic committed Jan 3, 2020
1 parent ebe652a commit 31efa66
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 17 deletions.
2 changes: 0 additions & 2 deletions x-pack/legacy/plugins/canvas/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import { CoreSetup, PluginsSetup } from './shim';
import { routes } from './routes';
import { functions } from '../canvas_plugin_src/functions/server';
import { registerCanvasUsageCollector } from './usage';
import { loadSampleData } from './sample_data';

export class Plugin {
Expand Down Expand Up @@ -61,7 +60,6 @@ export class Plugin {
},
});

registerCanvasUsageCollector(plugins.usageCollection, core);
loadSampleData(
plugins.home.sampleData.addSavedObjectsToSampleDataset,
plugins.home.sampleData.addAppLinksToSampleDataset
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/canvas/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"configPath": ["xpack", "canvas"],
"server": true,
"ui": false,
"requiredPlugins": []
"requiredPlugins": [],
"optionalPlugins": ["usageCollection"]
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@

import { CallCluster } from 'src/legacy/core_plugins/elasticsearch';
import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import { CoreSetup } from '../shim';
// @ts-ignore missing local declaration
import { CANVAS_USAGE_TYPE } from '../../common/lib/constants';
import { CANVAS_USAGE_TYPE } from '../../../../legacy/plugins/canvas/common/lib/constants';
import { TelemetryCollector } from '../../types';

import { workpadCollector } from './workpad_collector';
import { customElementCollector } from './custom_element_collector';
import { TelemetryCollector } from '../../types';

const collectors: TelemetryCollector[] = [workpadCollector, customElementCollector];

Expand All @@ -24,10 +23,13 @@ const collectors: TelemetryCollector[] = [workpadCollector, customElementCollect
A usage collector function returns an object derived from current data in the ES Cluster.
*/
export function registerCanvasUsageCollector(
usageCollection: UsageCollectionSetup,
core: CoreSetup
usageCollection: UsageCollectionSetup | undefined,
kibanaIndex: string
) {
const kibanaIndex = core.getServerConfig().get<string>('kibana.index');
if (!usageCollection) {
return;
}

const canvasCollector = usageCollection.makeUsageCollector({
type: CANVAS_USAGE_TYPE,
isReady: () => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import { SearchParams } from 'elasticsearch';
import { get } from 'lodash';
import { fromExpression } from '@kbn/interpreter/common';
import { collectFns } from './collector_helpers';
import { TelemetryCollector } from '../../types';
import { ExpressionAST, TelemetryCustomElement, TelemetryCustomElementDocument } from '../../types';
import {
ExpressionAST,
TelemetryCollector,
TelemetryCustomElement,
TelemetryCustomElementDocument,
} from '../../types';

const CUSTOM_ELEMENT_TYPE = 'canvas-element';
interface CustomElementSearch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import clonedeep from 'lodash.clonedeep';
import { summarizeWorkpads } from './workpad_collector';
import { workpads } from '../../__tests__/fixtures/workpads';
import { workpads } from '../../../../legacy/plugins/canvas/__tests__/fixtures/workpads';

describe('usage collector handle es response data', () => {
it('should summarize workpads, pages, and elements', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { SearchParams } from 'elasticsearch';
import { sum as arraySum, min as arrayMin, max as arrayMax, get } from 'lodash';
import { fromExpression } from '@kbn/interpreter/common';
import { CANVAS_TYPE } from '../../common/lib/constants';
import { CANVAS_TYPE } from '../../../../legacy/plugins/canvas/common/lib/constants';
import { collectFns } from './collector_helpers';
import { ExpressionAST, TelemetryCollector, CanvasWorkpad } from '../../types';

Expand Down
17 changes: 15 additions & 2 deletions x-pack/plugins/canvas/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,32 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { first } from 'rxjs/operators';
import { CoreSetup, PluginInitializerContext, Plugin, Logger } from 'src/core/server';
import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import { initRoutes } from './routes';
import { registerCanvasUsageCollector } from './collectors';

interface PluginsSetup {
usageCollection?: UsageCollectionSetup;
}

export class CanvasPlugin implements Plugin {
private readonly logger: Logger;
constructor(initializerContext: PluginInitializerContext) {
constructor(public readonly initializerContext: PluginInitializerContext) {
this.logger = initializerContext.logger.get();
}

public setup(coreSetup: CoreSetup): void {
public async setup(coreSetup: CoreSetup, plugins: PluginsSetup) {
const canvasRouter = coreSetup.http.createRouter();

initRoutes({ router: canvasRouter, logger: this.logger });

// we need the kibana index provided by global config for the Canvas usage collector
const globalConfig = await this.initializerContext.config.legacy.globalConfig$
.pipe(first())
.toPromise();
registerCanvasUsageCollector(plugins.usageCollection, globalConfig.kibana.index);
}

public start() {}
Expand Down
7 changes: 7 additions & 0 deletions x-pack/plugins/canvas/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export * from '../../../legacy/plugins/canvas/types';

0 comments on commit 31efa66

Please sign in to comment.