Skip to content

Commit

Permalink
Merge pull request grafana#14054 from marefr/6367_raw_document_reset
Browse files Browse the repository at this point in the history
Fix switching from es raw document metric breaks query editor
  • Loading branch information
torkelo authored Nov 14, 2018
2 parents 19b3578 + f065af3 commit 0a08014
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
6 changes: 6 additions & 0 deletions public/app/plugins/datasource/elasticsearch/metric_agg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ export class ElasticMetricAggCtrl {
$scope.agg.settings = {};
$scope.agg.meta = {};
$scope.showOptions = false;

// reset back to metric/group by query
if ($scope.target.bucketAggs.length === 0 && $scope.agg.type !== 'raw_document') {
$scope.target.bucketAggs = [queryDef.defaultBucketAgg()];
}

$scope.updatePipelineAggOptions();
$scope.onChange();
};
Expand Down
4 changes: 2 additions & 2 deletions public/app/plugins/datasource/elasticsearch/query_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ export class ElasticQueryBuilder {

build(target, adhocFilters?, queryString?) {
// make sure query has defaults;
target.metrics = target.metrics || [{ type: 'count', id: '1' }];
target.bucketAggs = target.bucketAggs || [{ type: 'date_histogram', id: '2', settings: { interval: 'auto' } }];
target.metrics = target.metrics || [queryDef.defaultMetricAgg()];
target.bucketAggs = target.bucketAggs || [queryDef.defaultBucketAgg()];
target.timeField = this.timeField;

let i, nestedAggs, metric;
Expand Down
13 changes: 13 additions & 0 deletions public/app/plugins/datasource/elasticsearch/query_ctrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ export class ElasticQueryCtrl extends QueryCtrl {
super($scope, $injector);

this.esVersion = this.datasource.esVersion;

this.target = this.target || {};
this.target.metrics = this.target.metrics || [queryDef.defaultMetricAgg()];
this.target.bucketAggs = this.target.bucketAggs || [queryDef.defaultBucketAgg()];

if (this.target.bucketAggs.length === 0) {
const metric = this.target.metrics[0];
if (!metric || metric.type !== 'raw_document') {
this.target.bucketAggs = [queryDef.defaultBucketAgg()];
}
this.refresh();
}

this.queryUpdated();
}

Expand Down
8 changes: 8 additions & 0 deletions public/app/plugins/datasource/elasticsearch/query_def.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,11 @@ export function describeOrderBy(orderBy, target) {
return 'metric not found';
}
}

export function defaultMetricAgg() {
return { type: 'count', id: '1' };
}

export function defaultBucketAgg() {
return { type: 'date_histogram', id: '2', settings: { interval: 'auto' } };
}

0 comments on commit 0a08014

Please sign in to comment.