Skip to content

Commit

Permalink
[lens] Basic usage telemetry for total visualizations, and by type
Browse files Browse the repository at this point in the history
  • Loading branch information
wylieconlon committed Oct 8, 2019
1 parent b9bbde6 commit 93b7af4
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 8 deletions.
11 changes: 5 additions & 6 deletions x-pack/legacy/plugins/lens/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import * as Joi from 'joi';
import { resolve } from 'path';
import { LegacyPluginInitializer } from 'src/legacy/types';
import KbnServer, { Server } from 'src/legacy/server/kbn_server';
import { CoreSetup } from 'src/core/server';
import mappings from './mappings.json';
import { PLUGIN_ID, getEditPath } from './common';
import { lensServerPlugin } from './server';
Expand Down Expand Up @@ -84,11 +83,11 @@ export const lens: LegacyPluginInitializer = kibana => {

// Set up with the new platform plugin lifecycle API.
const plugin = lensServerPlugin();
plugin.setup(({
http: {
...kbnServer.newPlatform.setup.core.http,
},
} as unknown) as CoreSetup);
plugin.setup(kbnServer.newPlatform.setup.core, {
// Legacy APIs
savedObjects: server.savedObjects,
usage: server.usage,
});

server.events.on('stop', () => {
plugin.stop();
Expand Down
12 changes: 12 additions & 0 deletions x-pack/legacy/plugins/lens/public/app_plugin/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ import {
} from '../datatable_visualization_plugin';
import { App } from './app';
import { EditorFrameInstance } from '../types';
import {
createUiStatsReporter,
METRIC_TYPE,
} from '../../../../../../src/legacy/core_plugins/ui_metric/public';

const trackLensUiMetric = createUiStatsReporter('lens');

export interface LensPluginStartDependencies {
data: DataPublicPluginStart;
Expand Down Expand Up @@ -64,6 +70,12 @@ export class AppPlugin {
this.instance = editorFrameStartInterface.createInstance({});

const renderEditor = (routeProps: RouteComponentProps<{ id?: string }>) => {
if (routeProps.match.params.id) {
trackLensUiMetric(METRIC_TYPE.LOADED, 'lens_from_saved');
} else {
trackLensUiMetric(METRIC_TYPE.LOADED, 'lens_loaded');
}

return (
<App
core={core}
Expand Down
17 changes: 15 additions & 2 deletions x-pack/legacy/plugins/lens/server/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,27 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Plugin, CoreSetup } from 'src/core/server';
import { Plugin, CoreSetup, SavedObjectsLegacyService } from 'src/core/server';
import { setupRoutes } from './routes';
import { registerLensUsageCollector } from './usage';

export class LensServer implements Plugin<{}, {}, {}, {}> {
constructor() {}

setup(core: CoreSetup) {
setup(
core: CoreSetup,
plugins: {
savedObjects: SavedObjectsLegacyService;
usage: {
collectorSet: {
makeUsageCollector: (options: unknown) => unknown;
register: (options: unknown) => unknown;
};
};
}
) {
setupRoutes(core);
registerLensUsageCollector(core, plugins);

return {};
}
Expand Down
50 changes: 50 additions & 0 deletions x-pack/legacy/plugins/lens/server/usage/collectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.
*/

import { CallCluster } from 'src/legacy/core_plugins/elasticsearch';
import { CoreSetup, SavedObjectsLegacyService } from 'src/core/server';
import { getVisualizationCounts } from './visualization_counts';

export function getSavedObjectsClient(
savedObjects: SavedObjectsLegacyService,
callAsInternalUser: unknown
) {
const { SavedObjectsClient, getSavedObjectsRepository } = savedObjects;
const internalRepository = getSavedObjectsRepository(callAsInternalUser);
return new SavedObjectsClient(internalRepository);
}

export function registerLensUsageCollector(
core: CoreSetup,
plugins: {
savedObjects: SavedObjectsLegacyService;
usage: {
collectorSet: {
makeUsageCollector: (options: unknown) => unknown;
register: (options: unknown) => unknown;
};
};
}
) {
const lensUsageCollector = plugins.usage.collectorSet.makeUsageCollector({
type: 'lens',
fetch: async (callCluster: CallCluster) => {
const savedObjectsClient = getSavedObjectsClient(plugins.savedObjects, callCluster);
try {
return getVisualizationCounts(savedObjectsClient);
} catch (err) {
return {
lens: {
total: 0,
visualization_types: {},
},
};
}
},
isReady: () => true,
});
plugins.usage.collectorSet.register(lensUsageCollector);
}
7 changes: 7 additions & 0 deletions x-pack/legacy/plugins/lens/server/usage/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 './collectors';
35 changes: 35 additions & 0 deletions x-pack/legacy/plugins/lens/server/usage/visualization_counts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.
*/

import { SavedObjectsClient } from 'src/core/server';

export async function getVisualizationCounts(savedObjectsClient: SavedObjectsClient) {
const lensSavedObjects = await savedObjectsClient.find({
type: 'lens',
});

const byType: Record<string, number> = {};

lensSavedObjects.saved_objects.forEach(({ attributes }) => {
let type;
if (attributes.visualizationType === 'lnsXY') {
type = attributes.state.visualization.preferredSeriesType;
} else {
type = attributes.visualizationType;
}

if (byType[type]) {
byType[type] += 1;
} else {
byType[type] = 1;
}
});

return {
total: lensSavedObjects.total,
visualization_types: byType,
};
}

0 comments on commit 93b7af4

Please sign in to comment.