Skip to content

Commit

Permalink
Add: New multi_selection report format param type
Browse files Browse the repository at this point in the history
A new parameter type for report formats and report configs is added that
allows selecting multiple predefined options.

Some small style changes have been made to make params with multi-line
values more readable and to make the params in report formats and report
configs more consistent.

The new param type can later be used to add new customization options
to report formats like a column selection for CSV.
  • Loading branch information
timopollmeier committed May 17, 2024
1 parent c0f1c6f commit 397a388
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 23 deletions.
18 changes: 18 additions & 0 deletions src/gmp/commands/__tests__/reportconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,19 @@ describe('ReportConfigCommand tests', () => {
'param 1': 'value 1',
'param 2': 'value 2',
'param 3': ['report-format-1', 'report-format-2'],
'param 4': ['option-1', 'option-2'],
},
params_using_default: {
'param 1': false,
'param 2': true,
'param 3': false,
'param 4': false,
},
param_types: {
'param 1': 'string',
'param 2': 'text',
'param 3': 'report_format_list',
'param 4': 'multi_selection',
},
})
.then(resp => {
Expand All @@ -81,6 +89,7 @@ describe('ReportConfigCommand tests', () => {
'param:param 1': 'value 1',
'param:param 2': 'value 2',
'param:param 3': 'report-format-1,report-format-2',
'param:param 4': '["option-1","option-2"]',
'param_using_default:param 2': 1,
},
});
Expand All @@ -106,11 +115,19 @@ describe('ReportConfigCommand tests', () => {
'param 1': 'value A',
'param 2': 'value B',
'param 3': ['report-format-A', 'report-format-B'],
'param 4': ['option-1', 'option-2'],
},
params_using_default: {
'param 1': true,
'param 2': false,
'param 3': false,
'param 4': false,
},
param_types: {
'param 1': 'string',
'param 2': 'text',
'param 3': 'report_format_list',
'param 4': 'multi_selection',
},
})
.then(resp => {
Expand All @@ -122,6 +139,7 @@ describe('ReportConfigCommand tests', () => {
'param:param 1': 'value A',
'param:param 2': 'value B',
'param:param 3': 'report-format-A,report-format-B',
'param:param 4': '["option-1","option-2"]',
'param_using_default:param 1': 1,
},
});
Expand Down
24 changes: 20 additions & 4 deletions src/gmp/commands/reportconfigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class ReportConfigCommand extends EntityCommand {
report_format_id,
params = {},
params_using_default = {},
param_types = {},
} = args;

const data = {
Expand All @@ -52,8 +53,12 @@ export class ReportConfigCommand extends EntityCommand {

for (const prefname in params) {
let value = params[prefname];
if (isArray(params[prefname])) {
value = params[prefname].join(',');
if (isArray(value)) {
if (param_types[prefname] === 'report_format_list') {
value = params[prefname].join(',');
} else {
value = JSON.stringify(params[prefname]);
}
}
data['param:' + prefname] = value;
}
Expand All @@ -71,7 +76,14 @@ export class ReportConfigCommand extends EntityCommand {
}

save(args) {
const {id, comment, name, params = {}, params_using_default = {}} = args;
const {
id,
comment,
name,
params = {},
params_using_default = {},
param_types = {},
} = args;

const data = {
cmd: 'save_report_config',
Expand All @@ -91,7 +103,11 @@ export class ReportConfigCommand extends EntityCommand {
for (const prefname in params) {
let value = params[prefname];
if (isArray(params[prefname])) {
value = params[prefname].join(',');
if (param_types[prefname] === 'report_format_list') {
value = params[prefname].join(',');
} else {
value = JSON.stringify(params[prefname]);
}
}
data['param:' + prefname] = value;
}
Expand Down
3 changes: 3 additions & 0 deletions src/gmp/models/reportconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class Param {
forEach(other.default.report_format, format => {
this.default_labels[format._id] = format.name;
});
} else if (this.type === 'multi_selection') {
this.value = JSON.parse(get_value(value));
this.default = JSON.parse(get_value(other.default));
} else if (this.type === 'integer') {
this.value = parseInt(get_value(value));
this.default = parseInt(get_value(other.default));
Expand Down
3 changes: 3 additions & 0 deletions src/gmp/models/reportformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class Param {

if (this.type === 'report_format_list') {
this.value = map(value.report_format, format => format._id);
} else if (this.type === 'multi_selection') {
this.value = JSON.parse(get_value(value));
this.default = JSON.parse(get_value(other.default));
} else {
this.value = get_value(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ exports[`Report Config Details tests > should render full Details 1`] = `
class=""
>
<tr>
<td>
<td
class="c7"
>
<div
class="c5"
>
Expand All @@ -183,7 +185,9 @@ exports[`Report Config Details tests > should render full Details 1`] = `
</td>
</tr>
<tr>
<td>
<td
class="c7"
>
<div
class="c5"
>
Expand All @@ -201,7 +205,9 @@ exports[`Report Config Details tests > should render full Details 1`] = `
</td>
</tr>
<tr>
<td>
<td
class="c7"
>
<div
class="c5"
>
Expand All @@ -217,7 +223,9 @@ exports[`Report Config Details tests > should render full Details 1`] = `
</td>
</tr>
<tr>
<td>
<td
class="c7"
>
<div
class="c5"
>
Expand All @@ -233,7 +241,9 @@ exports[`Report Config Details tests > should render full Details 1`] = `
</td>
</tr>
<tr>
<td>
<td
class="c7"
>
<div
class="c5"
>
Expand All @@ -249,7 +259,9 @@ exports[`Report Config Details tests > should render full Details 1`] = `
</td>
</tr>
<tr>
<td>
<td
class="c7"
>
<div
class="c5"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,9 @@ exports[`Report Config Details Page tests > should render full Details page with
class=""
>
<tr>
<td>
<td
class="c34"
>
<div
class="c29"
>
Expand All @@ -892,7 +894,9 @@ exports[`Report Config Details Page tests > should render full Details page with
</td>
</tr>
<tr>
<td>
<td
class="c34"
>
<div
class="c29"
>
Expand All @@ -910,7 +914,9 @@ exports[`Report Config Details Page tests > should render full Details page with
</td>
</tr>
<tr>
<td>
<td
class="c34"
>
<div
class="c29"
>
Expand All @@ -926,7 +932,9 @@ exports[`Report Config Details Page tests > should render full Details page with
</td>
</tr>
<tr>
<td>
<td
class="c34"
>
<div
class="c29"
>
Expand All @@ -942,7 +950,9 @@ exports[`Report Config Details Page tests > should render full Details page with
</td>
</tr>
<tr>
<td>
<td
class="c34"
>
<div
class="c29"
>
Expand All @@ -958,7 +968,9 @@ exports[`Report Config Details Page tests > should render full Details page with
</td>
</tr>
<tr>
<td>
<td
class="c34"
>
<div
class="c29"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1354,11 +1354,12 @@ exports[`Edit Report Config Dialog component tests > should render dialog with d
class="c13 c34"
>
<input
checked=""
class="c35 c44"
data-testid="radio-input"
name="BooleanParam"
type="radio"
value="1"
value="true"
/>
<span
class="c45"
Expand All @@ -1383,7 +1384,7 @@ exports[`Edit Report Config Dialog component tests > should render dialog with d
data-testid="radio-input"
name="BooleanParam"
type="radio"
value="0"
value="false"
/>
<span
class="c45"
Expand Down
6 changes: 6 additions & 0 deletions src/web/pages/reportconfigs/__tests__/component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ describe('Report Config Component tests', () => {
entityType: 'reportconfig',
id: 'rc123',
name: 'test report config',
param_types: {
'test param': 'string',
},
params: {
'test param': 'ABC',
},
Expand Down Expand Up @@ -224,6 +227,9 @@ describe('Report Config Component tests', () => {
expect(createReportConfig).toHaveBeenCalledWith({
name: 'Unnamed',
comment: '',
param_types: {
'test param': 'string',
},
params: {
'test param': 'ABC',
},
Expand Down
29 changes: 29 additions & 0 deletions src/web/pages/reportconfigs/__tests__/dialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ describe('Edit Report Config Dialog component tests', () => {

expect(handleSave).toHaveBeenCalledWith({
...config,
param_types: {
BooleanParam: 'boolean',
IntegerParam: 'integer',
ReportFormatListParam: 'report_format_list',
SelectionParam: 'selection',
StringParam: 'string',
TextParam: 'text',
},
params: {
BooleanParam: true,
IntegerParam: 12,
Expand Down Expand Up @@ -210,6 +218,14 @@ describe('Edit Report Config Dialog component tests', () => {
...config,
name: 'lorem',
comment: 'ipsum',
param_types: {
BooleanParam: 'boolean',
IntegerParam: 'integer',
ReportFormatListParam: 'report_format_list',
SelectionParam: 'selection',
StringParam: 'string',
TextParam: 'text',
},
params: {
BooleanParam: false,
IntegerParam: 7,
Expand Down Expand Up @@ -279,6 +295,14 @@ describe('Edit Report Config Dialog component tests', () => {

expect(handleSave).toHaveBeenCalledWith({
...config,
param_types: {
BooleanParam: 'boolean',
IntegerParam: 'integer',
ReportFormatListParam: 'report_format_list',
SelectionParam: 'selection',
StringParam: 'string',
TextParam: 'text',
},
params: {
BooleanParam: true,
IntegerParam: 12,
Expand Down Expand Up @@ -435,6 +459,11 @@ describe('New Report Config Dialog component tests', () => {
name: 'lorem',
comment: 'ipsum',
report_format_id: '1234567',
param_types: {
Param1: 'string',
Param2: 'string',
ReportFormatListParam: 'report_format_list',
},
params: {
Param1: 'ABC',
Param2: 'XYZ',
Expand Down
16 changes: 15 additions & 1 deletion src/web/pages/reportconfigs/details.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import {map} from 'gmp/utils/array';
import {isDefined} from 'gmp/utils/identity';
import {renderYesNo} from 'web/utils/render';

import styled from 'styled-components';

export const ReportConfigParamValue = ({
param,
value = param.value,
Expand All @@ -58,6 +60,18 @@ export const ReportConfigParamValue = ({
</DetailsLink>
);
});
} else if (param.type === 'multi_selection') {
const OptionsList = styled.ul`
margin: 0;
padding-left: 1em;
`;
return (
<OptionsList>
{param.value.map(option => (
<li key={param.name + '=' + option}>{option}</li>
))}
</OptionsList>
);
} else if (param.type === 'text') {
return <pre>{value}</pre>;
} else if (param.type === 'boolean') {
Expand Down Expand Up @@ -94,7 +108,7 @@ const ReportConfigDetails = ({entity, links = true}) => {
params.map(param => {
return (
<TableRow key={param.name}>
<TableData>{param.name}</TableData>
<TableDataAlignTop>{param.name}</TableDataAlignTop>
<TableData>
<ReportConfigParamValue param={param} links={links} />
</TableData>
Expand Down
Loading

0 comments on commit 397a388

Please sign in to comment.