Skip to content

Commit 0e4270a

Browse files
committed
Closes #263 - Adding the ability to add defaults to the visualization schemas
1 parent 3fef72a commit 0e4270a

File tree

4 files changed

+81
-6
lines changed

4 files changed

+81
-6
lines changed

src/kibana/components/agg_types/_agg_params.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,4 @@ define(function (require) {
7777

7878
return AggParams;
7979
};
80-
});
80+
});

src/kibana/components/vis/_agg_configs.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,41 @@ define(function (require) {
66

77
_(AggConfigs).inherits(Registry);
88
function AggConfigs(vis, configStates) {
9+
var self = this;
910
this.vis = vis;
1011

12+
1113
AggConfigs.Super.call(this, {
1214
index: ['id'],
13-
group: ['schema.group', 'type.name'],
15+
group: ['schema.group', 'type.name', 'schema.name'],
1416
initialSet: (configStates || []).map(function (aggConfigState) {
1517
if (aggConfigState instanceof AggConfig) return aggConfigState;
1618
return new AggConfig(vis, aggConfigState);
1719
})
1820
});
21+
22+
23+
// Set the defaults for any schema which has them. If the defaults
24+
// for some reason has more then the max only set the max number
25+
// of defaults (not sure why a someone define more...
26+
// but whatever). Also if a schema.name is already set then don't
27+
// set anything.
28+
_(vis.type.schemas.all)
29+
.filter(function (schema) {
30+
return _.isArray(schema.defaults) && schema.defaults.length > 0;
31+
})
32+
.each(function (schema) {
33+
if (!self.bySchemaName[schema.name]) {
34+
var defaults = schema.defaults.slice(0, schema.max);
35+
_.each(defaults, function (def) {
36+
self.push(new AggConfig(vis, {
37+
schema: schema.name,
38+
type: def
39+
}));
40+
});
41+
}
42+
});
43+
1944
}
2045

2146
AggConfigs.prototype.toDsl = function () {
@@ -52,4 +77,4 @@ define(function (require) {
5277

5378
return AggConfigs;
5479
};
55-
});
80+
});

src/kibana/components/vis_types/histogram.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ define(function (require) {
1717
name: 'metric',
1818
title: 'Y-Axis',
1919
min: 1,
20-
max: 1
20+
max: 1,
21+
defaults: [
22+
'count'
23+
]
2124
},
2225
{
2326
group: 'buckets',
@@ -43,4 +46,4 @@ define(function (require) {
4346
])
4447
});
4548
};
46-
});
49+
});

test/unit/specs/components/vis/_agg_configs.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ define(function (require) {
99
var AggConfigs;
1010
var SpiedAggConfig;
1111
var indexPattern;
12+
var Schemas;
1213

1314
beforeEach(module('kibana'));
1415
beforeEach(inject(function (Private) {
@@ -23,6 +24,7 @@ define(function (require) {
2324
AggConfigs = Private(require('components/vis/_agg_configs'));
2425
Registry = require('utils/registry/registry');
2526
indexPattern = Private(require('fixtures/stubbed_logstash_index_pattern'));
27+
Schemas = Private(require('components/vis_types/_schemas'));
2628
}));
2729

2830
it('extends Registry', function () {
@@ -61,6 +63,51 @@ define(function (require) {
6163
expect(ac).to.have.length(2);
6264
expect(SpiedAggConfig).to.have.property('callCount', 1);
6365
});
66+
67+
describe('defaults', function () {
68+
var vis;
69+
beforeEach(function () {
70+
vis = {
71+
type: {
72+
schemas: new Schemas([
73+
{
74+
group: 'metrics',
75+
name: 'metric',
76+
title: 'Simple',
77+
min: 1,
78+
max: 2,
79+
defaults: [ 'count', 'avg', 'sum' ]
80+
},
81+
{
82+
group: 'buckets',
83+
name: 'segment',
84+
title: 'Example',
85+
min: 0,
86+
max: 1,
87+
defaults: [ 'terms', 'fitlers' ]
88+
}
89+
])
90+
}
91+
};
92+
});
93+
94+
it('should only set the number of defaults defined by the max', function () {
95+
var ac = new AggConfigs(vis);
96+
expect(ac.bySchemaName['metric']).to.have.length(2);
97+
});
98+
99+
it('should set the defaults defined in the schema when none exist', function () {
100+
var ac = new AggConfigs(vis);
101+
expect(ac).to.have.length(3);
102+
});
103+
104+
it('should NOT set the defaults defined in the schema when some exist', function () {
105+
var ac = new AggConfigs(vis, [{ schema: 'segment', type: 'date_histogram' }]);
106+
expect(ac).to.have.length(3);
107+
expect(ac.bySchemaName['segment'][0].type.name).to.equal('date_histogram');
108+
});
109+
110+
});
64111
});
65112

66113
describe('#getSorted', function () {
@@ -190,4 +237,4 @@ define(function (require) {
190237
});
191238
});
192239
}];
193-
});
240+
});

0 commit comments

Comments
 (0)