Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] Adding job overrides to the module setup endpoint #42946

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions x-pack/legacy/plugins/ml/common/util/__tests__/job_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,12 +521,12 @@ describe('ML - job utils', () => {

describe('prefixDatafeedId', () => {

it('returns datafeed-prefix-job"', () => {
it('returns datafeed-prefix-job from datafeed-job"', () => {
expect(prefixDatafeedId('datafeed-job', 'prefix-')).to.be('datafeed-prefix-job');
});

it('returns prefix-job"', () => {
expect(prefixDatafeedId('job', 'prefix-')).to.be('prefix-job');
it('returns datafeed-prefix-job from job"', () => {
expect(prefixDatafeedId('job', 'prefix-')).to.be('datafeed-prefix-job');
});
});

Expand Down
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/ml/common/util/job_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export const ML_DATA_PREVIEW_COUNT = 10;
export function prefixDatafeedId(datafeedId, prefix) {
return (datafeedId.match(/^datafeed-/)) ?
datafeedId.replace(/^datafeed-/, `datafeed-${prefix}`) :
`${prefix}${datafeedId}`;
`datafeed-${prefix}${datafeedId}`;
}

// Returns a name which is safe to use in elasticsearch aggregations for the supplied
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import fs from 'fs';
import Boom from 'boom';
import { merge } from 'lodash';
import { getLatestDataOrBucketTimestamp, prefixDatafeedId } from '../../../common/util/job_utils';
import { mlLog } from '../../client/log';
import { jobServiceProvider } from '../job_service';
Expand Down Expand Up @@ -261,6 +262,8 @@ export class DataRecognizer {
startDatafeed,
start,
end,
jobOverrides,
datafeedOverrides,
request
) {

Expand Down Expand Up @@ -300,6 +303,9 @@ export class DataRecognizer {
datafeeds: [],
savedObjects: []
};

this.applyJobConfigOverrides(moduleConfig, jobOverrides, jobPrefix);
this.applyDatafeedConfigOverrides(moduleConfig, datafeedOverrides, jobPrefix);
this.updateDatafeedIndices(moduleConfig);
this.updateJobUrlIndexPatterns(moduleConfig);

Expand Down Expand Up @@ -754,4 +760,79 @@ export class DataRecognizer {
return false;
}

applyJobConfigOverrides(moduleConfig, jobOverrides, jobPrefix = '') {
if(jobOverrides !== undefined) {
jgowdyelastic marked this conversation as resolved.
Show resolved Hide resolved
if (typeof jobOverrides !== 'object') {
throw Boom.badRequest(
`Incompatible jobOverrides type (${typeof jobOverrides}). It needs to be an object or array of objects.`
);
}

// jobOverrides could be a single object or an array of objects.
// if single, convert to an array
const overrides = Array.isArray(jobOverrides) ? jobOverrides : [jobOverrides];
const { jobs } = moduleConfig;

// first collect the overrides which don't contain a job id
// these will be applied to all jobs in the module
const generalOverrides = overrides.filter(o => o.job_id === undefined);
jgowdyelastic marked this conversation as resolved.
Show resolved Hide resolved
generalOverrides.forEach(o => {
jobs.forEach(({ config }) => merge(config, o));
});

// collect all job specific overrides
const jobSpecificOverrides = overrides.filter(o => o.job_id !== undefined);
jobSpecificOverrides.forEach(o => {
// for each override, find the relevant job.
// note, the job id already has the prefix prepended to it
const job = jobs.find(j => j.id === `${jobPrefix}${o.job_id}`);
if (job !== undefined) {
// delete the job_id in the override as this shouldn't be overridden
delete o.job_id;
merge(job.config, o);
}
});
}
}

applyDatafeedConfigOverrides(moduleConfig, datafeedOverrides, jobPrefix = '') {
if(datafeedOverrides !== undefined) {
jgowdyelastic marked this conversation as resolved.
Show resolved Hide resolved
if (typeof datafeedOverrides !== 'object') {
throw Boom.badRequest(
`Incompatible datafeedOverrides type (${typeof datafeedOverrides}). It needs to be an object or array of objects.`
);
}

// jobOverrides could be a single object or an array of objects.
// if single, convert to an array
const overrides = Array.isArray(datafeedOverrides) ? datafeedOverrides : [datafeedOverrides];
const { datafeeds } = moduleConfig;

// first collect the overrides which don't contain a datafeed id or a job id
// these will be applied to all jobs in the module
const generalOverrides = overrides.filter(o => o.datafeed_id === undefined && o.job_id === undefined);
jgowdyelastic marked this conversation as resolved.
Show resolved Hide resolved
generalOverrides.forEach(o => {
datafeeds.forEach(({ config }) => {
merge(config, o);
});
});

// collect all the overrides which contain either a job id or a datafeed id
const datafeedSpecificOverrides = overrides.filter(o => (o.datafeed_id !== undefined || o.job_id !== undefined));
datafeedSpecificOverrides.forEach(o => {
// either a job id or datafeed id has been specified, so create a new id
// containing either one plus the prefix
const tempId = o.datafeed_id !== undefined ? o.datafeed_id : o.job_id;
const dId = prefixDatafeedId(tempId, jobPrefix);

const datafeed = datafeeds.find(d => d.id === dId);
if (datafeed !== undefined) {
delete o.job_id;
delete o.datafeed_id;
merge(datafeed.config, o);
}
});
}
}

}
10 changes: 9 additions & 1 deletion x-pack/legacy/plugins/ml/server/routes/modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ function saveModuleItems(
startDatafeed,
start,
end,
jobOverrides,
datafeedOverrides,
request
) {
const dr = new DataRecognizer(callWithRequest);
Expand All @@ -49,6 +51,8 @@ function saveModuleItems(
startDatafeed,
start,
end,
jobOverrides,
datafeedOverrides,
request);
}

Expand Down Expand Up @@ -107,7 +111,9 @@ export function dataRecognizer({ commonRouteConfig, elasticsearchPlugin, route }
useDedicatedIndex,
startDatafeed,
start,
end
end,
jobOverrides,
datafeedOverrides,
} = request.payload;

return saveModuleItems(
Expand All @@ -121,6 +127,8 @@ export function dataRecognizer({ commonRouteConfig, elasticsearchPlugin, route }
startDatafeed,
start,
end,
jobOverrides,
datafeedOverrides,
request
)
.catch(resp => wrapError(resp));
Expand Down