[ML] Fixing overflowing metric values in field cards (#29255)

* [ML] Fixing overflowing metric values in field cards

* adding better rounding for tiny numbers

* removing debug line
This commit is contained in:
James Gowdy 2019-01-24 20:55:20 +00:00 committed by GitHub
parent 0917e5aa20
commit 18415fdc1b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 5 deletions

View file

@ -16,8 +16,6 @@ import { FieldTypeIcon } from '../../../components/field_type_icon';
export function FieldStatsCard({ field }) {
const percent = Math.round(field.percent * 100) / 100;
let type = field.type;
if (type === 'double' || type === 'long') {
type = 'number';
@ -45,7 +43,7 @@ export function FieldStatsCard({ field }) {
defaultMessage="{fieldCount, plural, zero {# document} one {# document} other {# documents}} ({fieldPercent}%)"
values={{
fieldCount: field.count,
fieldPercent: percent,
fieldPercent: field.percent,
}}
/>
</div>
@ -61,7 +59,7 @@ export function FieldStatsCard({ field }) {
</div>
{
(field.mean_value) &&
(field.median_value) &&
<React.Fragment>
<div>
<div className="stat min heading">

View file

@ -12,6 +12,7 @@ import React, {
import { FieldStatsCard } from './field_stats_card';
import { getFieldNames } from './get_field_names';
import { ML_JOB_FIELD_TYPES } from '../../../../common/constants/field_types';
import { roundToDecimalPlace } from '../../../formatters/round_to_decimal_place';
export class FieldsStats extends Component {
constructor(props) {
@ -79,7 +80,16 @@ function createFields(results) {
}
}
field.percent = ((field.count / numMessagesAnalyzed) * 100);
const percent = ((field.count / numMessagesAnalyzed) * 100);
field.percent = roundToDecimalPlace(percent);
// round min, max, median, mean to 2dp.
if (field.median_value !== undefined) {
field.median_value = roundToDecimalPlace(field.median_value);
field.mean_value = roundToDecimalPlace(field.mean_value);
field.min_value = roundToDecimalPlace(field.min_value);
field.max_value = roundToDecimalPlace(field.max_value);
}
return field;
} else {

View file

@ -0,0 +1,38 @@
/*
* 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 expect from 'expect.js';
import { roundToDecimalPlace } from '../round_to_decimal_place';
describe('ML - roundToDecimalPlace formatter', () => {
it('returns the correct format using default decimal place', () => {
expect(roundToDecimalPlace(12)).to.be(12);
expect(roundToDecimalPlace(12.3)).to.be(12.3);
expect(roundToDecimalPlace(12.34)).to.be(12.34);
expect(roundToDecimalPlace(12.345)).to.be(12.35);
expect(roundToDecimalPlace(12.045)).to.be(12.05);
expect(roundToDecimalPlace(12.005)).to.be(12.01);
expect(roundToDecimalPlace(12.0005)).to.be(12);
expect(roundToDecimalPlace(0.05)).to.be(0.05);
expect(roundToDecimalPlace(0.005)).to.be('5.00e-3');
expect(roundToDecimalPlace(0.0005)).to.be('5.00e-4');
expect(roundToDecimalPlace(-0.0005)).to.be('-5.00e-4');
expect(roundToDecimalPlace(-12.045)).to.be(-12.04);
});
it('returns the correct format using specified decimal place', () => {
expect(roundToDecimalPlace(12), 4).to.be(12);
expect(roundToDecimalPlace(12.3, 4)).to.be(12.3);
expect(roundToDecimalPlace(12.3456, 4)).to.be(12.3456);
expect(roundToDecimalPlace(12.345678, 4)).to.be(12.3457);
expect(roundToDecimalPlace(0.05, 4)).to.be(0.05);
expect(roundToDecimalPlace(0.0005, 4)).to.be(0.0005);
expect(roundToDecimalPlace(0.00005, 4)).to.be('5.00e-5');
expect(roundToDecimalPlace(-0.00005, 4)).to.be('-5.00e-5');
});
});

View file

@ -0,0 +1,15 @@
/*
* 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.
*/
export function roundToDecimalPlace(num, dp = 2) {
if (Math.abs(num) < Math.pow(10, -dp)) {
return Number.parseFloat(num).toExponential(2);
} else {
const m = Math.pow(10, dp);
return Math.round(num * m) / m;
}
}