import { find , uniqBy } from 'lodash' ; import { Connection , ConnectionNode } from '../../../common/service_map' ; import { Setup } from '../../lib/helpers/setup_request' ; import { fetchServicePathsFromTraceIds } from './fetch_service_paths_from_trace_ids' ; export function getConnections ( { paths , } ) { if ( ! paths ) { return [ ] ; } const connectionsArr = paths . flatMap ( ( path ) => { return path . reduce ( ( conns , location , index ) => { const prev = path [ index - 1 ] ; if ( prev ) { return conns . concat ( { source : prev , destination : location , } ) ; } return conns ; } , [ ] as Connection [ ] ) ; } , [ ] as Connection [ ] ) ; const connections = uniqBy ( connectionsArr , ( value ) => find ( connectionsArr , value ) ) ; return connections ; } export async function getServiceMapFromTraceIds ( { setup , traceIds , start , end , } ) { const serviceMapFromTraceIdsScriptResponse = await fetchServicePathsFromTraceIds ( setup , traceIds , start , end ) ; const serviceMapScriptedAggValue = serviceMapFromTraceIdsScriptResponse . aggregations ?. service_map . value ; return { connections : getConnections ( { paths : serviceMapScriptedAggValue ?. paths , } ) , discoveredServices : serviceMapScriptedAggValue ?. discoveredServices ?? [ ] , } ; }