From ce8ec351736b1645d1ec6999d580e4cf55b3a39b Mon Sep 17 00:00:00 2001 From: cauemarcondes Date: Wed, 8 Jul 2020 14:33:37 +0200 Subject: [PATCH] adding moment duration to get the units as seconds --- .../utils/get_bucket_size/unit_to_seconds.ts | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/x-pack/plugins/observability/public/utils/get_bucket_size/unit_to_seconds.ts b/x-pack/plugins/observability/public/utils/get_bucket_size/unit_to_seconds.ts index 33f7a128cb5c8..657726d988495 100644 --- a/x-pack/plugins/observability/public/utils/get_bucket_size/unit_to_seconds.ts +++ b/x-pack/plugins/observability/public/utils/get_bucket_size/unit_to_seconds.ts @@ -3,16 +3,21 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ +import moment, { unitOfTime as UnitOfTIme } from 'moment'; + +function getDurationAsSeconds(value: number, unitOfTime: UnitOfTIme.Base) { + return moment.duration(value, unitOfTime).asSeconds(); +} const units = { - ms: 0.001, - s: 1, - m: 60, - h: 3600, - d: 86400, - w: 86400 * 7, // Hum... might be wrong - M: 86400 * 30, // this too... 29,30,31? - y: 86400 * 356, // Leap year? + ms: getDurationAsSeconds(1, 'millisecond'), + s: getDurationAsSeconds(1, 'second'), + m: getDurationAsSeconds(1, 'minute'), + h: getDurationAsSeconds(1, 'hour'), + d: getDurationAsSeconds(1, 'day'), + w: getDurationAsSeconds(1, 'week'), + M: getDurationAsSeconds(1, 'month'), + y: getDurationAsSeconds(1, 'year'), }; export function unitToSeconds(unit: string) {