diff --git a/public/app/features/dashboard/services/TimeSrv.test.ts b/public/app/features/dashboard/services/TimeSrv.test.ts index 910e85506ed..2e1dacca53e 100644 --- a/public/app/features/dashboard/services/TimeSrv.test.ts +++ b/public/app/features/dashboard/services/TimeSrv.test.ts @@ -144,6 +144,39 @@ describe('timeSrv', () => { expect(timeSrv.time.from).toEqual('now-6h'); 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', () => { diff --git a/public/app/features/dashboard/services/TimeSrv.ts b/public/app/features/dashboard/services/TimeSrv.ts index bf640721de1..7d8fcaed7df 100644 --- a/public/app/features/dashboard/services/TimeSrv.ts +++ b/public/app/features/dashboard/services/TimeSrv.ts @@ -93,8 +93,30 @@ export class TimeSrv { 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() { const params = this.$location.search(); + + if (params.time && params['time.window']) { + this.time = this.getTimeWindow(params.time, params['time.window']); + } + if (params.from) { this.time.from = this.parseUrlParam(params.from) || this.time.from; }