refactor addGroupBy and removeGroupByPart

This commit is contained in:
Sven Klemm
2018-07-14 16:46:53 +02:00
parent 569a8567f9
commit 2dc9d4e960

View File

@ -86,42 +86,41 @@ export default class PostgresQuery {
} }
addGroupBy(partType, value) { addGroupBy(partType, value) {
let partModel = sqlPart.create({ type: partType, params: [value] }); let params = [value];
let partCount = this.target.groupBy.length; if (partType === 'time') {
params = ['1m', 'none'];
if (partCount === 0) {
this.target.groupBy.push(partModel.part);
} else if (partType === 'time') {
// put timeGroup at start
this.target.groupBy.splice(0, 0, partModel.part);
} else {
this.target.groupBy.push(partModel.part);
} }
let partModel = sqlPart.create({ type: partType, params: params });
if (partType === 'time') { if (partType === 'time') {
partModel.part.params = ['1m', 'none']; // put timeGroup at start
this.groupByParts.splice(0, 0, partModel);
} else {
this.groupByParts(partModel);
} }
// add aggregates when adding group by // add aggregates when adding group by
for (let i = 0; i < this.target.select.length; i++) { for (let i = 0; i < this.selectModels.length; i++) {
var selectParts = this.target.select[i]; var selectParts = this.selectModels[i];
if (!selectParts.some(part => part.type === 'aggregate')) { if (!selectParts.some(part => part.def.type === 'aggregate')) {
selectParts.splice(1, 0, { type: 'aggregate', params: ['avg'] }); let aggregate = sqlPart.create({ type: 'aggregate', params: ['avg'] });
if (!selectParts.some(part => part.type === 'alias')) { selectParts.splice(1, 0, aggregate);
selectParts.push({ type: 'alias', params: [selectParts[0].params[0]] }); if (!selectParts.some(part => part.def.type === 'alias')) {
let alias = sqlPart.create({ type: 'alias', params: [selectParts[0].part.params[0]] });
selectParts.push(alias);
} }
} }
} }
this.updateProjection(); this.updatePersistedParts();
} }
removeGroupByPart(part, index) { removeGroupByPart(part, index) {
if (part.def.type === 'time') { if (part.def.type === 'time') {
// remove aggregations // remove aggregations
this.target.select = _.map(this.target.select, (s: any) => { this.selectModels = _.map(this.selectModels, (s: any) => {
return _.filter(s, (part: any) => { return _.filter(s, (part: any) => {
if (part.type === 'aggregate') { if (part.def.type === 'aggregate') {
return false; return false;
} }
return true; return true;
@ -129,8 +128,8 @@ export default class PostgresQuery {
}); });
} }
this.target.groupBy.splice(index, 1); this.groupByParts.splice(index, 1);
this.updateProjection(); this.updatePersistedParts();
} }
removeSelectPart(selectParts, part) { removeSelectPart(selectParts, part) {