Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usersettings default severity #907

Merged
merged 3 commits into from
Sep 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gsa/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion gsa/src/gmp/commands/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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,
Expand Down
56 changes: 56 additions & 0 deletions gsa/src/gmp/utils/__tests__/number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* Greenbone Security Assistant
*
* Authors:
* Björn Ricks <[email protected]>
*
* 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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this equal 1.2 as well?

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:
50 changes: 50 additions & 0 deletions gsa/src/gmp/utils/number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* Greenbone Security Assistant
*
* Authors:
* Björn Ricks <[email protected]>
*
* 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:
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down Expand Up @@ -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,
Expand Down
13 changes: 5 additions & 8 deletions gsa/src/web/components/form/numberfield.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -137,15 +134,15 @@ class NumberInput extends React.Component {
parsedValue = min;
}

const newDisplayedValue = displayValue(parsedValue, precision);
const newDisplayedValue = fixedValue(parsedValue, precision);

this.setState({
displayedValue: newDisplayedValue,
lastValidValue: parsedValue,
});
}
else {
this.setState({displayedValue: displayValue(lastValidValue, precision)});
this.setState({displayedValue: fixedValue(lastValidValue, precision)});
}
}

Expand Down
3 changes: 2 additions & 1 deletion gsa/src/web/components/form/spinner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion gsa/src/web/pages/overrides/row.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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');
};
Expand Down