diff --git a/public/app/plugins/datasource/postgres/postgres_query.ts b/public/app/plugins/datasource/postgres/postgres_query.ts index 4c9bd862658..9715665fd4b 100644 --- a/public/app/plugins/datasource/postgres/postgres_query.ts +++ b/public/app/plugins/datasource/postgres/postgres_query.ts @@ -135,7 +135,7 @@ export default class PostgresQuery { query = columnName.params[0]; let aggregate = _.find(column, (g: any) => g.type === 'aggregate' || g.type === 'percentile'); - let special = _.find(column, (g: any) => g.type === 'window'); + let windows = _.find(column, (g: any) => g.type === 'window'); if (aggregate) { let func = aggregate.params[0]; @@ -144,7 +144,7 @@ export default class PostgresQuery { if (func === 'first' || func === 'last') { query = func + '(' + query + ',' + this.target.timeColumn + ')'; } else { - if (special) { + if (windows) { query = func + '(' + query + ' ORDER BY ' + this.target.timeColumn + ')'; } else { query = func + '(' + query + ')'; @@ -157,7 +157,7 @@ export default class PostgresQuery { } } - if (special) { + if (windows) { let overParts = []; if (this.hasMetricColumn()) { overParts.push('PARTITION BY ' + this.target.metricColumn); @@ -169,7 +169,7 @@ export default class PostgresQuery { let over = overParts.join(' '); let curr: string; let prev: string; - switch (special.params[0]) { + switch (windows.params[0]) { case 'increase': curr = query; prev = 'lag(' + curr + ') OVER (' + over + ')'; @@ -187,7 +187,7 @@ export default class PostgresQuery { query += '/extract(epoch from ' + timeColumn + ' - lag(' + timeColumn + ') OVER (' + over + '))'; break; default: - query = special.params[0] + '(' + query + ') OVER (' + over + ')'; + query = windows.params[0] + '(' + query + ') OVER (' + over + ')'; break; } } diff --git a/public/app/plugins/datasource/postgres/query_ctrl.ts b/public/app/plugins/datasource/postgres/query_ctrl.ts index 181a3a9234c..b15a9773436 100644 --- a/public/app/plugins/datasource/postgres/query_ctrl.ts +++ b/public/app/plugins/datasource/postgres/query_ctrl.ts @@ -246,6 +246,10 @@ export class PostgresQueryCtrl extends QueryCtrl { }; } + findAggregateIndex(selectParts) { + return _.findIndex(selectParts, (p: any) => p.def.type === 'aggregate' || p.def.type === 'percentile'); + } + addSelectPart(selectParts, item, subItem) { let partModel = sqlPart.create({ type: item.value }); if (subItem) { @@ -267,7 +271,7 @@ export class PostgresQueryCtrl extends QueryCtrl { if (this.target.group.length === 0) { this.addGroup('time', '1m'); } - let aggIndex = _.findIndex(selectParts, (p: any) => p.def.type === 'aggregate' || p.def.type === 'percentile'); + let aggIndex = this.findAggregateIndex(selectParts); if (aggIndex !== -1) { // replace current aggregation selectParts[aggIndex] = partModel; @@ -284,10 +288,7 @@ export class PostgresQueryCtrl extends QueryCtrl { // replace current window function selectParts[windowIndex] = partModel; } else { - let aggIndex = _.findIndex( - selectParts, - (p: any) => p.def.type === 'aggregate' || p.def.type === 'percentile' - ); + let aggIndex = this.findAggregateIndex(selectParts); if (aggIndex !== -1) { selectParts.splice(aggIndex + 1, 0, partModel); } else {