2015-09-03 07:55:48 -05:00
|
|
|
define([
|
2015-09-15 06:23:36 -05:00
|
|
|
'app/plugins/datasource/elasticsearch/queryBuilder'
|
2015-09-03 07:55:48 -05:00
|
|
|
], function(ElasticQueryBuilder) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
describe('ElasticQueryBuilder', function() {
|
2015-09-07 01:57:46 -05:00
|
|
|
var builder;
|
2015-09-03 07:55:48 -05:00
|
|
|
|
2015-09-07 01:57:46 -05:00
|
|
|
beforeEach(function() {
|
|
|
|
builder = new ElasticQueryBuilder({timeField: '@timestamp'});
|
|
|
|
});
|
2015-09-03 07:55:48 -05:00
|
|
|
|
2015-09-07 01:57:46 -05:00
|
|
|
it('with defaults', function() {
|
2015-09-03 07:55:48 -05:00
|
|
|
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-21 05:07:03 -05:00
|
|
|
it('with raw query', function() {
|
|
|
|
var query = builder.build({
|
|
|
|
rawQuery: '{"query": "$lucene_query"}',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(query.query).to.be("$lucene_query");
|
|
|
|
});
|
|
|
|
|
2015-09-04 09:05:47 -05:00
|
|
|
it('with multiple bucket aggs', function() {
|
|
|
|
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 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 query = builder.build({
|
2015-09-07 09:35:40 -05:00
|
|
|
metrics: [
|
|
|
|
{type: 'count', id: '1'},
|
|
|
|
{type: 'avg', field: '@value', id: '5'}
|
|
|
|
],
|
2015-09-05 05:24:14 -05:00
|
|
|
bucketAggs: [
|
2015-09-07 09:35:40 -05:00
|
|
|
{type: 'terms', field: '@host', settings: {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-05 11:31:42 -05:00
|
|
|
it('with metric percentiles', function() {
|
|
|
|
var query = builder.build({
|
|
|
|
metrics: [
|
|
|
|
{
|
|
|
|
id: '1',
|
|
|
|
type: 'percentiles',
|
|
|
|
field: '@load_time',
|
|
|
|
settings: {
|
|
|
|
percents: [1,2,3,4]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
bucketAggs: [
|
|
|
|
{type: 'date_histogram', field: '@timestamp', id: '3'}
|
|
|
|
],
|
|
|
|
}, 100, 1000);
|
|
|
|
|
|
|
|
var firstLevel = query.aggs["3"];
|
|
|
|
|
|
|
|
expect(firstLevel.aggs["1"].percentiles.field).to.be("@load_time");
|
|
|
|
expect(firstLevel.aggs["1"].percentiles.percents).to.eql([1,2,3,4]);
|
|
|
|
});
|
|
|
|
|
2015-09-21 12:23:18 -05:00
|
|
|
it('with filters aggs', function() {
|
|
|
|
var query = builder.build({
|
|
|
|
metrics: [{type: 'count', id: '1'}],
|
|
|
|
timeField: '@timestamp',
|
|
|
|
bucketAggs: [
|
2015-09-21 13:29:05 -05:00
|
|
|
{
|
|
|
|
id: '2',
|
|
|
|
type: 'filters',
|
|
|
|
settings: {
|
|
|
|
filters: [
|
|
|
|
{query: '@metric:cpu' },
|
|
|
|
{query: '@metric:logins.count' },
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
2015-09-21 12:23:18 -05:00
|
|
|
{type: 'date_histogram', field: '@timestamp', id: '4'}
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(query.aggs["2"].filters.filters["@metric:cpu"].query.query_string.query).to.be("@metric:cpu");
|
|
|
|
expect(query.aggs["2"].filters.filters["@metric:logins.count"].query.query_string.query).to.be("@metric:logins.count");
|
|
|
|
expect(query.aggs["2"].aggs["4"].date_histogram.field).to.be("@timestamp");
|
|
|
|
});
|
|
|
|
|
2015-09-03 07:55:48 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|