import { i18n } from '@osd/i18n' ; import { OSD_FIELD_TYPES } from '../../../../common' ; import { AggTypesDependencies } from '../agg_types' ; import { BaseAggParams } from '../types' ; import { MetricAggType } from './metric_agg_type' ; import { getResponseAggConfigClass , IResponseAggConfig } from './lib/get_response_agg_config_class' ; import { getPercentileValue } from './percentiles_get_value' ; import { METRIC_TYPES } from './metric_agg_types' ; export interface AggParamsPercentileRanks extends BaseAggParams { field : string ; values ? : number [ ] ; } export type IPercentileRanksAggConfig = IResponseAggConfig ; export interface PercentileRanksMetricAggDependencies { getFieldFormatsStart : AggTypesDependencies [ 'getFieldFormatsStart' ] ; } const getValueProps = ( getFieldFormatsStart ) => { return { makeLabel ( this ) { const { getDefaultInstance } = getFieldFormatsStart ( ) ; const field = this . getField ( ) ; const format = ( field && field . format ) || getDefaultInstance ( OSD_FIELD_TYPES . NUMBER ) ; const customLabel = this . getParam ( 'customLabel' ) ; const label = customLabel || this . getFieldDisplayName ( ) ; return i18n . translate ( 'data.search.aggs.metrics.percentileRanks.valuePropsLabel' , { defaultMessage : 'Percentile rank {format} of "{label}"' , values : { format : format . convert ( this . key , 'text' ) , label } , } ) ; } , } ; } ; export const getPercentileRanksMetricAgg = ( { getFieldFormatsStart , } ) => { return new MetricAggType < IPercentileRanksAggConfig > ( { name : METRIC_TYPES . PERCENTILE_RANKS , title : i18n . translate ( 'data.search.aggs.metrics.percentileRanksTitle' , { defaultMessage : 'Percentile Ranks' , } ) , makeLabel ( agg ) { return i18n . translate ( 'data.search.aggs.metrics.percentileRanksLabel' , { defaultMessage : 'Percentile ranks of {field}' , values : { field : agg . getFieldDisplayName ( ) } , } ) ; } , params : [ { name : 'field' , type : 'field' , filterFieldTypes : [ OSD_FIELD_TYPES . NUMBER , OSD_FIELD_TYPES . HISTOGRAM ] , } , { name : 'values' , default : [ ] , } , { write ( agg , output ) { output . params . keyed = false ; } , } , ] , getResponseAggs ( agg ) { const ValueAggConfig = getResponseAggConfigClass ( agg , getValueProps ( getFieldFormatsStart ) ) ; const values = agg . getParam ( 'values' ) ; return values . map ( ( value ) => new ValueAggConfig ( value ) ) ; } , getSerializedFormat ( agg ) { return { id : 'percent' , } ; } , getValue ( agg , bucket ) { return getPercentileValue ( agg , bucket ) / 100 ; } , } ) ; } ;