diff --git a/public/app/plugins/datasource/postgres/query_ctrl.ts b/public/app/plugins/datasource/postgres/query_ctrl.ts index b44692ed673..e95e4132e23 100644 --- a/public/app/plugins/datasource/postgres/query_ctrl.ts +++ b/public/app/plugins/datasource/postgres/query_ctrl.ts @@ -78,6 +78,7 @@ export class PostgresQueryCtrl extends QueryCtrl { this.selectMenu = [ { text: 'Aggregate', value: 'aggregate' }, { text: 'Math', value: 'math' }, + // { text: 'Special', value: 'special' }, { text: 'Alias', value: 'alias' }, { text: 'Column', value: 'column' }, ]; diff --git a/public/app/plugins/datasource/postgres/sql_part.ts b/public/app/plugins/datasource/postgres/sql_part.ts index e39a339c9da..3e4664c0caf 100644 --- a/public/app/plugins/datasource/postgres/sql_part.ts +++ b/public/app/plugins/datasource/postgres/sql_part.ts @@ -52,6 +52,30 @@ function replaceAggregationAddStrategy(selectParts, partModel) { selectParts.splice(1, 0, partModel); } +function replaceSpecialAddStrategy(selectParts, partModel) { + var hasAlias = false; + + // look for existing aggregation + for (var i = 0; i < selectParts.length; i++) { + var part = selectParts[i]; + if (part.def.type === 'special') { + selectParts[i] = partModel; + return; + } + if (part.def.type === 'alias') { + hasAlias = true; + } + } + + // add alias if none exists yet + if (!hasAlias) { + var aliasModel = createPart({ type: 'alias', params: [selectParts[0].params[0]] }); + selectParts.push(aliasModel); + } + + selectParts.splice(1, 0, partModel); +} + function addMathStrategy(selectParts, partModel) { var partCount = selectParts.length; if (partCount > 0) { @@ -184,6 +208,21 @@ register({ renderer: functionRenderer, }); +register({ + type: 'special', + style: 'label', + params: [ + { + name: 'function', + type: 'string', + options: ['increase', 'rate'], + }, + ], + defaultParams: ['increase'], + addStrategy: replaceSpecialAddStrategy, + renderer: aggregateRenderer, +}); + export default { create: createPart, };