Browse Source

fix(timerange): fix handling of invalid dates in from/to url parameters, fixes #3345

Torkel Ödegaard 10 năm trước cách đây
mục cha
commit
0b4552a8e7

+ 4 - 2
public/app/features/dashboard/timeSrv.js

@@ -21,6 +21,7 @@ define([
 
 
       this._initTimeFromUrl();
       this._initTimeFromUrl();
       this._parseTime();
       this._parseTime();
+      console.log(dashboard.time);
 
 
       if(this.dashboard.refresh) {
       if(this.dashboard.refresh) {
         this.setAutoRefresh(this.dashboard.refresh);
         this.setAutoRefresh(this.dashboard.refresh);
@@ -47,8 +48,9 @@ define([
       if (value.length === 15) {
       if (value.length === 15) {
         return moment.utc(value, 'YYYYMMDDTHHmmss');
         return moment.utc(value, 'YYYYMMDDTHHmmss');
       }
       }
-      var epoch = parseInt(value);
-      if (!_.isNaN(epoch)) {
+
+      if (!isNaN(value)) {
+        var epoch = parseInt(value);
         return moment(epoch);
         return moment(epoch);
       }
       }
 
 

+ 1 - 1
public/test/mocks/dashboard-mock.js

@@ -15,7 +15,7 @@ define([],
         rows: [],
         rows: [],
         pulldowns: [ { type: 'templating' },  { type: 'annotations' } ],
         pulldowns: [ { type: 'templating' },  { type: 'annotations' } ],
         nav: [ { type: 'timepicker' } ],
         nav: [ { type: 'timepicker' } ],
-        time: {from: '1h', to: 'now'},
+        time: {from: 'now-6h', to: 'now'},
         templating: {
         templating: {
           list: []
           list: []
         },
         },

+ 8 - 0
public/test/specs/time_srv_specs.js

@@ -75,6 +75,14 @@ define([
         expect(time.to.valueOf()).to.equal(1410337665699);
         expect(time.to.valueOf()).to.equal(1410337665699);
       });
       });
 
 
+      it('should handle bad dates', function() {
+        ctx.$routeParams.from = '20151126T00010%3C%2Fp%3E%3Cspan%20class';
+        ctx.$routeParams.to = 'now';
+        _dashboard.time.from = 'now-6h';
+        ctx.service.init(_dashboard);
+        expect(ctx.service.time.from).to.equal('now-6h');
+        expect(ctx.service.time.to).to.equal('now');
+      });
     });
     });
 
 
     describe('setTime', function() {
     describe('setTime', function() {