From f21f25a4eb21dd5943a73fc647beb2d0cd80cdd7 Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Fri, 23 Jun 2017 13:20:46 -0600 Subject: [PATCH] convert Dates to milliseconds --- src/ui/public/utils/__tests__/brush_event.js | 22 +++++++++++++++++++- src/ui/public/utils/brush_event.js | 11 ++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/ui/public/utils/__tests__/brush_event.js b/src/ui/public/utils/__tests__/brush_event.js index dcd9f29c3c38b..6006b23d51617 100644 --- a/src/ui/public/utils/__tests__/brush_event.js +++ b/src/ui/public/utils/__tests__/brush_event.js @@ -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; @@ -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); + }); }); }); diff --git a/src/ui/public/utils/brush_event.js b/src/ui/public/utils/brush_event.js index 09691e9812d8d..682c0c83d08f1 100644 --- a/src/ui/public/utils/brush_event.js +++ b/src/ui/public/utils/brush_event.js @@ -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;