Skip to content

Commit

Permalink
moved anomaly job fetch to new function getApmAnomalyDetectionJobs for
Browse files Browse the repository at this point in the history
improved readability and only handle a 404 status code response else throw
  • Loading branch information
ogupte committed Jun 16, 2020
1 parent 3b8d771 commit 2181b40
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions x-pack/plugins/apm/server/lib/service_map/get_service_anomalies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,33 @@ import { leftJoin } from '../../../common/utils/left_join';
import { Job as AnomalyDetectionJob } from '../../../../ml/server';
import { PromiseReturnType } from '../../../typings/common';
import { IEnvOptions } from './get_service_map';
import { Setup } from '../helpers/setup_request';
import {
APM_ML_JOB_GROUP_NAME,
encodeForMlApi,
} from '../../../common/ml_job_constants';

async function getApmAnomalyDetectionJobs(
setup: Setup
): Promise<AnomalyDetectionJob[]> {
const { ml } = setup;

if (!ml) {
return [];
}
try {
const { jobs } = await ml.anomalyDetectors.jobs(APM_ML_JOB_GROUP_NAME);
return jobs;
} catch (error) {
if (error.statusCode === 404) {
return [];
}
throw error;
}
}

type ApmMlJobCategory = NonNullable<ReturnType<typeof getApmMlJobCategory>>;

export const getApmMlJobCategory = (
mlJob: AnomalyDetectionJob,
serviceNames: string[]
Expand Down Expand Up @@ -62,13 +83,7 @@ export async function getServiceAnomalies(
return [];
}

let apmMlJobs: AnomalyDetectionJob[] = [];
try {
const { jobs } = await ml.anomalyDetectors.jobs(APM_ML_JOB_GROUP_NAME);
apmMlJobs = jobs;
} catch (error) {
// did not find any apm jobs
}
const apmMlJobs = await getApmAnomalyDetectionJobs(options.setup);
if (apmMlJobs.length === 0) {
return [];
}
Expand Down

0 comments on commit 2181b40

Please sign in to comment.