Skip to content

Commit 8a6aab3

Browse files
authored
[APM] Only add decimals for numbers below 10 (#69334)
1 parent 3ee0bf2 commit 8a6aab3

File tree

8 files changed

+102
-79
lines changed

8 files changed

+102
-79
lines changed

x-pack/plugins/apm/e2e/cypress/integration/snapshots.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module.exports = {
22
APM: {
33
'Transaction duration charts': {
4-
'1': '350.0 ms',
5-
'2': '175.0 ms',
6-
'3': '0.0 ms',
4+
'1': '350 ms',
5+
'2': '175 ms',
6+
'3': '0 ms',
77
},
88
},
99
__version: '4.5.0',

x-pack/plugins/apm/public/components/app/ServiceOverview/ServiceList/__test__/List.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('ServiceOverview -> List', () => {
4040
expect(renderedColumns[0]).toMatchSnapshot();
4141
expect(renderedColumns.slice(2)).toEqual([
4242
'python',
43-
'91.5 ms',
43+
'92 ms',
4444
'86.9 tpm',
4545
'12.6 err.',
4646
]);

x-pack/plugins/apm/public/components/shared/charts/CustomPlot/test/__snapshots__/CustomPlot.test.js.snap

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugins/apm/public/components/shared/charts/Histogram/__test__/Histogram.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import d3 from 'd3';
1111
import { HistogramInner } from '../index';
1212
import response from './response.json';
1313
import {
14-
asDecimal,
1514
getDurationFormatter,
15+
asInteger,
1616
} from '../../../../../utils/formatters';
1717
import { toJson } from '../../../../../utils/testHelpers';
1818
import { getFormattedBuckets } from '../../../../app/TransactionDetails/Distribution/index';
@@ -33,8 +33,8 @@ describe('Histogram', () => {
3333
transactionId="myTransactionId"
3434
onClick={onClick}
3535
formatX={(time) => timeFormatter(time).formatted}
36-
formatYShort={(t) => `${asDecimal(t)} occ.`}
37-
formatYLong={(t) => `${asDecimal(t)} occurrences`}
36+
formatYShort={(t) => `${asInteger(t)} occ.`}
37+
formatYLong={(t) => `${asInteger(t)} occurrences`}
3838
tooltipHeader={(bucket) => {
3939
const xFormatted = timeFormatter(bucket.x);
4040
const x0Formatted = timeFormatter(bucket.x0);
@@ -78,9 +78,9 @@ describe('Histogram', () => {
7878
const tooltips = wrapper.find('Tooltip');
7979

8080
expect(tooltips.length).toBe(1);
81-
expect(tooltips.prop('header')).toBe('811.1 - 926.9 ms');
81+
expect(tooltips.prop('header')).toBe('811 - 927 ms');
8282
expect(tooltips.prop('tooltipPoints')).toEqual([
83-
{ value: '49.0 occurrences' },
83+
{ value: '49 occurrences' },
8484
]);
8585
expect(tooltips.prop('x')).toEqual(869010);
8686
expect(tooltips.prop('y')).toEqual(27.5);

x-pack/plugins/apm/public/components/shared/charts/Histogram/__test__/__snapshots__/Histogram.test.js.snap

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugins/apm/public/components/shared/charts/Timeline/Marker/ErrorMarker.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('ErrorMarker', () => {
3737
act(() => {
3838
fireEvent.click(component.getByTestId('popover'));
3939
});
40-
expectTextsInDocument(component, ['10.0 ms']);
40+
expectTextsInDocument(component, ['10 ms']);
4141
return component;
4242
}
4343
function getKueryDecoded(url: string) {

x-pack/plugins/apm/public/utils/formatters/__test__/duration.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ describe('duration formatters', () => {
1414
expect(asDuration(1)).toEqual('1 μs');
1515
expect(asDuration(toMicroseconds(1, 'milliseconds'))).toEqual('1,000 μs');
1616
expect(asDuration(toMicroseconds(1000, 'milliseconds'))).toEqual(
17-
'1,000.0 ms'
17+
'1,000 ms'
1818
);
1919
expect(asDuration(toMicroseconds(10000, 'milliseconds'))).toEqual(
20-
'10,000.0 ms'
20+
'10,000 ms'
2121
);
22-
expect(asDuration(toMicroseconds(20, 'seconds'))).toEqual('20.0 s');
23-
expect(asDuration(toMicroseconds(10, 'minutes'))).toEqual('10.0 min');
24-
expect(asDuration(toMicroseconds(1, 'hours'))).toEqual('60.0 min');
22+
expect(asDuration(toMicroseconds(20, 'seconds'))).toEqual('20 s');
23+
expect(asDuration(toMicroseconds(10, 'minutes'))).toEqual('10 min');
24+
expect(asDuration(toMicroseconds(1, 'hours'))).toEqual('60 min');
2525
expect(asDuration(toMicroseconds(1.5, 'hours'))).toEqual('1.5 h');
2626
});
2727

@@ -41,7 +41,7 @@ describe('duration formatters', () => {
4141

4242
describe('asMilliseconds', () => {
4343
it('converts to formatted decimal milliseconds', () => {
44-
expect(asMillisecondDuration(0)).toEqual('0.0 ms');
44+
expect(asMillisecondDuration(0)).toEqual('0 ms');
4545
});
4646
});
4747
});

x-pack/plugins/apm/public/utils/formatters/duration.ts

Lines changed: 71 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@ interface FormatterOptions {
1818

1919
type DurationTimeUnit = TimeUnit | 'microseconds';
2020

21-
interface DurationUnit {
22-
[unit: string]: {
23-
label: string;
24-
convert: (value: number) => string;
25-
};
26-
}
27-
2821
interface ConvertedDuration {
2922
value: string;
3023
unit?: string;
@@ -38,42 +31,69 @@ export type TimeFormatter = (
3831

3932
type TimeFormatterBuilder = (max: number) => TimeFormatter;
4033

41-
const durationUnit: DurationUnit = {
42-
hours: {
43-
label: i18n.translate('xpack.apm.formatters.hoursTimeUnitLabel', {
44-
defaultMessage: 'h',
45-
}),
46-
convert: (value: number) =>
47-
asDecimal(moment.duration(value / 1000).asHours()),
48-
},
49-
minutes: {
50-
label: i18n.translate('xpack.apm.formatters.minutesTimeUnitLabel', {
51-
defaultMessage: 'min',
52-
}),
53-
convert: (value: number) =>
54-
asDecimal(moment.duration(value / 1000).asMinutes()),
55-
},
56-
seconds: {
57-
label: i18n.translate('xpack.apm.formatters.secondsTimeUnitLabel', {
58-
defaultMessage: 's',
59-
}),
60-
convert: (value: number) =>
61-
asDecimal(moment.duration(value / 1000).asSeconds()),
62-
},
63-
milliseconds: {
64-
label: i18n.translate('xpack.apm.formatters.millisTimeUnitLabel', {
65-
defaultMessage: 'ms',
66-
}),
67-
convert: (value: number) =>
68-
asDecimal(moment.duration(value / 1000).asMilliseconds()),
69-
},
70-
microseconds: {
71-
label: i18n.translate('xpack.apm.formatters.microsTimeUnitLabel', {
72-
defaultMessage: 'μs',
73-
}),
74-
convert: (value: number) => asInteger(value),
75-
},
76-
};
34+
function asDecimalOrInteger(value: number) {
35+
// exact 0 or above 10 should not have decimal
36+
if (value === 0 || value >= 10) {
37+
return asInteger(value);
38+
}
39+
return asDecimal(value);
40+
}
41+
42+
function getUnitLabelAndConvertedValue(
43+
unitKey: DurationTimeUnit,
44+
value: number
45+
) {
46+
switch (unitKey) {
47+
case 'hours': {
48+
return {
49+
unitLabel: i18n.translate('xpack.apm.formatters.hoursTimeUnitLabel', {
50+
defaultMessage: 'h',
51+
}),
52+
convertedValue: asDecimalOrInteger(
53+
moment.duration(value / 1000).asHours()
54+
),
55+
};
56+
}
57+
case 'minutes': {
58+
return {
59+
unitLabel: i18n.translate('xpack.apm.formatters.minutesTimeUnitLabel', {
60+
defaultMessage: 'min',
61+
}),
62+
convertedValue: asDecimalOrInteger(
63+
moment.duration(value / 1000).asMinutes()
64+
),
65+
};
66+
}
67+
case 'seconds': {
68+
return {
69+
unitLabel: i18n.translate('xpack.apm.formatters.secondsTimeUnitLabel', {
70+
defaultMessage: 's',
71+
}),
72+
convertedValue: asDecimalOrInteger(
73+
moment.duration(value / 1000).asSeconds()
74+
),
75+
};
76+
}
77+
case 'milliseconds': {
78+
return {
79+
unitLabel: i18n.translate('xpack.apm.formatters.millisTimeUnitLabel', {
80+
defaultMessage: 'ms',
81+
}),
82+
convertedValue: asDecimalOrInteger(
83+
moment.duration(value / 1000).asMilliseconds()
84+
),
85+
};
86+
}
87+
case 'microseconds': {
88+
return {
89+
unitLabel: i18n.translate('xpack.apm.formatters.microsTimeUnitLabel', {
90+
defaultMessage: 'μs',
91+
}),
92+
convertedValue: asInteger(value),
93+
};
94+
}
95+
}
96+
}
7797

7898
/**
7999
* Converts a microseconds value into the unit defined.
@@ -87,16 +107,19 @@ function convertTo({
87107
microseconds: Maybe<number>;
88108
defaultValue?: string;
89109
}): ConvertedDuration {
90-
const duration = durationUnit[unit];
91-
if (!duration || microseconds == null) {
110+
if (microseconds == null) {
92111
return { value: defaultValue, formatted: defaultValue };
93112
}
94113

95-
const convertedValue = duration.convert(microseconds);
114+
const { convertedValue, unitLabel } = getUnitLabelAndConvertedValue(
115+
unit,
116+
microseconds
117+
);
118+
96119
return {
97120
value: convertedValue,
98-
unit: duration.label,
99-
formatted: `${convertedValue} ${duration.label}`,
121+
unit: unitLabel,
122+
formatted: `${convertedValue} ${unitLabel}`,
100123
};
101124
}
102125

0 commit comments

Comments
 (0)