From 6101b540ecceb349f8a037ba590be90cd620eae4 Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Thu, 16 Jan 2020 08:16:36 -0700 Subject: [PATCH 1/8] Move maps telemetry to NP. Some clean-up, some ts conversion --- x-pack/legacy/plugins/maps/index.js | 11 +++- .../maps_telemetry/collectors/register.ts | 27 ++++++++ .../collectors/register_collector.test.js | 38 +++++++++++ .../{maps_telemetry.js => maps_telemetry.ts} | 63 ++++++++++--------- x-pack/legacy/plugins/maps/server/plugin.js | 7 ++- 5 files changed, 112 insertions(+), 34 deletions(-) create mode 100644 x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register.ts create mode 100644 x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register_collector.test.js rename x-pack/legacy/plugins/maps/server/maps_telemetry/{maps_telemetry.js => maps_telemetry.ts} (69%) diff --git a/x-pack/legacy/plugins/maps/index.js b/x-pack/legacy/plugins/maps/index.js index d28f483c9b98..6efdee57a56d 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'; @@ -91,12 +90,10 @@ 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; @@ -104,6 +101,7 @@ export function maps(kibana) { featuresPlugin: newPlatformPlugins.features, licensing: newPlatformPlugins.licensing, home: newPlatformPlugins.home, + usageCollection: newPlatformPlugins.usageCollection, }; // legacy dependencies @@ -117,6 +115,13 @@ 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, }, injectUiAppVars: server.injectUiAppVars, 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 000000000000..ebe3d38ef142 --- /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/collectors/register_collector.test.js b/x-pack/legacy/plugins/maps/server/maps_telemetry/collectors/register_collector.test.js new file mode 100644 index 000000000000..33eb33100acd --- /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(1); + expect(makeUsageCollectorStub).toHaveBeenCalledTimes(1); + expect(makeUsageCollectorStub).toHaveBeenCalledWith({ + type: expect.any(String), + isReady: expect.any(Function), + fetch: expect.any(Function), + }); + }); +}); 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 69% 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 848c964f4b6d..683399a2e7f6 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 @@ -12,20 +12,15 @@ import { TELEMETRY_TYPE, } from '../../common/constants'; -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) { +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] = { @@ -37,25 +32,33 @@ function getUniqueLayerCounts(layerCountsList, mapsCount) { }, {}); } -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 => + 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 }) { +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; }); @@ -65,7 +68,7 @@ export function buildMapsTelemetry({ mapSavedObjects, indexPatternSavedObjects, // Count of EMS Vector layers used const emsLayersCount = layerLists.map(lList => _(lList) - .countBy(layer => { + .countBy((layer: any) => { const isEmsFile = _.get(layer, 'sourceDescriptor.type') === EMS_FILE; return isEmsFile && _.get(layer, 'sourceDescriptor.id'); }) @@ -110,26 +113,26 @@ export function buildMapsTelemetry({ mapSavedObjects, indexPatternSavedObjects, }, }; } - -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) { - const savedObjectsClient = getSavedObjectsClient(server); - 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(TELEMETRY_TYPE, mapsTelemetry, { + return await savedObjectsClient.create('maps-telemetry', 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 6009cea330ab..ee4a249c898c 100644 --- a/x-pack/legacy/plugins/maps/server/plugin.js +++ b/x-pack/legacy/plugins/maps/server/plugin.js @@ -8,12 +8,13 @@ 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, home, licensing } = plugins; + const { featuresPlugin, home, licensing, usageCollection } = plugins; let routesInitialized = false; featuresPlugin.registerFeature({ @@ -51,6 +52,10 @@ export class MapPlugin { } }); + // Init telemetry + const { savedObjectsClient } = __LEGACY.savedObjects; + registerMapsUsageCollector(usageCollection, savedObjectsClient, __LEGACY.config); + const sampleDataLinkLabel = i18n.translate('xpack.maps.sampleDataLinkLabel', { defaultMessage: 'Map', }); From e66d31264467b374aceeb8b6711d73b369dd022d Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Thu, 16 Jan 2020 08:20:42 -0700 Subject: [PATCH 2/8] Update naming & org to be more in-line with guidelines --- .../maps/server/maps_telemetry/index.js | 7 ---- .../maps_telemetry/maps_usage_collector.js | 22 ------------ .../maps_usage_collector.test.js | 35 ------------------- 3 files changed, 64 deletions(-) delete mode 100644 x-pack/legacy/plugins/maps/server/maps_telemetry/index.js 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 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 513df3f76518..000000000000 --- 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_usage_collector.js b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.js deleted file mode 100644 index 9c575e66f755..000000000000 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.js +++ /dev/null @@ -1,22 +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'; -import { TELEMETRY_TYPE } from '../../common/constants'; - -export function initTelemetryCollection(usageCollection, server) { - if (!usageCollection) { - return; - } - - const mapsUsageCollector = usageCollection.makeUsageCollector({ - type: TELEMETRY_TYPE, - isReady: () => true, - fetch: async () => await getMapsTelemetry(server), - }); - - 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 c5a3fca89b56..000000000000 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.test.js +++ /dev/null @@ -1,35 +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 { initTelemetryCollection } from './maps_usage_collector'; - -describe('buildCollectorObj#fetch', () => { - let makeUsageCollectorStub; - let registerStub; - let usageCollection; - - beforeEach(() => { - makeUsageCollectorStub = jest.fn(); - registerStub = jest.fn(); - usageCollection = { - makeUsageCollector: makeUsageCollectorStub, - registerCollector: registerStub, - }; - }); - - test('makes and registers maps usage collector', async () => { - const serverPlaceholder = {}; - initTelemetryCollection(usageCollection, serverPlaceholder); - - expect(registerStub).toHaveBeenCalledTimes(1); - expect(makeUsageCollectorStub).toHaveBeenCalledTimes(1); - expect(makeUsageCollectorStub).toHaveBeenCalledWith({ - type: expect.any(String), - isReady: expect.any(Function), - fetch: expect.any(Function), - }); - }); -}); From 69c53785c06a0871b5dcb587256d8392fd694a7c Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Thu, 6 Feb 2020 09:02:41 -0700 Subject: [PATCH 3/8] Get TELEMETRY_TYPE from constants --- .../plugins/maps/server/maps_telemetry/collectors/register.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 ebe3d38ef142..1cc65f853e92 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 @@ -6,7 +6,8 @@ import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; // @ts-ignore -import { getMapsTelemetry, TELEMETRY_TYPE } from '../maps_telemetry'; +import { getMapsTelemetry } from '../maps_telemetry'; +import { TELEMETRY_TYPE } from '../../../common/constants'; export function registerMapsUsageCollector( usageCollection: UsageCollectionSetup, From b3bcc3cc00c9d78d7c3a58c13f39b48ef0c33aa3 Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Thu, 6 Feb 2020 11:42:51 -0700 Subject: [PATCH 4/8] Ignore ts error importing from js file --- .../plugins/maps/server/maps_telemetry/collectors/register.ts | 1 + .../legacy/plugins/maps/server/maps_telemetry/maps_telemetry.ts | 1 + 2 files changed, 2 insertions(+) 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 1cc65f853e92..51f9ee41e26b 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 @@ -7,6 +7,7 @@ import { UsageCollectionSetup } from 'src/plugins/usage_collection/server'; // @ts-ignore import { getMapsTelemetry } from '../maps_telemetry'; +// @ts-ignore import { TELEMETRY_TYPE } from '../../../common/constants'; export function registerMapsUsageCollector( 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.ts index 683399a2e7f6..4e0df2b454b1 100644 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.ts +++ b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.ts @@ -10,6 +10,7 @@ import { ES_GEO_FIELD_TYPE, MAP_SAVED_OBJECT_TYPE, TELEMETRY_TYPE, + // @ts-ignore } from '../../common/constants'; function getUniqueLayerCounts(layerCountsList: any[], mapsCount: number) { From 3bf9c38dd9011656ed0d55f55dec42093a1b43f0 Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Tue, 18 Feb 2020 15:01:22 -0700 Subject: [PATCH 5/8] Set original array type passed into the function to array of ILayerTypeCount. Set return type on reduce function --- .../server/maps_telemetry/maps_telemetry.ts | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) 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.ts index 7bd8bde9e14d..4a7297d5810e 100644 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.ts +++ b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.ts @@ -22,16 +22,23 @@ interface Stats { }; } -function getUniqueLayerCounts(layerCountsList: object[], mapsCount: number) { +interface ILayerTypeCount { + [key: string]: number; +} + +function getUniqueLayerCounts(layerCountsList: ILayerTypeCount[], mapsCount: number) { const uniqueLayerTypes = _.uniq(_.flatten(layerCountsList.map(lTypes => Object.keys(lTypes)))); return uniqueLayerTypes.reduce((accu: Stats, type: string) => { - const typeCounts = layerCountsList.reduce((tCountsAccu: number[], tCounts: any) => { - if (tCounts[type]) { - tCountsAccu.push(tCounts[type]); - } - return tCountsAccu; - }, []); + const typeCounts = layerCountsList.reduce( + (tCountsAccu: number[], tCounts: ILayerTypeCount): number[] => { + if (tCounts[type]) { + tCountsAccu.push(tCounts[type]); + } + return tCountsAccu; + }, + [] + ); const typeCountsSum = _.sum(typeCounts); accu[type] = { min: typeCounts.length ? _.min(typeCounts) : 0, From 51e47fed3a96a6604d6a754790cd371283dec04d Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Tue, 18 Feb 2020 16:00:21 -0700 Subject: [PATCH 6/8] Remove unneeded 'any' types where used. Add in interfaces for map & index pattern saved objects --- .../legacy/plugins/maps/common/constants.ts | 22 +++++++++++++++++++ .../server/maps_telemetry/maps_telemetry.ts | 20 +++++++++-------- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/x-pack/legacy/plugins/maps/common/constants.ts b/x-pack/legacy/plugins/maps/common/constants.ts index ab9a696fa3a1..e1204630284b 100644 --- a/x-pack/legacy/plugins/maps/common/constants.ts +++ b/x-pack/legacy/plugins/maps/common/constants.ts @@ -158,3 +158,25 @@ export const SYMBOLIZE_AS_TYPES = { }; export const DEFAULT_ICON = 'airfield'; + +export interface IMapSavedObject { + type: string; + id: string; + attributes: { + title: string; + description: string; + mapStateJSON: string; + layerListJSON: string; + uiStateJSON: string; + bounds: { + type: string; + coordinates: []; + }; + }; + references: [Record]; + migrationVersion: { + map: string; + }; + updated_at: string; + version: string; +} 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.ts index 4a7297d5810e..5271f7619174 100644 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.ts +++ b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.ts @@ -6,11 +6,13 @@ import _ from 'lodash'; import { SavedObjectsClientContract } from 'src/core/server'; +import { IIndexPattern } from 'src/plugins/data/public'; import { EMS_FILE, ES_GEO_FIELD_TYPE, MAP_SAVED_OBJECT_TYPE, TELEMETRY_TYPE, + IMapSavedObject, // @ts-ignore } from '../../common/constants'; @@ -49,11 +51,11 @@ function getUniqueLayerCounts(layerCountsList: ILayerTypeCount[], mapsCount: num }, {}); } -function getIndexPatternsWithGeoFieldCount(indexPatterns: any[]) { +function getIndexPatternsWithGeoFieldCount(indexPatterns: IIndexPattern[]) { const fieldLists = indexPatterns.map(indexPattern => JSON.parse(indexPattern.attributes.fields)); const fieldListsWithGeoFields = fieldLists.filter(fields => fields.some( - (field: any) => + field => field.type === ES_GEO_FIELD_TYPE.GEO_POINT || field.type === ES_GEO_FIELD_TYPE.GEO_SHAPE ) ); @@ -65,9 +67,9 @@ export function buildMapsTelemetry({ indexPatternSavedObjects, settings, }: { - mapSavedObjects: any[]; - indexPatternSavedObjects: any[]; - settings: any; + mapSavedObjects: IMapSavedObject[]; + indexPatternSavedObjects: IIndexPattern[]; + settings: object; }) { const layerLists = mapSavedObjects.map(savedMapObject => JSON.parse(savedMapObject.attributes.layerListJSON) @@ -75,7 +77,7 @@ export function buildMapsTelemetry({ 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; }); @@ -85,7 +87,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'); }) @@ -144,8 +146,8 @@ export async function getMapsTelemetry( savedObjectsClient: SavedObjectsClientContract, config: Function ) { - const mapSavedObjects: Array> = await getMapSavedObjects(savedObjectsClient); - const indexPatternSavedObjects: Array> = await getIndexPatternSavedObjects( + const mapSavedObjects: IMapSavedObject[] = await getMapSavedObjects(savedObjectsClient); + const indexPatternSavedObjects: IIndexPattern[] = await getIndexPatternSavedObjects( savedObjectsClient ); const settings = { From f59c79c8cb1a3ce2a947b2c6aa58ecfe18193934 Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Wed, 19 Feb 2020 08:26:00 -0700 Subject: [PATCH 7/8] Review feedback. Add layer, source, map saved object types and use --- .../legacy/plugins/maps/common/constants.ts | 22 ---------- .../plugins/maps/common/descriptor_types.d.ts | 44 +++++++++++++++++++ .../server/maps_telemetry/maps_telemetry.ts | 10 ++--- 3 files changed, 49 insertions(+), 27 deletions(-) create mode 100644 x-pack/legacy/plugins/maps/common/descriptor_types.d.ts diff --git a/x-pack/legacy/plugins/maps/common/constants.ts b/x-pack/legacy/plugins/maps/common/constants.ts index e1204630284b..ab9a696fa3a1 100644 --- a/x-pack/legacy/plugins/maps/common/constants.ts +++ b/x-pack/legacy/plugins/maps/common/constants.ts @@ -158,25 +158,3 @@ export const SYMBOLIZE_AS_TYPES = { }; export const DEFAULT_ICON = 'airfield'; - -export interface IMapSavedObject { - type: string; - id: string; - attributes: { - title: string; - description: string; - mapStateJSON: string; - layerListJSON: string; - uiStateJSON: string; - bounds: { - type: string; - coordinates: []; - }; - }; - references: [Record]; - migrationVersion: { - map: string; - }; - updated_at: string; - version: string; -} diff --git a/x-pack/legacy/plugins/maps/common/descriptor_types.d.ts b/x-pack/legacy/plugins/maps/common/descriptor_types.d.ts new file mode 100644 index 000000000000..4ac1e4e3e771 --- /dev/null +++ b/x-pack/legacy/plugins/maps/common/descriptor_types.d.ts @@ -0,0 +1,44 @@ +/* + * 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 { IFieldType } from '../../../../../src/plugins/data/common/index_patterns/fields'; + +export interface ISourceDescriptor { + id: string; + type: string; +} + +export interface ILayerDescriptor { + sourceDescriptor: ISourceDescriptor; + id: string; +} + +export interface IMapSavedObject { + [key: string]: any; + fields: IFieldType[]; + title: string; + id?: string; + type?: string; + timeFieldName?: string; + fieldFormatMap?: Record< + string, + { + id: string; + params: unknown; + } + >; + attributes?: { + title?: string; + description?: string; + mapStateJSON?: string; + layerListJSON?: string; + uiStateJSON?: string; + bounds?: { + type?: string; + coordinates?: []; + }; + }; +} 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.ts index 5271f7619174..ee33097cc563 100644 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.ts +++ b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.ts @@ -12,11 +12,11 @@ import { ES_GEO_FIELD_TYPE, MAP_SAVED_OBJECT_TYPE, TELEMETRY_TYPE, - IMapSavedObject, // @ts-ignore } from '../../common/constants'; +import { ILayerDescriptor, IMapSavedObject } from '../../common/descriptor_types'; -interface Stats { +interface IStats { [key: string]: { min: number; max: number; @@ -31,7 +31,7 @@ interface ILayerTypeCount { function getUniqueLayerCounts(layerCountsList: ILayerTypeCount[], mapsCount: number) { const uniqueLayerTypes = _.uniq(_.flatten(layerCountsList.map(lTypes => Object.keys(lTypes)))); - return uniqueLayerTypes.reduce((accu: Stats, type: string) => { + return uniqueLayerTypes.reduce((accu: IStats, type: string) => { const typeCounts = layerCountsList.reduce( (tCountsAccu: number[], tCounts: ILayerTypeCount): number[] => { if (tCounts[type]) { @@ -77,7 +77,7 @@ export function buildMapsTelemetry({ const mapsCount = layerLists.length; const dataSourcesCount = layerLists.map(lList => { - const sourceIdList = lList.map(layer => layer.sourceDescriptor.id); + const sourceIdList = lList.map((layer: ILayerDescriptor) => layer.sourceDescriptor.id); return _.uniq(sourceIdList).length; }); @@ -87,7 +87,7 @@ export function buildMapsTelemetry({ // Count of EMS Vector layers used const emsLayersCount = layerLists.map(lList => _(lList) - .countBy(layer => { + .countBy((layer: ILayerDescriptor) => { const isEmsFile = _.get(layer, 'sourceDescriptor.type') === EMS_FILE; return isEmsFile && _.get(layer, 'sourceDescriptor.id'); }) From 3e416ce6a6e807bddd68f39a9b588afc9ab27820 Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Wed, 19 Feb 2020 11:23:45 -0700 Subject: [PATCH 8/8] Review feedback. Updates based on type check --- .../plugins/maps/common/descriptor_types.d.ts | 27 ---------- .../server/maps_telemetry/maps_telemetry.ts | 51 +++++++++++++++---- 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/x-pack/legacy/plugins/maps/common/descriptor_types.d.ts b/x-pack/legacy/plugins/maps/common/descriptor_types.d.ts index 4ac1e4e3e771..05123c9c86b6 100644 --- a/x-pack/legacy/plugins/maps/common/descriptor_types.d.ts +++ b/x-pack/legacy/plugins/maps/common/descriptor_types.d.ts @@ -15,30 +15,3 @@ export interface ILayerDescriptor { sourceDescriptor: ISourceDescriptor; id: string; } - -export interface IMapSavedObject { - [key: string]: any; - fields: IFieldType[]; - title: string; - id?: string; - type?: string; - timeFieldName?: string; - fieldFormatMap?: Record< - string, - { - id: string; - params: unknown; - } - >; - attributes?: { - title?: string; - description?: string; - mapStateJSON?: string; - layerListJSON?: string; - uiStateJSON?: string; - bounds?: { - type?: string; - coordinates?: []; - }; - }; -} 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.ts index ee33097cc563..87642d9f8bea 100644 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.ts +++ b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_telemetry.ts @@ -5,8 +5,12 @@ */ import _ from 'lodash'; -import { SavedObjectsClientContract } from 'src/core/server'; -import { IIndexPattern } from 'src/plugins/data/public'; +import { + SavedObjectsClientContract, + SavedObjectAttributes, + SavedObjectAttribute, +} from 'src/core/server'; +import { IFieldType, IIndexPattern } from 'src/plugins/data/public'; import { EMS_FILE, ES_GEO_FIELD_TYPE, @@ -14,7 +18,7 @@ import { TELEMETRY_TYPE, // @ts-ignore } from '../../common/constants'; -import { ILayerDescriptor, IMapSavedObject } from '../../common/descriptor_types'; +import { ILayerDescriptor } from '../../common/descriptor_types'; interface IStats { [key: string]: { @@ -28,6 +32,33 @@ interface ILayerTypeCount { [key: string]: number; } +interface IMapSavedObject { + [key: string]: any; + fields: IFieldType[]; + title: string; + id?: string; + type?: string; + timeFieldName?: string; + fieldFormatMap?: Record< + string, + { + id: string; + params: unknown; + } + >; + attributes?: { + title?: string; + description?: string; + mapStateJSON?: string; + layerListJSON?: string; + uiStateJSON?: string; + bounds?: { + type?: string; + coordinates?: []; + }; + }; +} + function getUniqueLayerCounts(layerCountsList: ILayerTypeCount[], mapsCount: number) { const uniqueLayerTypes = _.uniq(_.flatten(layerCountsList.map(lTypes => Object.keys(lTypes)))); @@ -55,7 +86,7 @@ function getIndexPatternsWithGeoFieldCount(indexPatterns: IIndexPattern[]) { const fieldLists = indexPatterns.map(indexPattern => JSON.parse(indexPattern.attributes.fields)); const fieldListsWithGeoFields = fieldLists.filter(fields => fields.some( - field => + (field: IFieldType) => field.type === ES_GEO_FIELD_TYPE.GEO_POINT || field.type === ES_GEO_FIELD_TYPE.GEO_SHAPE ) ); @@ -69,10 +100,12 @@ export function buildMapsTelemetry({ }: { mapSavedObjects: IMapSavedObject[]; indexPatternSavedObjects: IIndexPattern[]; - settings: object; -}) { + settings: SavedObjectAttribute; +}): SavedObjectAttributes { const layerLists = mapSavedObjects.map(savedMapObject => - JSON.parse(savedMapObject.attributes.layerListJSON) + savedMapObject.attributes && savedMapObject.attributes.layerListJSON + ? JSON.parse(savedMapObject.attributes.layerListJSON) + : [] ); const mapsCount = layerLists.length; @@ -150,11 +183,11 @@ export async function getMapsTelemetry( const indexPatternSavedObjects: IIndexPattern[] = await getIndexPatternSavedObjects( savedObjectsClient ); - const settings = { + const settings: SavedObjectAttribute = { showMapVisualizationTypes: 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, });