Skip to content

Commit

Permalink
[Uptime] Update uptime ml job id to limit to 64 char (elastic#64394)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 committed Apr 29, 2020
1 parent 4f178de commit ac3c5bf
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { getMLJobId } from '../ml_anomaly';

describe('ML Anomaly API', () => {
it('it generates a lowercase job id', async () => {
const monitorId = 'ABC1334haa';

const jobId = getMLJobId(monitorId);

expect(jobId).toEqual(jobId.toLowerCase());
});

it('should truncate long monitor IDs', () => {
const longAndWeirdMonitorId =
'https://auto-mmmmxhhhhhccclongAndWeirdMonitorId123yyyyyrereauto-xcmpa-1345555454646';

expect(getMLJobId(longAndWeirdMonitorId)).toHaveLength(64);
});

it('should remove special characters and replace them with underscore', () => {
const monIdSpecialChars = '/ ? , " < > | * a';

const jobId = getMLJobId(monIdSpecialChars);

const format = /[/?,"<>|*]+/;

expect(format.test(jobId)).toBe(false);
});
});
27 changes: 21 additions & 6 deletions x-pack/legacy/plugins/uptime/public/state/api/ml_anomaly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,25 @@ import {
import { DataRecognizerConfigResponse } from '../../../../../../plugins/ml/common/types/modules';
import { JobExistResult } from '../../../../../../plugins/ml/common/types/data_recognizer';

export const getMLJobId = (monitorId: string) => `${monitorId}_${ML_JOB_ID}`.toLowerCase();
const getJobPrefix = (monitorId: string) => {
// ML App doesn't support upper case characters in job name
// Also Spaces and the characters / ? , " < > | * are not allowed
// so we will replace all special chars with _

const prefix = monitorId.replace(/[^A-Z0-9]+/gi, '_').toLowerCase();

// ML Job ID can't be greater than 64 length, so will be substring it, and hope
// At such big length, there is minimum chance of having duplicate monitor id
// Subtracting ML_JOB_ID constant as well
const postfix = '_' + ML_JOB_ID;

if ((prefix + postfix).length > 64) {
return prefix.substring(0, 64 - postfix.length) + '_';
}
return prefix + '_';
};

export const getMLJobId = (monitorId: string) => `${getJobPrefix(monitorId)}${ML_JOB_ID}`;

export const getMLCapabilities = async (): Promise<MlCapabilitiesResponse> => {
return await apiService.get(API_URLS.ML_CAPABILITIES);
Expand All @@ -34,11 +52,8 @@ export const createMLJob = async ({
}: MonitorIdParam & HeartbeatIndicesParam): Promise<CreateMLJobSuccess | null> => {
const url = API_URLS.ML_SETUP_MODULE + ML_MODULE_ID;

// ML App doesn't support upper case characters in job name
const lowerCaseMonitorId = monitorId.toLowerCase();

const data = {
prefix: `${lowerCaseMonitorId}_`,
prefix: `${getJobPrefix(monitorId)}`,
useDedicatedIndex: false,
startDatafeed: true,
start: moment()
Expand All @@ -48,7 +63,7 @@ export const createMLJob = async ({
query: {
bool: {
filter: [
{ term: { 'monitor.id': lowerCaseMonitorId } },
{ term: { 'monitor.id': monitorId } },
{ range: { 'monitor.duration.us': { gt: 0 } } },
],
},
Expand Down

0 comments on commit ac3c5bf

Please sign in to comment.