mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Elasticsearch: interpolate variables in Filters Bucket Aggregation (#28969)
This commit is contained in:
@@ -864,6 +864,34 @@ describe('ElasticDatasource', function(this: any) {
|
|||||||
expect(typeof JSON.parse(query.split('\n')[1]).query.bool.filter[0].range['@time'].gte).toBe('number');
|
expect(typeof JSON.parse(query.split('\n')[1]).query.bool.filter[0].range['@time'].gte).toBe('number');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should correctly interpolate variables in query', () => {
|
||||||
|
const query = {
|
||||||
|
alias: '',
|
||||||
|
bucketAggs: [{ type: 'filters', settings: { filters: [{ query: '$var', label: '' }] }, id: '1' }],
|
||||||
|
metrics: [{ type: 'count', id: '1' }],
|
||||||
|
query: '$var',
|
||||||
|
};
|
||||||
|
|
||||||
|
const interpolatedQuery = ctx.ds.interpolateVariablesInQueries([query], {})[0];
|
||||||
|
|
||||||
|
expect(interpolatedQuery.query).toBe('resolvedVariable');
|
||||||
|
expect(interpolatedQuery.bucketAggs[0].settings.filters[0].query).toBe('resolvedVariable');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should correctly handle empty query strings', () => {
|
||||||
|
const query = {
|
||||||
|
alias: '',
|
||||||
|
bucketAggs: [{ type: 'filters', settings: { filters: [{ query: '', label: '' }] }, id: '1' }],
|
||||||
|
metrics: [{ type: 'count', id: '1' }],
|
||||||
|
query: '',
|
||||||
|
};
|
||||||
|
|
||||||
|
const interpolatedQuery = ctx.ds.interpolateVariablesInQueries([query], {})[0];
|
||||||
|
|
||||||
|
expect(interpolatedQuery.query).toBe('*');
|
||||||
|
expect(interpolatedQuery.bucketAggs[0].settings.filters[0].query).toBe('*');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('enhanceDataFrame', () => {
|
describe('enhanceDataFrame', () => {
|
||||||
|
|||||||
@@ -308,6 +308,11 @@ export class ElasticDatasource extends DataSourceApi<ElasticsearchQuery, Elastic
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private interpolateLuceneQuery(queryString: string, scopedVars: ScopedVars) {
|
||||||
|
// Elasticsearch queryString should always be '*' if empty string
|
||||||
|
return this.templateSrv.replace(queryString, scopedVars, 'lucene') || '*';
|
||||||
|
}
|
||||||
|
|
||||||
interpolateVariablesInQueries(queries: ElasticsearchQuery[], scopedVars: ScopedVars): ElasticsearchQuery[] {
|
interpolateVariablesInQueries(queries: ElasticsearchQuery[], scopedVars: ScopedVars): ElasticsearchQuery[] {
|
||||||
let expandedQueries = queries;
|
let expandedQueries = queries;
|
||||||
if (queries && queries.length > 0) {
|
if (queries && queries.length > 0) {
|
||||||
@@ -315,8 +320,16 @@ export class ElasticDatasource extends DataSourceApi<ElasticsearchQuery, Elastic
|
|||||||
const expandedQuery = {
|
const expandedQuery = {
|
||||||
...query,
|
...query,
|
||||||
datasource: this.name,
|
datasource: this.name,
|
||||||
query: this.templateSrv.replace(query.query, scopedVars, 'lucene'),
|
query: this.interpolateLuceneQuery(query.query || '', scopedVars),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
for (let bucketAgg of query.bucketAggs || []) {
|
||||||
|
if (bucketAgg.type === 'filters') {
|
||||||
|
for (let filter of bucketAgg.settings.filters) {
|
||||||
|
filter.query = this.interpolateLuceneQuery(filter.query, scopedVars);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return expandedQuery;
|
return expandedQuery;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -363,7 +376,7 @@ export class ElasticDatasource extends DataSourceApi<ElasticsearchQuery, Elastic
|
|||||||
|
|
||||||
query(options: DataQueryRequest<ElasticsearchQuery>): Promise<DataQueryResponse> {
|
query(options: DataQueryRequest<ElasticsearchQuery>): Promise<DataQueryResponse> {
|
||||||
let payload = '';
|
let payload = '';
|
||||||
const targets = _.cloneDeep(options.targets);
|
const targets = this.interpolateVariablesInQueries(_.cloneDeep(options.targets), options.scopedVars);
|
||||||
const sentTargets: ElasticsearchQuery[] = [];
|
const sentTargets: ElasticsearchQuery[] = [];
|
||||||
|
|
||||||
// add global adhoc filters to timeFilter
|
// add global adhoc filters to timeFilter
|
||||||
@@ -374,25 +387,19 @@ export class ElasticDatasource extends DataSourceApi<ElasticsearchQuery, Elastic
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let queryString = this.templateSrv.replace(target.query, options.scopedVars, 'lucene');
|
|
||||||
// Elasticsearch queryString should always be '*' if empty string
|
|
||||||
if (!queryString || queryString === '') {
|
|
||||||
queryString = '*';
|
|
||||||
}
|
|
||||||
|
|
||||||
let queryObj;
|
let queryObj;
|
||||||
if (target.isLogsQuery || queryDef.hasMetricOfType(target, 'logs')) {
|
if (target.isLogsQuery || queryDef.hasMetricOfType(target, 'logs')) {
|
||||||
target.bucketAggs = [queryDef.defaultBucketAgg()];
|
target.bucketAggs = [queryDef.defaultBucketAgg()];
|
||||||
target.metrics = [];
|
target.metrics = [];
|
||||||
// Setting this for metrics queries that are typed as logs
|
// Setting this for metrics queries that are typed as logs
|
||||||
target.isLogsQuery = true;
|
target.isLogsQuery = true;
|
||||||
queryObj = this.queryBuilder.getLogsQuery(target, adhocFilters, queryString);
|
queryObj = this.queryBuilder.getLogsQuery(target, adhocFilters, target.query);
|
||||||
} else {
|
} else {
|
||||||
if (target.alias) {
|
if (target.alias) {
|
||||||
target.alias = this.templateSrv.replace(target.alias, options.scopedVars, 'lucene');
|
target.alias = this.templateSrv.replace(target.alias, options.scopedVars, 'lucene');
|
||||||
}
|
}
|
||||||
|
|
||||||
queryObj = this.queryBuilder.build(target, adhocFilters, queryString);
|
queryObj = this.queryBuilder.build(target, adhocFilters, target.query);
|
||||||
}
|
}
|
||||||
|
|
||||||
const esQuery = angular.toJson(queryObj);
|
const esQuery = angular.toJson(queryObj);
|
||||||
|
|||||||
Reference in New Issue
Block a user