[fieldChooser] fix the tests and fields on new hits

This commit is contained in:
Spencer Alger 2015-05-06 13:03:06 -07:00
parent ceafeff64c
commit 386e1b5867
3 changed files with 46 additions and 55 deletions

View file

@ -99,9 +99,8 @@ define(function (require) {
'[]columns',
'[]hits'
], function () {
var fields = getFields($scope.indexPattern, $scope.rows);
var columns = $scope.columns;
if (!fields || !columns) return;
var fields = getFields();
if (!fields || !$scope.columns) return;
$scope.fields = fields;
@ -207,13 +206,17 @@ define(function (require) {
}
};
function getFields(indexPattern, rows) {
if (!indexPattern || !rows || !$scope.fieldCounts) return;
function getFields() {
var prevFields = $scope.fields;
var indexPattern = $scope.indexPattern;
var hits = $scope.hits;
var fieldCounts = $scope.fieldCounts;
var ipFields = getIpFields(indexPattern);
var rowFieldNames = _.keys($scope.fieldCounts);
var unknownFieldNames = _.difference(rowFieldNames, ipFields.fieldNames);
if (!indexPattern || !hits || !fieldCounts) return;
var fieldNames = _.keys(fieldCounts);
var ipFieldNames = _.keys(indexPattern.fields.byName);
var unknownFieldNames = _.difference(fieldNames, ipFieldNames);
var unknownFields = unknownFieldNames.map(function (name) {
return new Field(indexPattern, {
name: name,
@ -221,28 +224,23 @@ define(function (require) {
});
});
return ipFields.concat(unknownFields).map(decorateField);
}
return [].concat(indexPattern.fields.raw, unknownFields).map(function (f) {
// clone the field with Object.create so that
// we can edit it without leaking our changes
// and so non-enumerable props/getters are
// preserved
var field = Object.create(f);
function getIpFields(indexPattern) {
var ipFields = indexPattern.fields.map(function (field) {
// clone the field with Object.create so that its getters
// and non-enumerable props are preserved
return Object.create(field);
field.displayOrder = _.indexOf($scope.columns, field.name) + 1;
field.display = !!field.displayOrder;
field.rowCount = $scope.fieldCounts[field.name];
var prev = _.find(prevFields, { name: field.name });
field.details = _.get(prev, 'details');
return field;
});
ipFields.fieldNames = _.pluck(ipFields, 'name');
return ipFields;
}
function decorateField(field) {
field.displayOrder = _.indexOf($scope.columns, field.name) + 1;
field.display = !!field.displayOrder;
field.rowCount = $scope.fieldCounts[field.name];
return field;
}
}
};
});

View file

@ -64,7 +64,7 @@
<disc-field-chooser
columns="state.columns"
refresh="refreshFieldList"
hits="hits"
hits="rows"
field-counts="fieldCounts"
filter="filterQuery"
index-pattern="searchSource.get('index')"

View file

@ -169,72 +169,65 @@ define(function (require) {
describe('details processing', function () {
var field;
function getField() { return _.find($scope.fields, { name: 'bytes' }); }
beforeEach(function () {
field = indexPattern.fields.byName.bytes;
field = getField();
});
afterEach(function () {
delete field.details;
});
it('should have a details function', function (done) {
it('should have a details function', function () {
expect($scope.details).to.be.a(Function);
done();
});
it('should increase the field popularity when called', function (done) {
it('should increase the field popularity when called', function () {
indexPattern.popularizeField = sinon.spy();
$scope.details(field);
expect(indexPattern.popularizeField.called).to.be(true);
done();
});
it('should append a details object to the field', function (done) {
it('should append a details object to the field', function () {
$scope.details(field);
expect(field.details).to.not.be(undefined);
done();
});
it('should delete the field details if they already exist', function (done) {
it('should delete the field details if they already exist', function () {
$scope.details(field);
expect(field.details).to.not.be(undefined);
$scope.details(field);
expect(field.details).to.be(undefined);
done();
});
it('... unless recompute is true', function (done) {
it('... unless recompute is true', function () {
$scope.details(field);
expect(field.details).to.not.be(undefined);
$scope.details(field, true);
expect(field.details).to.not.be(undefined);
done();
});
it('should create buckets with formatted and raw values', function (done) {
it('should create buckets with formatted and raw values', function () {
$scope.details(field);
expect(field.details.buckets).to.not.be(undefined);
expect(field.details.buckets[0].value).to.be(40.141592);
expect(field.details.buckets[0].display).to.be('40.142');
done();
});
it('should recalculate the details on open fields if the data changes', function () {
$scope.details(field);
sinon.stub($scope, 'details');
$scope.data = [];
it('should recalculate the details on open fields if the hits change', function () {
$scope.hits = [
{ _source: { bytes: 1024 } }
];
$scope.$apply();
expect($scope.details.called).to.be(true);
$scope.details.restore();
// close the field, make sure details isnt called again
field = getField();
$scope.details(field);
sinon.stub($scope, 'details');
$scope.data = ['foo'];
expect(getField().details.total).to.be(1);
$scope.hits = [
{ _source: { notbytes: 1024 } }
];
$scope.$apply();
expect($scope.details.called).to.be(false);
field = getField();
expect(field.details).to.not.have.property('total');
});
});
});