import { SavedSearchSavedObject } from '../../../../../../common/types/kibana' ; import { JobCreator } from './job_creator' ; import { Field , Aggregation , SplitField , AggFieldPair , } from '../../../../../../common/types/fields' ; import { Job , Datafeed , Detector } from '../../../../../../common/types/anomaly_detection_jobs' ; import { createBasicDetector } from './util/default_configs' ; import { JOB_TYPE , CREATED_BY_LABEL } from '../../../../../../common/constants/new_job' ; import { getRichDetectors } from './util/general' ; import type { DataView } from '../../../../../../../../../src/plugins/data_views/public' ; import { isSparseDataJob } from './util/general' ; export class MultiMetricJobCreator extends JobCreator { private _splitField : SplitField = null ; protected _type : JOB_TYPE = JOB_TYPE . MULTI_METRIC ; constructor ( indexPattern , savedSearch , query ) { super ( indexPattern , savedSearch , query ) ; this . createdBy = CREATED_BY_LABEL . MULTI_METRIC ; this . _wizardInitialized$ . next ( true ) ; } public setSplitField ( field ) { this . _splitField = field ; if ( this . _splitField === null ) { this . removeSplitField ( ) ; } else { for ( let i = 0 ; i < this . _detectors . length ; i ++ ) { this . _detectors [ i ] . partition_field_name = this . _splitField . id ; } } } public removeSplitField ( ) { this . _detectors . forEach ( ( d ) => { delete d . partition_field_name ; } ) ; } public get splitField ( ) : SplitField { return this . _splitField ; } public addDetector ( agg , field ) { const dtr = this . _createDetector ( agg , field ) ; this . _addDetector ( dtr , agg , field ) ; } public editDetector ( agg , field , index ) { const dtr = this . _createDetector ( agg , field ) ; this . _editDetector ( dtr , agg , field , index ) ; } private _createDetector ( agg , field ) { const dtr = createBasicDetector ( agg , field ) ; if ( this . _splitField !== null ) { dtr . partition_field_name = this . _splitField . id ; } return dtr ; } public removeDetector ( index ) { this . _removeDetector ( index ) ; } public get aggFieldPairs ( ) : AggFieldPair [ ] { return this . detectors . map ( ( d , i ) => ( { field : this . _fields [ i ] , agg : this . _aggs [ i ] , } ) ) ; } public cloneFromExistingJob ( job , datafeed ) { this . _overrideConfigs ( job , datafeed ) ; this . createdBy = CREATED_BY_LABEL . MULTI_METRIC ; this . _sparseData = isSparseDataJob ( job , datafeed ) ; const detectors = getRichDetectors ( job , datafeed , this . additionalFields , false ) ; if ( datafeed . aggregations !== undefined ) { delete datafeed . aggregations ; delete job . analysis_config . summary_count_field_name ; } this . removeAllDetectors ( ) ; detectors . forEach ( ( d , i ) => { const dtr = detectors [ i ] ; if ( dtr . agg !== null && dtr . field !== null ) { this . addDetector ( dtr . agg , dtr . field ) ; } } ) ; if ( detectors . length ) { if ( detectors [ 0 ] . partitionField !== null ) { this . setSplitField ( detectors [ 0 ] . partitionField ) ; } } } }