Commit 6d3a05a0 by Dominik Prokop Committed by GitHub

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)
parent 774b7267
......@@ -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', () => {
......
......@@ -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;
}
......
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