fix group by ui

This commit is contained in:
Sven Klemm
2018-07-05 10:19:22 +02:00
parent aacf555985
commit c604651092
3 changed files with 21 additions and 26 deletions

View File

@ -86,7 +86,7 @@
</div> </div>
<div class="gf-form"> <div class="gf-form">
<metric-segment segment="ctrl.groupBySegment" get-options="ctrl.getGroupByOptions()" on-change="ctrl.groupByAction(part, $index)"></metric-segment> <metric-segment segment="ctrl.groupByAdd" get-options="ctrl.getGroupByOptions()" on-change="ctrl.groupByAction(part, $index)"></metric-segment>
</div> </div>
<div class="gf-form gf-form--grow"> <div class="gf-form gf-form--grow">

View File

@ -65,25 +65,21 @@ export default class PostgresQuery {
return _.find(this.target.groupBy, (g: any) => g.type === 'time'); return _.find(this.target.groupBy, (g: any) => g.type === 'time');
} }
addGroupBy(value) { addGroupBy(partType, value) {
var stringParts = value.match(/^(\w+)(\((.*)\))?$/); var partModel = sqlPart.create({ type: partType, params: [value] });
var typePart = stringParts[1];
var args = stringParts[3].split(',');
var partModel = sqlPart.create({ type: typePart, params: args });
var partCount = this.target.groupBy.length; var partCount = this.target.groupBy.length;
if (partCount === 0) { if (partCount === 0) {
this.target.groupBy.push(partModel.part); this.target.groupBy.push(partModel.part);
} else if (typePart === 'time') { } else if (partType === 'time') {
// put timeGroup at start
this.target.groupBy.splice(0, 0, partModel.part); this.target.groupBy.splice(0, 0, partModel.part);
} else if (typePart === 'column') {
if (this.target.groupBy[partCount - 1].type === 'fill') {
this.target.groupBy.splice(partCount - 1, 0, partModel.part);
} else { } else {
this.target.groupBy.push(partModel.part); this.target.groupBy.push(partModel.part);
} }
} else {
this.target.groupBy.push(partModel.part); if (partType === 'time') {
partModel.part.params = ['1m', 'none'];
} }
this.updateProjection(); this.updateProjection();
@ -126,11 +122,6 @@ export default class PostgresQuery {
this.updatePersistedParts(); this.updatePersistedParts();
} }
removeWherePart(whereParts, part) {
var partIndex = _.indexOf(whereParts, part);
whereParts.splice(partIndex, 1);
}
addSelectPart(selectParts, type) { addSelectPart(selectParts, type) {
var partModel = sqlPart.create({ type: type }); var partModel = sqlPart.create({ type: type });
partModel.def.addStrategy(selectParts, partModel, this); partModel.def.addStrategy(selectParts, partModel, this);

View File

@ -33,7 +33,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
timeColumnSegment: any; timeColumnSegment: any;
metricColumnSegment: any; metricColumnSegment: any;
selectMenu: any; selectMenu: any;
groupBySegment: any; groupByAdd: any;
/** @ngInject **/ /** @ngInject **/
constructor($scope, $injector, private templateSrv, private $q, private uiSegmentSrv) { constructor($scope, $injector, private templateSrv, private $q, private uiSegmentSrv) {
@ -68,7 +68,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
this.buildSelectMenu(); this.buildSelectMenu();
this.buildWhereSegments(); this.buildWhereSegments();
this.whereAdd = this.uiSegmentSrv.newPlusButton(); this.whereAdd = this.uiSegmentSrv.newPlusButton();
this.groupBySegment = this.uiSegmentSrv.newPlusButton(); this.groupByAdd = this.uiSegmentSrv.newPlusButton();
this.panelCtrl.events.on('data-received', this.onDataReceived.bind(this), $scope); this.panelCtrl.events.on('data-received', this.onDataReceived.bind(this), $scope);
this.panelCtrl.events.on('data-error', this.onDataError.bind(this), $scope); this.panelCtrl.events.on('data-error', this.onDataError.bind(this), $scope);
@ -273,7 +273,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
break; break;
} }
case 'action': { case 'action': {
this.queryModel.removeWherePart(part, index); whereParts.splice(index, 1);
this.panelCtrl.refresh(); this.panelCtrl.refresh();
break; break;
} }
@ -302,7 +302,11 @@ export class PostgresQueryCtrl extends QueryCtrl {
this.queryModel.whereParts.push(sqlPart.create({ type: 'expression', params: ['value', '=', 'value'] })); this.queryModel.whereParts.push(sqlPart.create({ type: 'expression', params: ['value', '=', 'value'] }));
} }
} }
this.whereAdd = this.uiSegmentSrv.newPlusButton();
var plusButton = this.uiSegmentSrv.newPlusButton();
this.whereAdd.html = plusButton.html;
this.whereAdd.value = plusButton.value;
this.queryModel.updatePersistedParts(); this.queryModel.updatePersistedParts();
this.panelCtrl.refresh(); this.panelCtrl.refresh();
} }
@ -324,15 +328,15 @@ export class PostgresQueryCtrl extends QueryCtrl {
} }
groupByAction() { groupByAction() {
switch (this.groupBySegment.value) { switch (this.groupByAdd.value) {
default: { default: {
this.queryModel.addGroupBy(this.groupBySegment.value); this.queryModel.addGroupBy(this.groupByAdd.type, this.groupByAdd.value);
} }
} }
var plusButton = this.uiSegmentSrv.newPlusButton(); var plusButton = this.uiSegmentSrv.newPlusButton();
this.groupBySegment.value = plusButton.value; this.groupByAdd.html = plusButton.html;
this.groupBySegment.html = plusButton.html; this.groupByAdd.value = plusButton.value;
this.panelCtrl.refresh(); this.panelCtrl.refresh();
} }