import { i18n } from '@osd/i18n' ; import { OSD_FIELD_TYPES } from '../../../../common' ; import { AggTypesDependencies } from '../agg_types' ; import { BaseAggParams } from '../types' ; import { BucketAggType } from './bucket_agg_type' ; import { RangeKey } from './range_key' ; import { createFilterRange } from './create_filter/range' ; import { BUCKET_TYPES } from './bucket_agg_types' ; const rangeTitle = i18n . translate ( 'data.search.aggs.buckets.rangeTitle' , { defaultMessage : 'Range' , } ) ; export interface RangeBucketAggDependencies { getFieldFormatsStart : AggTypesDependencies [ 'getFieldFormatsStart' ] ; } export interface AggParamsRange extends BaseAggParams { field : string ; ranges ? : Array < { from : number ; to : number ; } > ; } export const getRangeBucketAgg = ( { getFieldFormatsStart } ) => { const keyCaches = new WeakMap ( ) ; return new BucketAggType ( { name : BUCKET_TYPES . RANGE , title : rangeTitle , createFilter : createFilterRange ( getFieldFormatsStart ) , makeLabel ( aggConfig ) { return i18n . translate ( 'data.search.aggs.aggTypesLabel' , { defaultMessage : '{fieldName} ranges' , values : { fieldName : aggConfig . getFieldDisplayName ( ) , } , } ) ; } , getKey ( bucket , key , agg ) { let keys = keyCaches . get ( agg ) ; if ( ! keys ) { keys = new Map ( ) ; keyCaches . set ( agg , keys ) ; } const id = RangeKey . idBucket ( bucket ) ; key = keys . get ( id ) ; if ( ! key ) { key = new RangeKey ( bucket ) ; keys . set ( id , key ) ; } return key ; } , getSerializedFormat ( agg ) { const format = agg . params . field ? agg . aggConfigs . indexPattern . getFormatterForField ( agg . params . field ) . toJSON ( ) : { id : undefined , params : undefined } ; return { id : 'range' , params : { id : format . id , params : format . params , } , } ; } , params : [ { name : 'field' , type : 'field' , filterFieldTypes : [ OSD_FIELD_TYPES . NUMBER ] , } , { name : 'ranges' , default : [ { from : 0 , to : 1000 } , { from : 1000 , to : 2000 } , ] , write ( aggConfig , output ) { output . params . ranges = aggConfig . params . ranges ; output . params . keyed = true ; } , } , ] , } ) ; } ;