2015-09-03 07:55:48 -05:00
|
|
|
define([
|
|
|
|
'plugins/datasource/elasticsearch/queryBuilder'
|
|
|
|
], function(ElasticQueryBuilder) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
describe('ElasticQueryBuilder', function() {
|
|
|
|
|
|
|
|
it('with defaults', function() {
|
|
|
|
var builder = new ElasticQueryBuilder();
|
|
|
|
|
|
|
|
var query = builder.build({
|
2015-09-05 05:24:14 -05:00
|
|
|
metrics: [{type: 'Count', id: '0'}],
|
2015-09-04 04:17:52 -05:00
|
|
|
timeField: '@timestamp',
|
2015-09-05 05:24:14 -05:00
|
|
|
bucketAggs: [{type: 'date_histogram', field: '@timestamp', id: '1'}],
|
2015-09-04 02:41:23 -05:00
|
|
|
});
|
2015-09-03 07:55:48 -05:00
|
|
|
|
2015-09-04 02:41:23 -05:00
|
|
|
expect(query.query.filtered.filter.bool.must[0].range["@timestamp"].gte).to.be("$timeFrom");
|
2015-09-05 05:24:14 -05:00
|
|
|
expect(query.aggs["1"].date_histogram.extended_bounds.min).to.be("$timeFrom");
|
2015-09-03 07:55:48 -05:00
|
|
|
});
|
|
|
|
|
2015-09-04 09:05:47 -05:00
|
|
|
it('with multiple bucket aggs', function() {
|
|
|
|
var builder = new ElasticQueryBuilder();
|
|
|
|
|
|
|
|
var query = builder.build({
|
2015-09-05 05:24:14 -05:00
|
|
|
metrics: [{type: 'count', id: '1'}],
|
2015-09-04 09:05:47 -05:00
|
|
|
timeField: '@timestamp',
|
|
|
|
bucketAggs: [
|
2015-09-05 05:24:14 -05:00
|
|
|
{type: 'terms', field: '@host', id: '2'},
|
|
|
|
{type: 'date_histogram', field: '@timestamp', id: '3'}
|
2015-09-04 09:05:47 -05:00
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2015-09-05 05:24:14 -05:00
|
|
|
expect(query.aggs["2"].terms.field).to.be("@host");
|
|
|
|
expect(query.aggs["2"].aggs["3"].date_histogram.field).to.be("@timestamp");
|
2015-09-04 09:05:47 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2015-09-03 07:55:48 -05:00
|
|
|
it('with select field', function() {
|
|
|
|
var builder = new ElasticQueryBuilder();
|
|
|
|
|
|
|
|
var query = builder.build({
|
2015-09-05 05:24:14 -05:00
|
|
|
metrics: [{type: 'avg', field: '@value', id: '1'}],
|
|
|
|
bucketAggs: [{type: 'date_histogram', field: '@timestamp', id: '2'}],
|
2015-09-03 07:55:48 -05:00
|
|
|
}, 100, 1000);
|
|
|
|
|
2015-09-05 05:24:14 -05:00
|
|
|
var aggs = query.aggs["2"].aggs;
|
|
|
|
expect(aggs["1"].avg.field).to.be("@value");
|
2015-09-03 07:55:48 -05:00
|
|
|
});
|
|
|
|
|
2015-09-05 05:24:14 -05:00
|
|
|
it('with term agg and order by metric agg', function() {
|
|
|
|
var builder = new ElasticQueryBuilder();
|
|
|
|
|
|
|
|
var query = builder.build({
|
2015-09-05 05:51:05 -05:00
|
|
|
metrics: [{type: 'count', id: '1'}, {type: 'avg', field: '@value', id: '5'}],
|
2015-09-05 05:24:14 -05:00
|
|
|
bucketAggs: [
|
2015-09-05 05:51:05 -05:00
|
|
|
{type: 'terms', field: '@host', size: 5, order: 'asc', orderBy: '5', id: '2' },
|
2015-09-05 05:24:14 -05:00
|
|
|
{type: 'date_histogram', field: '@timestamp', id: '3'}
|
|
|
|
],
|
|
|
|
}, 100, 1000);
|
|
|
|
|
|
|
|
var firstLevel = query.aggs["2"];
|
|
|
|
var secondLevel = firstLevel.aggs["3"];
|
|
|
|
|
2015-09-05 05:51:05 -05:00
|
|
|
expect(firstLevel.aggs["5"].avg.field).to.be("@value");
|
|
|
|
expect(secondLevel.aggs["5"].avg.field).to.be("@value");
|
2015-09-05 05:24:14 -05:00
|
|
|
});
|
2015-09-04 02:41:23 -05:00
|
|
|
|
2015-09-03 07:55:48 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|