import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey' ; import { ProcessorEvent } from '../../../common/processor_event' ; import { withApmSpan } from '../../utils/with_apm_span' ; import { getHistogramIntervalRequest , getHistogramRangeSteps , } from '../correlations/queries/query_histogram_range_steps' ; import { getTransactionDurationRangesRequest } from '../correlations/queries/query_ranges' ; import { getPercentileThresholdValue } from './get_percentile_threshold_value' ; import type { OverallLatencyDistributionOptions , OverallLatencyDistributionResponse , } from './types' ; interface Aggs extends estypes . AggregationsMultiBucketAggregateBase { buckets : Array < { from : number ; doc_count : number ; } > ; } export async function getOverallLatencyDistribution ( options ) { return withApmSpan ( 'get_overall_latency_distribution' , async ( ) => { const overallLatencyDistribution = { } ; const { setup , termFilters , ... rawParams } = options ; const { apmEventClient } = setup ; const params = { index : '' , ... rawParams , } ; overallLatencyDistribution . percentileThresholdValue = await getPercentileThresholdValue ( options ) ; if ( ! overallLatencyDistribution . percentileThresholdValue ) { return overallLatencyDistribution ; } const steps = 100 ; const { body : histogramIntervalRequestBody } = getHistogramIntervalRequest ( params ) ; const histogramIntervalResponse = ( await apmEventClient . search ( 'get_histogram_interval' , { apm : { events : [ ProcessorEvent . transaction ] } , body : histogramIntervalRequestBody , } ) ) as { aggregations ? : { transaction_duration_min : estypes . AggregationsRateAggregate ; transaction_duration_max : estypes . AggregationsRateAggregate ; } ; hits : { total : estypes . SearchTotalHits ; } ; } ; if ( ! histogramIntervalResponse . aggregations || histogramIntervalResponse . hits . total . value === 0 ) { return overallLatencyDistribution ; } const min = histogramIntervalResponse . aggregations . transaction_duration_min . value ; const max = histogramIntervalResponse . aggregations . transaction_duration_max . value * 2 ; const histogramRangeSteps = getHistogramRangeSteps ( min , max , steps ) ; const { body : transactionDurationRangesRequestBody } = getTransactionDurationRangesRequest ( params , histogramRangeSteps , termFilters ) ; const transactionDurationRangesResponse = ( await apmEventClient . search ( 'get_transaction_duration_ranges' , { apm : { events : [ ProcessorEvent . transaction ] } , body : transactionDurationRangesRequestBody , } ) ) as { aggregations ? : { logspace_ranges : Aggs ; } ; } ; if ( ! transactionDurationRangesResponse . aggregations ) { return overallLatencyDistribution ; } overallLatencyDistribution . overallHistogram = transactionDurationRangesResponse . aggregations . logspace_ranges . buckets . map ( ( d ) => ( { key : d . from , doc_count : d . doc_count , } ) ) . filter ( ( d ) => d . key !== undefined ) ; return overallLatencyDistribution ; } ) ; }