Skip to content

Commit

Permalink
Closes #263 - Adding the ability to add defaults to the visualization…
Browse files Browse the repository at this point in the history
… schemas
  • Loading branch information
simianhacker committed Sep 10, 2014
1 parent 3fef72a commit 0e4270a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/kibana/components/agg_types/_agg_params.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ define(function (require) {

return AggParams;
};
});
});
29 changes: 27 additions & 2 deletions src/kibana/components/vis/_agg_configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -52,4 +77,4 @@ define(function (require) {

return AggConfigs;
};
});
});
7 changes: 5 additions & 2 deletions src/kibana/components/vis_types/histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ define(function (require) {
name: 'metric',
title: 'Y-Axis',
min: 1,
max: 1
max: 1,
defaults: [
'count'
]
},
{
group: 'buckets',
Expand All @@ -43,4 +46,4 @@ define(function (require) {
])
});
};
});
});
49 changes: 48 additions & 1 deletion test/unit/specs/components/vis/_agg_configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ define(function (require) {
var AggConfigs;
var SpiedAggConfig;
var indexPattern;
var Schemas;

beforeEach(module('kibana'));
beforeEach(inject(function (Private) {
Expand All @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -190,4 +237,4 @@ define(function (require) {
});
});
}];
});
});

0 comments on commit 0e4270a

Please sign in to comment.