[ML] Fixing cloning of single metric distinct count job (#43435) (#43520)

This commit is contained in:
James Gowdy 2019-08-19 11:52:20 +01:00 committed by GitHub
parent be7aa6a4e0
commit f03472e7df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 61 additions and 12 deletions

View file

@ -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 {

View file

@ -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[];
@ -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>;
};
}

View file

@ -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();

View file

@ -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();

View file

@ -80,7 +80,7 @@ export class SingleMetricJobCreator extends JobCreator {
buckets: {
date_histogram: {
field: timeField,
interval: `${interval}ms`,
fixed_interval: `${interval}ms`,
},
aggregations: {
[timeField]: {
@ -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]: {
@ -135,7 +135,7 @@ export class SingleMetricJobCreator extends JobCreator {
buckets: {
date_histogram: {
field: timeField,
interval: `${interval}ms`,
fixed_interval: `${interval}ms`,
},
aggregations: {
[timeField]: {
@ -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();

View file

@ -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),
@ -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;
}

View file

@ -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');

View file

@ -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}