import { startsWith } from 'lodash' ; import { toPercentileNumber } from '../../../../common/to_percentile_number' ; import { METRIC_TYPES } from '../../../../../../data/common' ; import { TSVB_METRIC_TYPES } from '../../../../common/enums' ; import type { Metric } from '../../../../common/types' ; const percentileTest = /\[[0-9\.]+\]$/ ; export const getBucketsPath = ( id , metrics ) => { const metric = metrics . find ( ( m ) => startsWith ( id , m . id ) ) ; let bucketsPath = String ( id ) ; if ( metric ) { switch ( metric . type ) { case METRIC_TYPES . DERIVATIVE : bucketsPath += '[normalized_value]' ; break ; case TSVB_METRIC_TYPES . PERCENTILE : if ( percentileTest . test ( bucketsPath ) ) break ; if ( metric . percentiles ?. length ) { const percent = metric . percentiles [ 0 ] ; bucketsPath += `[${ toPercentileNumber ( percent . value ! ) }]` ; } break ; case TSVB_METRIC_TYPES . PERCENTILE_RANK : if ( percentileTest . test ( bucketsPath ) ) break ; bucketsPath += `[${ toPercentileNumber ( metric . value ! ) }]` ; break ; case TSVB_METRIC_TYPES . STD_DEVIATION : case TSVB_METRIC_TYPES . VARIANCE : case TSVB_METRIC_TYPES . SUM_OF_SQUARES : if ( /^std_deviation/ . test ( metric . type ) && [ 'upper' , 'lower' ] . includes ( metric . mode ! ) ) { bucketsPath += `[std_${ metric . mode }]` ; } else { bucketsPath += `[${ metric . type }]` ; } break ; } } return bucketsPath ; } ;