Adding #flattenWith method to IndexPattern

- Added a flattenWith method to IndexPattern class
- Added test for flattenWith so it only flattens as far as the index
  mapping
- Changed discover to use indexPattern.flattenWith instead of
  _.flattenWith
This commit is contained in:
Chris Cowan 2014-08-13 11:36:34 -07:00
parent be859ee0ac
commit a5ff8d7c0f
5 changed files with 77 additions and 3 deletions

View file

@ -74,6 +74,7 @@ define(function (require) {
var defaultFormat = courier.indexPatterns.fieldFormats.defaultByType.string;
var stateDefaults = {
query: initialQuery || '',
columns: ['_source'],
@ -304,7 +305,9 @@ define(function (require) {
if (sortFn && hit._formatted) return;
// Flatten the fields
hit._source = _.flattenWith('.', hit._source);
var indexPattern = $scope.searchSource.get('index');
hit._source = indexPattern.flattenWith('.', hit._source);
hit._formatted = _.mapValues(hit._source, function (value, name) {
// add up the counts for each field name

View file

@ -0,0 +1,24 @@
define(function (require) {
var _ = require('lodash');
return function (delimiter, nestedObj) {
var key; // original key
var stack = []; // track key stack
var flatObj = {};
var self = this;
(function flattenObj(obj) {
_.keys(obj).forEach(function (key) {
stack.push(key);
var flattenKey = stack.join(delimiter);
if (self.fieldsByName[flattenKey]) {
flatObj[flattenKey] = obj[key];
} else if (_.isObject(obj[key])) {
flattenObj(obj[key]);
}
stack.pop();
});
}(nestedObj));
return flatObj;
};
});

View file

@ -11,6 +11,7 @@ define(function (require) {
var intervals = Private(require('components/index_patterns/_intervals'));
var mappingSetup = Private(require('utils/mapping_setup'));
var DocSource = Private(require('components/courier/data_source/doc_source'));
var flattenWith = require('components/index_patterns/_flatten_with');
var type = 'index-pattern';
@ -171,7 +172,10 @@ define(function (require) {
pattern.toString = function () {
return '' + pattern.toJSON();
};
pattern.flattenWith = flattenWith.bind(pattern);
}
return IndexPattern;
};
});
});

View file

@ -78,7 +78,8 @@
'specs/state_management/state',
'specs/utils/diff_object',
'specs/utils/diff_time_picker_vals',
'specs/factories/events'
'specs/factories/events',
'specs/index_patterns/_flatten_with'
], function (kibana, sinon) {
kibana.load(function () {
var xhr = sinon.useFakeXMLHttpRequest();

View file

@ -0,0 +1,42 @@
define(function (require) {
var flattenWith = require('components/index_patterns/_flatten_with');
describe('IndexPattern#flattenWith()', function () {
var indexPattern = {
fieldsByName: {
'message': { type: 'string' },
'geo.coordinates': { type: 'geo_point' },
'geo.dest': { type: 'string' },
'geo.src': { type: 'string' },
'bytes': { type: 'number' },
'@timestamp': { type: 'date' }
}
};
indexPattern.flattenWith = flattenWith.bind(indexPattern);
var fixture = {
message: 'Hello World',
geo: {
coordinates: { lat: 33.4500, lon: 112.0667 },
dest: 'US',
src: 'IN'
},
bytes: 10039103,
'@timestamp': (new Date()).toString()
};
it('should only flatten keys as far as the mapping', function () {
var obj = indexPattern.flattenWith('.', fixture);
expect(obj).to.have.property('geo.coordinates', fixture.geo.coordinates);
expect(obj).to.not.have.property('geo.coordinates.lat');
expect(obj).to.not.have.property('geo.coordinates.lon');
expect(obj).to.have.property('geo.dest', 'US');
expect(obj).to.have.property('geo.src', 'IN');
expect(obj).to.have.property('@timestamp', fixture['@timestamp']);
expect(obj).to.have.property('message', 'Hello World');
expect(obj).to.have.property('bytes', 10039103);
});
});
});