[8.13] [Profiling] Fix default value of profiling settings (#177716) (#178151)

# Backport

This will backport the following commits from `main` to `8.13`:
- [[Profiling] Fix default value of profiling settings
(#177716)](https://github.com/elastic/kibana/pull/177716)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Elena
Stoeva","email":"59341489+ElenaStoeva@users.noreply.github.com"},"sourceCommit":{"committedDate":"2024-03-06T17:30:35Z","message":"[Profiling]
Fix default value of profiling settings (#177716)\n\nFixes:
https://github.com/elastic/kibana/issues/177592\r\n\r\n##
Summary\r\n\r\nThis PR changes the format of the default value for
the\r\n`profilingAWSCostDiscountRate` and the
`profilingAzureCostDiscountRate`\r\nsettings from a string to a number.
Since the `type` property is not\r\nspecified in these setting
definitions, the advanced settings components\r\nassume the type of the
settings based on the type of the default value.\r\nSince the default
value was specified as a string, the settings\r\ncomponents used a text
field instead of a number field and so the\r\nvalidation did not work
properly.\r\n\r\nThe PR also adds a small fix to the settings
normalization function\r\nwhere, if the provided `value` property in the
setting definition is\r\n`0`, it is incorrectly translated to
`undefined` rather than `0`.\r\n\r\n**How to test:**\r\n1. Go to Stack
Management -> Advanced settings\r\n2. Verify that the initial value in
the fields is `0` rather than empty\r\nfields.\r\n3. Set an invalid
value (<0 or >100) for these two settings and verify\r\nthat an error
message is displayed.\r\n\r\n\r\n<img width=\"1335\" alt=\"Screenshot
2024-02-23 at 13 53
04\"\r\nsrc=\"9b6f191b-ae17-4f46-9814-2a325d083fd6\">\r\n\r\n<img
width=\"1335\" alt=\"Screenshot 2024-02-23 at 13 41
14\"\r\nsrc=\"95fe4685-4504-44c9-b312-9530dcbe1b2b\">\r\n\r\n---------\r\n\r\nCo-authored-by:
kibanamachine
<42973632+kibanamachine@users.noreply.github.com>\r\nCo-authored-by:
Cauê Marcondes
<55978943+cauemarcondes@users.noreply.github.com>","sha":"16bcae8833a7e156372d63b8d3440691a86a9a5f","branchLabelMapping":{"^v8.14.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:Deployment
Management","release_note:skip","Feature:uiSettings","backport:prev-minor","Team:obs-ux-management","v8.13.0","v8.14.0"],"title":"[Profiling]
Fix default value of profiling
settings","number":177716,"url":"https://github.com/elastic/kibana/pull/177716","mergeCommit":{"message":"[Profiling]
Fix default value of profiling settings (#177716)\n\nFixes:
https://github.com/elastic/kibana/issues/177592\r\n\r\n##
Summary\r\n\r\nThis PR changes the format of the default value for
the\r\n`profilingAWSCostDiscountRate` and the
`profilingAzureCostDiscountRate`\r\nsettings from a string to a number.
Since the `type` property is not\r\nspecified in these setting
definitions, the advanced settings components\r\nassume the type of the
settings based on the type of the default value.\r\nSince the default
value was specified as a string, the settings\r\ncomponents used a text
field instead of a number field and so the\r\nvalidation did not work
properly.\r\n\r\nThe PR also adds a small fix to the settings
normalization function\r\nwhere, if the provided `value` property in the
setting definition is\r\n`0`, it is incorrectly translated to
`undefined` rather than `0`.\r\n\r\n**How to test:**\r\n1. Go to Stack
Management -> Advanced settings\r\n2. Verify that the initial value in
the fields is `0` rather than empty\r\nfields.\r\n3. Set an invalid
value (<0 or >100) for these two settings and verify\r\nthat an error
message is displayed.\r\n\r\n\r\n<img width=\"1335\" alt=\"Screenshot
2024-02-23 at 13 53
04\"\r\nsrc=\"9b6f191b-ae17-4f46-9814-2a325d083fd6\">\r\n\r\n<img
width=\"1335\" alt=\"Screenshot 2024-02-23 at 13 41
14\"\r\nsrc=\"95fe4685-4504-44c9-b312-9530dcbe1b2b\">\r\n\r\n---------\r\n\r\nCo-authored-by:
kibanamachine
<42973632+kibanamachine@users.noreply.github.com>\r\nCo-authored-by:
Cauê Marcondes
<55978943+cauemarcondes@users.noreply.github.com>","sha":"16bcae8833a7e156372d63b8d3440691a86a9a5f"}},"sourceBranch":"main","suggestedTargetBranches":["8.13"],"targetPullRequestStates":[{"branch":"8.13","label":"v8.13.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.14.0","branchLabelMappingKey":"^v8.14.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/177716","number":177716,"mergeCommit":{"message":"[Profiling]
Fix default value of profiling settings (#177716)\n\nFixes:
https://github.com/elastic/kibana/issues/177592\r\n\r\n##
Summary\r\n\r\nThis PR changes the format of the default value for
the\r\n`profilingAWSCostDiscountRate` and the
`profilingAzureCostDiscountRate`\r\nsettings from a string to a number.
Since the `type` property is not\r\nspecified in these setting
definitions, the advanced settings components\r\nassume the type of the
settings based on the type of the default value.\r\nSince the default
value was specified as a string, the settings\r\ncomponents used a text
field instead of a number field and so the\r\nvalidation did not work
properly.\r\n\r\nThe PR also adds a small fix to the settings
normalization function\r\nwhere, if the provided `value` property in the
setting definition is\r\n`0`, it is incorrectly translated to
`undefined` rather than `0`.\r\n\r\n**How to test:**\r\n1. Go to Stack
Management -> Advanced settings\r\n2. Verify that the initial value in
the fields is `0` rather than empty\r\nfields.\r\n3. Set an invalid
value (<0 or >100) for these two settings and verify\r\nthat an error
message is displayed.\r\n\r\n\r\n<img width=\"1335\" alt=\"Screenshot
2024-02-23 at 13 53
04\"\r\nsrc=\"9b6f191b-ae17-4f46-9814-2a325d083fd6\">\r\n\r\n<img
width=\"1335\" alt=\"Screenshot 2024-02-23 at 13 41
14\"\r\nsrc=\"95fe4685-4504-44c9-b312-9530dcbe1b2b\">\r\n\r\n---------\r\n\r\nCo-authored-by:
kibanamachine
<42973632+kibanamachine@users.noreply.github.com>\r\nCo-authored-by:
Cauê Marcondes
<55978943+cauemarcondes@users.noreply.github.com>","sha":"16bcae8833a7e156372d63b8d3440691a86a9a5f"}}]}]
BACKPORT-->

---------

Co-authored-by: Elena Stoeva <59341489+ElenaStoeva@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2024-03-08 14:27:18 -05:00 committed by GitHub
parent ad7c080b7c
commit cf91a454a0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 37 additions and 3 deletions

View file

@ -7,6 +7,7 @@
*/
import { normalizeSettings } from './normalize_settings';
import { UiSettingsType } from '@kbn/core-ui-settings-common';
describe('normalizeSettings', () => {
describe('adds a missing type if there is a value', () => {
@ -57,6 +58,34 @@ describe('normalizeSettings', () => {
});
});
describe('correctly derives a value', () => {
describe('with a specified numeric type', () => {
const type: UiSettingsType = 'number';
it('provided value is a number or a numeric string', () => {
const numberSetting = { name: 'foo', type, value: 10 };
const numericStringSetting = { name: 'foo', type, value: '5' };
const settings = { foo: numberSetting, bar: numericStringSetting };
expect(normalizeSettings(settings)).toEqual({
foo: { ...numberSetting, value: 10 },
bar: { ...numericStringSetting, value: 5 },
});
});
it('provided value is non-numeric', () => {
const values = [undefined, '', 'test', false];
values.forEach((value) => {
const setting = { name: 'foo', type, value };
const settings = { foo: setting };
expect(normalizeSettings(settings)).toEqual({
foo: { ...setting, value: undefined },
});
});
});
});
});
it('throws if the value is an object', () => {
const setting = { name: 'foo', value: { bar: 'baz' } };
const settings = { foo: setting };

View file

@ -49,6 +49,10 @@ const deriveType = (setting: UiSetting<SettingType>): SettingType => {
return typeofVal;
};
const isNumericValue = (value: unknown): boolean => {
return value != null && value !== '' && !isNaN(Number(String(value)));
};
/**
* UiSettings have an extremely permissive set of types, which makes it difficult to code
* against them. The `value` property is typed as `unknown`, but the setting has a `type`
@ -71,7 +75,7 @@ const deriveValue = (type: SettingType, value: unknown): Value => {
case 'string':
return value as string;
case 'number':
return value ? Number(value) : undefined;
return isNumericValue(value) ? Number(value) : undefined;
case 'boolean':
return Boolean(value);
case 'array':

View file

@ -18,5 +18,6 @@
"kbn_references": [
"@kbn/management-settings-types",
"@kbn/i18n",
"@kbn/core-ui-settings-common",
]
}

View file

@ -528,7 +528,7 @@ export const uiSettings: Record<string, UiSettings> = {
name: i18n.translate('xpack.observability.profilingAWSCostDiscountRateUiSettingName', {
defaultMessage: 'AWS EDP discount rate (%)',
}),
value: '0',
value: 0,
schema: schema.number({ min: 0, max: 100 }),
requiresPageReload: true,
description: i18n.translate(
@ -544,7 +544,7 @@ export const uiSettings: Record<string, UiSettings> = {
name: i18n.translate('xpack.observability.profilingAzureCostDiscountRateUiSettingName', {
defaultMessage: 'Azure discount rate (%)',
}),
value: '0',
value: 0,
schema: schema.number({ min: 0, max: 100 }),
requiresPageReload: true,
description: i18n.translate(