import { constant , noop , identity } from 'lodash' ; import { i18n } from '@osd/i18n' ; import { ISearchSource } from 'src/plugins/data/public' ; import { SerializedFieldFormat } from 'src/plugins/expressions/common' ; import type { RequestAdapter } from 'src/plugins/inspector/common' ; import { initParams } from './agg_params' ; import { AggConfig } from './agg_config' ; import { IAggConfigs } from './agg_configs' ; import { BaseParamType } from './param_types/base' ; import { AggParamType } from './param_types/agg' ; export interface AggTypeConfig < TAggConfig extends AggConfig = AggConfig , TParam extends AggParamType < TAggConfig > = AggParamType < TAggConfig > > { name : string ; title : string ; createFilter ? : ( aggConfig : TAggConfig , key : any , params ? : any ) => any ; type ? : string ; dslName ? : string ; expressionName ? : string ; makeLabel ? : ( ( aggConfig : TAggConfig ) => string ) | ( ( ) => string ) ; ordered ? : any ; hasNoDsl ? : boolean ; params ? : Array < Partial < TParam > > ; getRequestAggs ? : ( ( aggConfig : TAggConfig ) => TAggConfig [ ] ) | ( ( ) => TAggConfig [ ] | void ) ; getResponseAggs ? : ( ( aggConfig : TAggConfig ) => TAggConfig [ ] ) | ( ( ) => TAggConfig [ ] | void ) ; customLabels ? : boolean ; json ? : boolean ; decorateAggConfig ? : ( ) => any ; postFlightRequest ? : ( resp : any , aggConfigs : IAggConfigs , aggConfig : TAggConfig , searchSource : ISearchSource , inspectorRequestAdapter : RequestAdapter , abortSignal ? : AbortSignal ) => Promise < any > ; getSerializedFormat ? : ( agg : TAggConfig ) => SerializedFieldFormat ; getValue ? : ( agg : TAggConfig , bucket : any ) => any ; getKey ? : ( bucket : any , key : any , agg : TAggConfig ) => any ; } export type IAggType = AggType ; export class AggType < TAggConfig extends AggConfig = AggConfig , TParam extends AggParamType < TAggConfig > = AggParamType < TAggConfig > > { property type { string } name : string ; type : string ; subtype ? : string ; property type { string } dslName : string ; property type { string } expressionName ? : string ; property type { string } title : string ; method param { AggConfig } aggConfig returns { string } makeLabel : ( ( aggConfig : TAggConfig ) => string ) | ( ( ) => string ) ; property type { object | undefined } ordered : any ; type { Boolean } hasNoDsl : boolean ; param { object } aggConfig param { mixed } key returns { object } createFilter : ( ( aggConfig : TAggConfig , key : any , params ? : any ) => any ) | undefined ; property type { AggParams } params : TParam [ ] ; method returns { array [ AggConfig ] } getRequestAggs : ( ( aggConfig : TAggConfig ) => TAggConfig [ ] ) | ( ( ) => TAggConfig [ ] | void ) ; method returns { array [ AggConfig ] | undefined } getResponseAggs : ( ( aggConfig : TAggConfig ) => TAggConfig [ ] ) | ( ( ) => TAggConfig [ ] | void ) ; /**
     * A function that will be called each time an aggConfig of this type
     * is created, giving the agg type a chance to modify the agg config
     */ decorateAggConfig : ( ) => any ; param aggConfigs param aggConfig param searchSourceAggs param resp param nestedSearchSource return { Promise } postFlightRequest : ( resp : any , aggConfigs : IAggConfigs , aggConfig : TAggConfig , searchSource : ISearchSource , inspectorRequestAdapter : RequestAdapter , abortSignal ? : AbortSignal ) => Promise < any > ; param { agg } agg return { SerializedFieldFormat } getSerializedFormat : ( agg : TAggConfig ) => SerializedFieldFormat ; getValue : ( agg : TAggConfig , bucket : any ) => any ; getKey ? : ( bucket : any , key : any , agg : TAggConfig ) => any ; paramByName = ( name ) => { return this . params . find ( ( p ) => p . name === name ) ; } ; class private param { object } config constructor ( config ) { this . name = config . name ; this . type = config . type || 'metrics' ; this . dslName = config . dslName || config . name ; this . expressionName = config . expressionName ; this . title = config . title ; this . makeLabel = config . makeLabel || constant ( this . name ) ; this . ordered = config . ordered ; this . hasNoDsl = ! ! config . hasNoDsl ; if ( config . createFilter ) { this . createFilter = config . createFilter ; } if ( config . params && config . params . length && config . params [ 0 ] instanceof BaseParamType ) { this . params = config . params as TParam [ ] ; } else { const params = config . params ? [ ... config . params ] : [ ] ; if ( config . json !== false ) { params . push ( { name : 'json' , type : 'json' , advanced : true , } ) ; } if ( config . customLabels !== false ) { params . push ( { name : 'customLabel' , displayName : i18n . translate ( 'data.search.aggs.string.customLabel' , { defaultMessage : 'Custom label' , } ) , type : 'string' , write : noop , } ) ; } this . params = initParams ( params ) ; } this . getRequestAggs = config . getRequestAggs || noop ; this . getResponseAggs = config . getResponseAggs || ( ( ) => { } ) ; this . decorateAggConfig = config . decorateAggConfig || ( ( ) => ( { } ) ) ; this . postFlightRequest = config . postFlightRequest || identity ; this . getSerializedFormat = config . getSerializedFormat || ( ( agg ) => { return agg . params . field ? agg . aggConfigs . indexPattern . getFormatterForField ( agg . params . field ) . toJSON ( ) : { } ; } ) ; this . getValue = config . getValue || ( ( agg , bucket ) => { } ) ; } }