diff --git a/public/app/plugins/datasource/postgres/query_ctrl.ts b/public/app/plugins/datasource/postgres/query_ctrl.ts index 41472a95da0..0ef5dadf739 100644 --- a/public/app/plugins/datasource/postgres/query_ctrl.ts +++ b/public/app/plugins/datasource/postgres/query_ctrl.ts @@ -111,10 +111,27 @@ export class PostgresQueryCtrl extends QueryCtrl { this.panelCtrl.refresh(); } + timescaleAggCheck() { + const aggIndex = this.findAggregateIndex(this.selectParts[0]); + + // add or remove TimescaleDB aggregate functions as needed + if (aggIndex !== -1) { + const baseOpts = this.selectParts[0][aggIndex].def.params[0].baseOptions; + const timescaleOpts = baseOpts.concat(this.selectParts[0][aggIndex].def.params[0].timescaleOptions); + + if (this.datasource.jsonData.timescaledb === true) { + this.selectParts[0][aggIndex].def.params[0].options = timescaleOpts; + } else { + this.selectParts[0][aggIndex].def.params[0].options = baseOpts; + } + } + } + updateProjection() { this.selectParts = map(this.target.select, (parts: any) => { return map(parts, sqlPart.create).filter((n) => n); }); + this.timescaleAggCheck(); this.whereParts = map(this.target.where, sqlPart.create).filter((n) => n); this.groupParts = map(this.target.group, sqlPart.create).filter((n) => n); } @@ -125,6 +142,7 @@ export class PostgresQueryCtrl extends QueryCtrl { return { type: part.def.type, datatype: part.datatype, params: part.params }; }); }); + this.timescaleAggCheck(); this.target.where = map(this.whereParts, (part: any) => { return { type: part.def.type, datatype: part.datatype, name: part.name, params: part.params }; }); diff --git a/public/app/plugins/datasource/postgres/sql_part.ts b/public/app/plugins/datasource/postgres/sql_part.ts index 17d78995e1e..13266f783dc 100644 --- a/public/app/plugins/datasource/postgres/sql_part.ts +++ b/public/app/plugins/datasource/postgres/sql_part.ts @@ -49,7 +49,9 @@ register({ { name: 'name', type: 'string', - options: ['avg', 'count', 'min', 'max', 'sum', 'stddev', 'variance'], + options: [], + baseOptions: ['avg', 'count', 'min', 'max', 'sum', 'stddev', 'variance'], + timescaleOptions: ['first', 'last'], }, ], defaultParams: ['avg'],