import type { ElasticsearchClient } from 'src/core/server' ; import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey' ; import type { FieldValuePair , CorrelationsParams , } from '../../../../common/correlations/types' ; import { TERMS_SIZE } from '../../../../common/correlations/constants' ; import { splitAllSettledPromises } from '../utils' ; import { getQueryWithParams } from './get_query_with_params' ; import { getRequestBase } from './get_request_base' ; export const getTermsAggRequest = ( params , fieldName ) : estypes . SearchRequest => ( { ... getRequestBase ( params ) , body : { query : getQueryWithParams ( { params } ) , size : 0 , aggs : { attribute_terms : { terms : { field : fieldName , size : TERMS_SIZE , } , } , } , } , } ) ; interface Aggs extends estypes . AggregationsMultiBucketAggregateBase { buckets : Array < { key : string ; key_as_string ? : string ; } > ; } const fetchTransactionDurationFieldTerms = async ( esClient , params , fieldName ) : Promise < FieldValuePair [ ] > => { const resp = await esClient . search < unknown , { attribute_terms : Aggs ; } > ( getTermsAggRequest ( params , fieldName ) ) ; if ( resp . body . aggregations === undefined ) { throw new Error ( 'fetchTransactionDurationFieldTerms failed, did not return aggregations.' ) ; } const buckets = resp . body . aggregations . attribute_terms ?. buckets ; if ( buckets ?. length >= 1 ) { return buckets . map ( ( d ) => ( { fieldName , fieldValue : d . key_as_string ?? d . key , } ) ) ; } return [ ] ; } ; export const fetchTransactionDurationFieldValuePairs = async ( esClient , params , fieldCandidates ) : Promise < { fieldValuePairs : FieldValuePair [ ] ; errors : any [ ] ; } > => { const { fulfilled : responses , rejected : errors } = splitAllSettledPromises ( await Promise . allSettled ( fieldCandidates . map ( ( fieldCandidate ) => fetchTransactionDurationFieldTerms ( esClient , params , fieldCandidate ) ) ) ) ; return { fieldValuePairs : responses . flat ( ) , errors } ; } ;