import { Field , NewJobCapsResponse } from '../../../../common/types/fields' ; import { ES_FIELD_TYPES } from '../../../../../../../src/plugins/data/public' ; import { DataView } from '../../../../../../../src/plugins/data_views/public' ; import { processTextAndKeywordFields , NewJobCapabilitiesServiceBase } from './new_job_capabilities' ; import { ml } from '../ml_api_service' ; export function removeNestedFieldChildren ( resp , indexPatternTitle ) { const results = resp [ indexPatternTitle ] ; const fields = [ ] ; const nestedFields = { } ; if ( results !== undefined ) { results . fields . forEach ( ( field ) => { if ( field . type === ES_FIELD_TYPES . NESTED && nestedFields [ field . name ] === undefined ) { nestedFields [ field . name ] = true ; fields . push ( field ) ; } } ) ; if ( Object . keys ( nestedFields ) . length > 0 ) { results . fields . forEach ( ( field ) => { if ( field . type !== ES_FIELD_TYPES . NESTED ) { const fieldNameParts = field . name . split ( '.' ) ; const rootOfField = fieldNameParts . shift ( ) ; if ( rootOfField && nestedFields [ rootOfField ] === undefined ) { fields . push ( field ) ; } } } ) ; } else { fields . push ( ... results . fields ) ; } } return fields ; } class NewJobCapsServiceAnalytics extends NewJobCapabilitiesServiceBase { public async initializeFromDataVIew ( dataView ) { try { const resp = await ml . dataFrameAnalytics . newJobCapsAnalytics ( dataView . title , dataView . type === 'rollup' ) ; const allFields = removeNestedFieldChildren ( resp , dataView . title ) ; const { fieldsPreferringKeyword } = processTextAndKeywordFields ( allFields ) ; this . _fields = fieldsPreferringKeyword ; } catch ( error ) { console . error ( 'Unable to load analytics index fields' , error ) ; } } } export const newJobCapsServiceAnalytics = new NewJobCapsServiceAnalytics ( ) ;