[8.14] [Entity Analytics] Do not display -0.00 for criticality contributions less than -0.01 (#182356) (#182777)

# Backport

This will backport the following commits from `main` to `8.14`:
- [[Entity Analytics] Do not display -0.00 for criticality contributions
less than -0.01
(#182356)](https://github.com/elastic/kibana/pull/182356)

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

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

<!--BACKPORT [{"author":{"name":"Mark
Hopkin","email":"mark.hopkin@elastic.co"},"sourceCommit":{"committedDate":"2024-05-07T08:18:34Z","message":"[Entity
Analytics] Do not display -0.00 for criticality contributions less than
-0.01 (#182356)\n\nCloses #182224\r\n\r\nIf an asset criticality
contribution score was below -0.01 e.g 0.0000001\r\nthen we would
display it as -0.0.0\r\n\r\nSkipping release notes as this feature was
introduced in 8.14 and I'm\r\nback-porting.\r\n\r\nBefore:\r\n<img
width=\"686\" alt=\"Screenshot 2024-05-02 at 13 13
36\"\r\nsrc=\"8d4fa427-6762-486c-b6e2-ac0f1c544f09\">\r\n\r\nAfter:\r\n<img
width=\"604\" alt=\"Screenshot 2024-05-02 at 13 13
56\"\r\nsrc=\"a6f8808b-cf0c-4c86-8286-f8d3998ae69e\">\r\n\r\n---------\r\n\r\nCo-authored-by:
Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"a8a6003c406099ada70e16ded665eea0778b99f5","branchLabelMapping":{"^v8.15.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","backport:prev-minor","Team:Entity
Analytics","v8.15.0"],"title":"[Entity Analytics] Do not display -0.00
for criticality contributions less than
-0.01","number":182356,"url":"https://github.com/elastic/kibana/pull/182356","mergeCommit":{"message":"[Entity
Analytics] Do not display -0.00 for criticality contributions less than
-0.01 (#182356)\n\nCloses #182224\r\n\r\nIf an asset criticality
contribution score was below -0.01 e.g 0.0000001\r\nthen we would
display it as -0.0.0\r\n\r\nSkipping release notes as this feature was
introduced in 8.14 and I'm\r\nback-porting.\r\n\r\nBefore:\r\n<img
width=\"686\" alt=\"Screenshot 2024-05-02 at 13 13
36\"\r\nsrc=\"8d4fa427-6762-486c-b6e2-ac0f1c544f09\">\r\n\r\nAfter:\r\n<img
width=\"604\" alt=\"Screenshot 2024-05-02 at 13 13
56\"\r\nsrc=\"a6f8808b-cf0c-4c86-8286-f8d3998ae69e\">\r\n\r\n---------\r\n\r\nCo-authored-by:
Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"a8a6003c406099ada70e16ded665eea0778b99f5"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.15.0","branchLabelMappingKey":"^v8.15.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/182356","number":182356,"mergeCommit":{"message":"[Entity
Analytics] Do not display -0.00 for criticality contributions less than
-0.01 (#182356)\n\nCloses #182224\r\n\r\nIf an asset criticality
contribution score was below -0.01 e.g 0.0000001\r\nthen we would
display it as -0.0.0\r\n\r\nSkipping release notes as this feature was
introduced in 8.14 and I'm\r\nback-porting.\r\n\r\nBefore:\r\n<img
width=\"686\" alt=\"Screenshot 2024-05-02 at 13 13
36\"\r\nsrc=\"8d4fa427-6762-486c-b6e2-ac0f1c544f09\">\r\n\r\nAfter:\r\n<img
width=\"604\" alt=\"Screenshot 2024-05-02 at 13 13
56\"\r\nsrc=\"a6f8808b-cf0c-4c86-8286-f8d3998ae69e\">\r\n\r\n---------\r\n\r\nCo-authored-by:
Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"a8a6003c406099ada70e16ded665eea0778b99f5"}}]}]
BACKPORT-->

Co-authored-by: Mark Hopkin <mark.hopkin@elastic.co>
This commit is contained in:
Kibana Machine 2024-05-07 05:40:13 -04:00 committed by GitHub
parent fe4c4954f6
commit f72ef67361
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 78 additions and 2 deletions

View file

@ -49,6 +49,12 @@ const riskScore = {
},
};
const riskScoreWithAssetCriticalityContribution = (contribution: number) => {
const score = JSON.parse(JSON.stringify(riskScore));
score.user.risk.category_2_score = contribution;
return score;
};
describe('RiskInputsTab', () => {
beforeEach(() => {
jest.clearAllMocks();
@ -119,6 +125,62 @@ describe('RiskInputsTab', () => {
expect(queryByTestId('risk-input-contexts-title')).toBeInTheDocument();
});
it('Displays 0.00 for the asset criticality contribution if the contribution value is less than -0.01', () => {
mockUseUiSetting.mockReturnValue([true]);
mockUseRiskScore.mockReturnValue({
loading: false,
error: false,
data: [riskScoreWithAssetCriticalityContribution(-0.0000001)],
});
const { getByTestId } = render(
<TestProviders>
<RiskInputsTab entityType={RiskScoreEntity.user} entityName="elastic" />
</TestProviders>
);
const contextsTable = getByTestId('risk-input-contexts-table');
expect(contextsTable).not.toHaveTextContent('-0.00');
expect(contextsTable).toHaveTextContent('0.00');
});
it('Displays 0.00 for the asset criticality contribution if the contribution value is less than 0.01', () => {
mockUseUiSetting.mockReturnValue([true]);
mockUseRiskScore.mockReturnValue({
loading: false,
error: false,
data: [riskScoreWithAssetCriticalityContribution(0.0000001)],
});
const { getByTestId } = render(
<TestProviders>
<RiskInputsTab entityType={RiskScoreEntity.user} entityName="elastic" />
</TestProviders>
);
const contextsTable = getByTestId('risk-input-contexts-table');
expect(contextsTable).not.toHaveTextContent('+0.00');
expect(contextsTable).toHaveTextContent('0.00');
});
it('Adds a plus to positive asset criticality contribution scores', () => {
mockUseUiSetting.mockReturnValue([true]);
mockUseRiskScore.mockReturnValue({
loading: false,
error: false,
data: [riskScoreWithAssetCriticalityContribution(2.22)],
});
const { getByTestId } = render(
<TestProviders>
<RiskInputsTab entityType={RiskScoreEntity.user} entityName="elastic" />
</TestProviders>
);
expect(getByTestId('risk-input-contexts-table')).toHaveTextContent('+2.22');
});
it('shows extra alerts contribution message', () => {
const alerts = times(
(number) => ({

View file

@ -14,6 +14,7 @@ import { useUiSetting$ } from '@kbn/kibana-react-plugin/public';
import { ALERT_RULE_NAME } from '@kbn/rule-data-utils';
import { get } from 'lodash/fp';
import { formatRiskScore } from '../../../../common';
import type {
InputAlert,
UseRiskContributingAlertsResult,
@ -243,6 +244,7 @@ const ContextsSection: React.FC<{
<EuiInMemoryTable
compressed={true}
loading={loading}
data-test-subj="risk-input-contexts-table"
columns={contextColumns}
items={[
{
@ -347,5 +349,17 @@ const ExtraAlertsMessage: React.FC<ExtraAlertsMessageProps> = ({ riskScore, aler
);
};
const formatContribution = (value: number) =>
value > 0 ? `+${value.toFixed(2)}` : value.toFixed(2);
const formatContribution = (value: number): string => {
const fixedValue = formatRiskScore(value);
// prevent +0.00 for values like 0.0001
if (fixedValue === '0.00') {
return fixedValue;
}
if (value > 0) {
return `+${fixedValue}`;
}
return fixedValue;
};