[TSVB] Allow negative time offsets (#14494) (#14563)

* Allow negative offsets in TSVB

* Fix linting issue
This commit is contained in:
Tim Roes 2017-10-25 09:28:35 +02:00 committed by GitHub
parent 475c3bf84c
commit 12cac055c3
2 changed files with 34 additions and 3 deletions

View file

@ -39,5 +39,36 @@ describe('offsetTime(req, by)', () => {
.isSame(to)).to.equal(true);
});
});
it('should return a moment object for to and from offset by -2 minute', () => {
const req = {
payload: {
timerange: {
min: '2017-01-10T01:00:00Z',
max: '2017-01-10T02:00:00Z'
}
}
};
const { from, to } = offsetTime(req, '-2m');
expect(moment.isMoment(from)).to.equal(true);
expect(moment.isMoment(to)).to.equal(true);
expect(moment.utc('2017-01-10T01:02:00Z').isSame(from)).to.equal(true);
expect(moment.utc('2017-01-10T02:02:00Z').isSame(to)).to.equal(true);
});
it('should work when prefixing positive offsets with the plus sign', () => {
const req = {
payload: {
timerange: {
min: '2017-01-10T01:00:00Z',
max: '2017-01-10T02:00:00Z'
}
}
};
const { from: fromSigned, to: toSigned } = offsetTime(req, '+1m');
const { from, to } = offsetTime(req, '1m');
expect(fromSigned.isSame(from)).to.equal(true);
expect(toSigned.isSame(to)).to.equal(true);
});
});

View file

@ -1,8 +1,8 @@
import getTimerange from './helpers/get_timerange';
export default function offsetTime(req, by) {
const { from, to } = getTimerange(req);
if (!/^([\d]+)([shmdwMy]|ms)$/.test(by)) return { from, to };
const matches = by.match(/^([\d]+)([shmdwMy]|ms)$/);
if (!/^[+-]?([\d]+)([shmdwMy]|ms)$/.test(by)) return { from, to };
const matches = by.match(/^([+-]?[\d]+)([shmdwMy]|ms)$/);
const offsetValue = Number(matches[1]);
const offsetUnit = matches[2];
return {