Skip to content

Commit

Permalink
[ML] Injectables refactor (elastic#50512) (elastic#50910)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
jgowdyelastic authored Nov 18, 2019
1 parent 14579af commit dd5f7db
Show file tree
Hide file tree
Showing 63 changed files with 571 additions and 656 deletions.
70 changes: 0 additions & 70 deletions x-pack/legacy/plugins/ml/common/util/__tests__/parse_interval.js

This file was deleted.

6 changes: 0 additions & 6 deletions x-pack/legacy/plugins/ml/common/util/group_color_utils.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import * as euiVars from '@elastic/eui/dist/eui_theme_dark.json';
import { stringHash } from './string_utils';

import euiVars from '@elastic/eui/dist/eui_theme_dark.json';

const COLORS = [
euiVars.euiColorVis0,
Expand All @@ -20,18 +18,32 @@ const COLORS = [
euiVars.euiColorVis8,
euiVars.euiColorVis9,
euiVars.euiColorDarkShade,
euiVars.euiColorPrimary
euiVars.euiColorPrimary,
];

const colorMap = {};
const colorMap: Record<string, string> = {};

export function tabColor(name) {
export function tabColor(name: string): string {
if (colorMap[name] === undefined) {
const n = stringHash(name);
const color = COLORS[(n % COLORS.length)];
const color = COLORS[n % COLORS.length];
colorMap[name] = color;
return color;
} else {
return colorMap[name];
}
}

function stringHash(str: string): number {
let hash = 0;
let chr = 0;
if (str.length === 0) {
return hash;
}
for (let i = 0; i < str.length; i++) {
chr = str.charCodeAt(i);
hash = (hash << 5) - hash + chr; // eslint-disable-line no-bitwise
hash |= 0; // eslint-disable-line no-bitwise
}
return hash < 0 ? hash * -2 : hash;
}
2 changes: 2 additions & 0 deletions x-pack/legacy/plugins/ml/common/util/job_utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ export const ML_DATA_PREVIEW_COUNT: number;
export function isJobIdValid(jobId: string): boolean;

export function processCreatedBy(customSettings: { created_by?: string }): void;

export function mlFunctionToESAggregation(functionName: string): string | null;
34 changes: 34 additions & 0 deletions x-pack/legacy/plugins/ml/common/util/parse_interval.test.ts
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 { parseInterval } from './parse_interval';

describe('ML parse interval util', () => {
test('correctly parses an interval containing unit and value', () => {
expect(parseInterval('1d')!.as('d')).toBe(1);
expect(parseInterval('2y')!.as('y')).toBe(2);
expect(parseInterval('5M')!.as('M')).toBe(5);
expect(parseInterval('5m')!.as('m')).toBe(5);
expect(parseInterval('250ms')!.as('ms')).toBe(250);
expect(parseInterval('100s')!.as('s')).toBe(100);
expect(parseInterval('23d')!.as('d')).toBe(23);
expect(parseInterval('52w')!.as('w')).toBe(52);
expect(parseInterval('0s')!.as('s')).toBe(0);
expect(parseInterval('0s')!.as('h')).toBe(0);
});

test('correctly handles zero value intervals', () => {
expect(parseInterval('0h')!.as('h')).toBe(0);
expect(parseInterval('0d')).toBe(null);
});

test('returns null for an invalid interval', () => {
expect(parseInterval('')).toBe(null);
expect(parseInterval('234asdf')).toBe(null);
expect(parseInterval('m')).toBe(null);
expect(parseInterval('1.5h')).toBe(null);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
* you may not use this file except in compliance with the Elastic License.
*/



import moment from 'moment';
import { duration, Duration, unitOfTime } from 'moment';
import dateMath from '@elastic/datemath';

type SupportedUnits = unitOfTime.Base;

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

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

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

try {
const value = parseInt(matches[1]);
const unit = matches[2];
const value = parseInt(matches[1], 10);
const unit = matches[2] as SupportedUnits;

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

return moment.duration(value, unit);
return duration(value, unit);
} catch (e) {
return null;
}
Expand Down
8 changes: 0 additions & 8 deletions x-pack/legacy/plugins/ml/common/util/string_utils.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,24 @@
* you may not use this file except in compliance with the Elastic License.
*/



import expect from '@kbn/expect';
import { renderTemplate } from '../string_utils';
import { renderTemplate } from './string_utils';

describe('ML - string utils', () => {
describe('renderTemplate', () => {

it('returns plain string', () => {
test('returns plain string', () => {
const templateString = 'plain string';
const result = renderTemplate(templateString);
expect(result).to.be(result);
expect(result).toBe(result);
});
it('returns rendered template with one replacement', () => {
test('returns rendered template with one replacement', () => {
const templateString = 'string with {{one}} replacement';
const result = renderTemplate(templateString, { one: '1' });
expect(result).to.be('string with 1 replacement');
expect(result).toBe('string with 1 replacement');
});
it('returns rendered template with two replacements', () => {
test('returns rendered template with two replacements', () => {
const templateString = 'string with {{one}} replacement, and a {{two}} one.';
const result = renderTemplate(templateString, { one: '1', two: '2nd' });
expect(result).to.be('string with 1 replacement, and a 2nd one.');
expect(result).toBe('string with 1 replacement, and a 2nd one.');
});

});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,16 @@
* you may not use this file except in compliance with the Elastic License.
*/




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

if (Array.isArray(matches)) {
if (Array.isArray(matches) && data !== undefined) {
matches.forEach(v => {
str = str.replace(v, data[v.replace(/{{|}}/g, '')]);
});
}

return str;
}

export function stringHash(str) {
let hash = 0;
let chr = '';
if (str.length === 0) {
return hash;
}
for (let i = 0; i < str.length; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0;
}
return hash < 0 ? hash * -2 : hash;
}
7 changes: 0 additions & 7 deletions x-pack/legacy/plugins/ml/common/util/validation_utils.d.ts

This file was deleted.

54 changes: 0 additions & 54 deletions x-pack/legacy/plugins/ml/common/util/validation_utils.js

This file was deleted.

Loading

0 comments on commit dd5f7db

Please sign in to comment.