Skip to content

Commit

Permalink
Merge pull request #4677 from spalger/fix/4670
Browse files Browse the repository at this point in the history
dedupe x-axis values more carefully
  • Loading branch information
lukasolson committed Aug 18, 2015
2 parents 9bfa645 + 30bd32f commit c9e8895
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 20 deletions.
25 changes: 25 additions & 0 deletions src/ui/public/agg_types/buckets/RangeKey.js
Original file line number Diff line number Diff line change
@@ -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;
};
7 changes: 6 additions & 1 deletion src/ui/public/agg_types/buckets/create_filter/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
};
};
});
33 changes: 25 additions & 8 deletions src/ui/public/agg_types/buckets/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +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',
Expand All @@ -14,22 +17,36 @@ 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 = RangeKey.idBucket(bucket);

return range;
var key = keys.get(id);
if (!key) {
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: [
{
Expand Down
2 changes: 2 additions & 0 deletions src/ui/public/chrome/chrome.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require('babel/polyfill');

var _ = require('lodash');
var $ = require('jquery');
var angular = require('angular');
Expand Down
10 changes: 8 additions & 2 deletions src/ui/public/filter_bar/lib/mapScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
});
}
Expand Down
7 changes: 5 additions & 2 deletions src/ui/public/filter_manager/lib/range.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
8 changes: 4 additions & 4 deletions src/ui/public/vislib/components/zero_injection/uniq_keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ define(function (require) {
}

var flattenedData = flattenDataArray(obj);
var uniqueXValues = {};
var uniqueXValues = new Map();

var charts;
if (!obj.series) {
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit c9e8895

Please sign in to comment.