forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Closes elastic#2394 - Allow relative from and to * Closes elastic#6732 - Adding support future realtive for time picker
- Loading branch information
1 parent
0bd7858
commit c85f46d
Showing
7 changed files
with
333 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/ui/public/timepicker/__tests__/parse_relative_parts.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { parseRelativeString, parseRelativeParts } from '../parse_relative_parts'; | ||
import expect from 'expect.js'; | ||
import moment from 'moment'; | ||
|
||
describe('parseRelativeParts(from, to, relativeOptions)', () => { | ||
|
||
it('should parse relative string', () => { | ||
const results = parseRelativeString('now-2h'); | ||
expect(results).to.have.property('count', 2); | ||
expect(results).to.have.property('unit', 'h'); | ||
expect(results).to.have.property('round', false); | ||
}); | ||
|
||
it('should parse now', () => { | ||
const results = parseRelativeString('now'); | ||
expect(results).to.have.property('count', 0); | ||
expect(results).to.have.property('unit', 's'); | ||
expect(results).to.have.property('round', false); | ||
}); | ||
|
||
it('should parse set round options', () => { | ||
const results = parseRelativeString('now-2h/h'); | ||
expect(results).to.have.property('round', true); | ||
}); | ||
|
||
it('should parse now-2h to now-10m/m', () => { | ||
expect(parseRelativeParts('now-2h', 'now-10m/m')).to.eql({ | ||
from: { | ||
count: 2, | ||
unit: 'h', | ||
round: false | ||
}, | ||
to: { | ||
count: 10, | ||
unit: 'm', | ||
round: true | ||
} | ||
}); | ||
}); | ||
|
||
it('should parse now-2h to now+10m/m', () => { | ||
expect(parseRelativeParts('now-2h', 'now+10m/m')).to.eql({ | ||
from: { | ||
count: 2, | ||
unit: 'h', | ||
round: false | ||
}, | ||
to: { | ||
count: 10, | ||
unit: 'm+', | ||
round: true | ||
} | ||
}); | ||
}); | ||
|
||
it('should parse 3 months ago to now', () => { | ||
expect(parseRelativeParts(moment().subtract(3, 'M'), moment())).to.eql({ | ||
from: { | ||
count: 3, | ||
unit: 'M', | ||
round: false | ||
}, | ||
to: { | ||
count: 0, | ||
unit: 's', | ||
round: false | ||
} | ||
}); | ||
}); | ||
|
||
it('should parse 3 months ago to 15 minutes ago', () => { | ||
const from = moment().subtract(3, 'M'); | ||
const to = moment().subtract(15, 'm'); | ||
expect(parseRelativeParts(from, to)).to.eql({ | ||
from: { | ||
count: 3, | ||
unit: 'M', | ||
round: false | ||
}, | ||
to: { | ||
count: 15, | ||
unit: 'm', | ||
round: false | ||
} | ||
}); | ||
}); | ||
|
||
it('should parse 3 months ago to 2 hours from now', () => { | ||
const from = moment().subtract(3, 'M'); | ||
const to = moment().add(2, 'h'); | ||
expect(parseRelativeParts(from, to)).to.eql({ | ||
from: { | ||
count: 3, | ||
unit: 'M', | ||
round: false | ||
}, | ||
to: { | ||
count: 2, | ||
unit: 'h+', | ||
round: false | ||
} | ||
}); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import dateMath from '@elastic/datemath'; | ||
import moment from 'moment'; | ||
import _ from 'lodash'; | ||
import { relativeOptions } from './relative_options'; | ||
|
||
export function parseRelativeString(part) { | ||
let results = {}; | ||
const matches = _.isString(part) && part.match(/now(([\-\+])([0-9]+)([smhdwMy])(\/[smhdwMy])?)?/); | ||
|
||
const isNow = matches && !matches[1]; | ||
const opperator = matches && matches[2]; | ||
const count = matches && matches[3]; | ||
const unit = matches && matches[4]; | ||
const roundBy = matches && matches[5]; | ||
|
||
if (isNow) { | ||
return { count: 0, unit: 's', round: false }; | ||
} | ||
|
||
if (count && unit) { | ||
results.count = parseInt(count, 10); | ||
results.unit = unit; | ||
if (opperator === '+') results.unit += '+'; | ||
results.round = roundBy ? true : false; | ||
return results; | ||
|
||
} else { | ||
results = { count: 0, unit: 's', round: false }; | ||
const duration = moment.duration(moment().diff(dateMath.parse(part))); | ||
const units = _.pluck(_.clone(relativeOptions).reverse(), 'value') | ||
.filter(s => /^[smhdwMy]$/.test(s)); | ||
let unitOp = ''; | ||
for (let i = 0; i < units.length; i++) { | ||
const as = duration.as(units[i]); | ||
if (as < 0) unitOp = '+'; | ||
if (Math.abs(as) > 1) { | ||
results.count = Math.round(Math.abs(as)); | ||
results.unit = units[i] + unitOp; | ||
results.round = false; | ||
break; | ||
} | ||
} | ||
return results; | ||
} | ||
|
||
|
||
} | ||
|
||
export function parseRelativeParts(from, to) { | ||
const results = {}; | ||
results.from = parseRelativeString(from); | ||
results.to = parseRelativeString(to); | ||
if (results.from && results.to) return results; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export const relativeOptions = [ | ||
{ text: 'Seconds ago', value: 's' }, | ||
{ text: 'Minutes ago', value: 'm' }, | ||
{ text: 'Hours ago', value: 'h' }, | ||
{ text: 'Days ago', value: 'd' }, | ||
{ text: 'Weeks ago', value: 'w' }, | ||
{ text: 'Months ago', value: 'M' }, | ||
{ text: 'Years ago', value: 'y' }, | ||
|
||
{ text: 'Seconds from now', value: 's+' }, | ||
{ text: 'Minutes from now', value: 'm+' }, | ||
{ text: 'Hours from now', value: 'h+' }, | ||
{ text: 'Days from now', value: 'd+' }, | ||
{ text: 'Weeks from now', value: 'w+' }, | ||
{ text: 'Months from now', value: 'M+' }, | ||
{ text: 'Years from now', value: 'y+' }, | ||
|
||
]; |
Oops, something went wrong.