import { i18n } from '@kbn/i18n' ; import { Adapters } from 'src/plugins/inspector/public' ; import { GeoJsonProperties } from 'geojson' ; import { IESSource } from '../es_source' ; import { AbstractESSource } from '../es_source' ; import { esAggFieldsFactory , IESAggField } from '../../fields/agg' ; import { AGG_TYPE , COUNT_PROP_LABEL , FIELD_ORIGIN } from '../../../../common/constants' ; import { getSourceAggKey } from '../../../../common/get_agg_key' ; import { AbstractESAggSourceDescriptor , AggDescriptor } from '../../../../common/descriptor_types' ; import { IndexPattern } from '../../../../../../../src/plugins/data/public' ; import { IField } from '../../fields/field' ; import { ITooltipProperty } from '../../tooltips/tooltip_property' ; export const DEFAULT_METRIC = { type : AGG_TYPE . COUNT } ; export interface IESAggSource extends IESSource { getAggKey ( aggType , fieldName ) : string ; getAggLabel ( aggType , fieldLabel ) : string ; getMetricFields ( ) : IESAggField [ ] ; getMetricFieldForName ( fieldName ) : IESAggField | null ; getValueAggsDsl ( indexPattern ) : { [ key ] : unknown ; } ; } export abstract class AbstractESAggSource extends AbstractESSource implements IESAggSource { private readonly _metricFields : IESAggField [ ] ; static createDescriptor ( descriptor ) { const normalizedDescriptor = AbstractESSource . createDescriptor ( descriptor ) ; return { ... normalizedDescriptor , type : descriptor . type ? descriptor . type : '' , metrics : descriptor . metrics && descriptor . metrics . length > 0 ? descriptor . metrics : [ DEFAULT_METRIC ] , } ; } constructor ( descriptor , inspectorAdapters ? ) { super ( descriptor , inspectorAdapters ) ; this . _metricFields = [ ] ; if ( descriptor . metrics ) { descriptor . metrics . forEach ( ( aggDescriptor ) => { this . _metricFields . push ( ... esAggFieldsFactory ( aggDescriptor , this , this . getOriginForField ( ) ) ) ; } ) ; } } getFieldByName ( fieldName ) { return this . getMetricFieldForName ( fieldName ) ; } createField ( { fieldName } ) { throw new Error ( 'Cannot create a new field from just a fieldname for an es_agg_source.' ) ; } getMetricFieldForName ( fieldName ) { const targetMetricField = this . getMetricFields ( ) . find ( ( metricField ) => { return metricField . getName ( ) === fieldName ; } ) ; return targetMetricField ? targetMetricField : null ; } getOriginForField ( ) { return FIELD_ORIGIN . SOURCE ; } getMetricFields ( ) { const metrics = this . _metricFields . filter ( ( esAggField ) => esAggField . isValid ( ) ) ; return metrics . length === 0 ? esAggFieldsFactory ( { type : AGG_TYPE . COUNT } , this , this . getOriginForField ( ) ) : metrics ; } getAggKey ( aggType , fieldName ) { return getSourceAggKey ( { aggType , aggFieldName : fieldName , } ) ; } getAggLabel ( aggType , fieldLabel ) { switch ( aggType ) { case AGG_TYPE . COUNT : return COUNT_PROP_LABEL ; case AGG_TYPE . TERMS : return i18n . translate ( 'xpack.maps.source.esAggSource.topTermLabel' , { defaultMessage : `Top {fieldLabel}` , values : { fieldLabel } , } ) ; default : return `${ aggType } ${ fieldLabel }` ; } } async getFields ( ) { return this . getMetricFields ( ) ; } getValueAggsDsl ( indexPattern ) { const valueAggsDsl = { } ; this . getMetricFields ( ) . forEach ( ( esAggMetric ) => { const aggDsl = esAggMetric . getValueAggDsl ( indexPattern ) ; if ( aggDsl ) { valueAggsDsl [ esAggMetric . getName ( ) ] = esAggMetric . getValueAggDsl ( indexPattern ) ; } } ) ; return valueAggsDsl ; } async getTooltipProperties ( mbProperties ) { const metricFields = await this . getFields ( ) ; const promises = [ ] ; metricFields . forEach ( ( metricField ) => { let value ; for ( const key in mbProperties ) { if ( mbProperties . hasOwnProperty ( key ) && metricField . getMbFieldName ( ) === key ) { value = mbProperties [ key ] ; break ; } } const tooltipPromise = metricField . createTooltipProperty ( value ) ; promises . push ( tooltipPromise ) ; } ) ; return await Promise . all ( promises ) ; } }