diff --git a/x-pack/legacy/plugins/canvas/server/plugin.ts b/x-pack/legacy/plugins/canvas/server/plugin.ts index 2dc87e4a61e0..07f4b7d9ac6d 100644 --- a/x-pack/legacy/plugins/canvas/server/plugin.ts +++ b/x-pack/legacy/plugins/canvas/server/plugin.ts @@ -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 { @@ -61,7 +60,6 @@ export class Plugin { }, }); - registerCanvasUsageCollector(plugins.usageCollection, core); loadSampleData( plugins.home.sampleData.addSavedObjectsToSampleDataset, plugins.home.sampleData.addAppLinksToSampleDataset diff --git a/x-pack/plugins/canvas/kibana.json b/x-pack/plugins/canvas/kibana.json index 87214f028705..f18e7fe0590b 100644 --- a/x-pack/plugins/canvas/kibana.json +++ b/x-pack/plugins/canvas/kibana.json @@ -5,6 +5,6 @@ "configPath": ["xpack", "canvas"], "server": true, "ui": false, - "requiredPlugins": [] + "requiredPlugins": [], + "optionalPlugins": ["usageCollection"] } - \ No newline at end of file diff --git a/x-pack/legacy/plugins/canvas/server/usage/collector.ts b/x-pack/plugins/canvas/server/collectors/collector.ts similarity index 84% rename from x-pack/legacy/plugins/canvas/server/usage/collector.ts rename to x-pack/plugins/canvas/server/collectors/collector.ts index ae009f926572..8e9e5ede2e7f 100644 --- a/x-pack/legacy/plugins/canvas/server/usage/collector.ts +++ b/x-pack/plugins/canvas/server/collectors/collector.ts @@ -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]; @@ -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('kibana.index'); + if (!usageCollection) { + return; + } + const canvasCollector = usageCollection.makeUsageCollector({ type: CANVAS_USAGE_TYPE, isReady: () => true, diff --git a/x-pack/legacy/plugins/canvas/server/usage/collector_helpers.ts b/x-pack/plugins/canvas/server/collectors/collector_helpers.ts similarity index 100% rename from x-pack/legacy/plugins/canvas/server/usage/collector_helpers.ts rename to x-pack/plugins/canvas/server/collectors/collector_helpers.ts diff --git a/x-pack/legacy/plugins/canvas/server/usage/custom_element_collector.test.ts b/x-pack/plugins/canvas/server/collectors/custom_element_collector.test.ts similarity index 100% rename from x-pack/legacy/plugins/canvas/server/usage/custom_element_collector.test.ts rename to x-pack/plugins/canvas/server/collectors/custom_element_collector.test.ts diff --git a/x-pack/legacy/plugins/canvas/server/usage/custom_element_collector.ts b/x-pack/plugins/canvas/server/collectors/custom_element_collector.ts similarity index 95% rename from x-pack/legacy/plugins/canvas/server/usage/custom_element_collector.ts rename to x-pack/plugins/canvas/server/collectors/custom_element_collector.ts index 218ac0fed08c..5f1944bea3ea 100644 --- a/x-pack/legacy/plugins/canvas/server/usage/custom_element_collector.ts +++ b/x-pack/plugins/canvas/server/collectors/custom_element_collector.ts @@ -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 { diff --git a/x-pack/legacy/plugins/canvas/server/usage/index.ts b/x-pack/plugins/canvas/server/collectors/index.ts similarity index 100% rename from x-pack/legacy/plugins/canvas/server/usage/index.ts rename to x-pack/plugins/canvas/server/collectors/index.ts diff --git a/x-pack/legacy/plugins/canvas/server/usage/workpad_collector.test.ts b/x-pack/plugins/canvas/server/collectors/workpad_collector.test.ts similarity index 96% rename from x-pack/legacy/plugins/canvas/server/usage/workpad_collector.test.ts rename to x-pack/plugins/canvas/server/collectors/workpad_collector.test.ts index 420b785771bf..70bc074ff3df 100644 --- a/x-pack/legacy/plugins/canvas/server/usage/workpad_collector.test.ts +++ b/x-pack/plugins/canvas/server/collectors/workpad_collector.test.ts @@ -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', () => { diff --git a/x-pack/legacy/plugins/canvas/server/usage/workpad_collector.ts b/x-pack/plugins/canvas/server/collectors/workpad_collector.ts similarity index 98% rename from x-pack/legacy/plugins/canvas/server/usage/workpad_collector.ts rename to x-pack/plugins/canvas/server/collectors/workpad_collector.ts index 5e6e2fa6dbd6..6c86b8b2c746 100644 --- a/x-pack/legacy/plugins/canvas/server/usage/workpad_collector.ts +++ b/x-pack/plugins/canvas/server/collectors/workpad_collector.ts @@ -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'; diff --git a/x-pack/plugins/canvas/server/plugin.ts b/x-pack/plugins/canvas/server/plugin.ts index 76b86c2ac39b..0f27c68903b3 100644 --- a/x-pack/plugins/canvas/server/plugin.ts +++ b/x-pack/plugins/canvas/server/plugin.ts @@ -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() {} diff --git a/x-pack/plugins/canvas/types/index.ts b/x-pack/plugins/canvas/types/index.ts new file mode 100644 index 000000000000..014b203754a2 --- /dev/null +++ b/x-pack/plugins/canvas/types/index.ts @@ -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';