diff --git a/src/kibana/components/agg_types/_agg_params.js b/src/kibana/components/agg_types/_agg_params.js index f5ec725f6ebb9..2965e0848c619 100644 --- a/src/kibana/components/agg_types/_agg_params.js +++ b/src/kibana/components/agg_types/_agg_params.js @@ -77,4 +77,4 @@ define(function (require) { return AggParams; }; -}); \ No newline at end of file +}); diff --git a/src/kibana/components/vis/_agg_configs.js b/src/kibana/components/vis/_agg_configs.js index 9b3a1cf430ab5..9550e00c19411 100644 --- a/src/kibana/components/vis/_agg_configs.js +++ b/src/kibana/components/vis/_agg_configs.js @@ -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; }; -}); \ No newline at end of file +}); diff --git a/src/kibana/components/vis_types/histogram.js b/src/kibana/components/vis_types/histogram.js index d6c1ab6493062..34f0182de4431 100644 --- a/src/kibana/components/vis_types/histogram.js +++ b/src/kibana/components/vis_types/histogram.js @@ -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) { ]) }); }; -}); \ No newline at end of file +}); diff --git a/test/unit/specs/components/vis/_agg_configs.js b/test/unit/specs/components/vis/_agg_configs.js index c8d98ad7a2ba0..b1940419afe53 100644 --- a/test/unit/specs/components/vis/_agg_configs.js +++ b/test/unit/specs/components/vis/_agg_configs.js @@ -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) { }); }); }]; -}); \ No newline at end of file +});