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..b32daff214983 100644 --- a/src/kibana/components/vis/_agg_configs.js +++ b/src/kibana/components/vis/_agg_configs.js @@ -6,16 +6,40 @@ 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. + if (vis && vis.type && vis.type.schemas && vis.type.schemas.all) { + _(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, def)); + }); + } + }); + } + } AggConfigs.prototype.toDsl = function () { @@ -52,4 +76,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..a93ec681e20a6 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: [ + { schema: 'metric', type: '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..05a7442c8097c 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 () { @@ -38,7 +40,7 @@ define(function (require) { }); var ac = new AggConfigs(vis); - expect(ac).to.have.length(0); + expect(ac).to.have.length(1); }); it('converts configStates into AggConfig objects if they are not already', function () { @@ -58,8 +60,60 @@ define(function (require) { }) ]); - expect(ac).to.have.length(2); - expect(SpiedAggConfig).to.have.property('callCount', 1); + expect(ac).to.have.length(3); + expect(SpiedAggConfig).to.have.property('callCount', 3); + }); + + describe('defaults', function () { + var vis; + beforeEach(function () { + vis = { + type: { + schemas: new Schemas([ + { + group: 'metrics', + name: 'metric', + title: 'Simple', + min: 1, + max: 2, + defaults: [ + { schema: 'metric', type: 'count' }, + { schema: 'metric', type: 'avg' }, + { schema: 'metric', type: 'sum' } + ] + }, + { + group: 'buckets', + name: 'segment', + title: 'Example', + min: 0, + max: 1, + defaults: [ + { schema: 'segment', type: 'terms' }, + { schema: 'segment', type: 'filters' } + ] + } + ]) + } + }; + }); + + 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'); + }); + }); }); @@ -140,7 +194,7 @@ define(function (require) { } }(vis.aggs.toDsl())); - expect(aggInfos).to.have.length(0); + expect(aggInfos).to.have.length(1); }); it('skips aggs that don\'t have a dsl representation', function () { @@ -190,4 +244,4 @@ define(function (require) { }); }); }]; -}); \ No newline at end of file +});