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] Retains created_by setting when exporting anomaly detection jobs #167319

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export class JobsExportService {
constructor(private _mlApiServices: MlApiServices) {}

public async exportAnomalyDetectionJobs(jobIds: string[]) {
const configs = await Promise.all(jobIds.map(this._mlApiServices.jobs.jobForCloning));
const configs = await Promise.all(
jobIds.map((id) => this._mlApiServices.jobs.jobForCloning(id, true))
);
this._export(configs, 'anomaly-detector');
}

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

jobForCloning(jobId: string) {
const body = JSON.stringify({ jobId });
jobForCloning(jobId: string, retainCreatedBy = false) {
const body = JSON.stringify({ jobId, retainCreatedBy });
return httpService.http<{ job?: Job; datafeed?: Datafeed } | undefined>({
path: `${ML_INTERNAL_BASE_PATH}/jobs/job_for_cloning`,
method: 'POST',
Expand Down
22 changes: 15 additions & 7 deletions x-pack/plugins/ml/server/models/job_service/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,22 +315,30 @@ export function jobsProvider(
return { jobs, jobsMap };
}

async function getJobForCloning(jobId: string) {
const [jobResults, datafeedResult] = await Promise.all([
async function getJobForCloning(jobId: string, retainCreatedBy = false) {
const [jobResults, datafeedResult, fullJobResults] = await Promise.all([
mlClient.getJobs({ job_id: jobId, exclude_generated: true }),
getDatafeedByJobId(jobId, true),
...(retainCreatedBy ? [mlClient.getJobs({ job_id: jobId })] : []),
]);
const result: { datafeed?: Datafeed; job?: Job } = { job: undefined, datafeed: undefined };
if (datafeedResult && datafeedResult.job_id === jobId) {
result.datafeed = datafeedResult;
}

if (jobResults && jobResults.jobs) {
const job = jobResults.jobs.find((j) => j.job_id === jobId);
if (job) {
removeUnClonableCustomSettings(job);
result.job = job;
if (jobResults?.jobs?.length > 0) {
const job = jobResults.jobs[0];
removeUnClonableCustomSettings(job);

// to retain the created by property we need to add it back in
// from the job which hasn't been loaded with exclude_generated: true
if (retainCreatedBy && fullJobResults?.jobs?.length > 0) {
const fullJob = fullJobResults.jobs[0];
if (fullJob.custom_settings?.created_by) {
job.custom_settings.created_by = fullJob.custom_settings.created_by;
}
}
result.job = job;
}
return result;
}
Expand Down
8 changes: 4 additions & 4 deletions x-pack/plugins/ml/server/routes/job_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
deleteJobsSchema,
} from './schemas/job_service_schema';

import { jobIdSchema } from './schemas/anomaly_detectors_schema';
import { jobForCloningSchema, jobIdSchema } from './schemas/anomaly_detectors_schema';

import { jobServiceProvider } from '../models/job_service';
import { getAuthorizationHeader } from '../lib/request_authorization';
Expand Down Expand Up @@ -428,16 +428,16 @@ export function jobServiceRoutes({ router, routeGuard }: RouteInitialization) {
version: '1',
validate: {
request: {
body: jobIdSchema,
body: jobForCloningSchema,
},
},
},
routeGuard.fullLicenseAPIGuard(async ({ client, mlClient, request, response }) => {
try {
const { getJobForCloning } = jobServiceProvider(client, mlClient);
const { jobId } = request.body;
const { jobId, retainCreatedBy } = request.body;

const resp = await getJobForCloning(jobId);
const resp = await getJobForCloning(jobId, retainCreatedBy);
return response.ok({
body: resp,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,10 @@ export const forceQuerySchema = schema.object({
/** force close */
force: schema.maybe(schema.boolean()),
});

export const jobForCloningSchema = schema.object({
/** Whether to retain the created_by custom setting. */
retainCreatedBy: schema.maybe(schema.boolean()),
/** Job ID */
jobId: schema.string(),
});
Loading