Skip to content

Commit ab0155c

Browse files
Issue grafana#2971 - add time units for use in singlestat panel.
1 parent 3b1ae3f commit ab0155c

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

public/app/core/utils/kbn.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
define([
22
'jquery',
33
'lodash',
4+
'moment'
45
],
5-
function($, _) {
6+
function($, _, moment) {
67
'use strict';
78

89
var kbn = {};
@@ -580,6 +581,45 @@ function($, _) {
580581
}
581582
};
582583

584+
// Date and time
585+
kbn.toDateTime = function(size, timeScale) {
586+
var datetime;
587+
if (timeScale === 's') {
588+
datetime = moment.unix(size);
589+
} else {
590+
datetime = moment(size);
591+
}
592+
return datetime;
593+
};
594+
595+
kbn.toDuration = function(size, timeScale) {
596+
return moment.duration(size, timeScale);
597+
};
598+
599+
kbn.valueFormats.dtms = function(size) {
600+
return kbn.toDateTime(size, 'ms').format('YYYY-MM-DD hh:mm:ss');
601+
};
602+
603+
kbn.valueFormats.dts = function(size) {
604+
return kbn.toDateTime(size, 's').format('YYYY-MM-DD hh:mm:ss');
605+
};
606+
607+
kbn.valueFormats.dtfromnowms = function(size) {
608+
return kbn.toDateTime(size, 'ms').fromNow(true);
609+
};
610+
611+
kbn.valueFormats.dtfromnows = function(size) {
612+
return kbn.toDateTime(size, 's').fromNow(true);
613+
};
614+
615+
kbn.valueFormats.dtdurationms = function(size) {
616+
return kbn.toDuration(size, 'ms').humanize();
617+
};
618+
619+
kbn.valueFormats.dtdurations = function(size) {
620+
return kbn.toDuration(size, 's').humanize();
621+
};
622+
583623
///// FORMAT MENU /////
584624

585625
kbn.getUnitFormats = function() {
@@ -618,6 +658,17 @@ function($, _) {
618658
{text: 'days (d)', value: 'd' },
619659
]
620660
},
661+
{
662+
text: 'date and time',
663+
submenu: [
664+
{text: 'date and time (ms)', value: 'dtms'},
665+
{text: 'date and time (s)', value: 'dts' },
666+
{text: 'from now (ms)', value: 'dtfromnowms' },
667+
{text: 'from now (s)', value: 'dtfromnows' },
668+
{text: 'duration (ms)', value: 'dtdurationms' },
669+
{text: 'duration (s)', value: 'dtdurations' }
670+
]
671+
},
621672
{
622673
text: 'data',
623674
submenu: [

0 commit comments

Comments
 (0)