Skip to content

Commit

Permalink
[Entity Analytics] Do not display -0.00 for criticality contributions…
Browse files Browse the repository at this point in the history
… less than -0.01 (elastic#182356)

Closes elastic#182224

If an asset criticality contribution score was below -0.01 e.g 0.0000001
then we would display it as -0.0.0

Skipping release notes as this feature was introduced in 8.14 and I'm
back-porting.

Before:
<img width="686" alt="Screenshot 2024-05-02 at 13 13 36"
src="https://github.com/elastic/kibana/assets/3315046/8d4fa427-6762-486c-b6e2-ac0f1c544f09">

After:
<img width="604" alt="Screenshot 2024-05-02 at 13 13 56"
src="https://github.com/elastic/kibana/assets/3315046/a6f8808b-cf0c-4c86-8286-f8d3998ae69e">

---------

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
hop-dev and kibanamachine authored May 7, 2024
1 parent aec4efb commit a8a6003
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -117,6 +123,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) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -242,6 +243,7 @@ const ContextsSection: React.FC<{
<EuiInMemoryTable
compressed={true}
loading={loading}
data-test-subj="risk-input-contexts-table"
columns={contextColumns}
items={[
{
Expand Down Expand Up @@ -346,5 +348,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;
};

0 comments on commit a8a6003

Please sign in to comment.