Skip to content

Commit

Permalink
[ML] Remove unused Angular filters (#51027)
Browse files Browse the repository at this point in the history
  • Loading branch information
peteharverson authored Nov 20, 2019
1 parent ff2a3ab commit cdc9caa
Show file tree
Hide file tree
Showing 10 changed files with 388 additions and 337 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 { abbreviateWholeNumber } from './abbreviate_whole_number';

describe('ML - abbreviateWholeNumber formatter', () => {
test('returns the correct format using default max digits', () => {
expect(abbreviateWholeNumber(1)).toBe(1);
expect(abbreviateWholeNumber(12)).toBe(12);
expect(abbreviateWholeNumber(123)).toBe(123);
expect(abbreviateWholeNumber(1234)).toBe('1k');
expect(abbreviateWholeNumber(12345)).toBe('12k');
expect(abbreviateWholeNumber(123456)).toBe('123k');
expect(abbreviateWholeNumber(1234567)).toBe('1m');
expect(abbreviateWholeNumber(12345678)).toBe('12m');
expect(abbreviateWholeNumber(123456789)).toBe('123m');
expect(abbreviateWholeNumber(1234567890)).toBe('1b');
expect(abbreviateWholeNumber(5555555555555.55)).toBe('6t');
});

test('returns the correct format using custom max digits', () => {
expect(abbreviateWholeNumber(1, 4)).toBe(1);
expect(abbreviateWholeNumber(12, 4)).toBe(12);
expect(abbreviateWholeNumber(123, 4)).toBe(123);
expect(abbreviateWholeNumber(1234, 4)).toBe(1234);
expect(abbreviateWholeNumber(12345, 4)).toBe('12k');
expect(abbreviateWholeNumber(123456, 6)).toBe(123456);
expect(abbreviateWholeNumber(1234567, 4)).toBe('1m');
expect(abbreviateWholeNumber(12345678, 3)).toBe('12m');
expect(abbreviateWholeNumber(123456789, 9)).toBe(123456789);
expect(abbreviateWholeNumber(1234567890, 3)).toBe('1b');
expect(abbreviateWholeNumber(5555555555555.55, 5)).toBe('6t');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
* you may not use this file except in compliance with the Elastic License.
*/


/*
* Formatter to abbreviate large whole numbers with metric prefixes.
* Uses numeral.js to format numbers longer than the specified number of
* digits with metric abbreviations e.g. 12345 as 12k, or 98000000 as 98m.
*/
*/
import numeral from '@elastic/numeral';

export function abbreviateWholeNumber(value, maxDigits = 3) {
export function abbreviateWholeNumber(value: number, maxDigits = 3) {
if (Math.abs(value) < Math.pow(10, maxDigits)) {
return value;
} else {
Expand Down
94 changes: 94 additions & 0 deletions x-pack/legacy/plugins/ml/public/formatters/format_value.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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 moment from 'moment-timezone';
import { AnomalyRecordDoc } from '../../common/types/anomalies';
import { formatValue } from './format_value';

describe('ML - formatValue formatter', () => {
const timeOfWeekRecord: AnomalyRecordDoc = {
job_id: 'gallery_time_of_week',
result_type: 'record',
probability: 0.012818,
record_score: 53.55134,
initial_record_score: 53,
bucket_span: 900,
detector_index: 0,
is_interim: false,
timestamp: 1530155700000,
by_field_name: 'clientip',
by_field_value: '65.55.215.39',
function: 'time_of_week',
function_description: 'time',
};

const timeOfDayRecord: AnomalyRecordDoc = {
job_id: 'gallery_time_of_day',
result_type: 'record',
probability: 0.012818,
record_score: 97.94245,
initial_record_score: 97,
bucket_span: 900,
detector_index: 0,
is_interim: false,
timestamp: 1517472900000,
by_field_name: 'clientip',
by_field_value: '157.56.93.83',
function: 'time_of_day',
function_description: 'time',
};

// Set timezone to US/Eastern for time_of_day and time_of_week tests.
beforeEach(() => {
moment.tz.setDefault('US/Eastern');
});

afterEach(() => {
moment.tz.setDefault('Browser');
});

// For time_of_day and time_of_week test values which are offsets in seconds
// from UTC start of week / day are formatted correctly using the test timezone.
test('correctly formats time_of_week value from numeric input', () => {
expect(formatValue(359739, 'time_of_week', undefined, timeOfWeekRecord)).toBe('Wed 23:55');
});

test('correctly formats time_of_day value from numeric input', () => {
expect(formatValue(73781, 'time_of_day', undefined, timeOfDayRecord)).toBe('15:29');
});

test('correctly formats number values from numeric input', () => {
expect(formatValue(1483228800, 'mean')).toBe(1483228800);
expect(formatValue(1234.5678, 'mean')).toBe(1234.6);
expect(formatValue(0.00012345, 'mean')).toBe(0.000123);
expect(formatValue(0, 'mean')).toBe(0);
expect(formatValue(-0.12345, 'mean')).toBe(-0.123);
expect(formatValue(-1234.5678, 'mean')).toBe(-1234.6);
expect(formatValue(-100000.1, 'mean')).toBe(-100000);
});

test('correctly formats time_of_week value from array input', () => {
expect(formatValue([359739], 'time_of_week', undefined, timeOfWeekRecord)).toBe('Wed 23:55');
});

test('correctly formats time_of_day value from array input', () => {
expect(formatValue([73781], 'time_of_day', undefined, timeOfDayRecord)).toBe('15:29');
});

test('correctly formats number values from array input', () => {
expect(formatValue([1483228800], 'mean')).toBe(1483228800);
expect(formatValue([1234.5678], 'mean')).toBe(1234.6);
expect(formatValue([0.00012345], 'mean')).toBe(0.000123);
expect(formatValue([0], 'mean')).toBe(0);
expect(formatValue([-0.12345], 'mean')).toBe(-0.123);
expect(formatValue([-1234.5678], 'mean')).toBe(-1234.6);
expect(formatValue([-100000.1], 'mean')).toBe(-100000);
});

test('correctly formats multi-valued array', () => {
expect(formatValue([30.3, 26.2], 'lat_long')).toBe('[30.3,26.2]');
});
});
Loading

0 comments on commit cdc9caa

Please sign in to comment.