diff --git a/public/app/core/utils/rangeutil.ts b/public/app/core/utils/rangeutil.ts index 7f2f2fd72b1..1e564ee7ea6 100644 --- a/public/app/core/utils/rangeutil.ts +++ b/public/app/core/utils/rangeutil.ts @@ -82,8 +82,9 @@ function formatDate(date) { // now/d // if no to then to now is assumed export function describeTextRange(expr: any) { + let isLast = (expr.indexOf('+') !== 0); if (expr.indexOf('now') === -1) { - expr = 'now-' + expr; + expr = (isLast ? 'now-' : 'now') + expr; } let opt = rangeIndex[expr + ' to now']; @@ -91,15 +92,20 @@ export function describeTextRange(expr: any) { return opt; } - opt = {from: expr, to: 'now'}; + if (isLast) { + opt = {from: expr, to: 'now'}; + } else { + opt = {from: 'now', to: expr}; + } - let parts = /^now-(\d+)(\w)/.exec(expr); + let parts = /^now([-+])(\d+)(\w)/.exec(expr); if (parts) { - let unit = parts[2]; - let amount = parseInt(parts[1]); + let unit = parts[3]; + let amount = parseInt(parts[2]); let span = spans[unit]; if (span) { - opt.display = 'Last ' + amount + ' ' + span.display; + opt.display = isLast ? 'Last ' : 'Next '; + opt.display += amount + ' ' + span.display; opt.section = span.section; if (amount > 1) { opt.display += 's'; diff --git a/public/test/core/utils/rangeutil_specs.ts b/public/test/core/utils/rangeutil_specs.ts index 36556fcbc4c..1deb708b1bc 100644 --- a/public/test/core/utils/rangeutil_specs.ts +++ b/public/test/core/utils/rangeutil_specs.ts @@ -31,6 +31,13 @@ describe("rangeUtil", () => { expect(info.from).to.be('now-13h') }); + it('should handle non default future amount', () => { + var info = rangeUtil.describeTextRange('+3h'); + expect(info.display).to.be('Next 3 hours') + expect(info.from).to.be('now') + expect(info.to).to.be('now+3h') + }); + it('should handle now/d', () => { var info = rangeUtil.describeTextRange('now/d'); expect(info.display).to.be('Today so far');