import { IndexPattern } from 'src/plugins/data/public' ; import { AGG_TYPE } from '../../../../common/constants' ; import { TileMetaFeature } from '../../../../common/descriptor_types' ; import { CountAggField } from './count_agg_field' ; import { isMetricCountable } from '../../util/is_metric_countable' ; import { CountAggFieldParams } from './agg_field_types' ; import { addFieldToDSL , getField } from '../../../../common/elasticsearch_util' ; import { IField } from '../field' ; const TERMS_AGG_SHARD_SIZE = 5 ; export interface AggFieldParams extends CountAggFieldParams { esDocField ? : IField ; aggType : AGG_TYPE ; } export class AggField extends CountAggField { private readonly _esDocField ? : IField ; private readonly _aggType : AGG_TYPE ; constructor ( params ) { super ( params ) ; this . _esDocField = params . esDocField ; this . _aggType = params . aggType ; } supportsFieldMetaFromEs ( ) { return ! isMetricCountable ( this . _getAggType ( ) ) ; } supportsFieldMetaFromLocalData ( ) { return this . _getDataTypeSynchronous ( ) === 'number' ; } isValid ( ) { return ! ! this . _esDocField ; } getMbFieldName ( ) { return this . _source . isMvt ( ) ? this . getName ( ) + '.value' : this . getName ( ) ; } canValueBeFormatted ( ) { return this . _getAggType ( ) !== AGG_TYPE . UNIQUE_COUNT ; } isCount ( ) { return this . _getAggType ( ) === AGG_TYPE . UNIQUE_COUNT ; } _getAggType ( ) { return this . _aggType ; } getValueAggDsl ( indexPattern ) { const field = getField ( indexPattern , this . getRootName ( ) ) ; const aggType = this . _getAggType ( ) ; const aggBody = aggType === AGG_TYPE . TERMS ? { size : 1 , shard_size : TERMS_AGG_SHARD_SIZE } : { } ; return { [ aggType ] : addFieldToDSL ( aggBody , field ) , } ; } getRootName ( ) { return this . _esDocField ? this . _esDocField . getName ( ) : '' ; } async getLabel ( ) { return this . _label ? this . _label : this . _source . getAggLabel ( this . _aggType , this . _esDocField ? await this . _esDocField . getLabel ( ) : '' ) ; } _getDataTypeSynchronous ( ) { return this . _getAggType ( ) === AGG_TYPE . TERMS ? 'string' : 'number' ; } async getDataType ( ) { return this . _getDataTypeSynchronous ( ) ; } getBucketCount ( ) { return this . _getAggType ( ) === AGG_TYPE . TERMS ? TERMS_AGG_SHARD_SIZE : 0 ; } async getExtendedStatsFieldMetaRequest ( ) { return this . _esDocField ? await this . _esDocField . getExtendedStatsFieldMetaRequest ( ) : null ; } async getPercentilesFieldMetaRequest ( percentiles ) { return this . _esDocField ? await this . _esDocField . getPercentilesFieldMetaRequest ( percentiles ) : null ; } async getCategoricalFieldMetaRequest ( size ) { return this . _esDocField ? await this . _esDocField . getCategoricalFieldMetaRequest ( size ) : null ; } pluckRangeFromTileMetaFeature ( metaFeature ) { const minField = `aggregations.${ this . getName ( ) }.min` ; const maxField = `aggregations.${ this . getName ( ) }.max` ; return metaFeature . properties && typeof metaFeature . properties [ minField ] === 'number' && typeof metaFeature . properties [ maxField ] === 'number' ? { min : metaFeature . properties [ minField ] as number , max : metaFeature . properties [ maxField ] as number , } : null ; } }