From ab0155c861a0fd29b680184acf31c09fcfb6340d Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Tue, 21 Jun 2016 17:22:46 +0300 Subject: [PATCH] Issue #2971 - add time units for use in singlestat panel. --- public/app/core/utils/kbn.js | 53 +++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/public/app/core/utils/kbn.js b/public/app/core/utils/kbn.js index f54979b0e97..ccbb9900b65 100644 --- a/public/app/core/utils/kbn.js +++ b/public/app/core/utils/kbn.js @@ -1,8 +1,9 @@ define([ 'jquery', 'lodash', + 'moment' ], -function($, _) { +function($, _, moment) { 'use strict'; var kbn = {}; @@ -580,6 +581,45 @@ function($, _) { } }; + // Date and time + kbn.toDateTime = function(size, timeScale) { + var datetime; + if (timeScale === 's') { + datetime = moment.unix(size); + } else { + datetime = moment(size); + } + return datetime; + }; + + kbn.toDuration = function(size, timeScale) { + return moment.duration(size, timeScale); + }; + + kbn.valueFormats.dtms = function(size) { + return kbn.toDateTime(size, 'ms').format('YYYY-MM-DD hh:mm:ss'); + }; + + kbn.valueFormats.dts = function(size) { + return kbn.toDateTime(size, 's').format('YYYY-MM-DD hh:mm:ss'); + }; + + kbn.valueFormats.dtfromnowms = function(size) { + return kbn.toDateTime(size, 'ms').fromNow(true); + }; + + kbn.valueFormats.dtfromnows = function(size) { + return kbn.toDateTime(size, 's').fromNow(true); + }; + + kbn.valueFormats.dtdurationms = function(size) { + return kbn.toDuration(size, 'ms').humanize(); + }; + + kbn.valueFormats.dtdurations = function(size) { + return kbn.toDuration(size, 's').humanize(); + }; + ///// FORMAT MENU ///// kbn.getUnitFormats = function() { @@ -618,6 +658,17 @@ function($, _) { {text: 'days (d)', value: 'd' }, ] }, + { + text: 'date and time', + submenu: [ + {text: 'date and time (ms)', value: 'dtms'}, + {text: 'date and time (s)', value: 'dts' }, + {text: 'from now (ms)', value: 'dtfromnowms' }, + {text: 'from now (s)', value: 'dtfromnows' }, + {text: 'duration (ms)', value: 'dtdurationms' }, + {text: 'duration (s)', value: 'dtdurations' } + ] + }, { text: 'data', submenu: [