Procházet zdrojové kódy

TimeSrv: Enable value time windowing in TimeSrv (#18636)

This introduces change to TimeSrv that enables time & time.window query param. Thanks to that time range of a dashboard can be specified as a window(time.window param) around given timestamp(time param)
Dominik Prokop před 6 roky
rodič
revize
6d3a05a02b

+ 33 - 0
public/app/features/dashboard/services/TimeSrv.test.ts

@@ -144,6 +144,39 @@ describe('timeSrv', () => {
       expect(timeSrv.time.from).toEqual('now-6h');
       expect(timeSrv.time.from).toEqual('now-6h');
       expect(timeSrv.time.to).toEqual('now');
       expect(timeSrv.time.to).toEqual('now');
     });
     });
+
+    describe('data point windowing', () => {
+      it('handles time window specfied as interval string', () => {
+        location = {
+          search: jest.fn(() => ({
+            time: '1410337645000',
+            'time.window': '10s',
+          })),
+        };
+
+        timeSrv = new TimeSrv(rootScope as any, jest.fn() as any, location as any, timer, new ContextSrvStub() as any);
+
+        timeSrv.init(_dashboard);
+        const time = timeSrv.timeRange();
+        expect(time.from.valueOf()).toEqual(1410337640000);
+        expect(time.to.valueOf()).toEqual(1410337650000);
+      });
+      it('handles time window specified in ms', () => {
+        location = {
+          search: jest.fn(() => ({
+            time: '1410337645000',
+            'time.window': '10000',
+          })),
+        };
+
+        timeSrv = new TimeSrv(rootScope as any, jest.fn() as any, location as any, timer, new ContextSrvStub() as any);
+
+        timeSrv.init(_dashboard);
+        const time = timeSrv.timeRange();
+        expect(time.from.valueOf()).toEqual(1410337640000);
+        expect(time.to.valueOf()).toEqual(1410337650000);
+      });
+    });
   });
   });
 
 
   describe('setTime', () => {
   describe('setTime', () => {

+ 22 - 0
public/app/features/dashboard/services/TimeSrv.ts

@@ -93,8 +93,30 @@ export class TimeSrv {
     return null;
     return null;
   }
   }
 
 
+  private getTimeWindow(time: string, timeWindow: string) {
+    const valueTime = parseInt(time, 10);
+    let timeWindowMs;
+
+    if (timeWindow.match(/^\d+$/) && parseInt(timeWindow, 10)) {
+      // when time window specified in ms
+      timeWindowMs = parseInt(timeWindow, 10);
+    } else {
+      timeWindowMs = kbn.interval_to_ms(timeWindow);
+    }
+
+    return {
+      from: toUtc(valueTime - timeWindowMs / 2),
+      to: toUtc(valueTime + timeWindowMs / 2),
+    };
+  }
+
   private initTimeFromUrl() {
   private initTimeFromUrl() {
     const params = this.$location.search();
     const params = this.$location.search();
+
+    if (params.time && params['time.window']) {
+      this.time = this.getTimeWindow(params.time, params['time.window']);
+    }
+
     if (params.from) {
     if (params.from) {
       this.time.from = this.parseUrlParam(params.from) || this.time.from;
       this.time.from = this.parseUrlParam(params.from) || this.time.from;
     }
     }