From 0a8d26997d913d1c649e3ab05e6d231475a5fc5d Mon Sep 17 00:00:00 2001 From: James Gowdy Date: Fri, 16 Aug 2019 12:28:22 +0100 Subject: [PATCH] [ML] Fixing cloning of single metric distinct count job --- .../ml/common/constants/aggregation_types.ts | 5 +++ .../common/job_creator/configs/datafeed.ts | 12 ++++++- .../job_creator/multi_metric_job_creator.ts | 9 ++++- .../job_creator/population_job_creator.ts | 2 +- .../job_creator/single_metric_job_creator.ts | 8 ++--- .../common/job_creator/util/general.ts | 34 +++++++++++++++++-- .../single_metric_view/settings.tsx | 2 -- .../jobs/new_job_new/pages/new_job/wizard.tsx | 1 - 8 files changed, 61 insertions(+), 12 deletions(-) diff --git a/x-pack/legacy/plugins/ml/common/constants/aggregation_types.ts b/x-pack/legacy/plugins/ml/common/constants/aggregation_types.ts index 09173247237ace..e7655475eeab65 100644 --- a/x-pack/legacy/plugins/ml/common/constants/aggregation_types.ts +++ b/x-pack/legacy/plugins/ml/common/constants/aggregation_types.ts @@ -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 { diff --git a/x-pack/legacy/plugins/ml/public/jobs/new_job_new/common/job_creator/configs/datafeed.ts b/x-pack/legacy/plugins/ml/public/jobs/new_job_new/common/job_creator/configs/datafeed.ts index d0afbc757baf37..68ee45881586b0 100644 --- a/x-pack/legacy/plugins/ml/public/jobs/new_job_new/common/job_creator/configs/datafeed.ts +++ b/x-pack/legacy/plugins/ml/public/jobs/new_job_new/common/job_creator/configs/datafeed.ts @@ -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; + }; +} diff --git a/x-pack/legacy/plugins/ml/public/jobs/new_job_new/common/job_creator/multi_metric_job_creator.ts b/x-pack/legacy/plugins/ml/public/jobs/new_job_new/common/job_creator/multi_metric_job_creator.ts index df28af6b83c6a1..51931f8fe8af72 100644 --- a/x-pack/legacy/plugins/ml/public/jobs/new_job_new/common/job_creator/multi_metric_job_creator.ts +++ b/x-pack/legacy/plugins/ml/public/jobs/new_job_new/common/job_creator/multi_metric_job_creator.ts @@ -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(); diff --git a/x-pack/legacy/plugins/ml/public/jobs/new_job_new/common/job_creator/population_job_creator.ts b/x-pack/legacy/plugins/ml/public/jobs/new_job_new/common/job_creator/population_job_creator.ts index 1f6d3392cfa0c9..a54ac6e5652790 100644 --- a/x-pack/legacy/plugins/ml/public/jobs/new_job_new/common/job_creator/population_job_creator.ts +++ b/x-pack/legacy/plugins/ml/public/jobs/new_job_new/common/job_creator/population_job_creator.ts @@ -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(); diff --git a/x-pack/legacy/plugins/ml/public/jobs/new_job_new/common/job_creator/single_metric_job_creator.ts b/x-pack/legacy/plugins/ml/public/jobs/new_job_new/common/job_creator/single_metric_job_creator.ts index c38c52681fd318..2c8096a2cebeb6 100644 --- a/x-pack/legacy/plugins/ml/public/jobs/new_job_new/common/job_creator/single_metric_job_creator.ts +++ b/x-pack/legacy/plugins/ml/public/jobs/new_job_new/common/job_creator/single_metric_job_creator.ts @@ -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(); diff --git a/x-pack/legacy/plugins/ml/public/jobs/new_job_new/common/job_creator/util/general.ts b/x-pack/legacy/plugins/ml/public/jobs/new_job_new/common/job_creator/util/general.ts index 3f2375c452c3c4..d65be265ee54d9 100644 --- a/x-pack/legacy/plugins/ml/public/jobs/new_job_new/common/job_creator/util/general.ts +++ b/x-pack/legacy/plugins/ml/public/jobs/new_job_new/common/job_creator/util/general.ts @@ -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, + _ => _.aggregations.buckets.aggregations.dc_region.cardinality.field + ); + if (field !== undefined) { + detectors = [ + { + function: ML_JOB_AGGREGATION.DISTINCT_COUNT, + field_name: field, + }, + ]; + } + } + return detectors; +} diff --git a/x-pack/legacy/plugins/ml/public/jobs/new_job_new/pages/components/pick_fields_step/components/single_metric_view/settings.tsx b/x-pack/legacy/plugins/ml/public/jobs/new_job_new/pages/components/pick_fields_step/components/single_metric_view/settings.tsx index efdca95cafb868..047d781111bb06 100644 --- a/x-pack/legacy/plugins/ml/public/jobs/new_job_new/pages/components/pick_fields_step/components/single_metric_view/settings.tsx +++ b/x-pack/legacy/plugins/ml/public/jobs/new_job_new/pages/components/pick_fields_step/components/single_metric_view/settings.tsx @@ -39,8 +39,6 @@ export const SingleMetricSettings: FC = ({ 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'); diff --git a/x-pack/legacy/plugins/ml/public/jobs/new_job_new/pages/new_job/wizard.tsx b/x-pack/legacy/plugins/ml/public/jobs/new_job_new/pages/new_job/wizard.tsx index 9a4b7add7b77d4..5a00171e6ebed7 100644 --- a/x-pack/legacy/plugins/ml/public/jobs/new_job_new/pages/new_job/wizard.tsx +++ b/x-pack/legacy/plugins/ml/public/jobs/new_job_new/pages/new_job/wizard.tsx @@ -248,7 +248,6 @@ export const Wizard: FC = ({ id="xpack.ml.newJob.wizard.stepComponentWrapper.summaryTitle" defaultMessage="Summary" /> - Summary