From 966c2912fc1e262eca962659e21c520a1d85f85f Mon Sep 17 00:00:00 2001 From: Dhia MOAKHAR Date: Fri, 2 Jun 2017 23:56:48 +0000 Subject: [PATCH 1/4] [elasticsearch] Add option for result set size in raw_dcument it allows to specify the result set size in raw_document. Example: table panel could show more (or less) than 500 line if needed. Added test to spec --- .../plugins/datasource/elasticsearch/metric_agg.js | 2 ++ .../datasource/elasticsearch/partials/metric_agg.html | 5 +++++ .../plugins/datasource/elasticsearch/query_builder.js | 8 +++++--- .../elasticsearch/specs/query_builder_specs.ts | 11 ++++++++++- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/public/app/plugins/datasource/elasticsearch/metric_agg.js b/public/app/plugins/datasource/elasticsearch/metric_agg.js index c0c4ffc08d4..90c303a5722 100644 --- a/public/app/plugins/datasource/elasticsearch/metric_agg.js +++ b/public/app/plugins/datasource/elasticsearch/metric_agg.js @@ -103,6 +103,8 @@ function (angular, _, queryDef) { break; } case 'raw_document': { + $scope.agg.settings.size = $scope.agg.settings.size || 500; + $scope.settingsLinkText = 'Size: ' + $scope.agg.settings.size ; $scope.target.metrics = [$scope.agg]; $scope.target.bucketAggs = []; break; diff --git a/public/app/plugins/datasource/elasticsearch/partials/metric_agg.html b/public/app/plugins/datasource/elasticsearch/partials/metric_agg.html index 8c25c2d5347..cc2ec8e4918 100644 --- a/public/app/plugins/datasource/elasticsearch/partials/metric_agg.html +++ b/public/app/plugins/datasource/elasticsearch/partials/metric_agg.html @@ -72,6 +72,11 @@ +
+ + +
+
diff --git a/public/app/plugins/datasource/elasticsearch/query_builder.js b/public/app/plugins/datasource/elasticsearch/query_builder.js index 62a7b379c8e..ee7e9265f08 100644 --- a/public/app/plugins/datasource/elasticsearch/query_builder.js +++ b/public/app/plugins/datasource/elasticsearch/query_builder.js @@ -109,8 +109,8 @@ function (queryDef) { return filterObj; }; - ElasticQueryBuilder.prototype.documentQuery = function(query) { - query.size = 500; + ElasticQueryBuilder.prototype.documentQuery = function(query, size) { + query.size = size === undefined ? 500 : parseInt(size , 10); query.sort = {}; query.sort[this.timeField] = {order: 'desc', unmapped_type: 'boolean'}; @@ -196,7 +196,9 @@ function (queryDef) { if (metric && metric.type !== 'raw_document') { throw {message: 'Invalid query'}; } - return this.documentQuery(query, target); + var size = metric && metric.hasOwnProperty("settings") && metric.settings.hasOwnProperty("size") + && metric.settings["size"] !== null ? metric.settings["size"] : 500 ; + return this.documentQuery(query,size); } nestedAggs = query; diff --git a/public/app/plugins/datasource/elasticsearch/specs/query_builder_specs.ts b/public/app/plugins/datasource/elasticsearch/specs/query_builder_specs.ts index 18645ad498d..201459e65d9 100644 --- a/public/app/plugins/datasource/elasticsearch/specs/query_builder_specs.ts +++ b/public/app/plugins/datasource/elasticsearch/specs/query_builder_specs.ts @@ -156,13 +156,22 @@ describe('ElasticQueryBuilder', function() { it('with raw_document metric', function() { var query = builder.build({ - metrics: [{type: 'raw_document', id: '1'}], + metrics: [{type: 'raw_document', id: '1',settings: {}}], timeField: '@timestamp', bucketAggs: [], }); expect(query.size).to.be(500); }); + it('with raw_document metric size set', function() { + var query = builder.build({ + metrics: [{type: 'raw_document', id: '1',settings: {size: '1337'}}], + timeField: '@timestamp', + bucketAggs: [], + }); + + expect(query.size).to.be(1337); + }); it('with moving average', function() { var query = builder.build({ From d55cc4e2a3c26d823611d06c25c20b0a6eead68c Mon Sep 17 00:00:00 2001 From: Dhia MOAKHAR Date: Sat, 3 Jun 2017 02:50:10 +0000 Subject: [PATCH 2/4] [elasticsearch] Fix add metric that was not working properly when selecting Raw Documet metric type, the $scope.target.metrics was replaced by [$scope.agg], however the pointer to this variables is shared with metricAggs. Instead we free the array and add $scope.agg --- public/app/plugins/datasource/elasticsearch/metric_agg.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/public/app/plugins/datasource/elasticsearch/metric_agg.js b/public/app/plugins/datasource/elasticsearch/metric_agg.js index 90c303a5722..88053b0efac 100644 --- a/public/app/plugins/datasource/elasticsearch/metric_agg.js +++ b/public/app/plugins/datasource/elasticsearch/metric_agg.js @@ -67,7 +67,6 @@ function (angular, _, queryDef) { } else if (!$scope.agg.field) { $scope.agg.field = 'select field'; } - switch($scope.agg.type) { case 'cardinality': { var precision_threshold = $scope.agg.settings.precision_threshold || ''; @@ -105,12 +104,12 @@ function (angular, _, queryDef) { case 'raw_document': { $scope.agg.settings.size = $scope.agg.settings.size || 500; $scope.settingsLinkText = 'Size: ' + $scope.agg.settings.size ; - $scope.target.metrics = [$scope.agg]; + $scope.target.metrics.splice(0,$scope.target.metrics.length, $scope.agg); + $scope.target.bucketAggs = []; break; } } - if ($scope.aggDef.supportsInlineScript) { // I know this stores the inline script twice // but having it like this simplifes the query_builder From 045f5e11fccee952312b77c792e4cdeb13797955 Mon Sep 17 00:00:00 2001 From: Dhia MOAKHAR Date: Sat, 3 Jun 2017 02:55:26 +0000 Subject: [PATCH 3/4] [elasticsearch] Fix bug when switching from "Raw Document" metric type when switch to "raw Document" metric type we do free all "Group by" however when we switch back to another type we do not reset the default aggregation (date histogram) Thus all modification will through exception as no "Group by" is defined and panel should be recreated the fix will reintialize the "Group by" by setting default value --- .../plugins/datasource/elasticsearch/query_builder.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/public/app/plugins/datasource/elasticsearch/query_builder.js b/public/app/plugins/datasource/elasticsearch/query_builder.js index ee7e9265f08..d753a2666c6 100644 --- a/public/app/plugins/datasource/elasticsearch/query_builder.js +++ b/public/app/plugins/datasource/elasticsearch/query_builder.js @@ -194,11 +194,12 @@ function (queryDef) { if (target.bucketAggs.length === 0) { metric = target.metrics[0]; if (metric && metric.type !== 'raw_document') { - throw {message: 'Invalid query'}; - } - var size = metric && metric.hasOwnProperty("settings") && metric.settings.hasOwnProperty("size") + target.bucketAggs = [{type: 'date_histogram', id: '2', settings: {interval: 'auto'}}]; + } else { + var size = metric && metric.hasOwnProperty("settings") && metric.settings.hasOwnProperty("size") && metric.settings["size"] !== null ? metric.settings["size"] : 500 ; - return this.documentQuery(query,size); + return this.documentQuery(query,size); + } } nestedAggs = query; From 7c1dc2444d6a7b70dd02d12a38b241e572580213 Mon Sep 17 00:00:00 2001 From: Dhia MOAKHAR Date: Sun, 4 Jun 2017 13:20:58 +0000 Subject: [PATCH 4/4] change size in raw_document from text to number update query builder and specs --- .../plugins/datasource/elasticsearch/partials/metric_agg.html | 2 +- public/app/plugins/datasource/elasticsearch/query_builder.js | 2 +- .../datasource/elasticsearch/specs/query_builder_specs.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/public/app/plugins/datasource/elasticsearch/partials/metric_agg.html b/public/app/plugins/datasource/elasticsearch/partials/metric_agg.html index cc2ec8e4918..b4a1a61ed32 100644 --- a/public/app/plugins/datasource/elasticsearch/partials/metric_agg.html +++ b/public/app/plugins/datasource/elasticsearch/partials/metric_agg.html @@ -74,7 +74,7 @@
- +
diff --git a/public/app/plugins/datasource/elasticsearch/query_builder.js b/public/app/plugins/datasource/elasticsearch/query_builder.js index d753a2666c6..1493fd3eb95 100644 --- a/public/app/plugins/datasource/elasticsearch/query_builder.js +++ b/public/app/plugins/datasource/elasticsearch/query_builder.js @@ -110,7 +110,7 @@ function (queryDef) { }; ElasticQueryBuilder.prototype.documentQuery = function(query, size) { - query.size = size === undefined ? 500 : parseInt(size , 10); + query.size = size === undefined ? 500 : size; query.sort = {}; query.sort[this.timeField] = {order: 'desc', unmapped_type: 'boolean'}; diff --git a/public/app/plugins/datasource/elasticsearch/specs/query_builder_specs.ts b/public/app/plugins/datasource/elasticsearch/specs/query_builder_specs.ts index 201459e65d9..4ea108359a1 100644 --- a/public/app/plugins/datasource/elasticsearch/specs/query_builder_specs.ts +++ b/public/app/plugins/datasource/elasticsearch/specs/query_builder_specs.ts @@ -165,7 +165,7 @@ describe('ElasticQueryBuilder', function() { }); it('with raw_document metric size set', function() { var query = builder.build({ - metrics: [{type: 'raw_document', id: '1',settings: {size: '1337'}}], + metrics: [{type: 'raw_document', id: '1',settings: {size: 1337}}], timeField: '@timestamp', bucketAggs: [], });