From 5640cd1e4ce22f97a5ee24beb1a8768032440fb1 Mon Sep 17 00:00:00 2001 From: spalger Date: Fri, 14 Aug 2015 17:29:44 -0700 Subject: [PATCH 1/3] [vislib/xAxis] dedupe xValues more carefully, allowing object keys --- src/ui/public/agg_types/buckets/range.js | 21 +++++++++++++++---- src/ui/public/chrome/chrome.js | 2 ++ .../zero_injection/ordered_x_keys.js | 7 ++++--- .../components/zero_injection/uniq_keys.js | 8 +++---- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/ui/public/agg_types/buckets/range.js b/src/ui/public/agg_types/buckets/range.js index 409e3fce009c..a2467cec6473 100644 --- a/src/ui/public/agg_types/buckets/range.js +++ b/src/ui/public/agg_types/buckets/range.js @@ -5,6 +5,7 @@ define(function (require) { var createFilter = Private(require('ui/agg_types/buckets/create_filter/range')); var FieldFormat = Private(require('ui/index_patterns/_field_format/FieldFormat')); + var keyCaches = new WeakMap(); return new BucketAggType({ name: 'range', @@ -14,12 +15,24 @@ define(function (require) { return aggConfig.params.field.displayName + ' ranges'; }, getKey: function (bucket, key, agg) { - let range = { gte: bucket.from, lt: bucket.to }; + var keys = keyCaches.get(agg); - if (range.gte == null) range.gte = -Infinity; - if (range.lt == null) range.lt = +Infinity; + if (!keys) { + keys = new Map(); + keyCaches.set(agg, keys); + } + + var id = `from:${bucket.from},to:${bucket.to}`; + var key = keys.get(id); + if (!key) { + keys.set(id, key = { + lt: bucket.to == null ? -Infinity : bucket.to, + gte: bucket.from == null ? +Infinity : bucket.from, + toString: () => id + }); + } - return range; + return key; }, getFormat: function (agg) { if (agg.$$rangeAggTypeFormat) return agg.$$rangeAggTypeFormat; diff --git a/src/ui/public/chrome/chrome.js b/src/ui/public/chrome/chrome.js index 9f58eb173ed8..f02f58b3918b 100644 --- a/src/ui/public/chrome/chrome.js +++ b/src/ui/public/chrome/chrome.js @@ -1,3 +1,5 @@ +require('babel/polyfill'); + var _ = require('lodash'); var $ = require('jquery'); var angular = require('angular'); diff --git a/src/ui/public/vislib/components/zero_injection/ordered_x_keys.js b/src/ui/public/vislib/components/zero_injection/ordered_x_keys.js index 3f19f3268ed7..0f93f272a61d 100644 --- a/src/ui/public/vislib/components/zero_injection/ordered_x_keys.js +++ b/src/ui/public/vislib/components/zero_injection/ordered_x_keys.js @@ -16,12 +16,13 @@ define(function (require) { throw new Error('OrderedXKeysUtilService expects an object'); } - var objKeys = getUniqKeys(obj); + var uniqKeys = getUniqKeys(obj); + var uniqKeysPairs = [...uniqKeys.entries()]; + var interval = _.get(obj, 'ordered.interval'); var dateInterval = moment.isDuration(interval) ? interval : false; - return _(objKeys) - .pairs() + return _(uniqKeysPairs) .sortBy(function (d) { if (d[1].isDate || d[1].isOrdered) { return +d[0]; diff --git a/src/ui/public/vislib/components/zero_injection/uniq_keys.js b/src/ui/public/vislib/components/zero_injection/uniq_keys.js index afdffcf2eef7..848cd6044827 100644 --- a/src/ui/public/vislib/components/zero_injection/uniq_keys.js +++ b/src/ui/public/vislib/components/zero_injection/uniq_keys.js @@ -17,7 +17,7 @@ define(function (require) { } var flattenedData = flattenDataArray(obj); - var uniqueXValues = {}; + var uniqueXValues = new Map(); var charts; if (!obj.series) { @@ -36,7 +36,7 @@ define(function (require) { flattenedData.forEach(function (d, i) { var key = d.x; - var prev = uniqueXValues[key]; + var prev = uniqueXValues.get(key); if (d.xi != null) { i = d.xi; @@ -46,12 +46,12 @@ define(function (require) { i = Math.min(i, prev.index); } - uniqueXValues[key] = { + uniqueXValues.set(key, { index: i, isDate: isDate, isOrdered: isOrdered, isNumber: _.isNumber(key) - }; + }); }); return uniqueXValues; From 44f7527e257c5e2427e146171f187bc11ec7f79b Mon Sep 17 00:00:00 2001 From: spalger Date: Fri, 14 Aug 2015 18:19:21 -0700 Subject: [PATCH 2/3] [agg/range] fix the signs on +/- infinity --- src/ui/public/agg_types/buckets/range.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ui/public/agg_types/buckets/range.js b/src/ui/public/agg_types/buckets/range.js index a2467cec6473..8b7e32df5988 100644 --- a/src/ui/public/agg_types/buckets/range.js +++ b/src/ui/public/agg_types/buckets/range.js @@ -26,8 +26,8 @@ define(function (require) { var key = keys.get(id); if (!key) { keys.set(id, key = { - lt: bucket.to == null ? -Infinity : bucket.to, - gte: bucket.from == null ? +Infinity : bucket.from, + gte: bucket.from == null ? -Infinity : bucket.from, + lt: bucket.to == null ? +Infinity : bucket.to, toString: () => id }); } From 30bd32f1563dd44e492ea8e942ea8b6d430156c2 Mon Sep 17 00:00:00 2001 From: spalger Date: Mon, 17 Aug 2015 18:59:07 -0700 Subject: [PATCH 3/3] [range] make RangeKey#toString non-enumerable --- src/ui/public/agg_types/buckets/RangeKey.js | 25 +++++++++++++++++++ .../agg_types/buckets/create_filter/range.js | 7 +++++- src/ui/public/agg_types/buckets/range.js | 24 ++++++++++-------- src/ui/public/filter_bar/lib/mapScript.js | 10 ++++++-- src/ui/public/filter_manager/lib/range.js | 7 ++++-- 5 files changed, 58 insertions(+), 15 deletions(-) create mode 100644 src/ui/public/agg_types/buckets/RangeKey.js diff --git a/src/ui/public/agg_types/buckets/RangeKey.js b/src/ui/public/agg_types/buckets/RangeKey.js new file mode 100644 index 000000000000..5917de5ce335 --- /dev/null +++ b/src/ui/public/agg_types/buckets/RangeKey.js @@ -0,0 +1,25 @@ +module.exports = function () { + + const id = Symbol('id'); + + class RangeKey { + constructor(bucket) { + this.gte = bucket.from == null ? -Infinity : bucket.from; + this.lt = bucket.to == null ? +Infinity : bucket.to; + + this[id] = RangeKey.idBucket(bucket); + } + + + static idBucket(bucket) { + return `from:${bucket.from},to:${bucket.to}`; + } + + toString() { + return this[id]; + } + } + + + return RangeKey; +}; diff --git a/src/ui/public/agg_types/buckets/create_filter/range.js b/src/ui/public/agg_types/buckets/create_filter/range.js index 7ac4ce305ad7..70af0a2c1909 100644 --- a/src/ui/public/agg_types/buckets/create_filter/range.js +++ b/src/ui/public/agg_types/buckets/create_filter/range.js @@ -2,7 +2,12 @@ define(function (require) { var buildRangeFilter = require('ui/filter_manager/lib/range'); return function createRangeFilterProvider(Private) { return function (aggConfig, key) { - return buildRangeFilter(aggConfig.params.field, key, aggConfig.vis.indexPattern); + return buildRangeFilter( + aggConfig.params.field, + key, + aggConfig.vis.indexPattern, + aggConfig.fieldFormatter()(key) + ); }; }; }); diff --git a/src/ui/public/agg_types/buckets/range.js b/src/ui/public/agg_types/buckets/range.js index 8b7e32df5988..8f5023f111c6 100644 --- a/src/ui/public/agg_types/buckets/range.js +++ b/src/ui/public/agg_types/buckets/range.js @@ -4,8 +4,10 @@ define(function (require) { var BucketAggType = Private(require('ui/agg_types/buckets/_bucket_agg_type')); var createFilter = Private(require('ui/agg_types/buckets/create_filter/range')); var FieldFormat = Private(require('ui/index_patterns/_field_format/FieldFormat')); + var RangeKey = Private(require('./RangeKey')); var keyCaches = new WeakMap(); + var formats = new WeakMap(); return new BucketAggType({ name: 'range', @@ -22,27 +24,29 @@ define(function (require) { keyCaches.set(agg, keys); } - var id = `from:${bucket.from},to:${bucket.to}`; + var id = RangeKey.idBucket(bucket); + var key = keys.get(id); if (!key) { - keys.set(id, key = { - gte: bucket.from == null ? -Infinity : bucket.from, - lt: bucket.to == null ? +Infinity : bucket.to, - toString: () => id - }); + key = new RangeKey(bucket); + keys.set(id, key); } return key; }, getFormat: function (agg) { - if (agg.$$rangeAggTypeFormat) return agg.$$rangeAggTypeFormat; + let format = formats.get(agg); + if (format) return format; - var RangeFormat = FieldFormat.from(function (range) { - var format = agg.fieldOwnFormatter(); + let RangeFormat = FieldFormat.from(function (range) { + let format = agg.fieldOwnFormatter(); return `${format(range.gte)} to ${format(range.lt)}`; }); - return (this.$$rangeAggTypeFormat = new RangeFormat()); + format = new RangeFormat(); + + formats.set(agg, format); + return format; }, params: [ { diff --git a/src/ui/public/filter_bar/lib/mapScript.js b/src/ui/public/filter_bar/lib/mapScript.js index d9da3a03285a..b25835b21cf4 100644 --- a/src/ui/public/filter_bar/lib/mapScript.js +++ b/src/ui/public/filter_bar/lib/mapScript.js @@ -10,8 +10,14 @@ define(function () { .get(filter.meta.index).then(function (indexPattern) { key = filter.meta.field; field = indexPattern.fields.byName[key]; - value = filter.script.params.value; - value = field.format.convert(value); + + if (filter.meta.formattedValue) { + value = filter.meta.formattedValue; + } else { + value = filter.script.params.value; + value = field.format.convert(value); + } + return { key: key, value: value }; }); } diff --git a/src/ui/public/filter_manager/lib/range.js b/src/ui/public/filter_manager/lib/range.js index 728ee81bb7b4..759518041e64 100644 --- a/src/ui/public/filter_manager/lib/range.js +++ b/src/ui/public/filter_manager/lib/range.js @@ -1,7 +1,10 @@ define(function (require) { var _ = require('lodash'); - return function buildRangeFilter(field, params, indexPattern) { - var filter = { meta: { index: indexPattern.id} }; + return function buildRangeFilter(field, params, indexPattern, formattedValue) { + var filter = { meta: { index: indexPattern.id } }; + if (formattedValue) filter.meta.formattedValue = formattedValue; + + params = _.clone(params); if (params.gte && params.gt) throw new Error('gte and gt are mutually exclusive'); if (params.lte && params.lt) throw new Error('lte and lt are mutually exclusive');