Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Uptime] Update uptime ml job id to limit to 64 char #64394

Merged
merged 11 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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