Skip to content

Commit

Permalink
convert Dates to milliseconds
Browse files Browse the repository at this point in the history
  • Loading branch information
nreese committed Jun 23, 2017
1 parent 90ca50e commit f21f25a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
22 changes: 21 additions & 1 deletion src/ui/public/utils/__tests__/brush_event.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe('brushEvent', function () {
dateEvent.data.xAxisField = dateField;
}));

it('by creating a new filter', function () {
it('creates a new range filter', function () {
const event = _.cloneDeep(dateEvent);
const rangeBegin = JAN_01_2014;
const rangeEnd = rangeBegin + DAY_IN_MS;
Expand All @@ -121,6 +121,26 @@ describe('brushEvent', function () {
expect($state.$newFilters[0].range.anotherTimeField.lt)
.to.equal(rangeEnd);
});

it('converts Date fields to milliseconds', function () {
const event = _.cloneDeep(dateEvent);
const rangeBeginMs = JAN_01_2014;
const rangeEndMs = rangeBeginMs + DAY_IN_MS;
const rangeBegin = new Date(rangeBeginMs);
const rangeEnd = new Date(rangeEndMs);
event.range = [rangeBegin, rangeEnd];
brushEvent(event);
expect($state)
.to.have.property('$newFilters');
expect($state.filters.length)
.to.equal(0);
expect($state.$newFilters.length)
.to.equal(1);
expect($state.$newFilters[0].range.anotherTimeField.gte)
.to.equal(rangeBeginMs);
expect($state.$newFilters[0].range.anotherTimeField.lt)
.to.equal(rangeEndMs);
});
});
});

Expand Down
11 changes: 9 additions & 2 deletions src/ui/public/utils/brush_event.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,15 @@ export function UtilsBrushEventProvider(timefilter) {
filter.meta && filter.meta.key === event.data.xAxisField.name
));

const min = event.range[0];
const max = event.range[event.range.length - 1];
let min = event.range[0];
let max = event.range[event.range.length - 1];
// Convert Dates to MS to avoid ES "parse date field" errors
if (min instanceof Date) {
min = min.getTime();
}
if (max instanceof Date) {
max = max.getTime();
}
const range = { gte: min, lt: max };
if (_.has(existingFilter, 'range')) {
existingFilter.range[event.data.xAxisField.name] = range;
Expand Down

0 comments on commit f21f25a

Please sign in to comment.