import { jsonRt } from '@kbn/io-ts-utils/json_rt' ; import { toNumberRt } from '@kbn/io-ts-utils/to_number_rt' ; import * as t from 'io-ts' ; import { LatencyAggregationType , latencyAggregationTypeRt , } from '../../../common/latency_aggregation_types' ; import { getSearchAggregatedTransactions } from '../../lib/helpers/transactions' ; import { setupRequest } from '../../lib/helpers/setup_request' ; import { getServiceTransactionGroups } from '../services/get_service_transaction_groups' ; import { getServiceTransactionGroupDetailedStatisticsPeriods } from '../services/get_service_transaction_group_detailed_statistics' ; import { getTransactionBreakdown } from './breakdown' ; import { getTransactionTraceSamples } from './trace_samples' ; import { getLatencyPeriods } from './get_latency_charts' ; import { getFailedTransactionRatePeriods } from './get_failed_transaction_rate_periods' ; import { createApmServerRoute } from '../apm_routes/create_apm_server_route' ; import { createApmServerRouteRepository } from '../apm_routes/create_apm_server_route_repository' ; import { comparisonRangeRt , environmentRt , kueryRt , rangeRt , } from '../default_api_types' ; const transactionGroupsMainStatisticsRoute = createApmServerRoute ( { endpoint : 'GET /internal/apm/services/{serviceName}/transactions/groups/main_statistics' , params : t . type ( { path : t . type ( { serviceName : t . string } ) , query : t . intersection ( [ environmentRt , kueryRt , rangeRt , t . type ( { transactionType : t . string , latencyAggregationType : latencyAggregationTypeRt , } ) , ] ) , } ) , options : { tags : [ 'access:apm' ] , } , handler : async ( resources ) => { const { params } = resources ; const setup = await setupRequest ( resources ) ; const { path : { serviceName } , query : { environment , kuery , latencyAggregationType , transactionType , start , end , } , } = params ; const searchAggregatedTransactions = await getSearchAggregatedTransactions ( { ... setup , kuery , start , end , } ) ; return getServiceTransactionGroups ( { environment , kuery , setup , serviceName , searchAggregatedTransactions , transactionType , latencyAggregationType , start , end , } ) ; } , } ) ; const transactionGroupsDetailedStatisticsRoute = createApmServerRoute ( { endpoint : 'GET /internal/apm/services/{serviceName}/transactions/groups/detailed_statistics' , params : t . type ( { path : t . type ( { serviceName : t . string } ) , query : t . intersection ( [ environmentRt , kueryRt , rangeRt , comparisonRangeRt , t . type ( { transactionNames : jsonRt . pipe ( t . array ( t . string ) ) , numBuckets : toNumberRt , transactionType : t . string , latencyAggregationType : latencyAggregationTypeRt , } ) , ] ) , } ) , options : { tags : [ 'access:apm' ] , } , handler : async ( resources ) => { const setup = await setupRequest ( resources ) ; const { params } = resources ; const { path : { serviceName } , query : { environment , kuery , transactionNames , latencyAggregationType , numBuckets , transactionType , comparisonStart , comparisonEnd , start , end , } , } = params ; const searchAggregatedTransactions = await getSearchAggregatedTransactions ( { ... setup , kuery , start , end , } ) ; return await getServiceTransactionGroupDetailedStatisticsPeriods ( { environment , kuery , setup , serviceName , transactionNames , searchAggregatedTransactions , transactionType , numBuckets , latencyAggregationType , comparisonStart , comparisonEnd , start , end , } ) ; } , } ) ; const transactionLatencyChartsRoute = createApmServerRoute ( { endpoint : 'GET /internal/apm/services/{serviceName}/transactions/charts/latency' , params : t . type ( { path : t . type ( { serviceName : t . string , } ) , query : t . intersection ( [ t . type ( { transactionType : t . string , latencyAggregationType : latencyAggregationTypeRt , } ) , t . partial ( { transactionName : t . string } ) , t . intersection ( [ environmentRt , kueryRt , rangeRt , comparisonRangeRt ] ) , ] ) , } ) , options : { tags : [ 'access:apm' ] } , handler : async ( resources ) => { const setup = await setupRequest ( resources ) ; const { params , logger } = resources ; const { serviceName } = params . path ; const { environment , kuery , transactionType , transactionName , latencyAggregationType , comparisonStart , comparisonEnd , start , end , } = params . query ; const searchAggregatedTransactions = await getSearchAggregatedTransactions ( { ... setup , kuery , start , end , } ) ; const options = { environment , kuery , serviceName , transactionType , transactionName , setup , searchAggregatedTransactions , logger , start , end , } ; const { currentPeriod , previousPeriod } = await getLatencyPeriods ( { ... options , latencyAggregationType : latencyAggregationType as LatencyAggregationType , comparisonStart , comparisonEnd , } ) ; return { currentPeriod , previousPeriod , } ; } , } ) ; const transactionTraceSamplesRoute = createApmServerRoute ( { endpoint : 'GET /internal/apm/services/{serviceName}/transactions/traces/samples' , params : t . type ( { path : t . type ( { serviceName : t . string , } ) , query : t . intersection ( [ t . type ( { transactionType : t . string , transactionName : t . string , } ) , t . partial ( { transactionId : t . string , traceId : t . string , sampleRangeFrom : toNumberRt , sampleRangeTo : toNumberRt , } ) , environmentRt , kueryRt , rangeRt , ] ) , } ) , options : { tags : [ 'access:apm' ] } , handler : async ( resources ) => { const setup = await setupRequest ( resources ) ; const { params } = resources ; const { serviceName } = params . path ; const { environment , kuery , transactionType , transactionName , transactionId = '' , traceId = '' , sampleRangeFrom , sampleRangeTo , start , end , } = params . query ; return getTransactionTraceSamples ( { environment , kuery , serviceName , transactionType , transactionName , transactionId , traceId , sampleRangeFrom , sampleRangeTo , setup , start , end , } ) ; } , } ) ; const transactionChartsBreakdownRoute = createApmServerRoute ( { endpoint : 'GET /internal/apm/services/{serviceName}/transaction/charts/breakdown' , params : t . type ( { path : t . type ( { serviceName : t . string , } ) , query : t . intersection ( [ t . type ( { transactionType : t . string } ) , t . partial ( { transactionName : t . string } ) , environmentRt , kueryRt , rangeRt , ] ) , } ) , options : { tags : [ 'access:apm' ] } , handler : async ( resources ) => { const setup = await setupRequest ( resources ) ; const { params } = resources ; const { serviceName } = params . path ; const { environment , kuery , transactionName , transactionType , start , end } = params . query ; return getTransactionBreakdown ( { environment , kuery , serviceName , transactionName , transactionType , setup , start , end , } ) ; } , } ) ; const transactionChartsErrorRateRoute = createApmServerRoute ( { endpoint : 'GET /internal/apm/services/{serviceName}/transactions/charts/error_rate' , params : t . type ( { path : t . type ( { serviceName : t . string , } ) , query : t . intersection ( [ t . type ( { transactionType : t . string } ) , t . partial ( { transactionName : t . string } ) , t . intersection ( [ environmentRt , kueryRt , rangeRt , comparisonRangeRt ] ) , ] ) , } ) , options : { tags : [ 'access:apm' ] } , handler : async ( resources ) => { const setup = await setupRequest ( resources ) ; const { params } = resources ; const { serviceName } = params . path ; const { environment , kuery , transactionType , transactionName , comparisonStart , comparisonEnd , start , end , } = params . query ; const searchAggregatedTransactions = await getSearchAggregatedTransactions ( { ... setup , kuery , start , end , } ) ; return getFailedTransactionRatePeriods ( { environment , kuery , serviceName , transactionType , transactionName , setup , searchAggregatedTransactions , comparisonStart , comparisonEnd , start , end , } ) ; } , } ) ; export const transactionRouteRepository = createApmServerRouteRepository ( ) . add ( transactionGroupsMainStatisticsRoute ) . add ( transactionGroupsDetailedStatisticsRoute ) . add ( transactionLatencyChartsRoute ) . add ( transactionTraceSamplesRoute ) . add ( transactionChartsBreakdownRoute ) . add ( transactionChartsErrorRateRoute ) ;