diff --git a/public/app/plugins/datasource/postgres/query_ctrl.ts b/public/app/plugins/datasource/postgres/query_ctrl.ts index 27c3d1e50fa..19e2f8b3a18 100644 --- a/public/app/plugins/datasource/postgres/query_ctrl.ts +++ b/public/app/plugins/datasource/postgres/query_ctrl.ts @@ -112,9 +112,11 @@ export class PostgresQueryCtrl extends QueryCtrl { this.target.rawQuery = !this.target.rawQuery; } - resetPlusButton(button) {} - - // schema functions + resetPlusButton(button) { + let plusButton = this.uiSegmentSrv.newPlusButton(); + button.html = plusButton.html; + button.value = plusButton.value; + } getSchemaSegments() { return this.datasource @@ -128,8 +130,6 @@ export class PostgresQueryCtrl extends QueryCtrl { this.panelCtrl.refresh(); } - // table functions - getTableSegments() { return this.datasource .metricFindQuery(this.queryBuilder.buildTableQuery()) @@ -137,6 +137,11 @@ export class PostgresQueryCtrl extends QueryCtrl { .catch(this.handleQueryError.bind(this)); } + tableChanged() { + this.target.table = this.tableSegment.value; + this.panelCtrl.refresh(); + } + getTimeColumnSegments() { return this.datasource .metricFindQuery(this.queryBuilder.buildColumnQuery('time')) @@ -144,6 +149,11 @@ export class PostgresQueryCtrl extends QueryCtrl { .catch(this.handleQueryError.bind(this)); } + timeColumnChanged() { + this.target.timeColumn = this.timeColumnSegment.value; + this.panelCtrl.refresh(); + } + getMetricColumnSegments() { return this.datasource .metricFindQuery(this.queryBuilder.buildColumnQuery('metric')) @@ -151,16 +161,6 @@ export class PostgresQueryCtrl extends QueryCtrl { .catch(this.handleQueryError.bind(this)); } - tableChanged() { - this.target.table = this.tableSegment.value; - this.panelCtrl.refresh(); - } - - timeColumnChanged() { - this.target.timeColumn = this.timeColumnSegment.value; - this.panelCtrl.refresh(); - } - metricColumnChanged() { this.target.metricColumn = this.metricColumnSegment.value; this.panelCtrl.refresh(); @@ -188,7 +188,7 @@ export class PostgresQueryCtrl extends QueryCtrl { transformToSegments(config) { return results => { - var segments = _.map(results, segment => { + let segments = _.map(results, segment => { return this.uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable, @@ -197,7 +197,7 @@ export class PostgresQueryCtrl extends QueryCtrl { if (config.addTemplateVars) { for (let variable of this.templateSrv.variables) { - var value; + let value; value = '$' + variable.name; if (config.templateQuoter && variable.multi === false) { value = config.templateQuoter(value); @@ -222,17 +222,15 @@ export class PostgresQueryCtrl extends QueryCtrl { } addSelectPart(selectParts, cat, subitem) { - if ('submenu' in cat) { - this.addSelectPart2(selectParts, subitem.value); - } else { - this.addSelectPart2(selectParts, cat.value); - } + let partModel = sqlPart.create({ type: cat.value }); + partModel.def.addStrategy(selectParts, partModel, this); + this.updatePersistedParts(); this.panelCtrl.refresh(); } removeSelectPart(selectParts, part) { - // if we remove the field remove the whole statement if (part.def.type === 'column') { + // remove all parts of column unless its last column if (this.selectModels.length > 1) { let modelsIndex = _.indexOf(this.selectModels, selectParts); this.selectModels.splice(modelsIndex, 1); @@ -245,12 +243,6 @@ export class PostgresQueryCtrl extends QueryCtrl { this.updatePersistedParts(); } - addSelectPart2(selectParts, type) { - let partModel = sqlPart.create({ type: type }); - partModel.def.addStrategy(selectParts, partModel, this); - this.updatePersistedParts(); - } - handleSelectPartEvent(selectParts, part, evt) { switch (evt.name) { case 'get-param-options': { @@ -320,8 +312,7 @@ export class PostgresQueryCtrl extends QueryCtrl { } // add aggregates when adding group by - for (let i = 0; i < this.selectModels.length; i++) { - var selectParts = this.selectModels[i]; + for (let selectParts of this.selectModels) { if (!selectParts.some(part => part.def.type === 'aggregate')) { let aggregate = sqlPart.create({ type: 'aggregate', params: ['avg'] }); selectParts.splice(1, 0, aggregate); @@ -352,11 +343,6 @@ export class PostgresQueryCtrl extends QueryCtrl { this.updatePersistedParts(); } - buildWhereSegments() { - // this.whereSegments = []; - // this.whereSegments.push(sqlPart.create({ type: 'expression', params: ['value', '=', 'value'] })); - } - handleWherePartEvent(whereParts, part, evt, index) { switch (evt.name) { case 'get-param-options': { @@ -413,11 +399,8 @@ export class PostgresQueryCtrl extends QueryCtrl { } } - var plusButton = this.uiSegmentSrv.newPlusButton(); - this.whereAdd.html = plusButton.html; - this.whereAdd.value = plusButton.value; - this.updatePersistedParts(); + this.resetPlusButton(this.whereAdd); this.panelCtrl.refresh(); } @@ -444,9 +427,7 @@ export class PostgresQueryCtrl extends QueryCtrl { } } - var plusButton = this.uiSegmentSrv.newPlusButton(); - this.groupByAdd.html = plusButton.html; - this.groupByAdd.value = plusButton.value; + this.resetPlusButton(this.groupByAdd); this.panelCtrl.refresh(); }