import { last } from 'lodash' ; import { getSplits , getLastMetric } from '../../helpers' ; import { toPercentileNumber } from '../../../../../common/to_percentile_number' ; import { TSVB_METRIC_TYPES } from '../../../../../common/enums' ; import type { TableResponseProcessorsFunction } from './types' ; import type { PanelDataArray } from '../../../../../common/types/vis_data' ; export const percentile = ( { response , panel , series , meta , extractFields } ) => ( next ) => async ( results ) => { const metric = getLastMetric ( series ) ; if ( metric . type !== TSVB_METRIC_TYPES . PERCENTILE ) { return next ( results ) ; } ( await getSplits ( response , panel , series , meta , extractFields ) ) . forEach ( ( split ) => { const lastPercentile = last ( metric . percentiles ) ?. value ?? 0 ; const percentileKey = toPercentileNumber ( lastPercentile ) ; const data = split . timeseries . buckets . map ( ( b ) => [ b . key , b [ metric . id ] . values [ percentileKey ] , ] ) as PanelDataArray [ ] ; results . push ( { id : split . id , label : `${ split . label } (${ lastPercentile ?? 0 })` , data : data ! , } ) ; } ) ; return next ( results ) ; } ;