import React , { createContext , ReactNode } from 'react' ; import { ValuesType } from 'utility-types' ; import { isRumAgentName } from '../../../common/agent_name' ; import { TRANSACTION_PAGE_LOAD , TRANSACTION_REQUEST , } from '../../../common/transaction_types' ; import { useServiceTransactionTypesFetcher } from './use_service_transaction_types_fetcher' ; import { useServiceAgentFetcher } from './use_service_agent_fetcher' ; import { APIReturnType } from '../../services/rest/createCallApmApi' ; import { useServiceAlertsFetcher } from './use_service_alerts_fetcher' ; import { useApmParams } from '../../hooks/use_apm_params' ; import { useTimeRange } from '../../hooks/use_time_range' ; import { useFallbackToTransactionsFetcher } from '../../hooks/use_fallback_to_transactions_fetcher' ; export type APMServiceAlert = ValuesType < APIReturnType < 'GET /internal/apm/services/{serviceName}/alerts' > [ 'alerts' ] > ; export interface APMServiceContextValue { serviceName : string ; agentName ? : string ; transactionType ? : string ; transactionTypes : string [ ] ; alerts : APMServiceAlert [ ] ; runtimeName ? : string ; fallbackToTransactions : boolean ; } export const APMServiceContext = createContext < APMServiceContextValue > ( { serviceName : '' , transactionTypes : [ ] , alerts : [ ] , fallbackToTransactions : false , } ) ; export function ApmServiceContextProvider ( { children , } ) { const { path : { serviceName } , query , query : { kuery , rangeFrom , rangeTo } , } = useApmParams ( '/services/{serviceName}' ) ; const { start , end } = useTimeRange ( { rangeFrom , rangeTo } ) ; const { agentName , runtimeName } = useServiceAgentFetcher ( { serviceName , start , end , } ) ; const transactionTypes = useServiceTransactionTypesFetcher ( { serviceName , start , end , } ) ; const transactionType = getTransactionType ( { transactionType : query . transactionType , transactionTypes , agentName , } ) ; const { alerts } = useServiceAlertsFetcher ( { serviceName , transactionType , environment : query . environment , start , end , } ) ; const { fallbackToTransactions } = useFallbackToTransactionsFetcher ( { kuery , } ) ; return ( < APMServiceContext . Provider value = { { serviceName , agentName , transactionType , transactionTypes , alerts , runtimeName , fallbackToTransactions , } } children = { children } / > ) ; } export function getTransactionType ( { transactionType , transactionTypes , agentName , } ) { if ( transactionType ) { return transactionType ; } if ( ! agentName || transactionTypes . length === 0 ) { return ; } const defaultTransactionType = isRumAgentName ( agentName ) ? TRANSACTION_PAGE_LOAD : TRANSACTION_REQUEST ; return transactionTypes . includes ( defaultTransactionType ) ? defaultTransactionType : transactionTypes [ 0 ] ; }