From db9c05abe26650e16b7a31df9c157edbcfa9dba0 Mon Sep 17 00:00:00 2001 From: shahzad Date: Fri, 24 Apr 2020 16:02:58 +0200 Subject: [PATCH] update tests --- .../state/api/__tests__/ml_anomaly.test.ts | 13 ++++++++----- .../uptime/public/state/api/ml_anomaly.ts | 16 +++++++++------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/x-pack/legacy/plugins/uptime/public/state/api/__tests__/ml_anomaly.test.ts b/x-pack/legacy/plugins/uptime/public/state/api/__tests__/ml_anomaly.test.ts index 76c59f20134c4..838e5b8246b4b 100644 --- a/x-pack/legacy/plugins/uptime/public/state/api/__tests__/ml_anomaly.test.ts +++ b/x-pack/legacy/plugins/uptime/public/state/api/__tests__/ml_anomaly.test.ts @@ -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); }); }); diff --git a/x-pack/legacy/plugins/uptime/public/state/api/ml_anomaly.ts b/x-pack/legacy/plugins/uptime/public/state/api/ml_anomaly.ts index b3e73bab013da..57792d96b28e5 100644 --- a/x-pack/legacy/plugins/uptime/public/state/api/ml_anomaly.ts +++ b/x-pack/legacy/plugins/uptime/public/state/api/ml_anomaly.ts @@ -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 => { return await apiService.get(API_URLS.ML_CAPABILITIES); @@ -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()