Skip to content

Commit eb4c47e

Browse files
[ML] Injectables refactor (#50512)
* [ML] Injectables refactor * removing unrelated files * additional typescript conversion * more typescript conversion * adding some return types * fixing eui errors * typescripting license checks * updated based on review * fixing merge conflict error * converting tests to jest * fixing types
1 parent 2166044 commit eb4c47e

File tree

63 files changed

+571
-656
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+571
-656
lines changed

x-pack/legacy/plugins/ml/common/util/__tests__/parse_interval.js

Lines changed: 0 additions & 70 deletions
This file was deleted.

x-pack/legacy/plugins/ml/common/util/group_color_utils.d.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

x-pack/legacy/plugins/ml/common/util/group_color_utils.js renamed to x-pack/legacy/plugins/ml/common/util/group_color_utils.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import * as euiVars from '@elastic/eui/dist/eui_theme_dark.json';
8-
import { stringHash } from './string_utils';
9-
7+
import euiVars from '@elastic/eui/dist/eui_theme_dark.json';
108

119
const COLORS = [
1210
euiVars.euiColorVis0,
@@ -20,18 +18,32 @@ const COLORS = [
2018
euiVars.euiColorVis8,
2119
euiVars.euiColorVis9,
2220
euiVars.euiColorDarkShade,
23-
euiVars.euiColorPrimary
21+
euiVars.euiColorPrimary,
2422
];
2523

26-
const colorMap = {};
24+
const colorMap: Record<string, string> = {};
2725

28-
export function tabColor(name) {
26+
export function tabColor(name: string): string {
2927
if (colorMap[name] === undefined) {
3028
const n = stringHash(name);
31-
const color = COLORS[(n % COLORS.length)];
29+
const color = COLORS[n % COLORS.length];
3230
colorMap[name] = color;
3331
return color;
3432
} else {
3533
return colorMap[name];
3634
}
3735
}
36+
37+
function stringHash(str: string): number {
38+
let hash = 0;
39+
let chr = 0;
40+
if (str.length === 0) {
41+
return hash;
42+
}
43+
for (let i = 0; i < str.length; i++) {
44+
chr = str.charCodeAt(i);
45+
hash = (hash << 5) - hash + chr; // eslint-disable-line no-bitwise
46+
hash |= 0; // eslint-disable-line no-bitwise
47+
}
48+
return hash < 0 ? hash * -2 : hash;
49+
}

x-pack/legacy/plugins/ml/common/util/job_utils.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ export const ML_DATA_PREVIEW_COUNT: number;
3333
export function isJobIdValid(jobId: string): boolean;
3434

3535
export function processCreatedBy(customSettings: { created_by?: string }): void;
36+
37+
export function mlFunctionToESAggregation(functionName: string): string | null;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { parseInterval } from './parse_interval';
8+
9+
describe('ML parse interval util', () => {
10+
test('correctly parses an interval containing unit and value', () => {
11+
expect(parseInterval('1d')!.as('d')).toBe(1);
12+
expect(parseInterval('2y')!.as('y')).toBe(2);
13+
expect(parseInterval('5M')!.as('M')).toBe(5);
14+
expect(parseInterval('5m')!.as('m')).toBe(5);
15+
expect(parseInterval('250ms')!.as('ms')).toBe(250);
16+
expect(parseInterval('100s')!.as('s')).toBe(100);
17+
expect(parseInterval('23d')!.as('d')).toBe(23);
18+
expect(parseInterval('52w')!.as('w')).toBe(52);
19+
expect(parseInterval('0s')!.as('s')).toBe(0);
20+
expect(parseInterval('0s')!.as('h')).toBe(0);
21+
});
22+
23+
test('correctly handles zero value intervals', () => {
24+
expect(parseInterval('0h')!.as('h')).toBe(0);
25+
expect(parseInterval('0d')).toBe(null);
26+
});
27+
28+
test('returns null for an invalid interval', () => {
29+
expect(parseInterval('')).toBe(null);
30+
expect(parseInterval('234asdf')).toBe(null);
31+
expect(parseInterval('m')).toBe(null);
32+
expect(parseInterval('1.5h')).toBe(null);
33+
});
34+
});

x-pack/legacy/plugins/ml/common/util/parse_interval.js renamed to x-pack/legacy/plugins/ml/common/util/parse_interval.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
8-
9-
import moment from 'moment';
7+
import { duration, Duration, unitOfTime } from 'moment';
108
import dateMath from '@elastic/datemath';
119

10+
type SupportedUnits = unitOfTime.Base;
11+
1212
// Assume interval is in the form (value)(unit), such as "1h"
1313
const INTERVAL_STRING_RE = new RegExp('^([0-9]*)\\s*(' + dateMath.units.join('|') + ')$');
1414

1515
// moment.js is only designed to allow fractional values between 0 and 1
1616
// for units of hour or less.
17-
const SUPPORT_ZERO_DURATION_UNITS = ['ms', 's', 'm', 'h'];
17+
const SUPPORT_ZERO_DURATION_UNITS: SupportedUnits[] = ['ms', 's', 'm', 'h'];
1818

1919
// Parses an interval String, such as 7d, 1h or 30m to a moment duration.
2020
// Differs from the Kibana ui/utils/parse_interval in the following ways:
@@ -25,22 +25,25 @@ const SUPPORT_ZERO_DURATION_UNITS = ['ms', 's', 'm', 'h'];
2525
// to work with units less than 'day'.
2626
// 3. Fractional intervals e.g. 1.5h or 4.5d are not allowed, in line with the behaviour
2727
// of the Elasticsearch date histogram aggregation.
28-
export function parseInterval(interval) {
29-
const matches = String(interval).trim().match(INTERVAL_STRING_RE);
30-
if (!Array.isArray(matches)) return null;
31-
if (matches.length < 3) return null;
28+
export function parseInterval(interval: string): Duration | null {
29+
const matches = String(interval)
30+
.trim()
31+
.match(INTERVAL_STRING_RE);
32+
if (!Array.isArray(matches) || matches.length < 3) {
33+
return null;
34+
}
3235

3336
try {
34-
const value = parseInt(matches[1]);
35-
const unit = matches[2];
37+
const value = parseInt(matches[1], 10);
38+
const unit = matches[2] as SupportedUnits;
3639

3740
// In line with moment.js, only allow zero value intervals when the unit is less than 'day'.
3841
// And check for isNaN as e.g. valueless 'm' will pass the regex test.
39-
if (isNaN(value) || (value < 1 && SUPPORT_ZERO_DURATION_UNITS.indexOf(unit) === -1)) {
42+
if (isNaN(value) || (value < 1 && SUPPORT_ZERO_DURATION_UNITS.indexOf(unit) === -1)) {
4043
return null;
4144
}
4245

43-
return moment.duration(value, unit);
46+
return duration(value, unit);
4447
} catch (e) {
4548
return null;
4649
}

x-pack/legacy/plugins/ml/common/util/string_utils.d.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

x-pack/legacy/plugins/ml/common/util/__tests__/string_utils.js renamed to x-pack/legacy/plugins/ml/common/util/string_utils.test.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,24 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
8-
9-
import expect from '@kbn/expect';
10-
import { renderTemplate } from '../string_utils';
7+
import { renderTemplate } from './string_utils';
118

129
describe('ML - string utils', () => {
1310
describe('renderTemplate', () => {
14-
15-
it('returns plain string', () => {
11+
test('returns plain string', () => {
1612
const templateString = 'plain string';
1713
const result = renderTemplate(templateString);
18-
expect(result).to.be(result);
14+
expect(result).toBe(result);
1915
});
20-
it('returns rendered template with one replacement', () => {
16+
test('returns rendered template with one replacement', () => {
2117
const templateString = 'string with {{one}} replacement';
2218
const result = renderTemplate(templateString, { one: '1' });
23-
expect(result).to.be('string with 1 replacement');
19+
expect(result).toBe('string with 1 replacement');
2420
});
25-
it('returns rendered template with two replacements', () => {
21+
test('returns rendered template with two replacements', () => {
2622
const templateString = 'string with {{one}} replacement, and a {{two}} one.';
2723
const result = renderTemplate(templateString, { one: '1', two: '2nd' });
28-
expect(result).to.be('string with 1 replacement, and a 2nd one.');
24+
expect(result).toBe('string with 1 replacement, and a 2nd one.');
2925
});
30-
3126
});
3227
});

x-pack/legacy/plugins/ml/common/util/string_utils.js renamed to x-pack/legacy/plugins/ml/common/util/string_utils.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,16 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
8-
9-
107
// A simple template renderer, it replaces mustache/angular style {{...}} tags with
118
// the values provided via the data object
12-
export function renderTemplate(str, data) {
9+
export function renderTemplate(str: string, data?: Record<string, string>): string {
1310
const matches = str.match(/{{(.*?)}}/g);
1411

15-
if (Array.isArray(matches)) {
12+
if (Array.isArray(matches) && data !== undefined) {
1613
matches.forEach(v => {
1714
str = str.replace(v, data[v.replace(/{{|}}/g, '')]);
1815
});
1916
}
2017

2118
return str;
2219
}
23-
24-
export function stringHash(str) {
25-
let hash = 0;
26-
let chr = '';
27-
if (str.length === 0) {
28-
return hash;
29-
}
30-
for (let i = 0; i < str.length; i++) {
31-
chr = str.charCodeAt(i);
32-
hash = ((hash << 5) - hash) + chr;
33-
hash |= 0;
34-
}
35-
return hash < 0 ? hash * -2 : hash;
36-
}

x-pack/legacy/plugins/ml/common/util/validation_utils.d.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)