66
77import _ from 'lodash' ;
88import {
9- SavedObjectsClientContract ,
10- SavedObjectAttributes ,
119 SavedObjectAttribute ,
10+ SavedObjectAttributes ,
11+ SavedObjectsClientContract ,
1212} from 'kibana/server' ;
1313import { IFieldType , IIndexPattern } from 'src/plugins/data/public' ;
14- import { SOURCE_TYPES , ES_GEO_FIELD_TYPE , MAP_SAVED_OBJECT_TYPE } from '../../common/constants' ;
15- import { LayerDescriptor } from '../../common/descriptor_types' ;
14+ import {
15+ ES_GEO_FIELD_TYPE ,
16+ LAYER_TYPE ,
17+ MAP_SAVED_OBJECT_TYPE ,
18+ SCALING_TYPES ,
19+ SOURCE_TYPES ,
20+ } from '../../common/constants' ;
21+ import {
22+ AbstractSourceDescriptor ,
23+ ESGeoGridSourceDescriptor ,
24+ ESSearchSourceDescriptor ,
25+ LayerDescriptor ,
26+ SourceDescriptor ,
27+ } from '../../common/descriptor_types' ;
1628import { MapSavedObject } from '../../common/map_saved_object_type' ;
1729// @ts -ignore
1830import { getInternalRepository } from '../kibana_server_services' ;
@@ -82,6 +94,111 @@ function getIndexPatternsWithGeoFieldCount(indexPatterns: IIndexPattern[]) {
8294 } ;
8395}
8496
97+ function getEMSLayerCount ( layerLists : LayerDescriptor [ ] [ ] ) : ILayerTypeCount [ ] {
98+ return layerLists . map ( ( layerList : LayerDescriptor [ ] ) => {
99+ const emsLayers = layerList . filter ( ( layer : LayerDescriptor ) => {
100+ return (
101+ layer . sourceDescriptor !== null &&
102+ layer . sourceDescriptor . type === SOURCE_TYPES . EMS_FILE &&
103+ ( layer . sourceDescriptor as AbstractSourceDescriptor ) . id
104+ ) ;
105+ } ) ;
106+ const emsCountsById = _ ( emsLayers ) . countBy ( ( layer : LayerDescriptor ) => {
107+ return ( layer . sourceDescriptor as AbstractSourceDescriptor ) . id ;
108+ } ) ;
109+
110+ const layerTypeCount = emsCountsById . value ( ) ;
111+ return layerTypeCount as ILayerTypeCount ;
112+ } ) as ILayerTypeCount [ ] ;
113+ }
114+
115+ function isFieldGeoShape (
116+ indexPatterns : IIndexPattern [ ] ,
117+ indexPatternId : string ,
118+ geoField : string | undefined
119+ ) : boolean {
120+ if ( ! geoField ) {
121+ return false ;
122+ }
123+
124+ const matchIndexPattern = indexPatterns . find ( ( indexPattern : IIndexPattern ) => {
125+ return indexPattern . id === indexPatternId ;
126+ } ) ;
127+
128+ if ( ! matchIndexPattern ) {
129+ return false ;
130+ }
131+
132+ const fieldList : IFieldType [ ] =
133+ matchIndexPattern . attributes && matchIndexPattern . attributes . fields
134+ ? JSON . parse ( matchIndexPattern . attributes . fields )
135+ : [ ] ;
136+
137+ const matchField = fieldList . find ( ( field : IFieldType ) => {
138+ return field . name === geoField ;
139+ } ) ;
140+
141+ return ! ! matchField && matchField . type === ES_GEO_FIELD_TYPE . GEO_SHAPE ;
142+ }
143+
144+ function isGeoShapeAggLayer ( indexPatterns : IIndexPattern [ ] , layer : LayerDescriptor ) : boolean {
145+ if ( layer . sourceDescriptor === null ) {
146+ return false ;
147+ }
148+
149+ if (
150+ layer . type !== LAYER_TYPE . VECTOR &&
151+ layer . type !== LAYER_TYPE . BLENDED_VECTOR &&
152+ layer . type !== LAYER_TYPE . HEATMAP
153+ ) {
154+ return false ;
155+ }
156+
157+ const sourceDescriptor : SourceDescriptor = layer . sourceDescriptor ;
158+ if ( sourceDescriptor . type === SOURCE_TYPES . ES_GEO_GRID ) {
159+ return isFieldGeoShape (
160+ indexPatterns ,
161+ ( sourceDescriptor as ESGeoGridSourceDescriptor ) . indexPatternId ,
162+ ( sourceDescriptor as ESGeoGridSourceDescriptor ) . geoField
163+ ) ;
164+ } else if (
165+ sourceDescriptor . type === SOURCE_TYPES . ES_SEARCH &&
166+ ( sourceDescriptor as ESSearchSourceDescriptor ) . scalingType === SCALING_TYPES . CLUSTERS
167+ ) {
168+ return isFieldGeoShape (
169+ indexPatterns ,
170+ ( sourceDescriptor as ESSearchSourceDescriptor ) . indexPatternId ,
171+ ( sourceDescriptor as ESSearchSourceDescriptor ) . geoField
172+ ) ;
173+ } else {
174+ return false ;
175+ }
176+ }
177+
178+ function getGeoShapeAggCount (
179+ layerLists : LayerDescriptor [ ] [ ] ,
180+ indexPatterns : IIndexPattern [ ]
181+ ) : number {
182+ const countsPerMap : number [ ] = layerLists . map ( ( layerList : LayerDescriptor [ ] ) => {
183+ const geoShapeAggLayers = layerList . filter ( ( layerDescriptor ) => {
184+ return isGeoShapeAggLayer ( indexPatterns , layerDescriptor ) ;
185+ } ) ;
186+ return geoShapeAggLayers . length ;
187+ } ) ;
188+
189+ return _ . sum ( countsPerMap ) ;
190+ }
191+
192+ export function getLayerLists ( mapSavedObjects : MapSavedObject [ ] ) : LayerDescriptor [ ] [ ] {
193+ return mapSavedObjects . map ( ( savedMapObject ) => {
194+ const layerList =
195+ savedMapObject . attributes && savedMapObject . attributes . layerListJSON
196+ ? JSON . parse ( savedMapObject . attributes . layerListJSON )
197+ : [ ] ;
198+ return layerList as LayerDescriptor [ ] ;
199+ } ) ;
200+ }
201+
85202export function buildMapsTelemetry ( {
86203 mapSavedObjects,
87204 indexPatternSavedObjects,
@@ -91,33 +208,21 @@ export function buildMapsTelemetry({
91208 indexPatternSavedObjects : IIndexPattern [ ] ;
92209 settings : SavedObjectAttribute ;
93210} ) : SavedObjectAttributes {
94- const layerLists = mapSavedObjects . map ( ( savedMapObject ) =>
95- savedMapObject . attributes && savedMapObject . attributes . layerListJSON
96- ? JSON . parse ( savedMapObject . attributes . layerListJSON )
97- : [ ]
98- ) ;
211+ const layerLists : LayerDescriptor [ ] [ ] = getLayerLists ( mapSavedObjects ) ;
99212 const mapsCount = layerLists . length ;
100213
101- const dataSourcesCount = layerLists . map ( ( lList ) => {
214+ const dataSourcesCount = layerLists . map ( ( layerList : LayerDescriptor [ ] ) => {
102215 // todo: not every source-descriptor has an id
103216 // @ts -ignore
104- const sourceIdList = lList . map ( ( layer : LayerDescriptor ) => layer . sourceDescriptor . id ) ;
217+ const sourceIdList = layerList . map ( ( layer : LayerDescriptor ) => layer . sourceDescriptor . id ) ;
105218 return _ . uniq ( sourceIdList ) . length ;
106219 } ) ;
107220
108221 const layersCount = layerLists . map ( ( lList ) => lList . length ) ;
109222 const layerTypesCount = layerLists . map ( ( lList ) => _ . countBy ( lList , 'type' ) ) ;
110223
111224 // Count of EMS Vector layers used
112- const emsLayersCount = layerLists . map ( ( lList ) =>
113- _ ( lList )
114- . countBy ( ( layer : LayerDescriptor ) => {
115- const isEmsFile = _ . get ( layer , 'sourceDescriptor.type' ) === SOURCE_TYPES . EMS_FILE ;
116- return isEmsFile && _ . get ( layer , 'sourceDescriptor.id' ) ;
117- } )
118- . pickBy ( ( val , key ) => key !== 'false' )
119- . value ( )
120- ) as ILayerTypeCount [ ] ;
225+ const emsLayersCount = getEMSLayerCount ( layerLists ) ;
121226
122227 const dataSourcesCountSum = _ . sum ( dataSourcesCount ) ;
123228 const layersCountSum = _ . sum ( layersCount ) ;
@@ -127,11 +232,16 @@ export function buildMapsTelemetry({
127232 indexPatternsWithGeoPointFieldCount,
128233 indexPatternsWithGeoShapeFieldCount,
129234 } = getIndexPatternsWithGeoFieldCount ( indexPatternSavedObjects ) ;
235+
236+ // Tracks whether user users Gold+ only functionality
237+ const geoShapeAggLayersCount = getGeoShapeAggCount ( layerLists , indexPatternSavedObjects ) ;
238+
130239 return {
131240 settings,
132241 indexPatternsWithGeoFieldCount,
133242 indexPatternsWithGeoPointFieldCount,
134243 indexPatternsWithGeoShapeFieldCount,
244+ geoShapeAggLayersCount,
135245 // Total count of maps
136246 mapsTotalCount : mapsCount ,
137247 // Time of capture
0 commit comments