From 1c338addbac5c5583096c70d09829ee183898a7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Mon, 3 Sep 2018 14:56:25 +0200 Subject: [PATCH 1/3] Add new gmp utils module for formatting numbers --- gsa/CMakeLists.txt | 1 + gsa/src/gmp/utils/__tests__/number.js | 56 +++++++++++++++++++++++++++ gsa/src/gmp/utils/number.js | 50 ++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 gsa/src/gmp/utils/__tests__/number.js create mode 100644 gsa/src/gmp/utils/number.js diff --git a/gsa/CMakeLists.txt b/gsa/CMakeLists.txt index dbdc41e6b4..b809baedc4 100644 --- a/gsa/CMakeLists.txt +++ b/gsa/CMakeLists.txt @@ -175,6 +175,7 @@ set (GSA_JS_SRC_FILES ${GSA_SRC_DIR}/src/gmp/utils/event.js ${GSA_SRC_DIR}/src/gmp/utils/id.js ${GSA_SRC_DIR}/src/gmp/utils/identity.js + ${GSA_SRC_DIR}/src/gmp/utils/number.js ${GSA_SRC_DIR}/src/gmp/utils/object.js ${GSA_SRC_DIR}/src/gmp/utils/string.js ${GSA_SRC_DIR}/src/web/app.js diff --git a/gsa/src/gmp/utils/__tests__/number.js b/gsa/src/gmp/utils/__tests__/number.js new file mode 100644 index 0000000000..9e81efe79d --- /dev/null +++ b/gsa/src/gmp/utils/__tests__/number.js @@ -0,0 +1,56 @@ +/* Greenbone Security Assistant + * + * Authors: + * Björn Ricks + * + * Copyright: + * Copyright (C) 2018 Greenbone Networks GmbH + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ +import {severityValue, fixedValue} from 'gmp/utils/number'; + +describe('severityValue function tests', () => { + + test('should convert numbers to severity', () => { + expect(severityValue(0)).toEqual('0.0'); + expect(severityValue(1)).toEqual('1.0'); + expect(severityValue(1.0)).toEqual('1.0'); + expect(severityValue(1.1)).toEqual('1.1'); + expect(severityValue(1.10)).toEqual('1.1'); + expect(severityValue(1.15)).toEqual('1.1'); + expect(severityValue(1.16)).toEqual('1.2'); + expect(severityValue(1.19)).toEqual('1.2'); + }); + +}); + +describe('fixedValue function tests', () => { + + test('should convert numbers to fixed values', () => { + const num = 12345.6789; + + expect(fixedValue(num)).toEqual('12345.6789'); + expect(fixedValue(num, 1)).toEqual('12345.7'); + expect(fixedValue(num, 6)).toEqual('12345.678900'); + + expect(fixedValue(2.34, 1)).toEqual('2.3'); + expect(fixedValue(2.35, 1)).toEqual('2.4'); + expect(fixedValue(2.55, 1)).toEqual('2.5'); + }); + +}); + +// vim: set ts=2 sw=2 tw=80: diff --git a/gsa/src/gmp/utils/number.js b/gsa/src/gmp/utils/number.js new file mode 100644 index 0000000000..4e0bcefb1f --- /dev/null +++ b/gsa/src/gmp/utils/number.js @@ -0,0 +1,50 @@ +/* Greenbone Security Assistant + * + * Authors: + * Björn Ricks + * + * Copyright: + * Copyright (C) 2018 Greenbone Networks GmbH + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ +import {isDefined} from './identity'; + +/** + * Formats a Number to a fixed number of digits to appear after the decimal + * point. + * + * Hint: The number is rounded and the fractional part is padded with zeros if + * necessary + * + * @param {Number} value A Number value to format + * @param {Number} digits Number of digest after the decimal point + * + * @returns {String} Formatted Number + */ +export const fixedValue = (value, digits) => isDefined(digits) ? + value.toFixed(digits) : + '' + value; + +/** + * Formats a Number to a Severity value + * + * @param {Number} severity Number to format as severity + * + * @returns {String} Formatted Severity + */ +export const severityValue = severity => fixedValue(severity, 1); + +// vim: set ts=2 sw=2 tw=80: From 283eedeaafc8b5b1bc232802cd01112b1fee2171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Mon, 3 Sep 2018 14:57:01 +0200 Subject: [PATCH 2/3] Use new number format module --- .../display/severity/severityclasstransform.js | 17 ++++++++--------- gsa/src/web/components/form/numberfield.js | 13 +++++-------- gsa/src/web/components/form/spinner.js | 3 ++- gsa/src/web/pages/overrides/row.js | 3 ++- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/gsa/src/web/components/dashboard/display/severity/severityclasstransform.js b/gsa/src/web/components/dashboard/display/severity/severityclasstransform.js index 78ea7d4706..989287a0b4 100644 --- a/gsa/src/web/components/dashboard/display/severity/severityclasstransform.js +++ b/gsa/src/web/components/dashboard/display/severity/severityclasstransform.js @@ -46,8 +46,7 @@ import { percent, riskFactorColorScale, } from '../utils'; - -const format = value => value.toFixed(1); +import {severityValue} from 'gmp/utils/number'; export const severityClassDataRow = row => [row.label, row.value]; @@ -94,23 +93,23 @@ const transformSeverityData = ( switch (riskFactor) { case HIGH: - toolTip = `${label} (${format(high)} - 10.0)`; + toolTip = `${label} (${severityValue(high)} - 10.0)`; filterValue = { - start: format(high - 0.1), + start: severityValue(high - 0.1), end: 10, }; break; case MEDIUM: - limit = format(high - 0.1); - toolTip = `${label} (${format(medium)} - ${limit})`; + limit = severityValue(high - 0.1); + toolTip = `${label} (${severityValue(medium)} - ${limit})`; filterValue = { - start: format(medium - 0.1), + start: severityValue(medium - 0.1), end: high, }; break; case LOW: - limit = format(medium - 0.1); - toolTip = `${label} (${format(low)} - ${limit})`; + limit = severityValue(medium - 0.1); + toolTip = `${label} (${severityValue(low)} - ${limit})`; filterValue = { start: low - 0.05, // to include 0.1 but exclude 0 end: medium, diff --git a/gsa/src/web/components/form/numberfield.js b/gsa/src/web/components/form/numberfield.js index 997f1219bb..dfdc8720ed 100644 --- a/gsa/src/web/components/form/numberfield.js +++ b/gsa/src/web/components/form/numberfield.js @@ -26,15 +26,12 @@ import React from 'react'; import {KeyCode} from 'gmp/utils/event'; import {isDefined} from 'gmp/utils/identity'; +import {fixedValue} from 'gmp/utils/number'; import {parseFloat} from 'gmp/parser'; import PropTypes from 'web/utils/proptypes'; -const displayValue = (value, precision) => isDefined(precision) ? - value.toFixed(precision) : - value; - class NumberInput extends React.Component { constructor(...args) { @@ -55,7 +52,7 @@ class NumberInput extends React.Component { KeyCode.SPACE, ]; - const displayedValue = displayValue(value, precision); + const displayedValue = fixedValue(value, precision); this.state = { displayedValue: displayedValue, @@ -71,7 +68,7 @@ class NumberInput extends React.Component { static getDerivedStateFromProps(props, state) { const {value, precision} = props; if (value !== state.prevValue) { - const displayedValue = displayValue(value, precision); + const displayedValue = fixedValue(value, precision); return { prevValue: value, displayedValue, @@ -137,7 +134,7 @@ class NumberInput extends React.Component { parsedValue = min; } - const newDisplayedValue = displayValue(parsedValue, precision); + const newDisplayedValue = fixedValue(parsedValue, precision); this.setState({ displayedValue: newDisplayedValue, @@ -145,7 +142,7 @@ class NumberInput extends React.Component { }); } else { - this.setState({displayedValue: displayValue(lastValidValue, precision)}); + this.setState({displayedValue: fixedValue(lastValidValue, precision)}); } } diff --git a/gsa/src/web/components/form/spinner.js b/gsa/src/web/components/form/spinner.js index 424b73caa3..086b47e7ea 100644 --- a/gsa/src/web/components/form/spinner.js +++ b/gsa/src/web/components/form/spinner.js @@ -29,6 +29,7 @@ import glamorous from 'glamorous'; import {debounce} from 'gmp/utils/event'; import {isDefined} from 'gmp/utils/identity'; +import {fixedValue} from 'gmp/utils/number'; import {parseFloat, parseInt} from 'gmp/parser'; @@ -240,7 +241,7 @@ class SpinnerComponent extends React.Component { value = base + above_min; // Fix precision from bad JS floating point math - value = parseFloat(value.toFixed(this.getPrecision())); + value = parseFloat(fixedValue(value, this.getPrecision())); this.setValue(value); } diff --git a/gsa/src/web/pages/overrides/row.js b/gsa/src/web/pages/overrides/row.js index 74a5aa8953..f419dbf99c 100644 --- a/gsa/src/web/pages/overrides/row.js +++ b/gsa/src/web/pages/overrides/row.js @@ -27,6 +27,7 @@ import _ from 'gmp/locale'; import {isDefined} from 'gmp/utils/identity'; import {shorten} from 'gmp/utils/string'; +import {severityValue} from 'gmp/utils/number'; import PropTypes from '../../utils/proptypes.js'; import {renderComponent} from '../../utils/render.js'; @@ -58,7 +59,7 @@ const render_severity = severity => { if (severity <= LOG_VALUE) { return translateRiskFactor(extraRiskFactor(severity)); } - return '> ' + (severity - 0.1).toFixed(1); + return '> ' + severityValue(severity - 0.1); } return _('Any'); }; From 16734c2a04bbbfb4c8e7e680f15d626b317bc7ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Mon, 3 Sep 2018 15:07:18 +0200 Subject: [PATCH 3/3] Format default severity when saving usersettings Always format passed default severity to a one digit decimal point value when saving the user settings. A JavaScript Number of 10 is the same as 10.0 but the backend checks for fractional digits. Fixes #890 --- gsa/src/gmp/commands/users.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gsa/src/gmp/commands/users.js b/gsa/src/gmp/commands/users.js index c1752344c4..933b234728 100644 --- a/gsa/src/gmp/commands/users.js +++ b/gsa/src/gmp/commands/users.js @@ -27,6 +27,7 @@ import registerCommand from '../command'; import {forEach, map} from '../utils/array'; import {isDefined} from '../utils/identity'; +import {severityValue} from '../utils/number'; import Capabilities from '../capabilities/capabilities'; @@ -222,7 +223,7 @@ class UserCommand extends EntityCommand { report_fname: data.reportExportFileName, severity_class: data.severityClass, dynamic_severity: data.dynamicSeverity, - default_severity: data.defaultSeverity, + default_severity: severityValue(data.defaultSeverity), /* eslint-disable max-len */ 'settings_default:f9f5a546-8018-48d0-bef5-5ad4926ea899': data.defaultAlert, 'settings_default:83545bcf-0c49-4b4c-abbf-63baf82cc2a7': data.defaultEsxiCredential,