import { Field , Aggregation , NewJobCaps , METRIC_AGG_TYPE , RollupFields , EVENT_RATE_FIELD_ID , } from '../types/fields' ; import { ES_FIELD_TYPES } from '../../../../../src/plugins/data/common' ; import { ML_JOB_AGGREGATION } from '../constants/aggregation_types' ; export function combineFieldsAndAggs ( fields , aggs , rollupFields ) { const keywordFields = getKeywordFields ( fields ) ; const textFields = getTextFields ( fields ) ; const numericalFields = getNumericalFields ( fields ) ; const ipFields = getIpFields ( fields ) ; const geoFields = getGeoFields ( fields ) ; const isRollup = Object . keys ( rollupFields ) . length > 0 ; const mix = mixFactory ( isRollup , rollupFields ) ; aggs . forEach ( ( a ) => { if ( a . type === METRIC_AGG_TYPE && a . fields !== undefined ) { switch ( a . id ) { case ML_JOB_AGGREGATION . LAT_LONG : geoFields . forEach ( ( f ) => mix ( f , a ) ) ; break ; case ML_JOB_AGGREGATION . INFO_CONTENT : case ML_JOB_AGGREGATION . HIGH_INFO_CONTENT : case ML_JOB_AGGREGATION . LOW_INFO_CONTENT : textFields . forEach ( ( f ) => mix ( f , a ) ) ; case ML_JOB_AGGREGATION . DISTINCT_COUNT : case ML_JOB_AGGREGATION . HIGH_DISTINCT_COUNT : case ML_JOB_AGGREGATION . LOW_DISTINCT_COUNT : keywordFields . forEach ( ( f ) => mix ( f , a ) ) ; ipFields . forEach ( ( f ) => mix ( f , a ) ) ; default : numericalFields . forEach ( ( f ) => { mix ( f , a ) ; } ) ; break ; } } } ) ; return { aggs , fields : isRollup ? filterFields ( fields ) : fields , } ; } function filterFields ( fields ) { return fields . filter ( ( f ) => f . aggs && ( f . aggs . length > 0 || ( f . aggs . length === 0 && f . type === ES_FIELD_TYPES . DATE ) ) ) ; } function mixFactory ( isRollup , rollupFields ) { return function mix ( field , agg ) : void { if ( isRollup === false || ( rollupFields [ field . id ] && rollupFields [ field . id ] . find ( ( f ) => f . agg === agg . dslName ) ) ) { if ( field . aggs !== undefined ) { field . aggs . push ( agg ) ; } if ( agg . fields !== undefined ) { agg . fields . push ( field ) ; } } } ; } function getKeywordFields ( fields ) { return fields . filter ( ( f ) => f . type === ES_FIELD_TYPES . KEYWORD ) ; } function getTextFields ( fields ) { return fields . filter ( ( f ) => f . type === ES_FIELD_TYPES . TEXT ) ; } function getIpFields ( fields ) { return fields . filter ( ( f ) => f . type === ES_FIELD_TYPES . IP ) ; } function getNumericalFields ( fields ) { return fields . filter ( ( f ) => f . type === ES_FIELD_TYPES . LONG || f . type === ES_FIELD_TYPES . UNSIGNED_LONG || f . type === ES_FIELD_TYPES . INTEGER || f . type === ES_FIELD_TYPES . SHORT || f . type === ES_FIELD_TYPES . BYTE || f . type === ES_FIELD_TYPES . DOUBLE || f . type === ES_FIELD_TYPES . FLOAT || f . type === ES_FIELD_TYPES . HALF_FLOAT || f . type === ES_FIELD_TYPES . SCALED_FLOAT ) ; } function getGeoFields ( fields ) { return fields . filter ( ( f ) => f . type === ES_FIELD_TYPES . GEO_POINT || f . type === ES_FIELD_TYPES . GEO_SHAPE ) ; } /**
 * Sort fields by name, keeping event rate at the beginning
 */ export function sortFields ( fields ) { if ( fields . length === 0 ) { return fields ; } let eventRate ; if ( fields [ 0 ] . id === EVENT_RATE_FIELD_ID ) { [ eventRate ] = fields . splice ( 0 , 1 ) ; } fields . sort ( ( a , b ) => a . name . localeCompare ( b . name ) ) ; if ( eventRate !== undefined ) { fields . splice ( 0 , 0 , eventRate ) ; } return fields ; }