Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 committed Apr 24, 2020
1 parent 675030c commit db9c05a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,28 @@
import { getMLJobId } from '../ml_anomaly';

describe('ML Anomaly API', () => {
it('it generates a valid ML job ID', async () => {
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';
const jobId1 = getMLJobId(longAndWeirdMonitorId);

expect(jobId1.length <= 64).toBe(true);
expect(getMLJobId(longAndWeirdMonitorId)).toHaveLength(64);
});

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

const jobId2 = getMLJobId(monIdSpecialChars);
const jobId = getMLJobId(monIdSpecialChars);

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

expect(format.test(jobId2)).toBe(false);
expect(format.test(jobId)).toBe(false);
});
});
16 changes: 9 additions & 7 deletions x-pack/legacy/plugins/uptime/public/state/api/ml_anomaly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@ const getJobPrefix = (monitorId: string) => {
// Also Spaces and the characters / ? , " < > | * are not allowed
// so we will replace all special chars with _

const lowerCaseMonitorId = monitorId.replace(/[^A-Z0-9]+/gi, '_').toLowerCase();
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 and _ char as well
if ((lowerCaseMonitorId + ML_JOB_ID + 1).length > 64) {
return lowerCaseMonitorId.substring(0, 64 - ML_JOB_ID.length - 1);
// Subtracting ML_JOB_ID constant as well
const postfix = '_' + ML_JOB_ID;

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

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

export const getMLCapabilities = async (): Promise<PrivilegesResponse> => {
return await apiService.get(API_URLS.ML_CAPABILITIES);
Expand All @@ -51,7 +53,7 @@ export const createMLJob = async ({
const url = API_URLS.ML_SETUP_MODULE + ML_MODULE_ID;

const data = {
prefix: `${getJobPrefix(monitorId)}_`,
prefix: `${getJobPrefix(monitorId)}`,
useDedicatedIndex: false,
startDatafeed: true,
start: moment()
Expand Down

0 comments on commit db9c05a

Please sign in to comment.