Commit ad13fd05 by Torkel Ödegaard

Added validation for correct timespans when using panel timeshift or relative…

Added validation for correct timespans when using panel timeshift or relative time override, Fixes #1494
parent 26eb6e55
......@@ -197,6 +197,11 @@ function($, _, moment) {
return kbn.parseDateMath(mathString, time);
};
kbn._timespanRegex = /^\d+[h,m,M,w,s,H,d]$/;
kbn.isValidTimeSpan = function(str) {
return kbn._timespanRegex.test(str);
};
kbn.parseDateMath = function(mathString, time, roundUp) {
var dateTime = moment(time);
for (var i = 0; i < mathString.length;) {
......
......@@ -123,14 +123,16 @@ function (angular, app, $, _, kbn, moment, TimeSeries, PanelMeta) {
$scope.render();
};
$scope.updateTimeRange = function () {
$scope.range = timeSrv.timeRange();
$scope.rangeUnparsed = timeSrv.timeRange(false);
$scope.panelMeta.timeInfo = "";
$scope.applyPanelTimeOverrides = function() {
$scope.panelMeta.timeInfo = '';
// check panel time overrrides
if ($scope.panel.timeFrom) {
if (!kbn.isValidTimeSpan($scope.panel.timeFrom)) {
$scope.panelMeta.timeInfo = 'invalid time override';
return;
}
if (_.isString($scope.rangeUnparsed.from)) {
$scope.panelMeta.timeInfo = "last " + $scope.panel.timeFrom;
$scope.rangeUnparsed.from = 'now-' + $scope.panel.timeFrom;
......@@ -139,12 +141,24 @@ function (angular, app, $, _, kbn, moment, TimeSeries, PanelMeta) {
}
if ($scope.panel.timeShift) {
if (!kbn.isValidTimeSpan($scope.panel.timeFrom)) {
$scope.panelMeta.timeInfo = 'invalid timeshift';
return;
}
var timeShift = '-' + $scope.panel.timeShift;
$scope.panelMeta.timeInfo += ' timeshift ' + timeShift;
$scope.range.from = kbn.parseDateMath(timeShift, $scope.range.from);
$scope.range.to = kbn.parseDateMath(timeShift, $scope.range.to);
$scope.rangeUnparsed = $scope.range;
}
};
$scope.updateTimeRange = function () {
$scope.range = timeSrv.timeRange();
$scope.rangeUnparsed = timeSrv.timeRange(false);
$scope.applyPanelTimeOverrides();
if ($scope.panel.maxDataPoints) {
$scope.resolution = $scope.panel.maxDataPoints;
......
......@@ -74,10 +74,17 @@ define([
var date = kbn.parseDateMath('-2d', new Date(2014,1,5));
expect(date.getTime()).to.equal(new Date(2014, 1, 3).getTime());
});
it('should handle multiple math expressions', function() {
var date = kbn.parseDateMath('-2d-6h', new Date(2014, 1, 5));
expect(date.toString()).to.equal(new Date(2014, 1, 2, 18).toString());
});
it('should return false when invalid expression', function() {
var date = kbn.parseDateMath('2', new Date(2014, 1, 5));
expect(date).to.equal(false);
});
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment