import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey' ; import { HttpFetchError } from '../../../../../../src/core/public' ; import type { IndexPattern } from '../../../../../../src/plugins/data/public' ; import type { PivotTransformPreviewRequestSchema , PostTransformsPreviewRequestSchema , PutTransformsLatestRequestSchema , PutTransformsPivotRequestSchema , PutTransformsRequestSchema , } from '../../../common/api_schemas/transforms' ; import { isPopulatedObject } from '../../../common/shared_imports' ; import { DateHistogramAgg , HistogramAgg , TermsAgg } from '../../../common/types/pivot_group_by' ; import { isIndexPattern } from '../../../common/types/index_pattern' ; import type { SavedSearchQuery } from '../hooks/use_search_items' ; import type { StepDefineExposedState } from '../sections/create_transform/components/step_define' ; import type { StepDetailsExposedState } from '../sections/create_transform/components/step_details' ; import { getEsAggFromAggConfig , getEsAggFromGroupByConfig , isGroupByDateHistogram , isGroupByHistogram , isGroupByTerms , GroupByConfigWithUiSupport , PivotAggsConfig , PivotGroupByConfig , } from './' ; export interface SimpleQuery { query_string : { query : string ; default_operator ? : estypes . QueryDslOperator ; } ; } export type PivotQuery = SimpleQuery | SavedSearchQuery ; export function getPivotQuery ( search ) { if ( typeof search === 'string' ) { return { query_string : { query : search , default_operator : 'AND' , } , } ; } return search ; } export function isSimpleQuery ( arg ) { return isPopulatedObject ( arg , [ 'query_string' ] ) ; } export const matchAllQuery = { match_all : { } } ; export function isMatchAllQuery ( query ) { return ( isPopulatedObject ( query , [ 'match_all' ] ) && typeof query . match_all === 'object' && query . match_all !== null && Object . keys ( query . match_all ) . length === 0 ) ; } export const defaultQuery = { query_string : { query : '*' } } ; export function isDefaultQuery ( query ) { return isSimpleQuery ( query ) && query . query_string . query === '*' ; } export function getCombinedRuntimeMappings ( indexPattern , runtimeMappings ? ) { let combinedRuntimeMappings = { } ; if ( isIndexPattern ( indexPattern ) ) { const computedFields = indexPattern . getComputedFields ( ) ; if ( computedFields ?. runtimeFields !== undefined ) { const ipRuntimeMappings = computedFields . runtimeFields ; if ( isPopulatedObject ( ipRuntimeMappings ) ) { combinedRuntimeMappings = { ... combinedRuntimeMappings , ... ipRuntimeMappings } ; } } } if ( isPopulatedObject ( runtimeMappings ) ) { combinedRuntimeMappings = { ... combinedRuntimeMappings , ... runtimeMappings } ; } if ( isPopulatedObject < keyof StepDefineExposedState [ 'runtimeMappings' ] > ( combinedRuntimeMappings ) ) { return combinedRuntimeMappings ; } return undefined ; } export const getMissingBucketConfig = ( g ) : { missing_bucket ? : boolean ; } => { return g . missing_bucket !== undefined ? { missing_bucket : g . missing_bucket } : { } ; } ; export const getRequestPayload = ( pivotAggsArr , pivotGroupByArr ) => { const request = { pivot : { group_by : { } , aggregations : { } , } as PivotTransformPreviewRequestSchema [ 'pivot' ] , } ; pivotGroupByArr . forEach ( ( g ) => { if ( isGroupByTerms ( g ) ) { const termsAgg = { terms : { field : g . field , ... getMissingBucketConfig ( g ) , } , } ; request . pivot . group_by [ g . aggName ] = termsAgg ; } else if ( isGroupByHistogram ( g ) ) { const histogramAgg = { histogram : { field : g . field , interval : g . interval , ... getMissingBucketConfig ( g ) , } , } ; request . pivot . group_by [ g . aggName ] = histogramAgg ; } else if ( isGroupByDateHistogram ( g ) ) { const dateHistogramAgg = { date_histogram : { field : g . field , calendar_interval : g . calendar_interval , ... getMissingBucketConfig ( g ) , } , } ; request . pivot . group_by [ g . aggName ] = dateHistogramAgg ; } else { request . pivot . group_by [ g . aggName ] = getEsAggFromGroupByConfig ( g ) ; } } ) ; pivotAggsArr . forEach ( ( agg ) => { const result = getEsAggFromAggConfig ( agg ) ; if ( result === null ) { return ; } request . pivot . aggregations [ agg . aggName ] = result ; } ) ; return request ; } ; export function getPreviewTransformRequestBody ( indexPatternTitle , query , partialRequest ? , runtimeMappings ? ) { const index = indexPatternTitle . split ( ',' ) . map ( ( name ) => name . trim ( ) ) ; return { source : { index , ... ( ! isDefaultQuery ( query ) && ! isMatchAllQuery ( query ) ? { query } : { } ) , ... ( isPopulatedObject ( runtimeMappings ) ? { runtime_mappings : runtimeMappings } : { } ) , } , ... ( partialRequest ?? { } ) , } ; } export const getCreateTransformSettingsRequestBody = ( transformDetailsState ) : { settings ? : PutTransformsRequestSchema [ 'settings' ] ; } => { const settings = { ... ( transformDetailsState . transformSettingsMaxPageSearchSize ? { max_page_search_size : transformDetailsState . transformSettingsMaxPageSearchSize } : { } ) , ... ( transformDetailsState . transformSettingsDocsPerSecond ? { docs_per_second : transformDetailsState . transformSettingsDocsPerSecond } : { } ) , } ; return Object . keys ( settings ) . length > 0 ? { settings } : { } ; } ; export const getCreateTransformRequestBody = ( indexPatternTitle , pivotState , transformDetailsState ) : PutTransformsPivotRequestSchema | PutTransformsLatestRequestSchema => ( { ... getPreviewTransformRequestBody ( indexPatternTitle , getPivotQuery ( pivotState . searchQuery ) , pivotState . previewRequest , pivotState . runtimeMappings ) , ... ( transformDetailsState . transformDescription !== '' ? { description : transformDetailsState . transformDescription } : { } ) , ... ( transformDetailsState . transformFrequency !== '' ? { frequency : transformDetailsState . transformFrequency } : { } ) , dest : { index : transformDetailsState . destinationIndex , } , ... ( transformDetailsState . isContinuousModeEnabled ? { sync : { time : { field : transformDetailsState . continuousModeDateField , delay : transformDetailsState . continuousModeDelay , } , } , } : { } ) , ... ( transformDetailsState . isRetentionPolicyEnabled ? { retention_policy : { time : { field : transformDetailsState . retentionPolicyDateField , max_age : transformDetailsState . retentionPolicyMaxAge , } , } , } : { } ) , ... ( transformDetailsState . _meta ? { _meta : transformDetailsState . _meta } : { } ) , ... getCreateTransformSettingsRequestBody ( transformDetailsState ) , } ) ; export function isHttpFetchError ( error ) { return ( error instanceof HttpFetchError && typeof error . name === 'string' && typeof error . message !== 'undefined' ) ; }