From 255ea61610f4a8ffe9a6b8e53b7dd5ec2ff267d0 Mon Sep 17 00:00:00 2001 From: Bart Van Remortele Date: Sun, 21 Jul 2019 16:57:02 +0200 Subject: [PATCH 1/2] Add time formatting support for very high durations (minutes and hours) to x-pack --- .../public/utils/__test__/formatters.test.ts | 2 + .../plugins/apm/public/utils/formatters.ts | 44 ++++++++++++++++++- .../translations/translations/ja-JP.json | 4 +- .../translations/translations/zh-CN.json | 4 +- 4 files changed, 51 insertions(+), 3 deletions(-) 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..14c5f7aeedd43 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 m'); + 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..b756d584197f0 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: 'm' + }); + 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 b1f22db183feb..54bac59b661f3 100644 --- a/x-pack/plugins/translations/translations/ja-JP.json +++ b/x-pack/plugins/translations/translations/ja-JP.json @@ -3757,6 +3757,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": "を保存できませんでした", @@ -10683,4 +10685,4 @@ "xpack.watcher.watchActions.logging.logTextIsRequiredValidationMessage": "ログテキストが必要です。", "xpack.watcher.watcherDescription": "アラートの作成、管理、監視によりデータへの変更を検知します。" } -} \ No newline at end of file +} diff --git a/x-pack/plugins/translations/translations/zh-CN.json b/x-pack/plugins/translations/translations/zh-CN.json index 406463d1dc573..412c80feb6a8b 100644 --- a/x-pack/plugins/translations/translations/zh-CN.json +++ b/x-pack/plugins/translations/translations/zh-CN.json @@ -3757,6 +3757,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": "无法保存", @@ -10682,4 +10684,4 @@ "xpack.watcher.watchActions.logging.logTextIsRequiredValidationMessage": "“日志文本”必填。", "xpack.watcher.watcherDescription": "通过创建、管理和监测警报来检测数据中的更改。" } -} \ No newline at end of file +} From 143cd68d64ef72be4dee832f8e528bd4f812979e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Louv-Jansen?= Date: Fri, 26 Jul 2019 10:56:14 +0200 Subject: [PATCH 2/2] Change minute abbreviation to `min` --- .../legacy/plugins/apm/public/utils/__test__/formatters.test.ts | 2 +- x-pack/legacy/plugins/apm/public/utils/formatters.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 14c5f7aeedd43..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,7 +22,7 @@ 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 m'); + expect(asTime(60000000 * 10)).toEqual('10.0 min'); expect(asTime(3600000000 * 1.5)).toEqual('1.5 h'); }); diff --git a/x-pack/legacy/plugins/apm/public/utils/formatters.ts b/x-pack/legacy/plugins/apm/public/utils/formatters.ts index b756d584197f0..fa144d2f64276 100644 --- a/x-pack/legacy/plugins/apm/public/utils/formatters.ts +++ b/x-pack/legacy/plugins/apm/public/utils/formatters.ts @@ -52,7 +52,7 @@ export function asMinutes( const minutesLabel = SPACE + i18n.translate('xpack.apm.formatters.minutesTimeUnitLabel', { - defaultMessage: 'm' + defaultMessage: 'min' }); const formatted = asDecimal(value / 60000000); return `${formatted}${withUnit ? minutesLabel : ''}`;