Browse Source

remove hardcoded $__timeFilter, make macros functional in where clause

Sven Klemm 7 years ago
parent
commit
7600c6efcb

+ 10 - 5
public/app/plugins/datasource/postgres/postgres_query.ts

@@ -63,7 +63,7 @@ export default class PostgresQuery {
       });
     });
     this.target.where = _.map(this.whereParts, function(part: any) {
-      return { type: part.def.type, params: part.params };
+      return { type: part.def.type, name: part.name, params: part.params };
     });
     this.target.groupBy = _.map(this.groupByParts, function(part: any) {
       return { type: part.def.type, params: part.params };
@@ -196,15 +196,20 @@ export default class PostgresQuery {
 
     query += ' FROM ' + target.schema + '.' + target.table + ' WHERE ';
     var conditions = _.map(target.where, (tag, index) => {
-      return tag.params.join(' ');
+      switch (tag.type) {
+        case 'macro':
+          return tag.name + '(' + target.timeColumn + ')';
+          break;
+        case 'expression':
+          return tag.params.join(' ');
+          break;
+      }
     });
 
     if (conditions.length > 0) {
-      query += '(' + conditions.join(' AND ') + ') AND ';
+      query += conditions.join(' AND ');
     }
 
-    query += '$__timeFilter(' + target.timeColumn + ')';
-
     var groupBySection = '';
     for (i = 0; i < this.groupByParts.length; i++) {
       var part = this.groupByParts[i];

+ 5 - 6
public/app/plugins/datasource/postgres/query_ctrl.ts

@@ -287,18 +287,17 @@ export class PostgresQueryCtrl extends QueryCtrl {
 
   getWhereOptions() {
     var options = [];
-    options.push(this.uiSegmentSrv.newSegment({ type: 'function', value: '$__timeFilter' }));
-    options.push(this.uiSegmentSrv.newSegment({ type: 'function', value: '$__unixEpochFilter' }));
-    options.push(this.uiSegmentSrv.newSegment({ type: 'function', value: 'Expression' }));
+    options.push(this.uiSegmentSrv.newSegment({ type: 'macro', value: '$__timeFilter' }));
+    options.push(this.uiSegmentSrv.newSegment({ type: 'macro', value: '$__unixEpochFilter' }));
+    options.push(this.uiSegmentSrv.newSegment({ type: 'expression', value: 'Expression' }));
     return this.$q.when(options);
   }
 
   whereAddAction(part, index) {
     switch (this.whereAdd.type) {
       case 'macro': {
-        this.queryModel.whereParts.push(
-          sqlPart.create({ type: 'function', name: this.whereAdd.value, params: ['value', '=', 'value'] })
-        );
+        this.queryModel.whereParts.push(sqlPart.create({ type: 'macro', name: this.whereAdd.value, params: [] }));
+        break;
       }
       default: {
         this.queryModel.whereParts.push(sqlPart.create({ type: 'expression', params: ['value', '=', 'value'] }));

+ 10 - 0
public/app/plugins/datasource/postgres/sql_part.ts

@@ -115,6 +115,16 @@ register({
   renderer: columnRenderer,
 });
 
+register({
+  type: 'macro',
+  style: 'function',
+  label: 'Macro:',
+  addStrategy: addExpressionStrategy,
+  params: [],
+  defaultParams: [],
+  renderer: columnRenderer,
+});
+
 register({
   type: 'aggregate',
   style: 'label',