feat(relative time override): You can now use the new relative time option in panel time override, closes #2575

This commit is contained in:
Torkel Ödegaard 2015-08-21 16:19:51 +02:00
parent 6d3b36d61b
commit 292db86c9e
3 changed files with 42 additions and 36 deletions

View File

@ -163,6 +163,20 @@ function($, _, moment) {
return info.sec * info.count; return info.sec * info.count;
}; };
kbn.getRelativeTimeInfo = function(str) {
var info = {value: str};
if (str === 'today') {
info.text = 'Today';
info.from = 'today';
info.to = 'now';
} else {
info.text = 'Last ' + str;
info.from = 'now-'+str;
info.to = 'now';
}
return info;
};
/* This is a simplified version of elasticsearch's date parser */ /* This is a simplified version of elasticsearch's date parser */
kbn.parseDate = function(text) { kbn.parseDate = function(text) {
if(_.isDate(text)) { if(_.isDate(text)) {
@ -205,7 +219,7 @@ function($, _, moment) {
return kbn.parseDateMath(mathString, time); return kbn.parseDateMath(mathString, time);
}; };
kbn._timespanRegex = /^\d+[h,m,M,w,s,H,d]$/; kbn._timespanRegex = /^(\d+[h,m,M,w,s,H,d])|today$/;
kbn.isValidTimeSpan = function(str) { kbn.isValidTimeSpan = function(str) {
return kbn._timespanRegex.test(str); return kbn._timespanRegex.test(str);
}; };
@ -215,8 +229,8 @@ function($, _, moment) {
for (var i = 0; i < mathString.length;) { for (var i = 0; i < mathString.length;) {
var c = mathString.charAt(i++), var c = mathString.charAt(i++),
type, type,
num, num,
unit; unit;
if (c === '/') { if (c === '/') {
type = 0; type = 0;
} else if (c === '+') { } else if (c === '+') {
@ -244,8 +258,8 @@ function($, _, moment) {
} }
unit = mathString.charAt(i++); unit = mathString.charAt(i++);
switch (unit) { switch (unit) {
case 'y': case 'y':
if (type === 0) { if (type === 0) {
roundUp ? dateTime.endOf('year') : dateTime.startOf('year'); roundUp ? dateTime.endOf('year') : dateTime.startOf('year');
} else if (type === 1) { } else if (type === 1) {
dateTime.add(num, 'years'); dateTime.add(num, 'years');
@ -253,8 +267,8 @@ function($, _, moment) {
dateTime.subtract(num, 'years'); dateTime.subtract(num, 'years');
} }
break; break;
case 'M': case 'M':
if (type === 0) { if (type === 0) {
roundUp ? dateTime.endOf('month') : dateTime.startOf('month'); roundUp ? dateTime.endOf('month') : dateTime.startOf('month');
} else if (type === 1) { } else if (type === 1) {
dateTime.add(num, 'months'); dateTime.add(num, 'months');
@ -262,8 +276,8 @@ function($, _, moment) {
dateTime.subtract(num, 'months'); dateTime.subtract(num, 'months');
} }
break; break;
case 'w': case 'w':
if (type === 0) { if (type === 0) {
roundUp ? dateTime.endOf('week') : dateTime.startOf('week'); roundUp ? dateTime.endOf('week') : dateTime.startOf('week');
} else if (type === 1) { } else if (type === 1) {
dateTime.add(num, 'weeks'); dateTime.add(num, 'weeks');
@ -271,8 +285,8 @@ function($, _, moment) {
dateTime.subtract(num, 'weeks'); dateTime.subtract(num, 'weeks');
} }
break; break;
case 'd': case 'd':
if (type === 0) { if (type === 0) {
roundUp ? dateTime.endOf('day') : dateTime.startOf('day'); roundUp ? dateTime.endOf('day') : dateTime.startOf('day');
} else if (type === 1) { } else if (type === 1) {
dateTime.add(num, 'days'); dateTime.add(num, 'days');
@ -280,9 +294,9 @@ function($, _, moment) {
dateTime.subtract(num, 'days'); dateTime.subtract(num, 'days');
} }
break; break;
case 'h': case 'h':
case 'H': case 'H':
if (type === 0) { if (type === 0) {
roundUp ? dateTime.endOf('hour') : dateTime.startOf('hour'); roundUp ? dateTime.endOf('hour') : dateTime.startOf('hour');
} else if (type === 1) { } else if (type === 1) {
dateTime.add(num, 'hours'); dateTime.add(num, 'hours');
@ -290,8 +304,8 @@ function($, _, moment) {
dateTime.subtract(num,'hours'); dateTime.subtract(num,'hours');
} }
break; break;
case 'm': case 'm':
if (type === 0) { if (type === 0) {
roundUp ? dateTime.endOf('minute') : dateTime.startOf('minute'); roundUp ? dateTime.endOf('minute') : dateTime.startOf('minute');
} else if (type === 1) { } else if (type === 1) {
dateTime.add(num, 'minutes'); dateTime.add(num, 'minutes');
@ -299,8 +313,8 @@ function($, _, moment) {
dateTime.subtract(num, 'minutes'); dateTime.subtract(num, 'minutes');
} }
break; break;
case 's': case 's':
if (type === 0) { if (type === 0) {
roundUp ? dateTime.endOf('second') : dateTime.startOf('second'); roundUp ? dateTime.endOf('second') : dateTime.startOf('second');
} else if (type === 1) { } else if (type === 1) {
dateTime.add(num, 'seconds'); dateTime.add(num, 'seconds');
@ -308,8 +322,8 @@ function($, _, moment) {
dateTime.subtract(num, 'seconds'); dateTime.subtract(num, 'seconds');
} }
break; break;
default: default:
return false; return false;
} }
} }
return dateTime.toDate(); return dateTime.toDate();
@ -509,9 +523,9 @@ function($, _, moment) {
kbn.slugifyForUrl = function(str) { kbn.slugifyForUrl = function(str) {
return str return str
.toLowerCase() .toLowerCase()
.replace(/[^\w ]+/g,'') .replace(/[^\w ]+/g,'')
.replace(/ +/g,'-'); .replace(/ +/g,'-');
}; };
kbn.exportSeriesListToCsv = function(seriesList) { kbn.exportSeriesListToCsv = function(seriesList) {

View File

@ -69,8 +69,10 @@ function (angular, _, kbn, $) {
} }
if (_.isString(scope.rangeUnparsed.from)) { if (_.isString(scope.rangeUnparsed.from)) {
scope.panelMeta.timeInfo = "last " + scope.panel.timeFrom; var timeInfo = kbn.getRelativeTimeInfo(scope.panel.timeFrom)
scope.rangeUnparsed.from = 'now-' + scope.panel.timeFrom; scope.panelMeta.timeInfo = timeInfo.text;
scope.rangeUnparsed.from = timeInfo.from;
scope.rangeUnparsed.to = timeInfo.to;
scope.range.from = kbn.parseDate(scope.rangeUnparsed.from); scope.range.from = kbn.parseDate(scope.rangeUnparsed.from);
} }
} }

View File

@ -74,17 +74,7 @@ function (angular, app, _, moment, kbn) {
$scope.loadTimeOptions = function() { $scope.loadTimeOptions = function() {
$scope.time_options = _.map($scope.panel.time_options, function(str) { $scope.time_options = _.map($scope.panel.time_options, function(str) {
var option = {value: str}; return kbn.getRelativeTimeInfo(str);
if (str === 'today') {
option.text = 'Today';
option.from = 'today';
option.to = 'now';
} else {
option.text = 'Last ' + str;
option.from = 'now-'+str;
option.to = 'now';
}
return option;
}); });
$scope.refreshMenuLeftSide = $scope.time.rangeString.length < 10; $scope.refreshMenuLeftSide = $scope.time.rangeString.length < 10;