Bläddra i källkod

4075: Interpolate tempvar on alias

SamuelToh 7 år sedan
förälder
incheckning
e1f6870fce

+ 4 - 0
public/app/plugins/datasource/elasticsearch/datasource.ts

@@ -254,6 +254,10 @@ export class ElasticDatasource {
         continue;
         continue;
       }
       }
 
 
+      if (target.alias) {
+        target.alias = this.templateSrv.replace(target.alias, options.scopedVars, 'lucene');
+      }
+
       const queryString = this.templateSrv.replace(target.query || '*', options.scopedVars, 'lucene');
       const queryString = this.templateSrv.replace(target.query || '*', options.scopedVars, 'lucene');
       const queryObj = this.queryBuilder.build(target, adhocFilters, queryString);
       const queryObj = this.queryBuilder.build(target, adhocFilters, queryString);
       const esQuery = angular.toJson(queryObj);
       const esQuery = angular.toJson(queryObj);

+ 17 - 4
public/app/plugins/datasource/elasticsearch/specs/datasource.test.ts

@@ -16,7 +16,13 @@ describe('ElasticDatasource', function(this: any) {
   };
   };
 
 
   const templateSrv = {
   const templateSrv = {
-    replace: jest.fn(text => text),
+    replace: jest.fn(text => {
+      if (text.startsWith("$")) {
+        return `resolvedVariable`;
+      } else {
+        return text;
+      }
+    }),
     getAdhocFilters: jest.fn(() => []),
     getAdhocFilters: jest.fn(() => []),
   };
   };
 
 
@@ -67,7 +73,7 @@ describe('ElasticDatasource', function(this: any) {
   });
   });
 
 
   describe('When issuing metric query with interval pattern', () => {
   describe('When issuing metric query with interval pattern', () => {
-    let requestOptions, parts, header;
+    let requestOptions, parts, header, query;
 
 
     beforeEach(() => {
     beforeEach(() => {
       createDatasource({
       createDatasource({
@@ -81,19 +87,22 @@ describe('ElasticDatasource', function(this: any) {
         return Promise.resolve({ data: { responses: [] } });
         return Promise.resolve({ data: { responses: [] } });
       });
       });
 
 
-      ctx.ds.query({
+      query = {
         range: {
         range: {
           from: moment.utc([2015, 4, 30, 10]),
           from: moment.utc([2015, 4, 30, 10]),
           to: moment.utc([2015, 5, 1, 10]),
           to: moment.utc([2015, 5, 1, 10]),
         },
         },
         targets: [
         targets: [
           {
           {
+            alias: "$varAlias",
             bucketAggs: [],
             bucketAggs: [],
             metrics: [{ type: 'raw_document' }],
             metrics: [{ type: 'raw_document' }],
             query: 'escape\\:test',
             query: 'escape\\:test',
           },
           },
         ],
         ],
-      });
+      };
+
+      ctx.ds.query(query);
 
 
       parts = requestOptions.data.split('\n');
       parts = requestOptions.data.split('\n');
       header = angular.fromJson(parts[0]);
       header = angular.fromJson(parts[0]);
@@ -103,6 +112,10 @@ describe('ElasticDatasource', function(this: any) {
       expect(header.index).toEqual(['asd-2015.05.30', 'asd-2015.05.31', 'asd-2015.06.01']);
       expect(header.index).toEqual(['asd-2015.05.30', 'asd-2015.05.31', 'asd-2015.06.01']);
     });
     });
 
 
+    it('should resolve the alias variable', () => {
+      expect(query.targets[0].alias).toEqual('resolvedVariable');
+    });
+
     it('should json escape lucene query', () => {
     it('should json escape lucene query', () => {
       const body = angular.fromJson(parts[1]);
       const body = angular.fromJson(parts[1]);
       expect(body.query.bool.filter[1].query_string.query).toBe('escape\\:test');
       expect(body.query.bool.filter[1].query_string.query).toBe('escape\\:test');