Closes #263 - Adding the ability to add defaults to the visualization schemas

This commit is contained in:
Chris Cowan 2014-09-10 12:26:12 -07:00
parent 3fef72ac21
commit 0e4270a53b
4 changed files with 81 additions and 6 deletions

View file

@ -77,4 +77,4 @@ define(function (require) {
return AggParams;
};
});
});

View file

@ -6,16 +6,41 @@ define(function (require) {
_(AggConfigs).inherits(Registry);
function AggConfigs(vis, configStates) {
var self = this;
this.vis = vis;
AggConfigs.Super.call(this, {
index: ['id'],
group: ['schema.group', 'type.name'],
group: ['schema.group', 'type.name', 'schema.name'],
initialSet: (configStates || []).map(function (aggConfigState) {
if (aggConfigState instanceof AggConfig) return aggConfigState;
return new AggConfig(vis, aggConfigState);
})
});
// Set the defaults for any schema which has them. If the defaults
// for some reason has more then the max only set the max number
// of defaults (not sure why a someone define more...
// but whatever). Also if a schema.name is already set then don't
// set anything.
_(vis.type.schemas.all)
.filter(function (schema) {
return _.isArray(schema.defaults) && schema.defaults.length > 0;
})
.each(function (schema) {
if (!self.bySchemaName[schema.name]) {
var defaults = schema.defaults.slice(0, schema.max);
_.each(defaults, function (def) {
self.push(new AggConfig(vis, {
schema: schema.name,
type: def
}));
});
}
});
}
AggConfigs.prototype.toDsl = function () {
@ -52,4 +77,4 @@ define(function (require) {
return AggConfigs;
};
});
});

View file

@ -17,7 +17,10 @@ define(function (require) {
name: 'metric',
title: 'Y-Axis',
min: 1,
max: 1
max: 1,
defaults: [
'count'
]
},
{
group: 'buckets',
@ -43,4 +46,4 @@ define(function (require) {
])
});
};
});
});

View file

@ -9,6 +9,7 @@ define(function (require) {
var AggConfigs;
var SpiedAggConfig;
var indexPattern;
var Schemas;
beforeEach(module('kibana'));
beforeEach(inject(function (Private) {
@ -23,6 +24,7 @@ define(function (require) {
AggConfigs = Private(require('components/vis/_agg_configs'));
Registry = require('utils/registry/registry');
indexPattern = Private(require('fixtures/stubbed_logstash_index_pattern'));
Schemas = Private(require('components/vis_types/_schemas'));
}));
it('extends Registry', function () {
@ -61,6 +63,51 @@ define(function (require) {
expect(ac).to.have.length(2);
expect(SpiedAggConfig).to.have.property('callCount', 1);
});
describe('defaults', function () {
var vis;
beforeEach(function () {
vis = {
type: {
schemas: new Schemas([
{
group: 'metrics',
name: 'metric',
title: 'Simple',
min: 1,
max: 2,
defaults: [ 'count', 'avg', 'sum' ]
},
{
group: 'buckets',
name: 'segment',
title: 'Example',
min: 0,
max: 1,
defaults: [ 'terms', 'fitlers' ]
}
])
}
};
});
it('should only set the number of defaults defined by the max', function () {
var ac = new AggConfigs(vis);
expect(ac.bySchemaName['metric']).to.have.length(2);
});
it('should set the defaults defined in the schema when none exist', function () {
var ac = new AggConfigs(vis);
expect(ac).to.have.length(3);
});
it('should NOT set the defaults defined in the schema when some exist', function () {
var ac = new AggConfigs(vis, [{ schema: 'segment', type: 'date_histogram' }]);
expect(ac).to.have.length(3);
expect(ac.bySchemaName['segment'][0].type.name).to.equal('date_histogram');
});
});
});
describe('#getSorted', function () {
@ -190,4 +237,4 @@ define(function (require) {
});
});
}];
});
});