From a695376591718c0b874c990e788a808b1e2965a5 Mon Sep 17 00:00:00 2001 From: Bart Van Remortele Date: Fri, 26 Jul 2019 14:01:18 +0200 Subject: [PATCH] [APM] Add support for very high durations (minutes and hours) (#41640) --- .../public/utils/__test__/formatters.test.ts | 2 + .../plugins/apm/public/utils/formatters.ts | 44 ++++++++++++++++++- .../translations/translations/ja-JP.json | 2 + .../translations/translations/zh-CN.json | 2 + 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/x-pack/legacy/plugins/apm/public/utils/__test__/formatters.test.ts b/x-pack/legacy/plugins/apm/public/utils/__test__/formatters.test.ts index 678ab9009edd7..093624240565f 100644 --- a/x-pack/legacy/plugins/apm/public/utils/__test__/formatters.test.ts +++ b/x-pack/legacy/plugins/apm/public/utils/__test__/formatters.test.ts @@ -22,6 +22,8 @@ describe('formatters', () => { expect(asTime(1000 * 1000)).toEqual('1,000 ms'); expect(asTime(1000 * 1000 * 10)).toEqual('10,000 ms'); expect(asTime(1000 * 1000 * 20)).toEqual('20.0 s'); + expect(asTime(60000000 * 10)).toEqual('10.0 min'); + expect(asTime(3600000000 * 1.5)).toEqual('1.5 h'); }); it('formats without unit', () => { diff --git a/x-pack/legacy/plugins/apm/public/utils/formatters.ts b/x-pack/legacy/plugins/apm/public/utils/formatters.ts index 60712bc761581..fa144d2f64276 100644 --- a/x-pack/legacy/plugins/apm/public/utils/formatters.ts +++ b/x-pack/legacy/plugins/apm/public/utils/formatters.ts @@ -9,6 +9,8 @@ import { i18n } from '@kbn/i18n'; import { memoize } from 'lodash'; import { NOT_AVAILABLE_LABEL } from '../../common/i18n'; +const HOURS_CUT_OFF = 3600000000; // 1 hour (in microseconds) +const MINUTES_CUT_OFF = 60000000; // 1 minute (in microseconds) const SECONDS_CUT_OFF = 10 * 1000000; // 10 seconds (in microseconds) const MILLISECONDS_CUT_OFF = 10 * 1000; // 10 milliseconds (in microseconds) const SPACE = ' '; @@ -24,6 +26,38 @@ interface FormatterOptions { defaultValue?: string; } +export function asHours( + value: FormatterValue, + { withUnit = true, defaultValue = NOT_AVAILABLE_LABEL }: FormatterOptions = {} +) { + if (value == null) { + return defaultValue; + } + const hoursLabel = + SPACE + + i18n.translate('xpack.apm.formatters.hoursTimeUnitLabel', { + defaultMessage: 'h' + }); + const formatted = asDecimal(value / 3600000000); + return `${formatted}${withUnit ? hoursLabel : ''}`; +} + +export function asMinutes( + value: FormatterValue, + { withUnit = true, defaultValue = NOT_AVAILABLE_LABEL }: FormatterOptions = {} +) { + if (value == null) { + return defaultValue; + } + const minutesLabel = + SPACE + + i18n.translate('xpack.apm.formatters.minutesTimeUnitLabel', { + defaultMessage: 'min' + }); + const formatted = asDecimal(value / 60000000); + return `${formatted}${withUnit ? minutesLabel : ''}`; +} + export function asSeconds( value: FormatterValue, { withUnit = true, defaultValue = NOT_AVAILABLE_LABEL }: FormatterOptions = {} @@ -81,6 +115,10 @@ type TimeFormatter = ( export const getTimeFormatter: TimeFormatter = memoize((max: number) => { const unit = timeUnit(max); switch (unit) { + case 'h': + return asHours; + case 'm': + return asMinutes; case 's': return asSeconds; case 'ms': @@ -91,7 +129,11 @@ export const getTimeFormatter: TimeFormatter = memoize((max: number) => { }); export function timeUnit(max: number) { - if (max > SECONDS_CUT_OFF) { + if (max > HOURS_CUT_OFF) { + return 'h'; + } else if (max > MINUTES_CUT_OFF) { + return 'm'; + } else if (max > SECONDS_CUT_OFF) { return 's'; } else if (max > MILLISECONDS_CUT_OFF) { return 'ms'; diff --git a/x-pack/plugins/translations/translations/ja-JP.json b/x-pack/plugins/translations/translations/ja-JP.json index 8860332e1944e..e3c922315c17a 100644 --- a/x-pack/plugins/translations/translations/ja-JP.json +++ b/x-pack/plugins/translations/translations/ja-JP.json @@ -3784,6 +3784,8 @@ "xpack.apm.formatters.millisTimeUnitLabel": "ミリ秒", "xpack.apm.formatters.requestsPerMinLabel": "1分あたりリクエスト数", "xpack.apm.formatters.secondsTimeUnitLabel": "秒", + "xpack.apm.formatters.minutesTimeUnitLabel": "分", + "xpack.apm.formatters.hoursTimeUnitLabel": "時", "xpack.apm.formatters.transactionsPerMinLabel": "1分あたりトランザクション数", "xpack.apm.header.badge.readOnly.text": "読み込み専用", "xpack.apm.header.badge.readOnly.tooltip": "を保存できませんでした", diff --git a/x-pack/plugins/translations/translations/zh-CN.json b/x-pack/plugins/translations/translations/zh-CN.json index 585c8dd9df192..f9c0560150760 100644 --- a/x-pack/plugins/translations/translations/zh-CN.json +++ b/x-pack/plugins/translations/translations/zh-CN.json @@ -3785,6 +3785,8 @@ "xpack.apm.formatters.millisTimeUnitLabel": "ms", "xpack.apm.formatters.requestsPerMinLabel": "rpm", "xpack.apm.formatters.secondsTimeUnitLabel": "s", + "xpack.apm.formatters.minutesTimeUnitLabel": "m", + "xpack.apm.formatters.hoursTimeUnitLabel": "h", "xpack.apm.formatters.transactionsPerMinLabel": "tpm", "xpack.apm.header.badge.readOnly.text": "只读", "xpack.apm.header.badge.readOnly.tooltip": "无法保存",