[ML] Creating number utils package for roundToDecimalPlace function (#154910)

`roundToDecimalPlace` is used in ML and data visualiser packages and
soon aiops, so this PR moves it to a shared package.
This commit is contained in:
James Gowdy 2023-04-13 18:53:37 +01:00 committed by GitHub
parent 27d7aa226b
commit c34dae2a0e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 178 additions and 165 deletions

1
.github/CODEOWNERS vendored
View file

@ -459,6 +459,7 @@ x-pack/packages/ml/is_defined @elastic/ml-ui
x-pack/packages/ml/is_populated_object @elastic/ml-ui
x-pack/packages/ml/local_storage @elastic/ml-ui
x-pack/packages/ml/nested_property @elastic/ml-ui
x-pack/packages/ml/number_utils @elastic/ml-ui
x-pack/plugins/ml @elastic/ml-ui
x-pack/packages/ml/query_utils @elastic/ml-ui
x-pack/packages/ml/random_sampler_utils @elastic/ml-ui

View file

@ -474,6 +474,7 @@
"@kbn/ml-is-populated-object": "link:x-pack/packages/ml/is_populated_object",
"@kbn/ml-local-storage": "link:x-pack/packages/ml/local_storage",
"@kbn/ml-nested-property": "link:x-pack/packages/ml/nested_property",
"@kbn/ml-number-utils": "link:x-pack/packages/ml/number_utils",
"@kbn/ml-plugin": "link:x-pack/plugins/ml",
"@kbn/ml-query-utils": "link:x-pack/packages/ml/query_utils",
"@kbn/ml-random-sampler-utils": "link:x-pack/packages/ml/random_sampler_utils",

View file

@ -912,6 +912,8 @@
"@kbn/ml-local-storage/*": ["x-pack/packages/ml/local_storage/*"],
"@kbn/ml-nested-property": ["x-pack/packages/ml/nested_property"],
"@kbn/ml-nested-property/*": ["x-pack/packages/ml/nested_property/*"],
"@kbn/ml-number-utils": ["x-pack/packages/ml/number_utils"],
"@kbn/ml-number-utils/*": ["x-pack/packages/ml/number_utils/*"],
"@kbn/ml-plugin": ["x-pack/plugins/ml"],
"@kbn/ml-plugin/*": ["x-pack/plugins/ml/*"],
"@kbn/ml-query-utils": ["x-pack/packages/ml/query_utils"],

View file

@ -0,0 +1,3 @@
# @kbn/ml-number-utils
Utility functions for operations on numbers

View file

@ -0,0 +1,8 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
export { roundToDecimalPlace } from './src/round_to_decimal_place';

View file

@ -0,0 +1,12 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
module.exports = {
preset: '@kbn/test',
rootDir: '../../../..',
roots: ['<rootDir>/x-pack/packages/ml/number_utils'],
};

View file

@ -0,0 +1,5 @@
{
"type": "shared-common",
"id": "@kbn/ml-number-utils",
"owner": "@elastic/ml-ui"
}

View file

@ -0,0 +1,6 @@
{
"name": "@kbn/ml-number-utils",
"private": true,
"version": "1.0.0",
"license": "Elastic License 2.0"
}

View file

@ -5,7 +5,15 @@
* 2.0.
*/
export function roundToDecimalPlace(num?: number, dp: number = 2): number | string {
/**
* Rounds a number to a specified decimal place.
* If the specified number is undefined, an empty string is returned.
*
* @param num - number to round
* @param dp - decimal place, default value is 2
* @returns rounded number
*/
export function roundToDecimalPlace(num: number | undefined, dp: number = 2): number | string {
if (num === undefined) return '';
if (num % 1 === 0) {
// no decimal place

View file

@ -0,0 +1,19 @@
{
"extends": "../../../../tsconfig.base.json",
"compilerOptions": {
"outDir": "target/types",
"types": [
"jest",
"node",
"react"
]
},
"include": [
"**/*.ts",
"**/*.tsx",
],
"exclude": [
"target/**/*"
],
"kbn_references": []
}

View file

@ -10,7 +10,7 @@ import { i18n } from '@kbn/i18n';
import { isDefined } from '@kbn/ml-is-defined';
import { FormattedMessage } from '@kbn/i18n-react';
import React, { useState } from 'react';
import { roundToDecimalPlace } from '../utils';
import { roundToDecimalPlace } from '@kbn/ml-number-utils';
import {
MIN_SAMPLER_PROBABILITY,
RANDOM_SAMPLER_PROBABILITIES,

View file

@ -6,10 +6,10 @@
*/
import { FindFileStructureResponse } from '@kbn/file-upload-plugin/common';
import { roundToDecimalPlace } from '@kbn/ml-number-utils';
import { getFieldNames, getSupportedFieldType } from './get_field_names';
import { FileBasedFieldVisConfig } from '../stats_table/types';
import { SUPPORTED_FIELD_TYPES } from '../../../../../common/constants';
import { roundToDecimalPlace } from '../utils';
export function createFields(results: FindFileStructureResponse) {
const {

View file

@ -10,11 +10,11 @@ import { EuiSpacer } from '@elastic/eui';
import { Axis, BarSeries, Chart, Settings, ScaleType } from '@elastic/charts';
import { FormattedMessage } from '@kbn/i18n-react';
import { roundToDecimalPlace } from '@kbn/ml-number-utils';
import { TopValues } from '../../../top_values';
import type { FieldDataRowProps } from '../../types/field_data_row';
import { ExpandedRowFieldHeader } from '../expanded_row_field_header';
import { getTFPercentage } from '../../utils';
import { roundToDecimalPlace } from '../../../utils';
import { useDataVizChartTheme } from '../../hooks';
import { DocumentStatsTable } from './document_stats';
import { ExpandedRowContent } from './expanded_row_content';

View file

@ -9,9 +9,9 @@ import { FormattedMessage } from '@kbn/i18n-react';
import React, { FC, ReactNode } from 'react';
import { i18n } from '@kbn/i18n';
import { EuiBasicTable, HorizontalAlignment, LEFT_ALIGNMENT, RIGHT_ALIGNMENT } from '@elastic/eui';
import { roundToDecimalPlace } from '@kbn/ml-number-utils';
import { ExpandedRowFieldHeader } from '../expanded_row_field_header';
import { FieldDataRowProps, isIndexBasedFieldVisConfig } from '../../types';
import { roundToDecimalPlace } from '../../../utils';
import { ExpandedRowPanel } from './expanded_row_panel';
const metaTableColumns = [

View file

@ -9,10 +9,10 @@ import { EuiIcon, EuiText } from '@elastic/eui';
import React from 'react';
import { ES_FIELD_TYPES, KBN_FIELD_TYPES } from '@kbn/field-types';
import { roundToDecimalPlace } from '@kbn/ml-number-utils';
import { useDataVisualizerKibana } from '../../../../../kibana_context';
import { isIndexBasedFieldVisConfig } from '../../../../../../../common/types/field_vis_config';
import type { FieldDataRowProps } from '../../types/field_data_row';
import { roundToDecimalPlace } from '../../../utils';
interface Props extends FieldDataRowProps {
showIcon?: boolean;

View file

@ -22,8 +22,9 @@ import { i18n } from '@kbn/i18n';
import { DataViewField } from '@kbn/data-views-plugin/public';
import { ES_FIELD_TYPES, KBN_FIELD_TYPES } from '@kbn/field-types';
import { css } from '@emotion/react';
import { roundToDecimalPlace } from '@kbn/ml-number-utils';
import { useDataVisualizerKibana } from '../../../kibana_context';
import { roundToDecimalPlace, kibanaFieldFormat } from '../utils';
import { kibanaFieldFormat } from '../utils';
import { ExpandedRowFieldHeader } from '../stats_table/components/expanded_row_field_header';
import { FieldVisStats } from '../../../../../common/types';
import { ExpandedRowPanel } from '../stats_table/components/field_data_expanded_row/expanded_row_panel';

View file

@ -6,7 +6,6 @@
*/
export { createUrlOverrides, processResults, readFile, DEFAULT_LINES_TO_SAMPLE } from './utils';
export { roundToDecimalPlace } from './round_to_decimal_place';
export { kibanaFieldFormat } from './kibana_field_format';
export { numberAsOrdinal } from './number_as_ordinal';
export { formatSingleValue } from './format_value';

View file

@ -12,53 +12,54 @@
"types/**/*"
],
"kbn_references": [
"@kbn/ace",
"@kbn/charts-plugin",
"@kbn/cloud-chat-plugin",
"@kbn/cloud-plugin",
"@kbn/core-execution-context-common",
"@kbn/core-http-browser",
"@kbn/core",
"@kbn/kibana-utils-plugin",
"@kbn/kibana-react-plugin",
"@kbn/data-plugin",
"@kbn/usage-collection-plugin",
"@kbn/custom-integrations-plugin",
"@kbn/security-plugin",
"@kbn/data-plugin",
"@kbn/data-view-field-editor-plugin",
"@kbn/data-views-plugin",
"@kbn/datemath",
"@kbn/discover-plugin",
"@kbn/embeddable-plugin",
"@kbn/embeddable-plugin",
"@kbn/es-query",
"@kbn/es-ui-shared-plugin",
"@kbn/field-formats-plugin",
"@kbn/field-types",
"@kbn/file-upload-plugin",
"@kbn/home-plugin",
"@kbn/i18n-react",
"@kbn/i18n",
"@kbn/kibana-react-plugin",
"@kbn/kibana-utils-plugin",
"@kbn/lens-plugin",
"@kbn/maps-plugin",
"@kbn/embeddable-plugin",
"@kbn/unified-search-plugin",
"@kbn/cloud-plugin",
"@kbn/cloud-chat-plugin",
"@kbn/embeddable-plugin",
"@kbn/discover-plugin",
"@kbn/field-formats-plugin",
"@kbn/datemath",
"@kbn/i18n",
"@kbn/es-query",
"@kbn/ml-is-populated-object",
"@kbn/charts-plugin",
"@kbn/share-plugin",
"@kbn/home-plugin",
"@kbn/data-view-field-editor-plugin",
"@kbn/ui-actions-plugin",
"@kbn/es-ui-shared-plugin",
"@kbn/ace",
"@kbn/rison",
"@kbn/data-views-plugin",
"@kbn/utility-types",
"@kbn/i18n-react",
"@kbn/core-http-browser",
"@kbn/react-field",
"@kbn/ui-theme",
"@kbn/ml-agg-utils",
"@kbn/test-jest-helpers",
"@kbn/field-types",
"@kbn/ml-nested-property",
"@kbn/ml-url-state",
"@kbn/ml-local-storage",
"@kbn/ml-date-picker",
"@kbn/ml-is-defined",
"@kbn/ml-is-populated-object",
"@kbn/ml-local-storage",
"@kbn/ml-nested-property",
"@kbn/ml-number-utils",
"@kbn/ml-query-utils",
"@kbn/ml-url-state",
"@kbn/react-field",
"@kbn/rison",
"@kbn/saved-search-plugin",
"@kbn/security-plugin",
"@kbn/share-plugin",
"@kbn/test-jest-helpers",
"@kbn/ui-actions-plugin",
"@kbn/ui-theme",
"@kbn/unified-field-list-plugin",
"@kbn/core-execution-context-common",
"@kbn/unified-search-plugin",
"@kbn/usage-collection-plugin",
"@kbn/utility-types",
],
"exclude": [
"target/**/*",

View file

@ -10,7 +10,7 @@ import {
buildRegressionDecisionPathData,
} from './use_classification_path_data';
import type { FeatureImportance } from '../../../../../../../common/types/feature_importance';
import { roundToDecimalPlace } from '../../../../../formatters/round_to_decimal_place';
import { roundToDecimalPlace } from '@kbn/ml-number-utils';
describe('buildClassificationDecisionPathData()', () => {
test('returns correct prediction probability for binary classification', () => {

View file

@ -1,38 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { roundToDecimalPlace } from './round_to_decimal_place';
describe('ML - roundToDecimalPlace formatter', () => {
it('returns the correct format using default decimal place', () => {
expect(roundToDecimalPlace(12)).toBe(12);
expect(roundToDecimalPlace(12.3)).toBe(12.3);
expect(roundToDecimalPlace(12.34)).toBe(12.34);
expect(roundToDecimalPlace(12.345)).toBe(12.35);
expect(roundToDecimalPlace(12.045)).toBe(12.05);
expect(roundToDecimalPlace(12.005)).toBe(12.01);
expect(roundToDecimalPlace(12.0005)).toBe(12);
expect(roundToDecimalPlace(0.05)).toBe(0.05);
expect(roundToDecimalPlace(0.005)).toBe('5.00e-3');
expect(roundToDecimalPlace(0.0005)).toBe('5.00e-4');
expect(roundToDecimalPlace(-0.0005)).toBe('-5.00e-4');
expect(roundToDecimalPlace(-12.045)).toBe(-12.04);
expect(roundToDecimalPlace(0)).toBe(0);
});
it('returns the correct format using specified decimal place', () => {
expect(roundToDecimalPlace(12, 4)).toBe(12);
expect(roundToDecimalPlace(12.3, 4)).toBe(12.3);
expect(roundToDecimalPlace(12.3456, 4)).toBe(12.3456);
expect(roundToDecimalPlace(12.345678, 4)).toBe(12.3457);
expect(roundToDecimalPlace(0.05, 4)).toBe(0.05);
expect(roundToDecimalPlace(0.0005, 4)).toBe(0.0005);
expect(roundToDecimalPlace(0.00005, 4)).toBe('5.00e-5');
expect(roundToDecimalPlace(-0.00005, 4)).toBe('-5.00e-5');
expect(roundToDecimalPlace(0, 4)).toBe(0);
});
});

View file

@ -1,20 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
export function roundToDecimalPlace(num?: number, dp: number = 2): number | string {
if (num === undefined) return '';
if (num % 1 === 0) {
// no decimal place
return num;
}
if (Math.abs(num) < Math.pow(10, -dp)) {
return Number.parseFloat(String(num)).toExponential(2);
}
const m = Math.pow(10, dp);
return Math.round(num * m) / m;
}

View file

@ -6,7 +6,7 @@
*/
import numeral from '@elastic/numeral';
import { roundToDecimalPlace } from '../../../../formatters/round_to_decimal_place';
import { roundToDecimalPlace } from '@kbn/ml-number-utils';
import { toLocaleString } from '../../../../util/string_utils';
import { timeFormatter } from '../../../../../../common/util/date_utils';

View file

@ -21,73 +21,74 @@
"@kbn/core",
{ "path": "../../../src/setup_node_env/tsconfig.json" },
// add references to other TypeScript projects the plugin depends on
"@kbn/embeddable-plugin",
"@kbn/data-views-plugin",
"@kbn/cloud-plugin",
"@kbn/features-plugin",
"@kbn/data-visualizer-plugin",
"@kbn/aiops-plugin",
"@kbn/license-management-plugin",
"@kbn/licensing-plugin",
"@kbn/maps-plugin",
"@kbn/security-plugin",
"@kbn/spaces-plugin",
"@kbn/alerting-plugin",
"@kbn/triggers-actions-ui-plugin",
"@kbn/unified-search-plugin",
"@kbn/cases-plugin",
"@kbn/i18n",
"@kbn/ml-is-populated-object",
"@kbn/data-plugin",
"@kbn/field-formats-plugin",
"@kbn/utility-types",
"@kbn/share-plugin",
"@kbn/datemath",
"@kbn/es-query",
"@kbn/ui-theme",
"@kbn/ml-string-hash",
"@kbn/ml-trained-models-utils",
"@kbn/core-http-browser",
"@kbn/home-plugin",
"@kbn/management-plugin",
"@kbn/lens-plugin",
"@kbn/ui-actions-plugin",
"@kbn/usage-collection-plugin",
"@kbn/dashboard-plugin",
"@kbn/charts-plugin",
"@kbn/i18n-react",
"@kbn/ml-agg-utils",
"@kbn/kibana-utils-plugin",
"@kbn/kibana-react-plugin",
"@kbn/inspector-plugin",
"@kbn/es-types",
"@kbn/std",
"@kbn/test-jest-helpers",
"@kbn/analytics",
"@kbn/rison",
"@kbn/field-types",
"@kbn/es-ui-shared-plugin",
"@kbn/ace",
"@kbn/actions-plugin",
"@kbn/task-manager-plugin",
"@kbn/aiops-plugin",
"@kbn/alerting-plugin",
"@kbn/analytics",
"@kbn/cases-plugin",
"@kbn/charts-plugin",
"@kbn/cloud-plugin",
"@kbn/config-schema",
"@kbn/ml-url-state",
"@kbn/ml-nested-property",
"@kbn/ml-local-storage",
"@kbn/core-http-browser",
"@kbn/dashboard-plugin",
"@kbn/data-plugin",
"@kbn/data-views-plugin",
"@kbn/data-visualizer-plugin",
"@kbn/datemath",
"@kbn/embeddable-plugin",
"@kbn/es-query",
"@kbn/es-types",
"@kbn/es-ui-shared-plugin",
"@kbn/features-plugin",
"@kbn/field-formats-plugin",
"@kbn/field-types",
"@kbn/home-plugin",
"@kbn/i18n-react",
"@kbn/i18n",
"@kbn/inspector-plugin",
"@kbn/kibana-react-plugin",
"@kbn/kibana-utils-plugin",
"@kbn/lens-plugin",
"@kbn/license-management-plugin",
"@kbn/licensing-plugin",
"@kbn/management-plugin",
"@kbn/maps-plugin",
"@kbn/ml-agg-utils",
"@kbn/ml-date-picker",
"@kbn/ml-is-defined",
"@kbn/ml-is-populated-object",
"@kbn/ml-local-storage",
"@kbn/ml-nested-property",
"@kbn/ml-number-utils",
"@kbn/ml-query-utils",
"@kbn/shared-ux-page-kibana-template",
"@kbn/shared-ux-link-redirect-app",
"@kbn/react-field",
"@kbn/unified-field-list-plugin",
"@kbn/shared-ux-router",
"@kbn/saved-search-plugin",
"@kbn/saved-objects-management-plugin",
"@kbn/saved-objects-finder-plugin",
"@kbn/monaco",
"@kbn/repo-info",
"@kbn/ml-trained-models-utils",
"@kbn/ml-route-utils",
"@kbn/ml-string-hash",
"@kbn/ml-trained-models-utils",
"@kbn/ml-trained-models-utils",
"@kbn/ml-url-state",
"@kbn/monaco",
"@kbn/react-field",
"@kbn/repo-info",
"@kbn/rison",
"@kbn/saved-objects-finder-plugin",
"@kbn/saved-objects-management-plugin",
"@kbn/saved-search-plugin",
"@kbn/security-plugin",
"@kbn/share-plugin",
"@kbn/shared-ux-link-redirect-app",
"@kbn/shared-ux-page-kibana-template",
"@kbn/shared-ux-router",
"@kbn/spaces-plugin",
"@kbn/std",
"@kbn/task-manager-plugin",
"@kbn/test-jest-helpers",
"@kbn/triggers-actions-ui-plugin",
"@kbn/ui-actions-plugin",
"@kbn/ui-theme",
"@kbn/unified-field-list-plugin",
"@kbn/unified-search-plugin",
"@kbn/usage-collection-plugin",
"@kbn/utility-types",
],
}

View file

@ -4561,6 +4561,10 @@
version "0.0.0"
uid ""
"@kbn/ml-number-utils@link:x-pack/packages/ml/number_utils":
version "0.0.0"
uid ""
"@kbn/ml-plugin@link:x-pack/plugins/ml":
version "0.0.0"
uid ""