import { Field , Aggregation , AggId , FieldId , EVENT_RATE_FIELD_ID , } from '../../../../common/types/fields' ; import { ES_FIELD_TYPES } from '../../../../../../../src/plugins/data/public' ; import { DataView } from '../../../../../../../src/plugins/data_views/public' ; import { ml } from '../ml_api_service' ; import { processTextAndKeywordFields , NewJobCapabilitiesServiceBase } from './new_job_capabilities' ; const categoryFieldTypes = [ ES_FIELD_TYPES . TEXT , ES_FIELD_TYPES . KEYWORD , ES_FIELD_TYPES . IP ] ; class NewJobCapsService extends NewJobCapabilitiesServiceBase { private _catFields : Field [ ] = [ ] ; private _dateFields : Field [ ] = [ ] ; private _includeEventRateField : boolean = true ; private _removeTextFields : boolean = true ; public get catFields ( ) : Field [ ] { return this . _catFields ; } public get dateFields ( ) : Field [ ] { return this . _dateFields ; } public get categoryFields ( ) : Field [ ] { return filterCategoryFields ( this . _fields ) ; } public async initializeFromDataVIew ( dataView , includeEventRateField = true , removeTextFields = true ) { try { this . _includeEventRateField = includeEventRateField ; this . _removeTextFields = removeTextFields ; const resp = await ml . jobs . newJobCaps ( dataView . title , dataView . type === 'rollup' ) ; const { fields : allFields , aggs } = createObjects ( resp , dataView . title ) ; if ( this . _includeEventRateField === true ) { addEventRateField ( aggs , allFields ) ; } const { fieldsPreferringKeyword , fieldsPreferringText } = processTextAndKeywordFields ( allFields ) ; const catFields = fieldsPreferringText . filter ( ( f ) => f . type === ES_FIELD_TYPES . KEYWORD || f . type === ES_FIELD_TYPES . TEXT ) ; const dateFields = fieldsPreferringText . filter ( ( f ) => f . type === ES_FIELD_TYPES . DATE ) ; const fields = this . _removeTextFields ? fieldsPreferringKeyword : allFields ; this . _fields = fields ; this . _catFields = catFields ; this . _dateFields = dateFields ; this . _aggs = aggs ; } catch ( error ) { console . error ( 'Unable to load new job capabilities' , error ) ; } } } function createObjects ( resp , indexPatternTitle ) { const results = resp [ indexPatternTitle ] ; const fields = [ ] ; const aggs = [ ] ; type AggMap = Record < AggId , Aggregation > ; const aggMap = { } as AggMap ; const aggIdMap = { } ; if ( results !== undefined ) { results . aggs . forEach ( ( a ) => { const agg = { ... a , ... ( a . fieldIds !== undefined ? { fields : [ ] } : { } ) , } ; aggMap [ agg . id ] = agg ; aggs . push ( agg ) ; } ) ; results . fields . forEach ( ( f ) => { const field = { ... f , aggs : [ ] , } ; if ( field . aggIds !== undefined ) { aggIdMap [ field . id ] = field . aggIds ; } fields . push ( field ) ; } ) ; fields . forEach ( ( field ) => { aggIdMap [ field . id ] . forEach ( ( aggId ) => { mix ( field , aggMap [ aggId ] ) ; } ) ; } ) ; } fields . forEach ( ( f ) => delete f . aggIds ) ; aggs . forEach ( ( a ) => delete a . fieldIds ) ; return { fields , aggs , } ; } function mix ( field , agg ) { if ( agg . fields === undefined ) { agg . fields = [ ] ; } if ( field . aggs === undefined ) { field . aggs = [ ] ; } agg . fields . push ( field ) ; field . aggs . push ( agg ) ; } function addEventRateField ( aggs , fields ) { const eventRateField = { id : EVENT_RATE_FIELD_ID , name : 'Event rate' , type : ES_FIELD_TYPES . INTEGER , aggregatable : true , aggs : [ ] , } ; aggs . forEach ( ( a ) => { if ( eventRateField . aggs !== undefined && a . fields === undefined ) { a . fields = [ eventRateField ] ; eventRateField . aggs . push ( a ) ; } } ) ; fields . splice ( 0 , 0 , eventRateField ) ; } export function filterCategoryFields ( fields ) { return fields . filter ( ( f ) => categoryFieldTypes . includes ( f . type ) ) ; } export const newJobCapsService = new NewJobCapsService ( ) ;