import { EuiComboBoxOptionOption } from '@elastic/eui' ; import { ES_FIELD_TYPES , IndexPattern , KBN_FIELD_TYPES , } from '../../../../../../../../../../src/plugins/data/public' ; import { getNestedProperty } from '../../../../../../../common/utils/object_utils' ; import { removeKeywordPostfix } from '../../../../../../../common/utils/field_utils' ; import { isRuntimeMappings } from '../../../../../../../common/shared_imports' ; import { DropDownLabel , DropDownOption , PivotAggsConfigWithUiSupportDict , pivotAggsFieldSupport , PivotGroupByConfigWithUiSupportDict , pivotGroupByFieldSupport , } from '../../../../../common' ; import { getDefaultAggregationConfig } from './get_default_aggregation_config' ; import { getDefaultGroupByConfig } from './get_default_group_by_config' ; import type { Field , StepDefineExposedState } from './types' ; const illegalEsAggNameChars = /[[\]>]/g ; export function getKibanaFieldTypeFromEsType ( type ) { switch ( type ) { case ES_FIELD_TYPES . FLOAT : case ES_FIELD_TYPES . HALF_FLOAT : case ES_FIELD_TYPES . SCALED_FLOAT : case ES_FIELD_TYPES . DOUBLE : case ES_FIELD_TYPES . INTEGER : case ES_FIELD_TYPES . LONG : case ES_FIELD_TYPES . SHORT : case ES_FIELD_TYPES . UNSIGNED_LONG : return KBN_FIELD_TYPES . NUMBER ; case ES_FIELD_TYPES . DATE : case ES_FIELD_TYPES . DATE_NANOS : return KBN_FIELD_TYPES . DATE ; case ES_FIELD_TYPES . KEYWORD : case ES_FIELD_TYPES . STRING : return KBN_FIELD_TYPES . STRING ; default : return type as KBN_FIELD_TYPES ; } } export function getPivotDropdownOptions ( indexPattern , runtimeMappings ? ) { const groupByOptions = [ ] ; const groupByOptionsData = { } ; const aggOptions = [ ] ; const aggOptionsData = { } ; const ignoreFieldNames = [ '_id' , '_index' , '_type' ] ; const indexPatternFields = indexPattern . fields . filter ( ( field ) => field . aggregatable === true && ! ignoreFieldNames . includes ( field . name ) && ! field . runtimeField && typeof field . type !== 'undefined' ) . map ( ( field ) : Field => ( { name : field . name , type : field . type as KBN_FIELD_TYPES } ) ) ; let runtimeFields = [ ] ; if ( isRuntimeMappings ( runtimeMappings ) ) { runtimeFields = Object . keys ( runtimeMappings ) . map ( ( fieldName ) => { const field = runtimeMappings [ fieldName ] ; return { name : fieldName , type : getKibanaFieldTypeFromEsType ( field . type ) } ; } ) ; } const sortByLabel = ( a , b ) => a . name . localeCompare ( b . name ) ; const combinedFields = [ ... indexPatternFields , ... runtimeFields ] . sort ( sortByLabel ) ; combinedFields . forEach ( ( field ) => { const rawFieldName = field . name ; const displayFieldName = removeKeywordPostfix ( rawFieldName ) ; const availableGroupByAggs = getNestedProperty ( pivotGroupByFieldSupport , field . type ) ; if ( availableGroupByAggs !== undefined ) { availableGroupByAggs . forEach ( ( groupByAgg ) => { const aggName = displayFieldName . replace ( illegalEsAggNameChars , '' ) . trim ( ) ; const dropDownName = `${ groupByAgg }(${ displayFieldName })` ; const groupByOption = { label : dropDownName } ; groupByOptions . push ( groupByOption ) ; groupByOptionsData [ dropDownName ] = getDefaultGroupByConfig ( aggName , dropDownName , rawFieldName , groupByAgg ) ; } ) ; } const aggOption = { label : displayFieldName , options : [ ] } ; const availableAggs = getNestedProperty ( pivotAggsFieldSupport , field . type ) ; if ( availableAggs !== undefined ) { availableAggs . forEach ( ( agg ) => { const aggName = `${ displayFieldName . replace ( illegalEsAggNameChars , '' ) . trim ( ) }.${ agg }` ; const dropDownName = `${ agg }(${ displayFieldName })` ; aggOption . options . push ( { label : dropDownName } ) ; aggOptionsData [ dropDownName ] = getDefaultAggregationConfig ( aggName , dropDownName , rawFieldName , agg ) ; } ) ; } aggOptions . push ( aggOption ) ; } ) ; return { fields : combinedFields , groupByOptions , groupByOptionsData , aggOptions , aggOptionsData , } ; }