Skip to content

Commit

Permalink
[ML] Fixing cloning of single metric distinct count job
Browse files Browse the repository at this point in the history
  • Loading branch information
jgowdyelastic committed Aug 16, 2019
1 parent 24b5648 commit 6665c66
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export enum ML_JOB_AGGREGATION {
MIN = 'min',
MAX = 'max',
DISTINCT_COUNT = 'distinct_count',
HIGH_DISTINCT_COUNT = 'high_distinct_count',
LOW_DISTINCT_COUNT = 'low_distinct_count',
NON_ZERO_COUNT = 'non_zero_count',
HIGH_NON_ZERO_COUNT = 'high_non_zero_count',
LOW_NON_ZERO_COUNT = 'low_non_zero_count',
}

export enum KIBANA_AGGREGATION {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type DatafeedId = string;

export interface Datafeed {
datafeed_id: DatafeedId;
aggregations?: object;
aggregations?: Aggregation;
chunking_config?: ChunkingConfig;
frequency?: string;
indices: IndexPatternTitle[];
Expand All @@ -26,3 +26,13 @@ export interface ChunkingConfig {
mode: 'auto' | 'manual' | 'off';
time_span?: string;
}

interface Aggregation {
buckets: {
date_histogram: {
field: string;
fixed_interval: string;
};
aggregations: Record<string, any>;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,14 @@ export class MultiMetricJobCreator extends JobCreator {
this._overrideConfigs(job, datafeed);
this.jobId = '';
this.createdBy = CREATED_BY_LABEL.MULTI_METRIC;
const detectors = getRichDetectors(job.analysis_config.detectors);
const detectors = getRichDetectors(job, datafeed);

if (datafeed.aggregations !== undefined) {
// if we've converting from a single metric job,
// delete the aggregations.
delete datafeed.aggregations;
delete job.analysis_config.summary_count_field_name;
}

this.removeAllDetectors();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class PopulationJobCreator extends JobCreator {
this._overrideConfigs(job, datafeed);
this.jobId = '';
this.createdBy = CREATED_BY_LABEL.POPULATION;
const detectors = getRichDetectors(job.analysis_config.detectors);
const detectors = getRichDetectors(job, datafeed);

this.removeAllDetectors();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class SingleMetricJobCreator extends JobCreator {
buckets: {
date_histogram: {
field: timeField,
interval: `${interval}ms`,
fixed_interval: `${interval}ms`,
},
aggregations: {
[timeField]: {
Expand All @@ -106,7 +106,7 @@ export class SingleMetricJobCreator extends JobCreator {
buckets: {
date_histogram: {
field: timeField,
interval: `${interval * 0.1}ms`, // use 10% of bucketSpan to allow for better sampling
fixed_interval: `${interval * 0.1}ms`, // use 10% of bucketSpan to allow for better sampling
},
aggregations: {
[fieldName]: {
Expand Down Expand Up @@ -135,7 +135,7 @@ export class SingleMetricJobCreator extends JobCreator {
buckets: {
date_histogram: {
field: timeField,
interval: `${interval}ms`,
fixed_interval: `${interval}ms`,
},
aggregations: {
[timeField]: {
Expand Down Expand Up @@ -182,7 +182,7 @@ export class SingleMetricJobCreator extends JobCreator {
this._overrideConfigs(job, datafeed);
this.jobId = '';
this.createdBy = CREATED_BY_LABEL.SINGLE_METRIC;
const detectors = getRichDetectors(job.analysis_config.detectors);
const detectors = getRichDetectors(job, datafeed);

this.removeAllDetectors();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Detector } from '../configs';
import { idx } from '@kbn/elastic-idx';
import { Job, Datafeed } from '../configs';
import { newJobCapsService } from '../../../../../services/new_job_capabilities_service';
import { ML_JOB_AGGREGATION } from '../../../../../../common/constants/aggregation_types';

// populate the detectors with Field and Agg objects loaded from the job capabilities service
export function getRichDetectors(detectors: Detector[]) {
export function getRichDetectors(job: Job, datafeed: Datafeed) {
const detectors = getDetectors(job, datafeed);
return detectors.map(d => {
return {
agg: newJobCapsService.getAggById(d.function),
Expand All @@ -24,3 +27,30 @@ export function getRichDetectors(detectors: Detector[]) {
};
});
}

function getDetectors(job: Job, datafeed: Datafeed) {
let detectors = job.analysis_config.detectors;

// if aggregations have been used in a single metric job and a distinct count detector
// was used, we need to rebuild the detector.
if (
datafeed.aggregations !== undefined &&
job.analysis_config.detectors[0].function === ML_JOB_AGGREGATION.NON_ZERO_COUNT
) {
// distinct count detector, field has been removed.
// determine field from datafeed aggregations
const field = idx<Datafeed, string>(
datafeed,
_ => _.aggregations.buckets.aggregations.dc_region.cardinality.field
);
if (field !== undefined) {
detectors = [
{
function: ML_JOB_AGGREGATION.DISTINCT_COUNT,
field_name: field,
},
];
}
}
return detectors;
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ export const SingleMetricSettings: FC<Props> = ({ isActive, setIsValid }) => {
...jobCreator.jobConfig,
datafeed_config: jobCreator.datafeedConfig,
};
delete mlJobService.tempJobCloningObjects.job.datafeed_config.aggregations;
delete mlJobService.tempJobCloningObjects.job.analysis_config.summary_count_field_name;

mlJobService.tempJobCloningObjects.skipTimeRangeStep = true;
window.location.href = window.location.href.replace('single_metric', 'multi_metric');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ export const Wizard: FC<Props> = ({
id="xpack.ml.newJob.wizard.stepComponentWrapper.summaryTitle"
defaultMessage="Summary"
/>
Summary
</Title>
<SummaryStep
isCurrentStep={currentStep === WIZARD_STEPS.SUMMARY}
Expand Down

0 comments on commit 6665c66

Please sign in to comment.