Skip to content

Commit a01836e

Browse files
committed
feat(adhoc filters): basic implementation for ad hoc filters for elasticsearch, grafana#6038
1 parent 1b02632 commit a01836e

File tree

6 files changed

+47
-6
lines changed

6 files changed

+47
-6
lines changed

public/app/features/templating/editor_ctrl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class VariableEditorCtrl {
8484
if ($scope.current.type === 'adhoc' && $scope.current.datasource !== null) {
8585
$scope.infoText = 'Adhoc filters are applied automatically to all queries that target this datasource';
8686
datasourceSrv.get($scope.current.datasource).then(ds => {
87-
if (!ds.supportAdhocFilters) {
87+
if (!ds.getTagKeys) {
8888
$scope.infoText = 'This datasource does not support adhoc filters yet.';
8989
}
9090
});

public/app/plugins/datasource/elasticsearch/datasource.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,14 @@ function (angular, _, moment, kbn, ElasticQueryBuilder, IndexPattern, ElasticRes
177177
var target;
178178
var sentTargets = [];
179179

180+
// add global adhoc filters to timeFilter
181+
var adhocFilters = templateSrv.getAdhocFilters(this.name);
182+
180183
for (var i = 0; i < options.targets.length; i++) {
181184
target = options.targets[i];
182185
if (target.hide) {continue;}
183186

184-
var queryObj = this.queryBuilder.build(target);
187+
var queryObj = this.queryBuilder.build(target, adhocFilters);
185188
var esQuery = angular.toJson(queryObj);
186189
var luceneQuery = target.query || '*';
187190
luceneQuery = templateSrv.replace(luceneQuery, options.scopedVars, 'lucene');
@@ -247,7 +250,7 @@ function (angular, _, moment, kbn, ElasticQueryBuilder, IndexPattern, ElasticRes
247250
// Hide meta-fields and check field type
248251
if (key[0] !== '_' &&
249252
(!query.type ||
250-
query.type && typeMap[subObj.type] === query.type)) {
253+
query.type && typeMap[subObj.type] === query.type)) {
251254

252255
fields[fieldName] = {
253256
text: fieldName,
@@ -314,6 +317,14 @@ function (angular, _, moment, kbn, ElasticQueryBuilder, IndexPattern, ElasticRes
314317
return this.getTerms(query);
315318
}
316319
};
320+
321+
this.getTagKeys = function() {
322+
return this.getFields({});
323+
};
324+
325+
this.getTagValues = function(options) {
326+
return this.getTerms({field: options.key, query: '*'});
327+
};
317328
}
318329

319330
return {

public/app/plugins/datasource/elasticsearch/query_builder.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,23 @@ function (queryDef) {
9898
return query;
9999
};
100100

101-
ElasticQueryBuilder.prototype.build = function(target) {
101+
ElasticQueryBuilder.prototype.addAdhocFilters = function(query, adhocFilters) {
102+
if (!adhocFilters) {
103+
return;
104+
}
105+
106+
var i, filter, condition;
107+
var must = query.query.filtered.filter.bool.must;
108+
109+
for (i = 0; i < adhocFilters.length; i++) {
110+
filter = adhocFilters[i];
111+
condition = {};
112+
condition[filter.key] = filter.value;
113+
must.push({"term": condition});
114+
}
115+
};
116+
117+
ElasticQueryBuilder.prototype.build = function(target, adhocFilters) {
102118
// make sure query has defaults;
103119
target.metrics = target.metrics || [{ type: 'count', id: '1' }];
104120
target.dsType = 'elasticsearch';
@@ -125,6 +141,8 @@ function (queryDef) {
125141
}
126142
};
127143

144+
this.addAdhocFilters(query, adhocFilters);
145+
128146
// handle document query
129147
if (target.bucketAggs.length === 0) {
130148
metric = target.metrics[0];

public/app/plugins/datasource/elasticsearch/specs/query_builder_specs.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,16 @@ describe('ElasticQueryBuilder', function() {
238238
expect(firstLevel.aggs["2"].derivative.buckets_path).to.be("3");
239239
});
240240

241+
it('with adhoc filters', function() {
242+
var query = builder.build({
243+
metrics: [{type: 'Count', id: '0'}],
244+
timeField: '@timestamp',
245+
bucketAggs: [{type: 'date_histogram', field: '@timestamp', id: '3'}],
246+
}, [
247+
{key: 'key1', operator: '=', value: 'value1'}
248+
]);
249+
250+
expect(query.query.filtered.filter.bool.must[1].term["key1"]).to.be("value1");
251+
});
252+
241253
});

public/app/plugins/datasource/influxdb/datasource.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import InfluxQuery from './influx_query';
99
import ResponseParser from './response_parser';
1010
import InfluxQueryBuilder from './query_builder';
1111

12+
1213
export default class InfluxDatasource {
1314
type: string;
1415
urls: any;
@@ -21,7 +22,6 @@ export default class InfluxDatasource {
2122
interval: any;
2223
supportAnnotations: boolean;
2324
supportMetrics: boolean;
24-
supportAdhocFilters: boolean;
2525
responseParser: any;
2626

2727
/** @ngInject */
@@ -40,7 +40,6 @@ export default class InfluxDatasource {
4040
this.interval = (instanceSettings.jsonData || {}).timeInterval;
4141
this.supportAnnotations = true;
4242
this.supportMetrics = true;
43-
this.supportAdhocFilters = true;
4443
this.responseParser = new ResponseParser();
4544
}
4645

public/test/specs/helpers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ define([
158158
return _.template(text, this.templateSettings)(this.data);
159159
};
160160
this.init = function() {};
161+
this.getAdhocFilters = function() { return []; };
161162
this.fillVariableValuesForUrl = function() {};
162163
this.updateTemplateData = function() { };
163164
this.variableExists = function() { return false; };

0 commit comments

Comments
 (0)