Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ogupte committed Jul 6, 2020
1 parent 8a2e968 commit e3b2cec
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ export const AnomalyDetection = () => {
<EuiSpacer size="l" />
{viewAddEnvironments ? (
<AddEnvironments
currentEnvironments={data.map(
({ 'service.environment': environment }) => environment
)}
currentEnvironments={data.map(({ environment }) => environment)}
onCreateJobSuccess={() => {
refetch();
setViewAddEnvironments(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { MLLink } from '../../../shared/Links/MachineLearningLinks/MLLink';

const columns: Array<ITableColumn<AnomalyDetectionJobByEnv>> = [
{
field: 'service.environment',
field: 'environment',
name: i18n.translate(
'xpack.apm.settings.anomalyDetection.jobList.environmentColumnLabel',
{ defaultMessage: 'Environment' }
Expand Down Expand Up @@ -122,7 +122,6 @@ export const JobsList = ({
}
columns={columns}
items={isLoading || hasFetchFailure ? [] : anomalyDetectionJobsByEnv}
pagination={false}
/>
<EuiSpacer size="l" />
</EuiPanel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Logger } from 'kibana/server';
import uuid from 'uuid/v4';
import { PromiseReturnType } from '../../../../observability/typings/common';
import { Setup } from '../helpers/setup_request';
import { JobResponse } from '../../../../ml/common/types/modules';
import {
SERVICE_ENVIRONMENT,
TRANSACTION_DURATION,
Expand All @@ -28,17 +27,17 @@ export async function createAnomalyDetectionJobs(
) {
const { ml, config } = setup;
if (!ml) {
logger.warn('Anomaly detection plugin is not available.');
return [];
}
const mlCapabilities = await ml.mlSystem.mlCapabilities();
if (
!(
mlCapabilities.mlFeatureEnabledInSpace &&
mlCapabilities.isPlatinumOrTrialLicense
)
) {
if (!mlCapabilities.mlFeatureEnabledInSpace) {
logger.warn('Anomaly detection feature is not enabled for the space.');
return [];
}
if (!mlCapabilities.isPlatinumOrTrialLicense) {
logger.warn(
'Anomaly detection integration is not available for this user.'
'Unable to create anomaly detection jobs due to insufficient license.'
);
return [];
}
Expand All @@ -47,41 +46,29 @@ export async function createAnomalyDetectionJobs(
);

const indexPatternName = config['apm_oss.transactionIndices']; // TODO [ML] - Do we want to use the config index name?
const dataRecognizerConfigResponses = await Promise.all(
const responses = await Promise.all(
environments.map((environment) =>
configureAnomalyDetectionJob({ ml, environment, indexPatternName })
createAnomalyDetectionJob({ ml, environment, indexPatternName })
)
);
const newJobResponses = dataRecognizerConfigResponses.reduce(
(acc, response) => {
return [...acc, ...response.jobs];
},
[] as JobResponse[]
);

const failedJobs = newJobResponses.filter(({ success }) => !success);
const jobResponses = responses.flatMap((response) => response.jobs);
const failedJobs = jobResponses.filter(({ success }) => !success);

if (failedJobs.length > 0) {
const allJobsFailed = failedJobs.length === newJobResponses.length;

logger.error('Failed to create anomaly detection ML jobs.');
const failedJobIds = failedJobs.map(({ id }) => id).join(', ');
logger.error(
`Failed to create anomaly detection ML jobs for: [${failedJobIds}]:`
);
failedJobs.forEach(({ error }) => logger.error(JSON.stringify(error)));

if (allJobsFailed) {
throw new Error('Failed to setup anomaly detection ML jobs.');
}
const failedJobIds = failedJobs.map(({ id }) => id);
throw new Error(
`Some anomaly detection ML jobs failed to setup: [${failedJobIds.join(
', '
)}]`
`Failed to create anomaly detection ML jobs for: [${failedJobIds}].`
);
}

return newJobResponses;
return jobResponses;
}

async function configureAnomalyDetectionJob({
async function createAnomalyDetectionJob({
ml,
environment,
indexPatternName = 'apm-*-transaction-*',
Expand Down Expand Up @@ -111,9 +98,7 @@ async function configureAnomalyDetectionJob({
jobOverrides: [
{
custom_settings: {
job_tags: {
[SERVICE_ENVIRONMENT]: environment,
},
job_tags: { environment },
},
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Logger } from 'kibana/server';
import { PromiseReturnType } from '../../../../observability/typings/common';
import { Setup } from '../helpers/setup_request';
import { AnomalyDetectionJobByEnv } from '../../../typings/anomaly_detection';
import { SERVICE_ENVIRONMENT } from '../../../common/elasticsearch_fieldnames';
import { ML_GROUP_NAME_APM } from './create_anomaly_detection_jobs';

export type AnomalyDetectionJobsAPIResponse = PromiseReturnType<
Expand Down Expand Up @@ -42,21 +41,15 @@ export async function getAnomalyDetectionJobs(
}
try {
const { jobs } = await ml.anomalyDetectors.jobs(ML_GROUP_NAME_APM);
return jobs.reduce((acc, anomalyDetectionJob) => {
if (
anomalyDetectionJob.custom_settings?.job_tags?.[SERVICE_ENVIRONMENT]
) {
return [
...acc,
{
job_id: anomalyDetectionJob.job_id,
[SERVICE_ENVIRONMENT]:
anomalyDetectionJob.custom_settings.job_tags[SERVICE_ENVIRONMENT],
},
];
}
return acc;
}, [] as AnomalyDetectionJobByEnv[]);
return jobs
.map((job) => {
const environment = job.custom_settings?.job_tags?.environment ?? '';
return {
job_id: job.job_id,
environment,
};
})
.filter((job) => job.environment);
} catch (error) {
if (error.statusCode !== 404) {
logger.warn('Unable to get APM ML jobs.');
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/apm/typings/anomaly_detection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
*/

export interface AnomalyDetectionJobByEnv {
'service.environment': string;
environment: string;
job_id: string;
}

0 comments on commit e3b2cec

Please sign in to comment.