import { i18n } from '@osd/i18n' ; import { AggType , AggTypeConfig } from '../agg_type' ; import { AggParamType } from '../param_types/agg' ; import { AggConfig } from '../agg_config' ; import { METRIC_TYPES } from './metric_agg_types' ; import { FieldTypes } from '../param_types' ; export interface IMetricAggConfig extends AggConfig { type : InstanceType < typeof MetricAggType > ; } export interface MetricAggParam < TMetricAggConfig extends AggConfig > extends AggParamType < TMetricAggConfig > { filterFieldTypes ? : FieldTypes ; onlyAggregatable ? : boolean ; } const metricType = 'metrics' ; interface MetricAggTypeConfig < TMetricAggConfig extends AggConfig > extends AggTypeConfig < TMetricAggConfig , MetricAggParam < TMetricAggConfig > > { isScalable ? : ( ) => boolean ; subtype ? : string ; } export type IMetricAggType = MetricAggType ; export class MetricAggType < TMetricAggConfig extends AggConfig = IMetricAggConfig > extends AggType < TMetricAggConfig , MetricAggParam < TMetricAggConfig > > { subtype : string ; isScalable : ( ) => boolean ; type = metricType ; getKey = ( ) => { } ; constructor ( config ) { super ( config ) ; this . getValue = config . getValue || ( ( agg , bucket ) => { const isSettableToZero = [ METRIC_TYPES . CARDINALITY , METRIC_TYPES . SUM ] . includes ( agg . type . name as METRIC_TYPES ) ; if ( ! bucket [ agg . id ] && isSettableToZero ) return 0 ; return bucket [ agg . id ] && bucket [ agg . id ] . value ; } ) ; this . subtype = config . subtype || i18n . translate ( 'data.search.aggs.metrics.metricAggregationsSubtypeTitle' , { defaultMessage : 'Metric Aggregations' , } ) ; this . isScalable = config . isScalable || ( ( ) => false ) ; } } export function isMetricAggType ( aggConfig ) { return aggConfig && aggConfig . type === metricType ; }