Skip to content

Commit

Permalink
fixing bucketspan estimator
Browse files Browse the repository at this point in the history
  • Loading branch information
jgowdyelastic committed Jun 22, 2021
1 parent 6f648b1 commit 9ba5c60
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const Description: FC = memo(({ children }) => {
title={<h3>{title}</h3>}
description={
<FormattedMessage
id="xpack.ml.newJob.wizard.pickFieldsStep.splitField.description"
id="xpack.ml.newJob.wizard.pickFieldsStep.rareField.description"
defaultMessage="Select a field in which to detect rare values."
/>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
jobCreatorFactory,
isAdvancedJobCreator,
isCategorizationJobCreator,
isRareJobCreator,
} from '../../common/job_creator';
import {
JOB_TYPE,
Expand Down Expand Up @@ -171,6 +172,10 @@ export const Page: FC<PageProps> = ({ existingJobsAndGroups, jobType }) => {

const { anomaly_detectors: anomalyDetectors } = getNewJobDefaults();
jobCreator.categorizationAnalyzer = anomalyDetectors.categorization_analyzer!;
} else if (isRareJobCreator(jobCreator)) {
const rare = newJobCapsService.getAggById('rare');
const freqRare = newJobCapsService.getAggById('freq_rare');
jobCreator.setDefaultDetectorProperties(rare, freqRare);
}
}

Expand Down

0 comments on commit 9ba5c60

Please sign in to comment.