Skip to content

Commit

Permalink
[ML] Update cloning for ad jobs to use exclude_generated
Browse files Browse the repository at this point in the history
  • Loading branch information
qn895 committed Jan 20, 2021
1 parent 11eaf09 commit 3a50b3c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ export function loadFullJob(jobId) {
});
}

export function loadClonableJob(jobId) {
return new Promise((resolve, reject) => {
ml.jobs
.jobs([jobId], true)
.then((jobs) => {
if (jobs.length) {
resolve(jobs[0]);
} else {
throw new Error(`Could not find job ${jobId}`);
}
})
.catch((error) => {
reject(error);
});
});
}

export function isStartable(jobs) {
return jobs.some(
(j) => j.datafeedState === DATAFEED_STATE.STOPPED && j.jobState !== JOB_STATE.CLOSING
Expand Down Expand Up @@ -180,10 +197,12 @@ function showResults(resp, action) {

export async function cloneJob(jobId) {
try {
const job = await loadFullJob(jobId);
if (job.custom_settings && job.custom_settings.created_by) {
const [job, originalJob] = await Promise.all([loadClonableJob(jobId), loadFullJob(jobId)]);
if (job.custom_settings && originalJob?.custom_settings?.created_by) {
// if the job is from a wizards, i.e. contains a created_by property
// use tempJobCloningObjects to temporarily store the job
job.custom_settings.created_by = originalJob?.custom_settings?.created_by;

mlJobService.tempJobCloningObjects.job = job;

if (
Expand Down Expand Up @@ -214,6 +233,7 @@ export async function cloneJob(jobId) {
// otherwise use the tempJobCloningObjects
mlJobService.tempJobCloningObjects.job = job;
}
mlJobService.tempJobCloningObjects.job = job;

if (job.calendars) {
mlJobService.tempJobCloningObjects.calendars = await mlCalendarService.fetchCalendarsByIds(
Expand Down
17 changes: 4 additions & 13 deletions x-pack/plugins/ml/public/application/services/job_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,26 +329,17 @@ class JobService {
// create a deep copy of a job object
// also remove items from the job which are set by the server and not needed
// in the future this formatting could be optional

const tempJob = cloneDeep(job);

// remove all of the items which should not be copied
// such as counts, state and times
delete tempJob.state;
delete tempJob.job_version;
delete tempJob.calendars;
delete tempJob.data_counts;
delete tempJob.create_time;
delete tempJob.finished_time;
delete tempJob.last_data_time;
delete tempJob.model_size_stats;
delete tempJob.node;
delete tempJob.average_bucket_processing_time_ms;
delete tempJob.model_snapshot_id;
delete tempJob.open_time;
delete tempJob.established_model_memory;
delete tempJob.calendars;
delete tempJob.timing_stats;
delete tempJob.forecasts_stats;
delete tempJob.assignment_explanation;
delete tempJob.state;
delete tempJob.timing_stats;

delete tempJob.analysis_config.use_per_partition_normalization;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export const jobsApiProvider = (httpService: HttpService) => ({
});
},

jobs(jobIds: string[]) {
const body = JSON.stringify({ jobIds });
jobs(jobIds: string[], excludeGenerated?: boolean) {
const body = JSON.stringify({ jobIds, excludeGenerated });
return httpService.http<CombinedJobWithStats[]>({
path: `${basePath()}/jobs/jobs`,
method: 'POST',
Expand Down
13 changes: 10 additions & 3 deletions x-pack/plugins/ml/server/models/job_service/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,17 @@ export function jobsProvider(client: IScopedClusterClient, mlClient: MlClient) {
return { jobs, jobsMap };
}

async function createFullJobsList(jobIds: string[] = []) {
async function createFullJobsList(jobIds: string[] = [], excludeGenerated = false) {
const jobs: CombinedJobWithStats[] = [];
const groups: { [jobId: string]: string[] } = {};
const datafeeds: { [id: string]: DatafeedWithStats } = {};
const calendarsByJobId: { [jobId: string]: string[] } = {};
const globalCalendars: string[] = [];

const jobIdsString = jobIds.join();

const datafeedParams = excludeGenerated ? { exclude_generated: excludeGenerated } : undefined;

const [
{ body: jobResults },
{ body: jobStatsResults },
Expand All @@ -273,11 +276,15 @@ export function jobsProvider(client: IScopedClusterClient, mlClient: MlClient) {
calendarResults,
latestBucketTimestampByJob,
] = await Promise.all([
mlClient.getJobs<MlJobsResponse>(jobIds.length > 0 ? { job_id: jobIdsString } : undefined),
mlClient.getJobs<MlJobsResponse>(
jobIds.length > 0
? { job_id: jobIdsString, exclude_generated: excludeGenerated }
: undefined
),
mlClient.getJobStats<MlJobsStatsResponse>(
jobIds.length > 0 ? { job_id: jobIdsString } : undefined
),
mlClient.getDatafeeds<MlDatafeedsResponse>(),
mlClient.getDatafeeds<MlDatafeedsResponse>(datafeedParams),
mlClient.getDatafeedStats<MlDatafeedsStatsResponse>(),
calMngr.getAllCalendars(),
getLatestBucketTimestampByJob(),
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/ml/server/routes/job_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ export function jobServiceRoutes({ router, routeGuard }: RouteInitialization) {
routeGuard.fullLicenseAPIGuard(async ({ client, mlClient, request, response }) => {
try {
const { createFullJobsList } = jobServiceProvider(client, mlClient);
const { jobIds } = request.body;
const resp = await createFullJobsList(jobIds);
const { jobIds, excludeGenerated } = request.body;
const resp = await createFullJobsList(jobIds, excludeGenerated);

return response.ok({
body: resp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const forceStartDatafeedSchema = schema.object({
export const jobIdsSchema = schema.object({
/** Optional list of job IDs. */
jobIds: schema.maybe(schema.arrayOf(schema.maybe(schema.string()))),
excludeGenerated: schema.maybe(schema.boolean()),
});

export const jobsWithTimerangeSchema = {
Expand Down

0 comments on commit 3a50b3c

Please sign in to comment.