Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CTI] shortens large numbers on Dashboard Link Panel #105269

Merged
merged 3 commits into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,41 @@
/*
* 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.
*/

/*
10000 to 10K
10100 to 10.1K
1000000 to 1M
1000000000 to 1B
*/
export const shortenCountIntoString = (count: number): string => {
if (count < 10000) {
return count.toString();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use .toLocaleString here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually for numbers under 10000 I believe we don't want to see 9,999 vs 9.999, just 9999 @yiyangliu9286

}
const si = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: there are a lot of one- or two-character variables in this function; more declarative names might help with legibility here. E.g. magnitude and unit instead of v and s

{ v: 1e3, s: 'K' },
{ v: 1e6, s: 'M' },
{ v: 1e9, s: 'B' },
{ v: 1e12, s: 'T' },
{ v: 1e15, s: 'P' },
{ v: 1e18, s: 'E' },
];
let i;
for (i = si.length - 1; i > 0; i--) {
if (count >= si[i].v) {
break;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could reverse the above array to simplify this logic:

Suggested change
let i;
for (i = si.length - 1; i > 0; i--) {
if (count >= si[i].v) {
break;
}
}
const { magnitude, unit } = abbreviations.find((abbreviation) => count >= abbreviation.magnitude);


return (
toFixedWithoutRounding(count / si[i].v, 1).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The @elastic/numeral package might be able to do some of this formatting for you, if you haven't yet explored that option.

);
};

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 @@ -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`}>
{count}
{shortenCountIntoString(count)}
</DashboardRightSideElement>
<DashboardRightSideElement key={`${title}-source`}>
{path ? (
Expand Down