feat(elasticsearch): lots of work on elasticsearch metrics query editor, #1034

This commit is contained in:
Torkel Ödegaard 2015-09-03 14:55:48 +02:00
parent 525724cc1f
commit 7e9f11ea1c
6 changed files with 56 additions and 15 deletions

View File

@ -149,7 +149,7 @@ function (angular, _, config, kbn, moment, ElasticQueryBuilder) {
var esQuery = queryBuilder.build(target, timeFrom, timeTo);
payload += header + '\n';
payload += esQuery + '\n';
payload += angular.toJson(esQuery) + '\n';
sentTargets.push(target);
});
@ -172,7 +172,7 @@ function (angular, _, config, kbn, moment, ElasticQueryBuilder) {
return date.getTime();
};
// this is quite complex
// This is quite complex
// neeed to recurise down the nested buckets to build series
ElasticDatasource.prototype._processBuckets = function(buckets, groupByFields, series, level, parentName, parentTime) {
var points = [];

View File

@ -56,6 +56,7 @@ function () {
};
var nestedAggs = query.aggs.histogram;
target.groupByFields = target.groupByFields || [];
for (var i = 0; i < target.groupByFields.length; i++) {
var field = target.groupByFields[i].field;
@ -66,9 +67,16 @@ function () {
nestedAggs = aggs;
}
console.log(angular.toJson(query, true));
for (var i = 0; i < target.select.length; i++) {
var select = target.select[i];
if (select.field) {
nestedAggs.aggs = {};
nestedAggs.aggs[select.field] = {};
nestedAggs.aggs[select.field][select.agg] = {field: select.field};
}
}
query = angular.toJson(query);
console.log(angular.toJson(query, true));
return query;
};

View File

@ -36,7 +36,7 @@ function (angular, _, ElasticQueryBuilder) {
if ($scope.selectSegments.length > 0) {
$scope.selectSegments.push(uiSegmentSrv.newCondition(" and "));
}
if (select.agg === 'Count') {
if (select.agg === 'count') {
$scope.selectSegments.push(uiSegmentSrv.newSegment({value: select.agg, type: 'agg'}));
} else {
$scope.selectSegments.push(uiSegmentSrv.newSegment({value: select.agg, type: 'agg'}));
@ -53,10 +53,10 @@ function (angular, _, ElasticQueryBuilder) {
$scope.getSelectSegments = function(segment, index) {
if (segment.type === 'agg' || segment.type === 'plus-button') {
var options = [
uiSegmentSrv.newSegment({value: 'Count', type: 'agg'}),
uiSegmentSrv.newSegment({value: 'Min', type: 'agg'}),
uiSegmentSrv.newSegment({value: 'Max', type: 'agg'}),
uiSegmentSrv.newSegment({value: 'Avg', type: 'agg'}),
uiSegmentSrv.newSegment({value: 'count', type: 'agg'}),
uiSegmentSrv.newSegment({value: 'min', type: 'agg', reqField: true}),
uiSegmentSrv.newSegment({value: 'count', type: 'agg', reqField: true}),
uiSegmentSrv.newSegment({value: 'avg', type: 'agg', reqField: true}),
];
if (index > 0) {
options.splice(0, 0, angular.copy($scope.removeSelectSegment));
@ -90,10 +90,8 @@ function (angular, _, ElasticQueryBuilder) {
if (segment.type === 'agg') {
var nextSegment = $scope.selectSegments[index + 1];
if (segment.value === 'Count') {
if (nextSegment && nextSegment.type === 'field') {
$scope.selectSegments.splice(index + 1, 1);
}
if (!segment.reqField && nextSegment && nextSegment.type === 'field') {
$scope.selectSegments.splice(index + 1, 1);
} else if (!nextSegment || nextSegment.type !== 'field') {
$scope.selectSegments.splice(index + 1, 0, uiSegmentSrv.newSegment({value: 'select field', fake: true, type: 'field'}));
}
@ -112,7 +110,7 @@ function (angular, _, ElasticQueryBuilder) {
var segment = $scope.selectSegments[i];
var select = {agg: segment.value };
if (segment.type === 'agg' && segment.value !== 'Count') {
if (segment.type === 'agg' && segment.reqField) {
select.field = $scope.selectSegments[i+1].value;
i += 2;
} else {

View File

@ -0,0 +1,34 @@
define([
'plugins/datasource/elasticsearch/queryBuilder'
], function(ElasticQueryBuilder) {
'use strict';
describe('ElasticQueryBuilder', function() {
it('with defaults', function() {
var builder = new ElasticQueryBuilder();
var query = builder.build({
select: [{agg: 'Count'}],
groupByFields: [],
}, 100, 1000);
expect(query.query.filtered.filter.bool.must[0].range["@timestamp"].gte).to.be(100);
expect(query.aggs.histogram.date_histogram.extended_bounds.min).to.be(100);
});
it('with select field', function() {
var builder = new ElasticQueryBuilder();
var query = builder.build({
select: [{agg: 'Avg', field: '@value'}],
groupByFields: [],
}, 100, 1000);
expect(query.query.filtered.filter.bool.must[0].range["@timestamp"].gte).to.be(100);
});
});
});

View File

@ -97,7 +97,7 @@ define([
});
});
describe('two group by query', function() {
describe('group by query 2 fields', function() {
var result;
beforeEach(function() {

View File

@ -153,6 +153,7 @@ require([
'specs/opentsdbDatasource-specs',
'specs/cloudwatch-datasource-specs',
'specs/elasticsearch-specs',
'specs/elasticsearch-querybuilder-specs',
];
var pluginSpecs = (config.plugins.specs || []).map(function (spec) {