From ea5da627af111cc74c3c21a27233bdbe35cdf2fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 26 Jan 2015 11:49:18 +0100 Subject: [PATCH] Panel: Different time periods, panels can override dashboard relative time and/or add a time shift, #171, only works for graph panel for now, need feedback for how to do it for singlestat panel --- CHANGELOG.md | 1 + src/app/panels/graph/module.js | 17 +++++++++-------- src/test/specs/kbn-format-specs.js | 11 +++++++++++ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d56edad01a1..5d5721604c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ **New features** - [Issue #1331](https://github.com/grafana/grafana/issues/1331). Graph & Singlestat: New axis/unit format selector and more units (kbytes, Joule, Watt, eV), and new design for graph axis & grid tab and single stat options tab views - [Issue #1241](https://github.com/grafana/grafana/issues/1242). Timepicker: New option in timepicker (under dashboard settings), to change ``now`` to be for example ``now-1m``, usefull when you want to ignore last minute because it contains incomplete data +- [Issue #171](https://github.com/grafana/grafana/issues/171). Panel: Different time periods, panels can override dashboard relative time and/or add a time shift **Enhancements** - [Issue #1297](https://github.com/grafana/grafana/issues/1297). Graphite: Added cumulative and minimumBelow graphite functions diff --git a/src/app/panels/graph/module.js b/src/app/panels/graph/module.js index eac410360ad..56a15a7b72e 100644 --- a/src/app/panels/graph/module.js +++ b/src/app/panels/graph/module.js @@ -124,18 +124,19 @@ function (angular, app, $, _, kbn, moment, TimeSeries, PanelMeta) { // check panel time overrrides if ($scope.panel.timeFrom) { if (_.isString($scope.rangeUnparsed.from)) { - $scope.panelMeta.timeInfo = "Last " + $scope.panel.timeFrom; + $scope.panelMeta.timeInfo = "last " + $scope.panel.timeFrom; $scope.rangeUnparsed.from = 'now-' + $scope.panel.timeFrom; $scope.range.from = kbn.parseDate($scope.rangeUnparsed.from); } } - // if ($scope.panel.timeShift) { - // // from: now-1h - // // to: now - // // timeshift: 1d - // // from: now-1d-1h - // // to: now-1d - // } + + if ($scope.panel.timeShift) { + 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; + } if ($scope.panel.maxDataPoints) { $scope.resolution = $scope.panel.maxDataPoints; diff --git a/src/test/specs/kbn-format-specs.js b/src/test/specs/kbn-format-specs.js index 785d9376411..7c75e7eca93 100644 --- a/src/test/specs/kbn-format-specs.js +++ b/src/test/specs/kbn-format-specs.js @@ -69,4 +69,15 @@ define([ }); + describe('relative time to date parsing', function() { + it('should handle negative time', function() { + 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()); + }); + }); + });