Skip to content

Commit

Permalink
[CTI] shortens large numbers on Dashboard Link Panel (#105269)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecezalp authored and kibanamachine committed Jul 13, 2021
1 parent b7a8c1e commit 3943f63
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { shortenCountIntoString } from './shorten_count_into_string';

describe('utils', () => {
describe('shortenCountIntoString', () => {
it('should not change small numbers', () => {
expect(shortenCountIntoString(0)).toBe('0');
expect(shortenCountIntoString(9999)).toBe('9999');
});

it('should add K when appropriate', () => {
expect(shortenCountIntoString(10000)).toBe('10K');
expect(shortenCountIntoString(109000)).toBe('109K');
expect(shortenCountIntoString(109800)).toBe('109.8K');
expect(shortenCountIntoString(109897)).toBe('109.8K');
});

it('should add M when appropriate', () => {
expect(shortenCountIntoString(10000000)).toBe('10M');
expect(shortenCountIntoString(109000000)).toBe('109M');
expect(shortenCountIntoString(109800000)).toBe('109.8M');
expect(shortenCountIntoString(109890000)).toBe('109.8M');
});

it('should add B when appropriate', () => {
expect(shortenCountIntoString(10000000000)).toBe('10B');
expect(shortenCountIntoString(109000000000)).toBe('109B');
expect(shortenCountIntoString(109800000000)).toBe('109.8B');
expect(shortenCountIntoString(109890000000)).toBe('109.8B');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export const shortenCountIntoString = (count: number): string => {
if (count < 10000) {
return count.toString();
}
const abbreviations = [
{ magnitude: 1e18, unit: 'E' },
{ magnitude: 1e15, unit: 'P' },
{ magnitude: 1e12, unit: 'T' },
{ magnitude: 1e9, unit: 'B' },
{ magnitude: 1e6, unit: 'M' },
{ magnitude: 1e3, unit: 'K' },
];
const { magnitude, unit } = abbreviations.find(
(abbreviation) => count >= abbreviation.magnitude
) ?? {
magnitude: 1,
unit: '',
};

return (
toFixedWithoutRounding(count / magnitude, 1).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + unit
);
};

const toFixedWithoutRounding = (n: number, p: number) => {
const result = n.toFixed(p);
return +result <= n ? result : (+result - Math.pow(0.1, p)).toFixed(p);
};
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('CtiNoEvents', () => {
);

expect(wrapper.find('[data-test-subj="cti-total-event-count"]').text()).toEqual(
'Showing: 0 events'
'Showing: 0 indicators'
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('CtiWithEvents', () => {
);

expect(wrapper.find('[data-test-subj="cti-total-event-count"]').text()).toEqual(
`Showing: ${mockCtiWithEventsProps.totalCount} events`
`Showing: ${mockCtiWithEventsProps.totalCount} indicators`
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe('ThreatIntelPanelView', () => {
);

expect(wrapper.find('[data-test-subj="cti-total-event-count"]').text()).toEqual(
`Showing: ${mockThreatIntelPanelViewProps.totalEventCount} events`
`Showing: ${mockThreatIntelPanelViewProps.totalEventCount} indicators`
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { CtiListItem } from '../../containers/overview_cti_links/helpers';
import { useKibana } from '../../../common/lib/kibana';
import { CtiInnerPanel } from './cti_inner_panel';
import * as i18n from './translations';
import { shortenCountIntoString } from '../../../common/utils/shorten_count_into_string';

const DashboardLink = styled.li`
margin: 0 ${({ theme }) => theme.eui.paddingSizes.s} 0 ${({ theme }) => theme.eui.paddingSizes.m};
Expand Down Expand Up @@ -84,7 +85,7 @@ export const ThreatIntelPanelView: React.FC<ThreatIntelPanelViewProps> = ({
() => (
<FormattedMessage
data-test-subj="cti-total-event-count"
defaultMessage="Showing: {totalEventCount} {totalEventCount, plural, one {event} other {events}}"
defaultMessage="Showing: {totalEventCount} {totalEventCount, plural, one {indicator} other {indicators}}"
id="xpack.securitySolution.overview.ctiDashboardSubtitle"
values={{ totalEventCount }}
/>
Expand Down Expand Up @@ -160,7 +161,7 @@ export const ThreatIntelPanelView: React.FC<ThreatIntelPanelViewProps> = ({
justifyContent="flexEnd"
>
<DashboardRightSideElement key={`${title}-count`} grow={false}>
{count}
{shortenCountIntoString(count)}
</DashboardRightSideElement>
<DashboardRightSideElement key={`${title}-source`} grow={false}>
{path ? (
Expand Down

0 comments on commit 3943f63

Please sign in to comment.