diff --git a/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/rare_job_creator.ts b/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/rare_job_creator.ts index dd311ec157422..a5d052f350ea3 100644 --- a/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/rare_job_creator.ts +++ b/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/rare_job_creator.ts @@ -7,8 +7,8 @@ import { SavedSearchSavedObject } from '../../../../../../common/types/kibana'; import { JobCreator } from './job_creator'; -import { Field, SplitField } from '../../../../../../common/types/fields'; -import { Job, Datafeed } from '../../../../../../common/types/anomaly_detection_jobs'; +import { Field, SplitField, Aggregation } from '../../../../../../common/types/fields'; +import { Job, Datafeed, Detector } from '../../../../../../common/types/anomaly_detection_jobs'; import { JOB_TYPE, CREATED_BY_LABEL } from '../../../../../../common/constants/new_job'; import { getRichDetectors } from './util/general'; import { IndexPattern } from '../../../../../../../../../src/plugins/data/public'; @@ -23,6 +23,8 @@ export class RareJobCreator extends JobCreator { protected _type: JOB_TYPE = JOB_TYPE.RARE; private _rareInPopulation: boolean = false; private _frequentlyRare: boolean = false; + private _rareAgg: Aggregation; + private _freqRareAgg: Aggregation; constructor( indexPattern: IndexPattern, @@ -32,6 +34,16 @@ export class RareJobCreator extends JobCreator { super(indexPattern, savedSearch, query); this.createdBy = CREATED_BY_LABEL.RARE; this._wizardInitialized$.next(true); + this._rareAgg = {} as Aggregation; + this._freqRareAgg = {} as Aggregation; + } + + public setDefaultDetectorProperties(rare: Aggregation | null, freqRare: Aggregation | null) { + if (rare === null || freqRare === null) { + return; + } + this._rareAgg = rare; + this._freqRareAgg = freqRare; } public setRareField(field: Field | null) { @@ -40,12 +52,21 @@ export class RareJobCreator extends JobCreator { if (field === null) { this.removePopulationField(); this.removeSplitField(); + this._removeDetector(0); this._detectors.length = 0; + this._fields.length = 0; return; } + const agg = this._frequentlyRare ? this._freqRareAgg : this._rareAgg; + + const dtr: Detector = { + function: agg.id, + }; if (this._detectors.length === 0) { - this._detectors.push({ function: ML_JOB_AGGREGATION.RARE }); + this._addDetector(dtr, agg, field); + } else { + this._editDetector(dtr, agg, field, 0); } this._detectors[0].by_field_name = field.id; @@ -73,7 +94,9 @@ export class RareJobCreator extends JobCreator { public set frequentlyRare(bool: boolean) { this._frequentlyRare = bool; if (this._detectors.length) { - this._detectors[0].function = bool ? ML_JOB_AGGREGATION.FREQ_RARE : ML_JOB_AGGREGATION.RARE; + const agg = bool ? this._freqRareAgg : this._rareAgg; + this._detectors[0].function = agg.id; + this._aggs[0] = agg; } } diff --git a/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/type_guards.ts b/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/type_guards.ts index bf9e0e086ffa3..902d67b82a9e3 100644 --- a/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/type_guards.ts +++ b/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/type_guards.ts @@ -49,8 +49,6 @@ export function isCategorizationJobCreator( return jobCreator.type === JOB_TYPE.CATEGORIZATION; } -export function isRareJobCreator( - jobCreator: JobCreatorType -): jobCreator is CategorizationJobCreator { +export function isRareJobCreator(jobCreator: JobCreatorType): jobCreator is RareJobCreator { return jobCreator.type === JOB_TYPE.RARE; } diff --git a/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/bucket_span_estimator/estimate_bucket_span.ts b/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/bucket_span_estimator/estimate_bucket_span.ts index 7455ffcd1c5a4..67673901494c7 100644 --- a/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/bucket_span_estimator/estimate_bucket_span.ts +++ b/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/bucket_span_estimator/estimate_bucket_span.ts @@ -14,6 +14,7 @@ import { isMultiMetricJobCreator, isPopulationJobCreator, isAdvancedJobCreator, + isRareJobCreator, } from '../../../../../common/job_creator'; import { ml } from '../../../../../../../services/ml_api_service'; import { useMlContext } from '../../../../../../../contexts/ml'; @@ -49,6 +50,13 @@ export function useEstimateBucketSpan() { data.splitField = jobCreator.splitField.id; } else if (isPopulationJobCreator(jobCreator) && jobCreator.populationField !== null) { data.splitField = jobCreator.populationField.id; + } else if (isRareJobCreator(jobCreator)) { + data.fields = [null]; + if (jobCreator.populationField) { + data.splitField = jobCreator.populationField.id; + } else { + data.splitField = jobCreator.rareField?.id; + } } else if (isAdvancedJobCreator(jobCreator)) { jobCreator.richDetectors.some((d) => { if (d.partitionField !== null) { diff --git a/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/rare_field/description.tsx b/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/rare_field/description.tsx index a78a4b8e0cd30..4c3b547d580d7 100644 --- a/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/rare_field/description.tsx +++ b/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/rare_field/description.tsx @@ -19,7 +19,7 @@ export const Description: FC = memo(({ children }) => { title={