refactor PostgresQueryCtrl

This commit is contained in:
Sven Klemm 2018-07-14 21:00:06 +02:00
parent d7ed706e12
commit 844beb660d

View File

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