Skip to content

Commit

Permalink
[ML] Update datafeed resp
Browse files Browse the repository at this point in the history
  • Loading branch information
qn895 committed Jan 28, 2021
1 parent a1fb963 commit 6ea5508
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function loadJobForCloning(jobId) {
ml.jobs
.jobForCloning(jobId)
.then((resp) => {
if (resp?.job) {
if (resp) {
resolve(resp);
} else {
throw new Error(`Could not find job ${jobId}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const jobsApiProvider = (httpService: HttpService) => ({

jobForCloning(jobId: string) {
const body = JSON.stringify({ jobId });
return httpService.http<{ job: Job; datafeed: Datafeed } | undefined>({
return httpService.http<{ job?: Job; datafeed?: Datafeed } | undefined>({
path: `${basePath()}/jobs/job_for_cloning`,
method: 'POST',
body,
Expand Down
49 changes: 30 additions & 19 deletions x-pack/plugins/ml/server/models/job_service/datafeeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,24 @@ export function datafeedsProvider(mlClient: MlClient) {
}, {} as { [id: string]: string });
}

async function getDatafeedIdByJobId(jobId: string) {
async function getDatafeedByJobId(
jobId: string,
excludeGenerated?: boolean
): Promise<Datafeed | undefined> {
async function findDatafeed() {
// if the job was doesn't use the standard datafeedId format
// get all the datafeeds and match it with the jobId
const {
body: { datafeeds },
} = await mlClient.getDatafeeds<MlDatafeedsResponse>(
excludeGenerated ? { exclude_generated: true } : {}
);
for (const result of datafeeds) {
if (result.job_id === jobId) {
return result;
}
}
}
// if the job was created by the wizard,
// then we can assume it uses the standard format of the datafeedId
const assumedDefaultDatafeedId = `datafeed-${jobId}`;
Expand All @@ -169,26 +186,20 @@ export function datafeedsProvider(mlClient: MlClient) {
body: { datafeeds: datafeedsResults },
} = await mlClient.getDatafeeds<MlDatafeedsResponse>({
datafeed_id: assumedDefaultDatafeedId,
...(excludeGenerated ? { exclude_generated: true } : {}),
});
if (Array.isArray(datafeedsResults)) {
if (datafeedsResults.length === 1) {
const datafeed = datafeedsResults[0];
if (datafeed.job_id === jobId) {
return datafeed.datafeed_id;
}
}
if (
Array.isArray(datafeedsResults) &&
datafeedsResults.length === 1 &&
datafeedsResults[0].job_id === jobId
) {
return datafeedsResults[0];
} else {
return await findDatafeed();
}
} catch (e) {
// if the job was doesn't use the standard datafeedId format
// get all the datafeeds and match it with the jobId
const {
body: { datafeeds: allDatafeedsResults },
} = await mlClient.getDatafeeds<MlDatafeedsResponse>();
for (const result of allDatafeedsResults) {
if (result.job_id === jobId) {
return result.datafeed_id;
}
}
// if assumedDefaultDatafeedId does not exist, ES will throw an error
return await findDatafeed();
}
}

Expand All @@ -198,6 +209,6 @@ export function datafeedsProvider(mlClient: MlClient) {
forceDeleteDatafeed,
getDatafeedIdsByJobId,
getJobIdsByDatafeedId,
getDatafeedIdByJobId,
getDatafeedByJobId,
};
}
27 changes: 11 additions & 16 deletions x-pack/plugins/ml/server/models/job_service/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
AuditMessage,
DatafeedWithStats,
CombinedJobWithStats,
Datafeed,
Job,
} from '../../../common/types/anomaly_detection_jobs';
import {
MlJobsResponse,
Expand Down Expand Up @@ -47,7 +49,7 @@ interface Results {
export function jobsProvider(client: IScopedClusterClient, mlClient: MlClient) {
const { asInternalUser } = client;

const { forceDeleteDatafeed, getDatafeedIdsByJobId, getDatafeedIdByJobId } = datafeedsProvider(
const { forceDeleteDatafeed, getDatafeedIdsByJobId, getDatafeedByJobId } = datafeedsProvider(
mlClient
);
const { getAuditMessagesSummary } = jobAuditMessagesProvider(client, mlClient);
Expand Down Expand Up @@ -260,29 +262,22 @@ export function jobsProvider(client: IScopedClusterClient, mlClient: MlClient) {
}

async function getJobForCloning(jobId: string) {
const datafeedId = await getDatafeedIdByJobId(jobId);

const [{ body: jobResults }, { body: datafeedResults }] = await Promise.all([
const [{ body: jobResults }, datafeedResult] = await Promise.all([
mlClient.getJobs<MlJobsResponse>({ job_id: jobId, exclude_generated: true }),
mlClient.getDatafeeds<MlDatafeedsResponse>({
datafeed_id: datafeedId,
exclude_generated: true,
}),
getDatafeedByJobId(jobId, true),
]);

let datafeed;
if (Array.isArray(datafeedResults?.datafeeds) && datafeedResults.datafeeds.length === 1) {
datafeed = datafeedResults?.datafeeds[0];
const result: { datafeed?: Datafeed; job?: Job } = { job: undefined, datafeed: undefined };
if (datafeedResult && datafeedResult.job_id === jobId) {
result.datafeed = datafeedResult;
}

// create jobs objects containing job stats, datafeeds, datafeed stats and calendars
if (jobResults && jobResults.jobs) {
const job = jobResults.jobs.find((j) => j.job_id === jobId);
if (job && datafeed) {
return { job, datafeed };
if (job) {
result.job = job;
}
}
return undefined;
return result;
}

async function createFullJobsList(jobIds: string[] = []) {
Expand Down

0 comments on commit 6ea5508

Please sign in to comment.