diff --git a/x-pack/legacy/plugins/ml/public/jobs/jobs_list/components/utils.js b/x-pack/legacy/plugins/ml/public/jobs/jobs_list/components/utils.js index e149261b7971..aae8f814c4ea 100644 --- a/x-pack/legacy/plugins/ml/public/jobs/jobs_list/components/utils.js +++ b/x-pack/legacy/plugins/ml/public/jobs/jobs_list/components/utils.js @@ -153,6 +153,13 @@ export function cloneJob(jobId) { // if the job is from a wizards, i.e. contains a created_by property // use tempJobCloningObjects to temporarily store the job mlJobService.tempJobCloningObjects.job = job; + + if (job.data_counts.earliest_record_timestamp !== undefined && job.data_counts.latest_record_timestamp !== undefined) { + // if the job has run before, use the earliest and latest record timestamp + // as the cloned job's time range + mlJobService.tempJobCloningObjects.start = job.data_counts.earliest_record_timestamp; + mlJobService.tempJobCloningObjects.end = job.data_counts.latest_record_timestamp; + } } else { // otherwise use the currentJob mlJobService.currentJob = job; 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 acfc9fceb4e2..91cf72d8f925 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 @@ -128,7 +128,8 @@ export function isSparseDataJob(job: Job, datafeed: Datafeed): boolean { function stashCombinedJob( jobCreator: JobCreatorType, skipTimeRangeStep: boolean = false, - advanced: boolean = false + advanced: boolean = false, + includeTimeRange: boolean = false ) { const combinedJob = { ...jobCreator.jobConfig, @@ -138,16 +139,22 @@ function stashCombinedJob( mlJobService.currentJob = combinedJob; } else { mlJobService.tempJobCloningObjects.job = combinedJob; - } - if (skipTimeRangeStep === true) { - mlJobService.tempJobCloningObjects.skipTimeRangeStep = true; + // skip over the time picker step of the wizard + mlJobService.tempJobCloningObjects.skipTimeRangeStep = skipTimeRangeStep; + + if (includeTimeRange === true) { + // auto select the start and end dates of the time picker + mlJobService.tempJobCloningObjects.start = jobCreator.start; + mlJobService.tempJobCloningObjects.end = jobCreator.end; + } } } export function convertToMultiMetricJob(jobCreator: JobCreatorType) { jobCreator.createdBy = CREATED_BY_LABEL.MULTI_METRIC; - stashCombinedJob(jobCreator, true, false); + jobCreator.modelPlot = false; + stashCombinedJob(jobCreator, true, false, true); window.location.href = window.location.href.replace( JOB_TYPE.SINGLE_METRIC, @@ -157,7 +164,7 @@ export function convertToMultiMetricJob(jobCreator: JobCreatorType) { export function convertToAdvancedJob(jobCreator: JobCreatorType) { jobCreator.createdBy = null; - stashCombinedJob(jobCreator, false, true); + stashCombinedJob(jobCreator, false, true, false); let jobType = JOB_TYPE.SINGLE_METRIC; if (isMultiMetricJobCreator(jobCreator)) { @@ -171,7 +178,7 @@ export function convertToAdvancedJob(jobCreator: JobCreatorType) { export function resetJob(jobCreator: JobCreatorType) { jobCreator.jobId = ''; - stashCombinedJob(jobCreator, true, false); + stashCombinedJob(jobCreator, true, false, true); window.location.href = '#/jobs/new_job'; } diff --git a/x-pack/legacy/plugins/ml/public/jobs/new_job_new/pages/new_job/page.tsx b/x-pack/legacy/plugins/ml/public/jobs/new_job_new/pages/new_job/page.tsx index 66ccd76d0489..039e55cc29aa 100644 --- a/x-pack/legacy/plugins/ml/public/jobs/new_job_new/pages/new_job/page.tsx +++ b/x-pack/legacy/plugins/ml/public/jobs/new_job_new/pages/new_job/page.tsx @@ -63,8 +63,22 @@ export const Page: FC = ({ existingJobsAndGroups, jobType }) => { if (skipTimeRangeStep === false) { jobCreator.jobId = ''; } + mlJobService.tempJobCloningObjects.skipTimeRangeStep = false; mlJobService.tempJobCloningObjects.job = undefined; + + if ( + mlJobService.tempJobCloningObjects.start !== undefined && + mlJobService.tempJobCloningObjects.end !== undefined + ) { + // auto select the start and end dates for the time range picker + jobCreator.setTimeRange( + mlJobService.tempJobCloningObjects.start, + mlJobService.tempJobCloningObjects.end + ); + mlJobService.tempJobCloningObjects.start = undefined; + mlJobService.tempJobCloningObjects.end = undefined; + } } else { jobCreator.bucketSpan = DEFAULT_BUCKET_SPAN; @@ -80,7 +94,7 @@ export const Page: FC = ({ existingJobsAndGroups, jobType }) => { jobCreator.modelPlot = true; } - if (kibanaContext.currentSavedSearch.id === undefined) { + if (kibanaContext.currentSavedSearch.id !== undefined) { // Jobs created from saved searches cannot be cloned in the wizard as the // ML job config holds no reference to the saved search ID. jobCreator.createdBy = null; diff --git a/x-pack/legacy/plugins/ml/public/services/job_service.d.ts b/x-pack/legacy/plugins/ml/public/services/job_service.d.ts index d32e55a58f3a..f94c0e866451 100644 --- a/x-pack/legacy/plugins/ml/public/services/job_service.d.ts +++ b/x-pack/legacy/plugins/ml/public/services/job_service.d.ts @@ -14,6 +14,8 @@ declare interface JobService { tempJobCloningObjects: { job: any; skipTimeRangeStep: boolean; + start?: number; + end?: number; }; skipTimeRangeStep: boolean; saveNewJob(job: any): Promise; diff --git a/x-pack/legacy/plugins/ml/public/services/job_service.js b/x-pack/legacy/plugins/ml/public/services/job_service.js index cdb7b430def6..f7b01e983fe0 100644 --- a/x-pack/legacy/plugins/ml/public/services/job_service.js +++ b/x-pack/legacy/plugins/ml/public/services/job_service.js @@ -34,6 +34,8 @@ class JobService { this.tempJobCloningObjects = { job: undefined, skipTimeRangeStep: false, + start: undefined, + end: undefined, }; this.jobs = [];