import { Setup } from '../../lib/helpers/setup_request' ; import { rangeQuery , kqlQuery } from '../../../../observability/server' ; import { environmentQuery } from '../../../common/utils/environment_query' ; import { ProcessorEvent } from '../../../common/processor_event' ; import { SERVICE_NAME , CONTAINER_ID , HOST_NAME , } from '../../../common/elasticsearch_fieldnames' ; export const getServiceInfrastructure = async ( { kuery , serviceName , environment , setup , start , end , } ) => { const { apmEventClient } = setup ; const response = await apmEventClient . search ( 'get_service_infrastructure' , { apm : { events : [ ProcessorEvent . metric ] , } , body : { size : 0 , query : { bool : { filter : [ { term : { [ SERVICE_NAME ] : serviceName } } , ... rangeQuery ( start , end ) , ... environmentQuery ( environment ) , ... kqlQuery ( kuery ) , ] , } , } , aggs : { containerIds : { terms : { field : CONTAINER_ID , size : 500 , } , } , hostNames : { terms : { field : HOST_NAME , size : 500 , } , } , } , } , } ) ; return { containerIds : response . aggregations ?. containerIds ?. buckets . map ( ( bucket ) => bucket . key as string ) ?? [ ] , hostNames : response . aggregations ?. hostNames ?. buckets . map ( ( bucket ) => bucket . key as string ) ?? [ ] , } ; } ;