remove hardcoded $__timeFilter, make macros functional in where clause

This commit is contained in:
Sven Klemm
2018-07-06 10:38:52 +02:00
parent 8ed210c8d5
commit 7600c6efcb
3 changed files with 25 additions and 11 deletions
@@ -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];
@@ -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'] }));
@@ -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',