import { i18n } from '@osd/i18n' ; import { MetricAggType } from './metric_agg_type' ; import { METRIC_TYPES } from './metric_agg_types' ; import { OSD_FIELD_TYPES } from '../../../../common' ; import { getResponseAggConfigClass , IResponseAggConfig } from './lib/get_response_agg_config_class' ; import { getPercentileValue } from './percentiles_get_value' ; import { ordinalSuffix } from './lib/ordinal_suffix' ; import { BaseAggParams } from '../types' ; export interface AggParamsPercentiles extends BaseAggParams { field : string ; percents ? : number [ ] ; } export type IPercentileAggConfig = IResponseAggConfig ; const valueProps = { makeLabel ( this ) { const customLabel = this . getParam ( 'customLabel' ) ; const label = customLabel || this . getFieldDisplayName ( ) ; return i18n . translate ( 'data.search.aggs.metrics.percentiles.valuePropsLabel' , { defaultMessage : '{percentile} percentile of {label}' , values : { percentile : ordinalSuffix ( this . key ) , label } , } ) ; } , } ; export const getPercentilesMetricAgg = ( ) => { return new MetricAggType < IPercentileAggConfig > ( { name : METRIC_TYPES . PERCENTILES , title : i18n . translate ( 'data.search.aggs.metrics.percentilesTitle' , { defaultMessage : 'Percentiles' , } ) , makeLabel ( agg ) { return i18n . translate ( 'data.search.aggs.metrics.percentilesLabel' , { defaultMessage : 'Percentiles of {field}' , values : { field : agg . getFieldDisplayName ( ) } , } ) ; } , params : [ { name : 'field' , type : 'field' , filterFieldTypes : [ OSD_FIELD_TYPES . NUMBER , OSD_FIELD_TYPES . DATE , OSD_FIELD_TYPES . HISTOGRAM ] , } , { name : 'percents' , default : [ 1 , 5 , 25 , 50 , 75 , 95 , 99 ] , } , { write ( agg , output ) { output . params . keyed = false ; } , } , ] , getResponseAggs ( agg ) { const ValueAggConfig = getResponseAggConfigClass ( agg , valueProps ) ; return agg . getParam ( 'percents' ) . map ( ( percent ) => new ValueAggConfig ( percent ) ) ; } , getValue : getPercentileValue , } ) ; } ;