diff --git a/public/app/plugins/datasource/elasticsearch/bucketAgg.js b/public/app/plugins/datasource/elasticsearch/bucketAgg.js index 6d5ef5adc68..86f35c09667 100644 --- a/public/app/plugins/datasource/elasticsearch/bucketAgg.js +++ b/public/app/plugins/datasource/elasticsearch/bucketAgg.js @@ -15,6 +15,7 @@ function (angular, _, queryDef) { $scope.bucketAggTypes = queryDef.bucketAggTypes; $scope.orderOptions = queryDef.orderOptions; $scope.sizeOptions = queryDef.sizeOptions; + $scope.intervalOptions = queryDef.intervalOptions; $rootScope.onAppEvent('elastic-query-updated', function() { $scope.validateModel(); @@ -27,36 +28,52 @@ function (angular, _, queryDef) { }; $scope.onChangeInternal = function() { - if ($scope.validateModel()) { - $scope.onChange(); - } + $scope.onChange(); + }; + + $scope.onTypeChanged = function() { + $scope.agg.settings = {}; + $scope.showOptions = false; + + $scope.validateModel(); + $scope.onChange(); }; $scope.validateModel = function() { $scope.index = _.indexOf(bucketAggs, $scope.agg); - $scope.isFirst = $scope.index === 0; $scope.isLast = $scope.index === bucketAggs.length - 1; - $scope.settingsLinkText = ""; - if ($scope.agg.type === "terms") { - $scope.agg.order = $scope.agg.order || "asc"; - $scope.agg.size = $scope.agg.size || "0"; - $scope.agg.orderBy = $scope.agg.orderBy || "_term"; + var settingsLinkText = ""; + var settings = $scope.agg.settings || {}; - if ($scope.agg.size !== '0') { - $scope.settingsLinkText = queryDef.describeOrder($scope.agg.order) + ' ' + $scope.agg.size + ', '; + switch($scope.agg.type) { + case 'terms': { + settings.order = settings.order || "asc"; + settings.size = settings.size || "0"; + settings.orderBy = settings.orderBy || "_term"; + + if (settings.size !== '0') { + settingsLinkText = queryDef.describeOrder(settings.order) + ' ' + settings.size + ', '; + } + + settingsLinkText += 'Order by: ' + queryDef.describeOrderBy(settings.orderBy, $scope.target); + + if (settings.size === '0') { + settingsLinkText += ' (' + settings.order + ')'; + } + + break; } - - $scope.settingsLinkText += 'Order by: ' + queryDef.describeOrderBy($scope.agg.orderBy, $scope.target); - - if ($scope.agg.size === '0') { - $scope.settingsLinkText += ' (' + $scope.agg.order + ')'; + case 'date_histogram': { + settings.interval = settings.interval || 'auto'; + $scope.agg.field = $scope.target.timeField; + settingsLinkText = 'Interval: ' + settings.interval; } - } else if ($scope.agg.type === 'date_histogram') { - $scope.agg.field = $scope.target.timeField; } + $scope.settingsLinkText = settingsLinkText; + $scope.agg.settings = settings; return true; }; diff --git a/public/app/plugins/datasource/elasticsearch/partials/bucketAgg.html b/public/app/plugins/datasource/elasticsearch/partials/bucketAgg.html index 55270c43523..e87872fd7bf 100644 --- a/public/app/plugins/datasource/elasticsearch/partials/bucketAgg.html +++ b/public/app/plugins/datasource/elasticsearch/partials/bucketAgg.html @@ -5,10 +5,10 @@ Then by
  • - +
  • - +
  • {{settingsLinkText}} @@ -27,6 +27,19 @@
    +
    +
    +
      +
    • + Interval +
    • +
    • + +
    • +
    +
    +
    +
      @@ -34,7 +47,7 @@ Order
    • - +
    @@ -45,7 +58,7 @@ Size
  • - +
  • @@ -56,7 +69,7 @@ Order By
  • - +
  • diff --git a/public/app/plugins/datasource/elasticsearch/partials/query.editor.html b/public/app/plugins/datasource/elasticsearch/partials/query.editor.html index 834bc61c57f..f3c7700922c 100644 --- a/public/app/plugins/datasource/elasticsearch/partials/query.editor.html +++ b/public/app/plugins/datasource/elasticsearch/partials/query.editor.html @@ -74,63 +74,4 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/public/app/plugins/datasource/elasticsearch/partials/query.options.html b/public/app/plugins/datasource/elasticsearch/partials/query.options.html index 934b9a77079..c56eea6d34d 100644 --- a/public/app/plugins/datasource/elasticsearch/partials/query.options.html +++ b/public/app/plugins/datasource/elasticsearch/partials/query.options.html @@ -1,24 +1,5 @@
    -
    - -
    -
    - -
    +
    @@ -56,30 +27,6 @@
    -
    -
    Stacking and fill
    - -
    - -
    -
    Group by time
    - -
    - - diff --git a/public/app/plugins/datasource/elasticsearch/queryBuilder.js b/public/app/plugins/datasource/elasticsearch/queryBuilder.js index d9b58d5acdf..6da10ded1fa 100644 --- a/public/app/plugins/datasource/elasticsearch/queryBuilder.js +++ b/public/app/plugins/datasource/elasticsearch/queryBuilder.js @@ -16,21 +16,24 @@ function (angular) { ElasticQueryBuilder.prototype.buildTermsAgg = function(aggDef, queryNode, target) { var metricRef, metric, size, y; - queryNode.terms = { "field": aggDef.field }; - size = parseInt(aggDef.size, 10); + if (!aggDef.settings) { + return queryNode; + } + + size = parseInt(aggDef.settings.size, 10); if (size > 0) { queryNode.terms.size = size; } - if (aggDef.orderBy !== void 0) { + if (aggDef.settings.orderBy !== void 0) { queryNode.terms.order = {}; - queryNode.terms.order[aggDef.orderBy] = aggDef.order; + queryNode.terms.order[aggDef.settings.orderBy] = aggDef.settings.order; // if metric ref, look it up and add it to this agg level - metricRef = parseInt(aggDef.orderBy, 10); + metricRef = parseInt(aggDef.settings.orderBy, 10); if (!isNaN(metricRef)) { for (y = 0; y < target.metrics.length; y++) { metric = target.metrics[y]; - if (metric.id === aggDef.orderBy) { + if (metric.id === aggDef.settings.orderBy) { queryNode.aggs = {}; queryNode.aggs[metric.id] = {}; queryNode.aggs[metric.id][metric.type] = {field: metric.field}; @@ -43,6 +46,14 @@ function (angular) { return queryNode; }; + ElasticQueryBuilder.prototype.getInterval = function(agg) { + if (agg.settings && agg.settings.interval !== 'auto') { + return agg.settings.interval; + } else { + return '$interval'; + } + }; + ElasticQueryBuilder.prototype.build = function(target) { if (target.rawQuery) { return angular.fromJson(target.rawQuery); @@ -77,7 +88,7 @@ function (angular) { switch(aggDef.type) { case 'date_histogram': { esAgg["date_histogram"] = { - "interval": target.interval || "$interval", + "interval": this.getInterval(aggDef), "field": this.timeField, "min_doc_count": 1, "extended_bounds": { "min": "$timeFrom", "max": "$timeTo" } diff --git a/public/app/plugins/datasource/elasticsearch/queryCtrl.js b/public/app/plugins/datasource/elasticsearch/queryCtrl.js index d5f380bee3a..837ceb5017f 100644 --- a/public/app/plugins/datasource/elasticsearch/queryCtrl.js +++ b/public/app/plugins/datasource/elasticsearch/queryCtrl.js @@ -14,7 +14,7 @@ function (angular, _) { if (!target) { return; } target.metrics = target.metrics || [{ type: 'count', id: '1' }]; - target.bucketAggs = target.bucketAggs || [{ type: 'date_histogram', id: '2'}]; + target.bucketAggs = target.bucketAggs || [{type: 'date_histogram', id: '2', settings: {interval: 'auto'}}]; target.timeField = $scope.datasource.timeField; }; diff --git a/public/app/plugins/datasource/elasticsearch/queryDef.js b/public/app/plugins/datasource/elasticsearch/queryDef.js index 5fe11e36327..1925c7373f3 100644 --- a/public/app/plugins/datasource/elasticsearch/queryDef.js +++ b/public/app/plugins/datasource/elasticsearch/queryDef.js @@ -53,6 +53,17 @@ function (_) { {text: 'Std Dev Lower', value: 'std_deviation_bounds_lower'}, ], + intervalOptions: [ + {text: 'auto', value: 'auto'}, + {text: '10s', value: '10s'}, + {text: '1m', value: '1m'}, + {text: '5m', value: '5m'}, + {text: '10m', value: '10m'}, + {text: '20m', value: '20m'}, + {text: '1h', value: '1h'}, + {text: '1d', value: '1d'}, + ], + getOrderByOptions: function(target) { var self = this; var metricRefs = []; diff --git a/public/test/specs/elasticsearch-querybuilder-specs.js b/public/test/specs/elasticsearch-querybuilder-specs.js index 1f31db95424..b56e0571e76 100644 --- a/public/test/specs/elasticsearch-querybuilder-specs.js +++ b/public/test/specs/elasticsearch-querybuilder-specs.js @@ -48,9 +48,12 @@ define([ it('with term agg and order by metric agg', function() { var query = builder.build({ - metrics: [{type: 'count', id: '1'}, {type: 'avg', field: '@value', id: '5'}], + metrics: [ + {type: 'count', id: '1'}, + {type: 'avg', field: '@value', id: '5'} + ], bucketAggs: [ - {type: 'terms', field: '@host', size: 5, order: 'asc', orderBy: '5', id: '2' }, + {type: 'terms', field: '@host', settings: {size: 5, order: 'asc', orderBy: '5'}, id: '2' }, {type: 'date_histogram', field: '@timestamp', id: '3'} ], }, 100, 1000);