From 2cf4d7933024303c7aa99d1ec3890350166f544b Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Wed, 11 Dec 2019 17:00:40 -0700 Subject: [PATCH 01/16] Remove task logic. Remove server refs and revise for np. Migrate a few files to ts --- x-pack/legacy/plugins/maps/index.js | 12 +- .../maps_telemetry/collectors/register.ts | 27 ++++ .../maps/server/maps_telemetry/index.js | 7 - .../{maps_telemetry.js => maps_telemetry.ts} | 106 ++++++++------ .../maps_telemetry/maps_usage_collector.js | 88 ------------ .../maps_usage_collector.test.js | 69 ---------- .../server/maps_telemetry/telemetry_task.js | 130 ------------------ .../maps_telemetry/telemetry_task.test.js | 71 ---------- x-pack/legacy/plugins/maps/server/plugin.js | 7 +- 9 files changed, 102 insertions(+), 415 deletions(-) create mode 100644 x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register.ts delete mode 100644 x-pack/legacy/plugins/maps/server/maps_telemetry/index.js rename x-pack/legacy/plugins/maps/server/maps_telemetry/{maps_telemetry.js => maps_telemetry.ts} (52%) delete mode 100644 x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.js delete mode 100644 x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.test.js delete mode 100644 x-pack/legacy/plugins/maps/server/maps_telemetry/telemetry_task.js delete mode 100644 x-pack/legacy/plugins/maps/server/maps_telemetry/telemetry_task.test.js diff --git a/x-pack/legacy/plugins/maps/index.js b/x-pack/legacy/plugins/maps/index.js index e5b3d5c615013..2116bbc56c8dd 100644 --- a/x-pack/legacy/plugins/maps/index.js +++ b/x-pack/legacy/plugins/maps/index.js @@ -8,7 +8,6 @@ import { i18n } from '@kbn/i18n'; import { resolve } from 'path'; import mappings from './mappings.json'; import { migrations } from './migrations'; -import { initTelemetryCollection } from './server/maps_telemetry'; import { getAppTitle } from './common/i18n_getters'; import _ from 'lodash'; import { MapPlugin } from './server/plugin'; @@ -95,18 +94,17 @@ export function maps(kibana) { init(server) { const mapsEnabled = server.config().get('xpack.maps.enabled'); - const { usageCollection } = server.newPlatform.setup.plugins; if (!mapsEnabled) { server.log(['info', 'maps'], 'Maps app disabled by configuration'); return; } - initTelemetryCollection(usageCollection, server); const coreSetup = server.newPlatform.setup.core; const newPlatformPlugins = server.newPlatform.setup.plugins; const pluginsSetup = { featuresPlugin: newPlatformPlugins.features, licensing: newPlatformPlugins.licensing, + usageCollection: newPlatformPlugins.usageCollection }; // legacy dependencies @@ -120,7 +118,13 @@ export function maps(kibana) { xpackMainPlugin: server.plugins.xpack_main }, savedObjects: { - getSavedObjectsRepository: server.savedObjects.getSavedObjectsRepository + savedObjectsClient: (() => { + const callCluster = server.plugins.elasticsearch.getCluster('admin') + .callWithInternalUser; + const { SavedObjectsClient, getSavedObjectsRepository } = server.savedObjects; + const internalRepository = getSavedObjectsRepository(callCluster); + return new SavedObjectsClient(internalRepository); + })(), }, addSavedObjectsToSampleDataset: server.addSavedObjectsToSampleDataset, addAppLinksToSampleDataset: server.addAppLinksToSampleDataset, diff --git a/x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register.ts b/x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register.ts new file mode 100644 index 0000000000000..ebe3d38ef1428 --- /dev/null +++ b/x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register.ts @@ -0,0 +1,27 @@ +/* + * 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 { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; +// @ts-ignore +import { getMapsTelemetry, TELEMETRY_TYPE } from '../maps_telemetry'; + +export function registerMapsUsageCollector( + usageCollection: UsageCollectionSetup, + savedObjectsClient: any, + config: Function +): void { + if (!usageCollection) { + return; + } + + const mapsUsageCollector = usageCollection.makeUsageCollector({ + type: TELEMETRY_TYPE, + isReady: () => true, + fetch: async () => await getMapsTelemetry(savedObjectsClient, config), + }); + + usageCollection.registerCollector(mapsUsageCollector); +} diff --git a/x-pack/legacy/plugins/maps/server/maps_telemetry/index.js b/x-pack/legacy/plugins/maps/server/maps_telemetry/index.js deleted file mode 100644 index 513df3f765186..0000000000000 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/index.js +++ /dev/null @@ -1,7 +0,0 @@ -/* - * 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 { initTelemetryCollection } from './maps_usage_collector'; diff --git a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.ts similarity index 52% rename from x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js rename to x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.ts index 1c875322f2343..8155602fb0bea 100644 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js +++ b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.ts @@ -5,49 +5,58 @@ */ import _ from 'lodash'; +// @ts-ignore import { EMS_FILE, ES_GEO_FIELD_TYPE, MAP_SAVED_OBJECT_TYPE } from '../../common/constants'; -function getSavedObjectsClient(server, callCluster) { - const { SavedObjectsClient, getSavedObjectsRepository } = server.savedObjects; - const internalRepository = getSavedObjectsRepository(callCluster); - return new SavedObjectsClient(internalRepository); -} +export const TELEMETRY_TYPE = 'maps-telemetry'; -function getUniqueLayerCounts(layerCountsList, mapsCount) { - const uniqueLayerTypes = _.uniq(_.flatten( - layerCountsList.map(lTypes => Object.keys(lTypes)))); +function getUniqueLayerCounts(layerCountsList: any[], mapsCount: number) { + const uniqueLayerTypes = _.uniq(_.flatten(layerCountsList.map(lTypes => Object.keys(lTypes)))); - return uniqueLayerTypes.reduce((accu, type) => { - const typeCounts = layerCountsList.reduce((accu, tCounts) => { - tCounts[type] && accu.push(tCounts[type]); - return accu; + return uniqueLayerTypes.reduce((accu: any, type) => { + const typeCounts = layerCountsList.reduce((tCountsAccu, tCounts) => { + if (tCounts[type]) { + tCountsAccu.push(tCounts[type]); + } + return tCountsAccu; }, []); const typeCountsSum = _.sum(typeCounts); accu[type] = { min: typeCounts.length ? _.min(typeCounts) : 0, max: typeCounts.length ? _.max(typeCounts) : 0, - avg: typeCountsSum ? typeCountsSum / mapsCount : 0 + avg: typeCountsSum ? typeCountsSum / mapsCount : 0, }; return accu; }, {}); } -function getIndexPatternsWithGeoFieldCount(indexPatterns) { +function getIndexPatternsWithGeoFieldCount(indexPatterns: any[]) { const fieldLists = indexPatterns.map(indexPattern => JSON.parse(indexPattern.attributes.fields)); - const fieldListsWithGeoFields = fieldLists.filter(fields => { - return fields.some(field => (field.type === ES_GEO_FIELD_TYPE.GEO_POINT || field.type === ES_GEO_FIELD_TYPE.GEO_SHAPE)); - }); + const fieldListsWithGeoFields = fieldLists.filter(fields => + fields.some( + (field: any) => + field.type === ES_GEO_FIELD_TYPE.GEO_POINT || field.type === ES_GEO_FIELD_TYPE.GEO_SHAPE + ) + ); return fieldListsWithGeoFields.length; } -export function buildMapsTelemetry({ mapSavedObjects, indexPatternSavedObjects, settings }) { - const layerLists = mapSavedObjects - .map(savedMapObject => - JSON.parse(savedMapObject.attributes.layerListJSON)); +export function buildMapsTelemetry({ + mapSavedObjects, + indexPatternSavedObjects, + settings, +}: { + mapSavedObjects: any[]; + indexPatternSavedObjects: any[]; + settings: any; +}) { + const layerLists = mapSavedObjects.map(savedMapObject => + JSON.parse(savedMapObject.attributes.layerListJSON) + ); const mapsCount = layerLists.length; const dataSourcesCount = layerLists.map(lList => { - const sourceIdList = lList.map(layer => layer.sourceDescriptor.id); + const sourceIdList = lList.map((layer: any) => layer.sourceDescriptor.id); return _.uniq(sourceIdList).length; }); @@ -55,18 +64,22 @@ export function buildMapsTelemetry({ mapSavedObjects, indexPatternSavedObjects, const layerTypesCount = layerLists.map(lList => _.countBy(lList, 'type')); // Count of EMS Vector layers used - const emsLayersCount = layerLists.map(lList => _(lList) - .countBy(layer => { - const isEmsFile = _.get(layer, 'sourceDescriptor.type') === EMS_FILE; - return isEmsFile && _.get(layer, 'sourceDescriptor.id'); - }) - .pick((val, key) => key !== 'false') - .value()); + const emsLayersCount = layerLists.map(lList => + _(lList) + .countBy((layer: any) => { + const isEmsFile = _.get(layer, 'sourceDescriptor.type') === EMS_FILE; + return isEmsFile && _.get(layer, 'sourceDescriptor.id'); + }) + .pick((val, key) => key !== 'false') + .value() + ); const dataSourcesCountSum = _.sum(dataSourcesCount); const layersCountSum = _.sum(layersCount); - const indexPatternsWithGeoFieldCount = getIndexPatternsWithGeoFieldCount(indexPatternSavedObjects); + const indexPatternsWithGeoFieldCount = getIndexPatternsWithGeoFieldCount( + indexPatternSavedObjects + ); return { settings, indexPatternsWithGeoFieldCount, @@ -79,43 +92,46 @@ export function buildMapsTelemetry({ mapSavedObjects, indexPatternSavedObjects, dataSourcesCount: { min: dataSourcesCount.length ? _.min(dataSourcesCount) : 0, max: dataSourcesCount.length ? _.max(dataSourcesCount) : 0, - avg: dataSourcesCountSum ? layersCountSum / mapsCount : 0 + avg: dataSourcesCountSum ? layersCountSum / mapsCount : 0, }, // Total count of layers per map layersCount: { min: layersCount.length ? _.min(layersCount) : 0, max: layersCount.length ? _.max(layersCount) : 0, - avg: layersCountSum ? layersCountSum / mapsCount : 0 + avg: layersCountSum ? layersCountSum / mapsCount : 0, }, // Count of layers by type layerTypesCount: { - ...getUniqueLayerCounts(layerTypesCount, mapsCount) + ...getUniqueLayerCounts(layerTypesCount, mapsCount), }, // Count of layer by EMS region emsVectorLayersCount: { - ...getUniqueLayerCounts(emsLayersCount, mapsCount) - } - } + ...getUniqueLayerCounts(emsLayersCount, mapsCount), + }, + }, }; } - -async function getMapSavedObjects(savedObjectsClient) { +async function getMapSavedObjects(savedObjectsClient: any) { const mapsSavedObjects = await savedObjectsClient.find({ type: MAP_SAVED_OBJECT_TYPE }); return _.get(mapsSavedObjects, 'saved_objects', []); } -async function getIndexPatternSavedObjects(savedObjectsClient) { +async function getIndexPatternSavedObjects(savedObjectsClient: any) { const indexPatternSavedObjects = await savedObjectsClient.find({ type: 'index-pattern' }); return _.get(indexPatternSavedObjects, 'saved_objects', []); } -export async function getMapsTelemetry(server, callCluster) { - const savedObjectsClient = getSavedObjectsClient(server, callCluster); - const mapSavedObjects = await getMapSavedObjects(savedObjectsClient); - const indexPatternSavedObjects = await getIndexPatternSavedObjects(savedObjectsClient); +export async function getMapsTelemetry(savedObjectsClient: any, config: Function) { + const mapSavedObjects: Array> = await getMapSavedObjects(savedObjectsClient); + const indexPatternSavedObjects: Array> = await getIndexPatternSavedObjects( + savedObjectsClient + ); const settings = { - showMapVisualizationTypes: server.config().get('xpack.maps.showMapVisualizationTypes') + showMapVisualizationTypes: config().get('xpack.maps.showMapVisualizationTypes'), }; const mapsTelemetry = buildMapsTelemetry({ mapSavedObjects, indexPatternSavedObjects, settings }); - return await savedObjectsClient.create('maps-telemetry', mapsTelemetry, { id: 'maps-telemetry', overwrite: true }); + return await savedObjectsClient.create('maps-telemetry', mapsTelemetry, { + id: TELEMETRY_TYPE, + overwrite: true, + }); } diff --git a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.js b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.js deleted file mode 100644 index c4d755b5908f0..0000000000000 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.js +++ /dev/null @@ -1,88 +0,0 @@ -/* - * 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 _ from 'lodash'; -import { TASK_ID, scheduleTask, registerMapsTelemetryTask } from './telemetry_task'; - -export function initTelemetryCollection(usageCollection, server) { - registerMapsTelemetryTask(server); - scheduleTask(server); - registerMapsUsageCollector(usageCollection, server); -} - -async function isTaskManagerReady(server) { - const result = await fetch(server); - return result !== null; -} - -async function fetch(server) { - let docs; - const taskManager = server.plugins.task_manager; - - if (!taskManager) { - return null; - } - - try { - ({ docs } = await taskManager.fetch({ - query: { - bool: { - filter: { - term: { - _id: `task:${TASK_ID}` - } - } - } - } - })); - } catch (err) { - const errMessage = err && err.message ? err.message : err.toString(); - /* - * The usage service WILL to try to fetch from this collector before the task manager has been initialized, because the task manager - * has to wait for all plugins to initialize first. - * It's fine to ignore it as next time around it will be initialized (or it will throw a different type of error) - */ - if (errMessage.indexOf('NotInitialized') >= 0) { - return null; - } else { - throw err; - } - } - - return docs; -} - -export function buildCollectorObj(server) { - let isCollectorReady = false; - async function determineIfTaskManagerIsReady() { - let isReady = false; - try { - isReady = await isTaskManagerReady(server); - } catch (err) {} // eslint-disable-line - - if (isReady) { - isCollectorReady = true; - } else { - setTimeout(determineIfTaskManagerIsReady, 500); - } - } - determineIfTaskManagerIsReady(); - - return { - type: 'maps', - isReady: () => isCollectorReady, - fetch: async () => { - const docs = await fetch(server); - return _.get(docs, '[0].state.stats'); - }, - }; -} - -export function registerMapsUsageCollector(usageCollection, server) { - const collectorObj = buildCollectorObj(server); - const mapsUsageCollector = usageCollection.makeUsageCollector(collectorObj); - usageCollection.registerCollector(mapsUsageCollector); -} diff --git a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.test.js b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.test.js deleted file mode 100644 index 186eb41bca8a2..0000000000000 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.test.js +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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 sinon from 'sinon'; -import { - getMockCallWithInternal, - getMockKbnServer, - getMockTaskFetch, -} from '../test_utils'; -import { buildCollectorObj } from './maps_usage_collector'; - -describe('buildCollectorObj#fetch', () => { - let mockKbnServer; - - beforeEach(() => { - mockKbnServer = getMockKbnServer(); - }); - - test('can return empty stats', async () => { - const { type, fetch } = buildCollectorObj(mockKbnServer); - expect(type).toBe('maps'); - const fetchResult = await fetch(); - expect(fetchResult).toEqual({}); - }); - - test('provides known stats', async () => { - const mockTaskFetch = getMockTaskFetch([ - { - state: { - runs: 2, - stats: { wombat_sightings: { total: 712, max: 84, min: 7, avg: 63 } }, - }, - }, - ]); - mockKbnServer = getMockKbnServer(getMockCallWithInternal(), mockTaskFetch); - - const { type, fetch } = buildCollectorObj(mockKbnServer); - expect(type).toBe('maps'); - const fetchResult = await fetch(); - expect(fetchResult).toEqual( - { wombat_sightings: { total: 712, max: 84, min: 7, avg: 63 } }, - ); - }); - - describe('Error handling', () => { - test('Silently handles Task Manager NotInitialized', async () => { - const mockTaskFetch = sinon.stub(); - mockTaskFetch.rejects( - new Error('NotInitialized taskManager is still waiting for plugins to load') - ); - mockKbnServer = getMockKbnServer(getMockCallWithInternal(), mockTaskFetch); - - const { fetch } = buildCollectorObj(mockKbnServer); - await expect(fetch()).resolves.toBe(undefined); - }); - // In real life, the CollectorSet calls fetch and handles errors - test('defers the errors', async () => { - const mockTaskFetch = sinon.stub(); - mockTaskFetch.rejects(new Error('Sad violin')); - mockKbnServer = getMockKbnServer(getMockCallWithInternal(), mockTaskFetch); - - const { fetch } = buildCollectorObj(mockKbnServer); - await expect(fetch()).rejects.toMatchObject(new Error('Sad violin')); - }); - }); -}); diff --git a/x-pack/legacy/plugins/maps/server/maps_telemetry/telemetry_task.js b/x-pack/legacy/plugins/maps/server/maps_telemetry/telemetry_task.js deleted file mode 100644 index 78b04543e72f2..0000000000000 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/telemetry_task.js +++ /dev/null @@ -1,130 +0,0 @@ -/* - * 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 { getMapsTelemetry } from './maps_telemetry'; - -const TELEMETRY_TASK_TYPE = 'maps_telemetry'; - -export const TASK_ID = `Maps-${TELEMETRY_TASK_TYPE}`; - -export function scheduleTask(server) { - const taskManager = server.plugins.task_manager; - - if (!taskManager) { - server.log(['debug', 'telemetry'], `Task manager is not available`); - return; - } - - const { kbnServer } = server.plugins.xpack_main.status.plugin; - - kbnServer.afterPluginsInit(() => { - // The code block below can't await directly within "afterPluginsInit" - // callback due to circular dependency. The server isn't "ready" until - // this code block finishes. Migrations wait for server to be ready before - // executing. Saved objects repository waits for migrations to finish before - // finishing the request. To avoid this, we'll await within a separate - // function block. - (async () => { - try { - await taskManager.ensureScheduled({ - id: TASK_ID, - taskType: TELEMETRY_TASK_TYPE, - state: { stats: {}, runs: 0 }, - }); - } catch(e) { - server.log(['warning', 'maps'], `Error scheduling telemetry task, received ${e.message}`); - } - })(); - }); -} - -export function registerMapsTelemetryTask(server) { - const taskManager = server.plugins.task_manager; - - if (!taskManager) { - server.log(['debug', 'telemetry'], `Task manager is not available`); - return; - } - - taskManager.registerTaskDefinitions({ - [TELEMETRY_TASK_TYPE]: { - title: 'Maps telemetry fetch task', - type: TELEMETRY_TASK_TYPE, - timeout: '1m', - createTaskRunner: telemetryTaskRunner(server), - }, - }); -} - -export function telemetryTaskRunner(server) { - - return ({ taskInstance }) => { - const { state } = taskInstance; - const prevState = state; - - const callCluster = server.plugins.elasticsearch.getCluster('admin') - .callWithInternalUser; - - let mapsTelemetryTask; - - return { - async run({ taskCanceled = false } = {}) { - try { - mapsTelemetryTask = makeCancelable( - getMapsTelemetry(server, callCluster), - taskCanceled - ); - } catch (err) { - server.log(['warning'], `Error loading maps telemetry: ${err}`); - } finally { - return mapsTelemetryTask - .promise - .then((mapsTelemetry = {}) => { - return { - state: { - runs: state.runs || 0 + 1, - stats: mapsTelemetry.attributes || prevState.stats || {}, - }, - runAt: getNextMidnight(), - }; - }) - .catch(errMsg => server.log(['warning'], - `Error executing maps telemetry task: ${errMsg}`)); - } - }, - async cancel() { - if (mapsTelemetryTask) { - mapsTelemetryTask.cancel(); - } else { - server.log(['warning'], `Can not cancel "mapsTelemetryTask", it has not been defined`); - } - } - }; - }; -} - -function makeCancelable(promise, isCanceled) { - const logMsg = 'Maps telemetry task has been cancelled'; - const wrappedPromise = new Promise((resolve, reject) => { - promise - .then(val => isCanceled ? reject(logMsg) : resolve(val)) - .catch(err => isCanceled ? reject(logMsg) : reject(err.message)); - }); - - return { - promise: wrappedPromise, - cancel() { - isCanceled = true; - }, - }; -} - -function getNextMidnight() { - const nextMidnight = new Date(); - nextMidnight.setHours(0, 0, 0, 0); - nextMidnight.setDate(nextMidnight.getDate() + 1); - return nextMidnight; -} diff --git a/x-pack/legacy/plugins/maps/server/maps_telemetry/telemetry_task.test.js b/x-pack/legacy/plugins/maps/server/maps_telemetry/telemetry_task.test.js deleted file mode 100644 index d5d90e8a11335..0000000000000 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/telemetry_task.test.js +++ /dev/null @@ -1,71 +0,0 @@ -/* - * 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 { - getMockKbnServer, - getMockTaskInstance, -} from '../test_utils'; -import { telemetryTaskRunner } from './telemetry_task'; -import * as mapsTelemetry from './maps_telemetry'; -jest.mock('./maps_telemetry'); - -const expectedAttributes = { - expect: 'values', - toBe: 'populated' -}; - -const generateTelemetry = ({ includeAttributes = true } = {}) => { - mapsTelemetry.getMapsTelemetry = async () => ({ // eslint-disable-line - attributes: includeAttributes ? expectedAttributes : {} - }); -}; - -describe('telemetryTaskRunner', () => { - let mockTaskInstance; - let mockKbnServer; - let taskRunner; - - beforeEach(() => { - mockTaskInstance = getMockTaskInstance(); - mockKbnServer = getMockKbnServer(); - taskRunner = telemetryTaskRunner(mockKbnServer)({ taskInstance: mockTaskInstance }); - }); - - test('returns empty stats as default', async () => { - generateTelemetry({ includeAttributes: false }); - - const runResult = await taskRunner.run(); - - expect(runResult).toMatchObject({ - state: { - runs: 1, - stats: {}, - }, - }); - }); - - // Return stats when run normally - test('returns stats normally', async () => { - generateTelemetry(); - - const runResult = await taskRunner.run(); - - expect(runResult).toMatchObject({ - state: { - runs: 1, - stats: expectedAttributes, - }, - }); - }); - - test('cancels when cancel flag set to "true", returns undefined', async () => { - generateTelemetry(); - - const runResult = await taskRunner.run({ taskCanceled: true }); - - expect(runResult).toBe(undefined); - }); -}); diff --git a/x-pack/legacy/plugins/maps/server/plugin.js b/x-pack/legacy/plugins/maps/server/plugin.js index bd1360730bdb8..0996d7cde8db3 100644 --- a/x-pack/legacy/plugins/maps/server/plugin.js +++ b/x-pack/legacy/plugins/maps/server/plugin.js @@ -11,10 +11,11 @@ import { getFlightsSavedObjects } from './sample_data/flights_saved_objects.js'; import { getWebLogsSavedObjects } from './sample_data/web_logs_saved_objects.js'; import { checkLicense } from '../check_license'; import { watchStatusAndLicenseToInitialize } from '../../../server/lib/watch_status_and_license_to_initialize'; +import { registerMapsUsageCollector } from './maps_telemetry/collectors/register'; export class MapPlugin { setup(core, plugins, __LEGACY) { - const { featuresPlugin } = plugins; + const { featuresPlugin, usageCollection } = plugins; let routesInitialized = false; featuresPlugin.registerFeature({ @@ -56,6 +57,10 @@ export class MapPlugin { .feature(APP_ID) .registerLicenseCheckResultsGenerator(checkLicense); + // Init telemetry + const { savedObjectsClient } = __LEGACY.savedObjects; + registerMapsUsageCollector(usageCollection, savedObjectsClient, __LEGACY.config); + const sampleDataLinkLabel = i18n.translate('xpack.maps.sampleDataLinkLabel', { defaultMessage: 'Map' }); From 5a466b0050e35e765ed4b23129b411180b3a1172 Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Wed, 11 Dec 2019 17:09:11 -0700 Subject: [PATCH 02/16] Remove unused reference --- x-pack/legacy/plugins/file_upload/public/plugin.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/x-pack/legacy/plugins/file_upload/public/plugin.ts b/x-pack/legacy/plugins/file_upload/public/plugin.ts index cc9ebbfc15b39..53b292b02760f 100644 --- a/x-pack/legacy/plugins/file_upload/public/plugin.ts +++ b/x-pack/legacy/plugins/file_upload/public/plugin.ts @@ -6,8 +6,6 @@ import { Plugin, CoreStart } from 'src/core/public'; // @ts-ignore -import { initResources } from './util/indexing_service'; -// @ts-ignore import { JsonUploadAndParse } from './components/json_upload_and_parse'; // @ts-ignore import { initServicesAndConstants } from './kibana_services'; From 310485c9bea5142ca456cfa38393f42b87037903 Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Fri, 13 Dec 2019 15:12:30 -0700 Subject: [PATCH 03/16] Update mappings --- x-pack/legacy/plugins/maps/mappings.json | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/x-pack/legacy/plugins/maps/mappings.json b/x-pack/legacy/plugins/maps/mappings.json index 7f80512980f0a..5e2e8c2c7e6e5 100644 --- a/x-pack/legacy/plugins/maps/mappings.json +++ b/x-pack/legacy/plugins/maps/mappings.json @@ -26,6 +26,16 @@ }, "maps-telemetry": { "properties": { + "settings": { + "properties": { + "showMapVisualizationTypes": { + "type": "boolean" + } + } + }, + "indexPatternsWithGeoFieldCount": { + "type": "long" + }, "mapsTotalCount": { "type": "long" }, @@ -72,4 +82,4 @@ } } } -} \ No newline at end of file +} From 92e07b50f5b1bf617df79ed7b38ad83c6b6e6fb8 Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Tue, 17 Dec 2019 15:37:25 -0700 Subject: [PATCH 04/16] Test usage collector register --- .../collectors/register_collector.test.js | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register_collector.test.js diff --git a/x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register_collector.test.js b/x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register_collector.test.js new file mode 100644 index 0000000000000..6bf5f5f6900aa --- /dev/null +++ b/x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register_collector.test.js @@ -0,0 +1,38 @@ +/* + * 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 { registerMapsUsageCollector } from './register'; + +describe('buildCollectorObj#fetch', () => { + let makeUsageCollectorStub; + let savedObjectsClient; + let registerStub; + let usageCollection; + let config; + + beforeEach(() => { + makeUsageCollectorStub = jest.fn(); + savedObjectsClient = jest.fn(); + registerStub = jest.fn(); + config = jest.fn(); + usageCollection = { + makeUsageCollector: makeUsageCollectorStub, + registerCollector: registerStub, + }; + }); + + test('makes and registers maps usage collector', async () => { + registerMapsUsageCollector(usageCollection, savedObjectsClient, config); + + expect(registerStub).toHaveBeenCalledTimes(0); + expect(makeUsageCollectorStub).toHaveBeenCalledTimes(0); + expect(makeUsageCollectorStub).toHaveBeenCalledWith({ + type: expect.any(String), + isReady: expect.any(Function), + fetch: expect.any(Function), + }); + }); +}); From 1196755e0c88bfa91dcf4eaf3e8817d6c50fcc1d Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Tue, 17 Dec 2019 15:38:35 -0700 Subject: [PATCH 05/16] Update api integration tests to include maps now that telemetry is 'normal' (not using task mgr state) --- x-pack/test/api_integration/apis/telemetry/telemetry_local.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/x-pack/test/api_integration/apis/telemetry/telemetry_local.js b/x-pack/test/api_integration/apis/telemetry/telemetry_local.js index 0bf24be0fa0b0..25f7c82569fa2 100644 --- a/x-pack/test/api_integration/apis/telemetry/telemetry_local.js +++ b/x-pack/test/api_integration/apis/telemetry/telemetry_local.js @@ -89,6 +89,7 @@ export default function({ getService }) { expect(stats.stack_stats.kibana.os.platformReleases[0].count).to.be(1); expect(stats.stack_stats.xpack.graph).to.be.an('object'); + expect(stats.stack_stats.xpack.maps).to.be.an('object'); expect(stats.stack_stats.xpack.transform).to.be.an('object'); expect(stats.stack_stats.xpack.transform.available).to.be.an('boolean'); expect(stats.stack_stats.xpack.transform.enabled).to.be.an('boolean'); @@ -162,6 +163,7 @@ export default function({ getService }) { 'stack_stats.kibana.visualization', 'stack_stats.xpack.ccr', 'stack_stats.xpack.transform', + 'stack_stats.xpack.maps', 'stack_stats.xpack.graph', 'stack_stats.xpack.ilm', 'stack_stats.xpack.logstash', From 60c7ab80cef30dcad807bf5275297fde9de94ac2 Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Tue, 17 Dec 2019 16:32:26 -0700 Subject: [PATCH 06/16] Update integration test to use stack stats --- x-pack/test/api_integration/apis/telemetry/telemetry_local.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/test/api_integration/apis/telemetry/telemetry_local.js b/x-pack/test/api_integration/apis/telemetry/telemetry_local.js index 25f7c82569fa2..5a66702ad0c46 100644 --- a/x-pack/test/api_integration/apis/telemetry/telemetry_local.js +++ b/x-pack/test/api_integration/apis/telemetry/telemetry_local.js @@ -79,6 +79,8 @@ export default function({ getService }) { expect(stats.stack_stats.kibana.plugins.apm.services_per_agent).to.be.an('object'); expect(stats.stack_stats.kibana.plugins.infraops.last_24_hours).to.be.an('object'); expect(stats.stack_stats.kibana.plugins.kql.defaultQueryLanguage).to.be.a('string'); + expect(stats.stack_stats.kibana.plugins.maps).to.be.an('object'); + expect(stats.stack_stats.kibana.plugins.reporting.enabled).to.be(true); expect(stats.stack_stats.kibana.plugins.rollups.index_patterns).to.be.an('object'); expect(stats.stack_stats.kibana.plugins.spaces.available).to.be(true); @@ -89,7 +91,6 @@ export default function({ getService }) { expect(stats.stack_stats.kibana.os.platformReleases[0].count).to.be(1); expect(stats.stack_stats.xpack.graph).to.be.an('object'); - expect(stats.stack_stats.xpack.maps).to.be.an('object'); expect(stats.stack_stats.xpack.transform).to.be.an('object'); expect(stats.stack_stats.xpack.transform.available).to.be.an('boolean'); expect(stats.stack_stats.xpack.transform.enabled).to.be.an('boolean'); @@ -163,7 +164,6 @@ export default function({ getService }) { 'stack_stats.kibana.visualization', 'stack_stats.xpack.ccr', 'stack_stats.xpack.transform', - 'stack_stats.xpack.maps', 'stack_stats.xpack.graph', 'stack_stats.xpack.ilm', 'stack_stats.xpack.logstash', From 1157510e6c206079c94653b7e771a68c0d9321b4 Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Wed, 18 Dec 2019 12:51:29 -0700 Subject: [PATCH 07/16] Update integration test to look for 'maps-telemetry' instead of 'maps' --- x-pack/test/api_integration/apis/telemetry/telemetry_local.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/test/api_integration/apis/telemetry/telemetry_local.js b/x-pack/test/api_integration/apis/telemetry/telemetry_local.js index 5a66702ad0c46..bb9306cbc1b23 100644 --- a/x-pack/test/api_integration/apis/telemetry/telemetry_local.js +++ b/x-pack/test/api_integration/apis/telemetry/telemetry_local.js @@ -79,7 +79,7 @@ export default function({ getService }) { expect(stats.stack_stats.kibana.plugins.apm.services_per_agent).to.be.an('object'); expect(stats.stack_stats.kibana.plugins.infraops.last_24_hours).to.be.an('object'); expect(stats.stack_stats.kibana.plugins.kql.defaultQueryLanguage).to.be.a('string'); - expect(stats.stack_stats.kibana.plugins.maps).to.be.an('object'); + expect(stats.stack_stats.kibana.plugins['maps-telemetry']).to.be.an('object'); expect(stats.stack_stats.kibana.plugins.reporting.enabled).to.be(true); expect(stats.stack_stats.kibana.plugins.rollups.index_patterns).to.be.an('object'); From a64b67f7c9c5bf0ae29a8b27cc454017f5a230fe Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Wed, 18 Dec 2019 14:05:44 -0700 Subject: [PATCH 08/16] Update jest test to reflect calls to register --- .../maps_telemetry/collectors/register_collector.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register_collector.test.js b/x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register_collector.test.js index 6bf5f5f6900aa..33eb33100acdf 100644 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register_collector.test.js +++ b/x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register_collector.test.js @@ -27,8 +27,8 @@ describe('buildCollectorObj#fetch', () => { test('makes and registers maps usage collector', async () => { registerMapsUsageCollector(usageCollection, savedObjectsClient, config); - expect(registerStub).toHaveBeenCalledTimes(0); - expect(makeUsageCollectorStub).toHaveBeenCalledTimes(0); + expect(registerStub).toHaveBeenCalledTimes(1); + expect(makeUsageCollectorStub).toHaveBeenCalledTimes(1); expect(makeUsageCollectorStub).toHaveBeenCalledWith({ type: expect.any(String), isReady: expect.any(Function), From adb52cdf1cb45ac0083da798507a1e863bab16f0 Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Thu, 19 Dec 2019 08:24:09 -0700 Subject: [PATCH 09/16] Follow the same pattern as other int tests and test reliable nested attribute --- x-pack/test/api_integration/apis/telemetry/telemetry_local.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/x-pack/test/api_integration/apis/telemetry/telemetry_local.js b/x-pack/test/api_integration/apis/telemetry/telemetry_local.js index bb9306cbc1b23..ef7124e2864f8 100644 --- a/x-pack/test/api_integration/apis/telemetry/telemetry_local.js +++ b/x-pack/test/api_integration/apis/telemetry/telemetry_local.js @@ -79,7 +79,9 @@ export default function({ getService }) { expect(stats.stack_stats.kibana.plugins.apm.services_per_agent).to.be.an('object'); expect(stats.stack_stats.kibana.plugins.infraops.last_24_hours).to.be.an('object'); expect(stats.stack_stats.kibana.plugins.kql.defaultQueryLanguage).to.be.a('string'); - expect(stats.stack_stats.kibana.plugins['maps-telemetry']).to.be.an('object'); + expect(stats.stack_stats.kibana.plugins['maps-telemetry'].attributes.timeCaptured).to.be.a( + 'string' + ); expect(stats.stack_stats.kibana.plugins.reporting.enabled).to.be(true); expect(stats.stack_stats.kibana.plugins.rollups.index_patterns).to.be.an('object'); From 60cfdf2237711b4c2fe9c73343d479925ed23b92 Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Thu, 19 Dec 2019 09:21:33 -0700 Subject: [PATCH 10/16] Back out np-related changes for separate PR --- x-pack/legacy/plugins/maps/index.js | 11 +--- .../maps_telemetry/collectors/register.ts | 5 +- .../collectors/register_collector.test.js | 7 +- .../{maps_telemetry.ts => maps_telemetry.js} | 64 +++++++++---------- x-pack/legacy/plugins/maps/server/plugin.js | 7 +- 5 files changed, 38 insertions(+), 56 deletions(-) rename x-pack/legacy/plugins/maps/server/maps_telemetry/{maps_telemetry.ts => maps_telemetry.js} (69%) diff --git a/x-pack/legacy/plugins/maps/index.js b/x-pack/legacy/plugins/maps/index.js index b5ccd14993992..a2f113cb44cc7 100644 --- a/x-pack/legacy/plugins/maps/index.js +++ b/x-pack/legacy/plugins/maps/index.js @@ -8,6 +8,7 @@ import { i18n } from '@kbn/i18n'; import { resolve } from 'path'; import mappings from './mappings.json'; import { migrations } from './migrations'; +import { registerMapsUsageCollector } from './server/maps_telemetry/collectors/register'; import { getAppTitle } from './common/i18n_getters'; import _ from 'lodash'; import { MapPlugin } from './server/plugin'; @@ -89,17 +90,18 @@ export function maps(kibana) { init(server) { const mapsEnabled = server.config().get('xpack.maps.enabled'); + const { usageCollection } = server.newPlatform.setup.plugins; if (!mapsEnabled) { server.log(['info', 'maps'], 'Maps app disabled by configuration'); return; } + registerMapsUsageCollector(usageCollection, server); const coreSetup = server.newPlatform.setup.core; const newPlatformPlugins = server.newPlatform.setup.plugins; const pluginsSetup = { featuresPlugin: newPlatformPlugins.features, licensing: newPlatformPlugins.licensing, - usageCollection: newPlatformPlugins.usageCollection, }; // legacy dependencies @@ -113,13 +115,6 @@ export function maps(kibana) { elasticsearch: server.plugins.elasticsearch, }, savedObjects: { - savedObjectsClient: (() => { - const callCluster = server.plugins.elasticsearch.getCluster('admin') - .callWithInternalUser; - const { SavedObjectsClient, getSavedObjectsRepository } = server.savedObjects; - const internalRepository = getSavedObjectsRepository(callCluster); - return new SavedObjectsClient(internalRepository); - })(), getSavedObjectsRepository: server.savedObjects.getSavedObjectsRepository, }, addSavedObjectsToSampleDataset: server.addSavedObjectsToSampleDataset, diff --git a/x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register.ts b/x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register.ts index ebe3d38ef1428..450212e50eb7e 100644 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register.ts +++ b/x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register.ts @@ -10,8 +10,7 @@ import { getMapsTelemetry, TELEMETRY_TYPE } from '../maps_telemetry'; export function registerMapsUsageCollector( usageCollection: UsageCollectionSetup, - savedObjectsClient: any, - config: Function + server: any ): void { if (!usageCollection) { return; @@ -20,7 +19,7 @@ export function registerMapsUsageCollector( const mapsUsageCollector = usageCollection.makeUsageCollector({ type: TELEMETRY_TYPE, isReady: () => true, - fetch: async () => await getMapsTelemetry(savedObjectsClient, config), + fetch: async () => await getMapsTelemetry(server), }); usageCollection.registerCollector(mapsUsageCollector); diff --git a/x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register_collector.test.js b/x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register_collector.test.js index 33eb33100acdf..638b87e68c64d 100644 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register_collector.test.js +++ b/x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register_collector.test.js @@ -8,16 +8,12 @@ import { registerMapsUsageCollector } from './register'; describe('buildCollectorObj#fetch', () => { let makeUsageCollectorStub; - let savedObjectsClient; let registerStub; let usageCollection; - let config; beforeEach(() => { makeUsageCollectorStub = jest.fn(); - savedObjectsClient = jest.fn(); registerStub = jest.fn(); - config = jest.fn(); usageCollection = { makeUsageCollector: makeUsageCollectorStub, registerCollector: registerStub, @@ -25,7 +21,8 @@ describe('buildCollectorObj#fetch', () => { }); test('makes and registers maps usage collector', async () => { - registerMapsUsageCollector(usageCollection, savedObjectsClient, config); + const serverPlaceholder = {}; + registerMapsUsageCollector(usageCollection, serverPlaceholder); expect(registerStub).toHaveBeenCalledTimes(1); expect(makeUsageCollectorStub).toHaveBeenCalledTimes(1); diff --git a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.ts b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js similarity index 69% rename from x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.ts rename to x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js index 8155602fb0bea..ce5d60ac51fa3 100644 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.ts +++ b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js @@ -5,20 +5,24 @@ */ import _ from 'lodash'; -// @ts-ignore import { EMS_FILE, ES_GEO_FIELD_TYPE, MAP_SAVED_OBJECT_TYPE } from '../../common/constants'; export const TELEMETRY_TYPE = 'maps-telemetry'; -function getUniqueLayerCounts(layerCountsList: any[], mapsCount: number) { +function getSavedObjectsClient(server) { + const { SavedObjectsClient, getSavedObjectsRepository } = server.savedObjects; + const callCluster = server.plugins.elasticsearch.getCluster('admin').callWithInternalUser; + const internalRepository = getSavedObjectsRepository(callCluster); + return new SavedObjectsClient(internalRepository); +} + +function getUniqueLayerCounts(layerCountsList, mapsCount) { const uniqueLayerTypes = _.uniq(_.flatten(layerCountsList.map(lTypes => Object.keys(lTypes)))); - return uniqueLayerTypes.reduce((accu: any, type) => { - const typeCounts = layerCountsList.reduce((tCountsAccu, tCounts) => { - if (tCounts[type]) { - tCountsAccu.push(tCounts[type]); - } - return tCountsAccu; + return uniqueLayerTypes.reduce((accu, type) => { + const typeCounts = layerCountsList.reduce((accu, tCounts) => { + tCounts[type] && accu.push(tCounts[type]); + return accu; }, []); const typeCountsSum = _.sum(typeCounts); accu[type] = { @@ -30,33 +34,25 @@ function getUniqueLayerCounts(layerCountsList: any[], mapsCount: number) { }, {}); } -function getIndexPatternsWithGeoFieldCount(indexPatterns: any[]) { +function getIndexPatternsWithGeoFieldCount(indexPatterns) { const fieldLists = indexPatterns.map(indexPattern => JSON.parse(indexPattern.attributes.fields)); - const fieldListsWithGeoFields = fieldLists.filter(fields => - fields.some( - (field: any) => + const fieldListsWithGeoFields = fieldLists.filter(fields => { + return fields.some( + field => field.type === ES_GEO_FIELD_TYPE.GEO_POINT || field.type === ES_GEO_FIELD_TYPE.GEO_SHAPE - ) - ); + ); + }); return fieldListsWithGeoFields.length; } -export function buildMapsTelemetry({ - mapSavedObjects, - indexPatternSavedObjects, - settings, -}: { - mapSavedObjects: any[]; - indexPatternSavedObjects: any[]; - settings: any; -}) { +export function buildMapsTelemetry({ mapSavedObjects, indexPatternSavedObjects, settings }) { const layerLists = mapSavedObjects.map(savedMapObject => JSON.parse(savedMapObject.attributes.layerListJSON) ); const mapsCount = layerLists.length; const dataSourcesCount = layerLists.map(lList => { - const sourceIdList = lList.map((layer: any) => layer.sourceDescriptor.id); + const sourceIdList = lList.map(layer => layer.sourceDescriptor.id); return _.uniq(sourceIdList).length; }); @@ -66,7 +62,7 @@ export function buildMapsTelemetry({ // Count of EMS Vector layers used const emsLayersCount = layerLists.map(lList => _(lList) - .countBy((layer: any) => { + .countBy(layer => { const isEmsFile = _.get(layer, 'sourceDescriptor.type') === EMS_FILE; return isEmsFile && _.get(layer, 'sourceDescriptor.id'); }) @@ -111,26 +107,26 @@ export function buildMapsTelemetry({ }, }; } -async function getMapSavedObjects(savedObjectsClient: any) { + +async function getMapSavedObjects(savedObjectsClient) { const mapsSavedObjects = await savedObjectsClient.find({ type: MAP_SAVED_OBJECT_TYPE }); return _.get(mapsSavedObjects, 'saved_objects', []); } -async function getIndexPatternSavedObjects(savedObjectsClient: any) { +async function getIndexPatternSavedObjects(savedObjectsClient) { const indexPatternSavedObjects = await savedObjectsClient.find({ type: 'index-pattern' }); return _.get(indexPatternSavedObjects, 'saved_objects', []); } -export async function getMapsTelemetry(savedObjectsClient: any, config: Function) { - const mapSavedObjects: Array> = await getMapSavedObjects(savedObjectsClient); - const indexPatternSavedObjects: Array> = await getIndexPatternSavedObjects( - savedObjectsClient - ); +export async function getMapsTelemetry(server) { + const savedObjectsClient = getSavedObjectsClient(server); + const mapSavedObjects = await getMapSavedObjects(savedObjectsClient); + const indexPatternSavedObjects = await getIndexPatternSavedObjects(savedObjectsClient); const settings = { - showMapVisualizationTypes: config().get('xpack.maps.showMapVisualizationTypes'), + showMapVisualizationTypes: server.config().get('xpack.maps.showMapVisualizationTypes'), }; const mapsTelemetry = buildMapsTelemetry({ mapSavedObjects, indexPatternSavedObjects, settings }); - return await savedObjectsClient.create('maps-telemetry', mapsTelemetry, { + return await savedObjectsClient.create(TELEMETRY_TYPE, mapsTelemetry, { id: TELEMETRY_TYPE, overwrite: true, }); diff --git a/x-pack/legacy/plugins/maps/server/plugin.js b/x-pack/legacy/plugins/maps/server/plugin.js index 71190f38db447..e6b474e1c78dd 100644 --- a/x-pack/legacy/plugins/maps/server/plugin.js +++ b/x-pack/legacy/plugins/maps/server/plugin.js @@ -8,13 +8,12 @@ import { APP_ID, APP_ICON, createMapPath, MAP_SAVED_OBJECT_TYPE } from '../commo import { getEcommerceSavedObjects } from './sample_data/ecommerce_saved_objects'; import { getFlightsSavedObjects } from './sample_data/flights_saved_objects.js'; import { getWebLogsSavedObjects } from './sample_data/web_logs_saved_objects.js'; -import { registerMapsUsageCollector } from './maps_telemetry/collectors/register'; import { LICENSE_CHECK_STATE } from '../../../../plugins/licensing/server'; import { initRoutes } from './routes'; export class MapPlugin { setup(core, plugins, __LEGACY) { - const { featuresPlugin, licensing, usageCollection } = plugins; + const { featuresPlugin, licensing } = plugins; let routesInitialized = false; featuresPlugin.registerFeature({ @@ -52,10 +51,6 @@ export class MapPlugin { } }); - // Init telemetry - const { savedObjectsClient } = __LEGACY.savedObjects; - registerMapsUsageCollector(usageCollection, savedObjectsClient, __LEGACY.config); - const sampleDataLinkLabel = i18n.translate('xpack.maps.sampleDataLinkLabel', { defaultMessage: 'Map', }); From 00180ef7cabdbd6090d2451ecf2eb5916d9ccb44 Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Mon, 23 Dec 2019 13:28:17 -0700 Subject: [PATCH 11/16] timeCaptured hasn't changed but for some reason stopped working. Getting iso string fixes issue --- .../legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js index ce5d60ac51fa3..2363082856dcf 100644 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js +++ b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js @@ -82,7 +82,7 @@ export function buildMapsTelemetry({ mapSavedObjects, indexPatternSavedObjects, // Total count of maps mapsTotalCount: mapsCount, // Time of capture - timeCaptured: new Date(), + timeCaptured: new Date().toISOString(), attributesPerMap: { // Count of data sources per map dataSourcesCount: { From 6734535fcc8240f43d445fab831757a9d9ccdb62 Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Thu, 26 Dec 2019 11:52:02 -0700 Subject: [PATCH 12/16] Back out file shuffling for separate PR --- x-pack/legacy/plugins/maps/index.js | 4 ++-- .../legacy/plugins/maps/server/maps_telemetry/index.js | 7 +++++++ .../{collectors/register.ts => maps_usage_collector.js} | 9 ++------- ...er_collector.test.js => maps_usage_collector.test.js} | 4 ++-- 4 files changed, 13 insertions(+), 11 deletions(-) create mode 100644 x-pack/legacy/plugins/maps/server/maps_telemetry/index.js rename x-pack/legacy/plugins/maps/server/maps_telemetry/{collectors/register.ts => maps_usage_collector.js} (66%) rename x-pack/legacy/plugins/maps/server/maps_telemetry/{collectors/register_collector.test.js => maps_usage_collector.test.js} (88%) diff --git a/x-pack/legacy/plugins/maps/index.js b/x-pack/legacy/plugins/maps/index.js index 2f1ef9e505cc2..83362e73fb314 100644 --- a/x-pack/legacy/plugins/maps/index.js +++ b/x-pack/legacy/plugins/maps/index.js @@ -8,7 +8,7 @@ import { i18n } from '@kbn/i18n'; import { resolve } from 'path'; import mappings from './mappings.json'; import { migrations } from './migrations'; -import { registerMapsUsageCollector } from './server/maps_telemetry/collectors/register'; +import { initTelemetryCollection } from './server/maps_telemetry'; import { getAppTitle } from './common/i18n_getters'; import _ from 'lodash'; import { MapPlugin } from './server/plugin'; @@ -95,7 +95,7 @@ export function maps(kibana) { server.log(['info', 'maps'], 'Maps app disabled by configuration'); return; } - registerMapsUsageCollector(usageCollection, server); + initTelemetryCollection(usageCollection, server); const coreSetup = server.newPlatform.setup.core; const newPlatformPlugins = server.newPlatform.setup.plugins; diff --git a/x-pack/legacy/plugins/maps/server/maps_telemetry/index.js b/x-pack/legacy/plugins/maps/server/maps_telemetry/index.js new file mode 100644 index 0000000000000..513df3f765186 --- /dev/null +++ b/x-pack/legacy/plugins/maps/server/maps_telemetry/index.js @@ -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 { initTelemetryCollection } from './maps_usage_collector'; diff --git a/x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register.ts b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.js similarity index 66% rename from x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register.ts rename to x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.js index 450212e50eb7e..c236abad74f56 100644 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register.ts +++ b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.js @@ -4,14 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ -import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; -// @ts-ignore -import { getMapsTelemetry, TELEMETRY_TYPE } from '../maps_telemetry'; +import { getMapsTelemetry, TELEMETRY_TYPE } from './maps_telemetry'; -export function registerMapsUsageCollector( - usageCollection: UsageCollectionSetup, - server: any -): void { +export function initTelemetryCollection(usageCollection, server) { if (!usageCollection) { return; } diff --git a/x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register_collector.test.js b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.test.js similarity index 88% rename from x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register_collector.test.js rename to x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.test.js index 638b87e68c64d..c5a3fca89b560 100644 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register_collector.test.js +++ b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.test.js @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { registerMapsUsageCollector } from './register'; +import { initTelemetryCollection } from './maps_usage_collector'; describe('buildCollectorObj#fetch', () => { let makeUsageCollectorStub; @@ -22,7 +22,7 @@ describe('buildCollectorObj#fetch', () => { test('makes and registers maps usage collector', async () => { const serverPlaceholder = {}; - registerMapsUsageCollector(usageCollection, serverPlaceholder); + initTelemetryCollection(usageCollection, serverPlaceholder); expect(registerStub).toHaveBeenCalledTimes(1); expect(makeUsageCollectorStub).toHaveBeenCalledTimes(1); From 756707b98632199ac3ad2c68295f2ccf886204ff Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Thu, 26 Dec 2019 14:06:08 -0700 Subject: [PATCH 13/16] Remove mappings updates (handled in separate PR) --- x-pack/legacy/plugins/maps/mappings.json | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/x-pack/legacy/plugins/maps/mappings.json b/x-pack/legacy/plugins/maps/mappings.json index 5e2e8c2c7e6e5..176d40859af26 100644 --- a/x-pack/legacy/plugins/maps/mappings.json +++ b/x-pack/legacy/plugins/maps/mappings.json @@ -26,16 +26,6 @@ }, "maps-telemetry": { "properties": { - "settings": { - "properties": { - "showMapVisualizationTypes": { - "type": "boolean" - } - } - }, - "indexPatternsWithGeoFieldCount": { - "type": "long" - }, "mapsTotalCount": { "type": "long" }, From bdaacc1428bc66d200c06df704c7d18afae983d2 Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Mon, 6 Jan 2020 08:48:14 -0700 Subject: [PATCH 14/16] Review feedback. Move telemetry type constant to constants file --- x-pack/legacy/plugins/maps/common/constants.js | 1 + .../plugins/maps/server/maps_telemetry/maps_telemetry.js | 3 +-- .../plugins/maps/server/maps_telemetry/maps_usage_collector.js | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/x-pack/legacy/plugins/maps/common/constants.js b/x-pack/legacy/plugins/maps/common/constants.js index b97845a458d51..6e7776d43f4d4 100644 --- a/x-pack/legacy/plugins/maps/common/constants.js +++ b/x-pack/legacy/plugins/maps/common/constants.js @@ -23,6 +23,7 @@ export const EMS_TILES_VECTOR_TILE_PATH = 'ems/tiles/vector/tile'; export const MAP_SAVED_OBJECT_TYPE = 'map'; export const APP_ID = 'maps'; export const APP_ICON = 'gisApp'; +export const TELEMETRY_TYPE = 'maps-telemetry'; export const MAP_APP_PATH = `app/${APP_ID}`; export const GIS_API_PATH = `api/${APP_ID}`; diff --git a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js index 2363082856dcf..e4167da7ade92 100644 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js +++ b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js @@ -6,8 +6,7 @@ import _ from 'lodash'; import { EMS_FILE, ES_GEO_FIELD_TYPE, MAP_SAVED_OBJECT_TYPE } from '../../common/constants'; - -export const TELEMETRY_TYPE = 'maps-telemetry'; +import { TELEMETRY_TYPE } from '../../common/constants'; function getSavedObjectsClient(server) { const { SavedObjectsClient, getSavedObjectsRepository } = server.savedObjects; diff --git a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.js b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.js index c236abad74f56..9c575e66f7556 100644 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.js +++ b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.js @@ -4,7 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ -import { getMapsTelemetry, TELEMETRY_TYPE } from './maps_telemetry'; +import { getMapsTelemetry } from './maps_telemetry'; +import { TELEMETRY_TYPE } from '../../common/constants'; export function initTelemetryCollection(usageCollection, server) { if (!usageCollection) { From 1ab2bfceb99d1f18b75b1e223698cd2f1489d61d Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Mon, 6 Jan 2020 09:38:42 -0700 Subject: [PATCH 15/16] Consolidate imports --- .../plugins/maps/server/maps_telemetry/maps_telemetry.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js index e4167da7ade92..e9c438050ff60 100644 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js +++ b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js @@ -5,8 +5,12 @@ */ import _ from 'lodash'; -import { EMS_FILE, ES_GEO_FIELD_TYPE, MAP_SAVED_OBJECT_TYPE } from '../../common/constants'; -import { TELEMETRY_TYPE } from '../../common/constants'; +import { + EMS_FILE, + ES_GEO_FIELD_TYPE, + MAP_SAVED_OBJECT_TYPE, + TELEMETRY_TYPE +} from '../../common/constants'; function getSavedObjectsClient(server) { const { SavedObjectsClient, getSavedObjectsRepository } = server.savedObjects; From 97730c8b0814587a65835496937558c4716868c4 Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Mon, 6 Jan 2020 10:51:48 -0700 Subject: [PATCH 16/16] Linting fix --- .../legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js index e9c438050ff60..848c964f4b6d4 100644 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js +++ b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.js @@ -9,7 +9,7 @@ import { EMS_FILE, ES_GEO_FIELD_TYPE, MAP_SAVED_OBJECT_TYPE, - TELEMETRY_TYPE + TELEMETRY_TYPE, } from '../../common/constants'; function getSavedObjectsClient(server) {