mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
consistent nameing fro group and select
This commit is contained in:
parent
ad26a319c5
commit
6ca7a03975
@ -111,7 +111,7 @@ table_schema IN (
|
||||
query += ' AND column_name <> ' + this.quoteIdentAsLiteral(this.target.timeColumn);
|
||||
break;
|
||||
}
|
||||
case 'groupby': {
|
||||
case 'group': {
|
||||
query += " AND data_type IN ('text','char','varchar')";
|
||||
break;
|
||||
}
|
||||
|
@ -31,7 +31,7 @@
|
||||
|
||||
</div>
|
||||
|
||||
<div class="gf-form-inline" ng-repeat="selectParts in ctrl.selectModels">
|
||||
<div class="gf-form-inline" ng-repeat="selectParts in ctrl.selectParts">
|
||||
<div class="gf-form">
|
||||
<label class="gf-form-label query-keyword width-6">
|
||||
<span ng-show="$index === 0">SELECT</span>
|
||||
@ -81,14 +81,14 @@
|
||||
<span>GROUP BY</span>
|
||||
</label>
|
||||
|
||||
<sql-part-editor ng-repeat="part in ctrl.groupByParts"
|
||||
<sql-part-editor ng-repeat="part in ctrl.groupParts"
|
||||
part="part" class="gf-form-label sql-part"
|
||||
handle-event="ctrl.handleGroupByPartEvent(part, $index, $event)">
|
||||
handle-event="ctrl.onGroupPartEvent(part, $index, $event)">
|
||||
</sql-part-editor>
|
||||
</div>
|
||||
|
||||
<div class="gf-form">
|
||||
<metric-segment segment="ctrl.groupByAdd" get-options="ctrl.getGroupByOptions()" on-change="ctrl.groupByAction(part, $index)"></metric-segment>
|
||||
<metric-segment segment="ctrl.groupAdd" get-options="ctrl.getGroupOptions()" on-change="ctrl.onGroupAction(part, $index)"></metric-segment>
|
||||
</div>
|
||||
|
||||
<div class="gf-form gf-form--grow">
|
||||
|
@ -15,7 +15,7 @@ export default class PostgresQuery {
|
||||
target.timeColumn = target.timeColumn || 'time';
|
||||
target.metricColumn = target.metricColumn || 'none';
|
||||
|
||||
target.groupBy = target.groupBy || [];
|
||||
target.group = target.group || [];
|
||||
target.where = target.where || [{ type: 'macro', name: '$__timeFilter', params: [] }];
|
||||
target.select = target.select || [[{ type: 'column', params: ['value'] }]];
|
||||
|
||||
@ -51,8 +51,8 @@ export default class PostgresQuery {
|
||||
return "'" + value.replace("'", "''") + "'";
|
||||
}
|
||||
|
||||
hasGroupByTime() {
|
||||
return _.find(this.target.groupBy, (g: any) => g.type === 'time');
|
||||
hasTimeGroup() {
|
||||
return _.find(this.target.group, (g: any) => g.type === 'time');
|
||||
}
|
||||
|
||||
hasMetricColumn() {
|
||||
@ -93,7 +93,7 @@ export default class PostgresQuery {
|
||||
}
|
||||
|
||||
buildTimeColumn() {
|
||||
let timeGroup = this.hasGroupByTime();
|
||||
let timeGroup = this.hasTimeGroup();
|
||||
let query;
|
||||
|
||||
if (timeGroup) {
|
||||
@ -201,24 +201,24 @@ export default class PostgresQuery {
|
||||
return query;
|
||||
}
|
||||
|
||||
buildGroupByClause() {
|
||||
buildGroupClause() {
|
||||
let query = '';
|
||||
let groupBySection = '';
|
||||
let groupSection = '';
|
||||
|
||||
for (let i = 0; i < this.target.groupBy.length; i++) {
|
||||
let part = this.target.groupBy[i];
|
||||
for (let i = 0; i < this.target.group.length; i++) {
|
||||
let part = this.target.group[i];
|
||||
if (i > 0) {
|
||||
groupBySection += ', ';
|
||||
groupSection += ', ';
|
||||
}
|
||||
if (part.type === 'time') {
|
||||
groupBySection += '1';
|
||||
groupSection += '1';
|
||||
} else {
|
||||
groupBySection += part.params[0];
|
||||
groupSection += part.params[0];
|
||||
}
|
||||
}
|
||||
|
||||
if (groupBySection.length) {
|
||||
query = '\nGROUP BY ' + groupBySection;
|
||||
if (groupSection.length) {
|
||||
query = '\nGROUP BY ' + groupSection;
|
||||
if (this.hasMetricColumn()) {
|
||||
query += ',2';
|
||||
}
|
||||
@ -238,7 +238,7 @@ export default class PostgresQuery {
|
||||
query += '\nFROM ' + this.target.table;
|
||||
|
||||
query += this.buildWhereClause();
|
||||
query += this.buildGroupByClause();
|
||||
query += this.buildGroupClause();
|
||||
|
||||
query += '\nORDER BY 1';
|
||||
|
||||
|
@ -33,10 +33,10 @@ export class PostgresQueryCtrl extends QueryCtrl {
|
||||
timeColumnSegment: any;
|
||||
metricColumnSegment: any;
|
||||
selectMenu: any[];
|
||||
selectModels: SqlPart[][];
|
||||
groupByParts: SqlPart[][];
|
||||
whereParts: SqlPart[][];
|
||||
groupByAdd: any;
|
||||
selectParts: SqlPart[][];
|
||||
groupParts: SqlPart[];
|
||||
whereParts: SqlPart[];
|
||||
groupAdd: any;
|
||||
|
||||
/** @ngInject **/
|
||||
constructor($scope, $injector, private templateSrv, private $q, private uiSegmentSrv) {
|
||||
@ -69,22 +69,22 @@ export class PostgresQueryCtrl extends QueryCtrl {
|
||||
|
||||
this.buildSelectMenu();
|
||||
this.whereAdd = this.uiSegmentSrv.newPlusButton();
|
||||
this.groupByAdd = this.uiSegmentSrv.newPlusButton();
|
||||
this.groupAdd = this.uiSegmentSrv.newPlusButton();
|
||||
|
||||
this.panelCtrl.events.on('data-received', this.onDataReceived.bind(this), $scope);
|
||||
this.panelCtrl.events.on('data-error', this.onDataError.bind(this), $scope);
|
||||
}
|
||||
|
||||
updateProjection() {
|
||||
this.selectModels = _.map(this.target.select, function(parts: any) {
|
||||
this.selectParts = _.map(this.target.select, function(parts: any) {
|
||||
return _.map(parts, sqlPart.create).filter(n => n);
|
||||
});
|
||||
this.whereParts = _.map(this.target.where, sqlPart.create).filter(n => n);
|
||||
this.groupByParts = _.map(this.target.groupBy, sqlPart.create).filter(n => n);
|
||||
this.groupParts = _.map(this.target.group, sqlPart.create).filter(n => n);
|
||||
}
|
||||
|
||||
updatePersistedParts() {
|
||||
this.target.select = _.map(this.selectModels, function(selectParts) {
|
||||
this.target.select = _.map(this.selectParts, function(selectParts) {
|
||||
return _.map(selectParts, function(part: any) {
|
||||
return { type: part.def.type, params: part.params };
|
||||
});
|
||||
@ -92,7 +92,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
|
||||
this.target.where = _.map(this.whereParts, function(part: any) {
|
||||
return { type: part.def.type, name: part.name, params: part.params };
|
||||
});
|
||||
this.target.groupBy = _.map(this.groupByParts, function(part: any) {
|
||||
this.target.group = _.map(this.groupParts, function(part: any) {
|
||||
return { type: part.def.type, params: part.params };
|
||||
});
|
||||
}
|
||||
@ -216,12 +216,12 @@ export class PostgresQueryCtrl extends QueryCtrl {
|
||||
let parts = _.map(selectParts, function(part: any) {
|
||||
return sqlPart.create({ type: part.def.type, params: _.clone(part.params) });
|
||||
});
|
||||
this.selectModels.push(parts);
|
||||
this.selectParts.push(parts);
|
||||
break;
|
||||
case 'aggregate':
|
||||
// add group by if no group by yet
|
||||
if (this.target.groupBy.length === 0) {
|
||||
this.addGroupBy('time', '1m');
|
||||
if (this.target.group.length === 0) {
|
||||
this.addGroup('time', '1m');
|
||||
}
|
||||
case 'special':
|
||||
let index = _.findIndex(selectParts, (p: any) => p.def.type === item.value);
|
||||
@ -256,9 +256,9 @@ export class PostgresQueryCtrl extends QueryCtrl {
|
||||
removeSelectPart(selectParts, part) {
|
||||
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);
|
||||
if (this.selectParts.length > 1) {
|
||||
let modelsIndex = _.indexOf(this.selectParts, selectParts);
|
||||
this.selectParts.splice(modelsIndex, 1);
|
||||
}
|
||||
} else {
|
||||
let partIndex = _.indexOf(selectParts, part);
|
||||
@ -299,7 +299,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
|
||||
}
|
||||
}
|
||||
|
||||
handleGroupByPartEvent(part, index, evt) {
|
||||
onGroupPartEvent(part, index, evt) {
|
||||
switch (evt.name) {
|
||||
case 'get-param-options': {
|
||||
return this.datasource
|
||||
@ -312,7 +312,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
|
||||
break;
|
||||
}
|
||||
case 'action': {
|
||||
this.removeGroupBy(part, index);
|
||||
this.removeGroup(part, index);
|
||||
this.panelCtrl.refresh();
|
||||
break;
|
||||
}
|
||||
@ -322,7 +322,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
|
||||
}
|
||||
}
|
||||
|
||||
addGroupBy(partType, value) {
|
||||
addGroup(partType, value) {
|
||||
let params = [value];
|
||||
if (partType === 'time') {
|
||||
params = ['1m', 'none'];
|
||||
@ -331,13 +331,13 @@ export class PostgresQueryCtrl extends QueryCtrl {
|
||||
|
||||
if (partType === 'time') {
|
||||
// put timeGroup at start
|
||||
this.groupByParts.splice(0, 0, partModel);
|
||||
this.groupParts.splice(0, 0, partModel);
|
||||
} else {
|
||||
this.groupByParts.push(partModel);
|
||||
this.groupParts.push(partModel);
|
||||
}
|
||||
|
||||
// add aggregates when adding group by
|
||||
for (let selectParts of this.selectModels) {
|
||||
for (let selectParts of this.selectParts) {
|
||||
if (!selectParts.some(part => part.def.type === 'aggregate')) {
|
||||
let aggregate = sqlPart.create({ type: 'aggregate', params: ['avg'] });
|
||||
selectParts.splice(1, 0, aggregate);
|
||||
@ -351,10 +351,10 @@ export class PostgresQueryCtrl extends QueryCtrl {
|
||||
this.updatePersistedParts();
|
||||
}
|
||||
|
||||
removeGroupBy(part, index) {
|
||||
removeGroup(part, index) {
|
||||
if (part.def.type === 'time') {
|
||||
// remove aggregations
|
||||
this.selectModels = _.map(this.selectModels, (s: any) => {
|
||||
this.selectParts = _.map(this.selectParts, (s: any) => {
|
||||
return _.filter(s, (part: any) => {
|
||||
if (part.def.type === 'aggregate') {
|
||||
return false;
|
||||
@ -364,7 +364,7 @@ export class PostgresQueryCtrl extends QueryCtrl {
|
||||
});
|
||||
}
|
||||
|
||||
this.groupByParts.splice(index, 1);
|
||||
this.groupParts.splice(index, 1);
|
||||
this.updatePersistedParts();
|
||||
}
|
||||
|
||||
@ -429,12 +429,12 @@ export class PostgresQueryCtrl extends QueryCtrl {
|
||||
this.panelCtrl.refresh();
|
||||
}
|
||||
|
||||
getGroupByOptions() {
|
||||
getGroupOptions() {
|
||||
return this.datasource
|
||||
.metricFindQuery(this.metaBuilder.buildColumnQuery('groupby'))
|
||||
.metricFindQuery(this.metaBuilder.buildColumnQuery('group'))
|
||||
.then(tags => {
|
||||
var options = [];
|
||||
if (!this.queryModel.hasGroupByTime()) {
|
||||
if (!this.queryModel.hasTimeGroup()) {
|
||||
options.push(this.uiSegmentSrv.newSegment({ type: 'time', value: 'time(1m,none)' }));
|
||||
}
|
||||
for (let tag of tags) {
|
||||
@ -445,14 +445,14 @@ export class PostgresQueryCtrl extends QueryCtrl {
|
||||
.catch(this.handleQueryError.bind(this));
|
||||
}
|
||||
|
||||
groupByAction() {
|
||||
switch (this.groupByAdd.value) {
|
||||
onGroupAction() {
|
||||
switch (this.groupAdd.value) {
|
||||
default: {
|
||||
this.addGroupBy(this.groupByAdd.type, this.groupByAdd.value);
|
||||
this.addGroup(this.groupAdd.type, this.groupAdd.value);
|
||||
}
|
||||
}
|
||||
|
||||
this.resetPlusButton(this.groupByAdd);
|
||||
this.resetPlusButton(this.groupAdd);
|
||||
this.panelCtrl.refresh();
|
||||
}
|
||||
|
||||
|
@ -27,12 +27,12 @@ describe('PostgresQuery', function() {
|
||||
|
||||
describe('When generating time column SQL with group by time', function() {
|
||||
let query = new PostgresQuery(
|
||||
{ timeColumn: 'time', groupBy: [{ type: 'time', params: ['5m', 'none'] }] },
|
||||
{ timeColumn: 'time', group: [{ type: 'time', params: ['5m', 'none'] }] },
|
||||
templateSrv
|
||||
);
|
||||
expect(query.buildTimeColumn()).toBe('$__timeGroup(time,5m)');
|
||||
|
||||
query = new PostgresQuery({ timeColumn: 'time', groupBy: [{ type: 'time', params: ['5m', 'NULL'] }] }, templateSrv);
|
||||
query = new PostgresQuery({ timeColumn: 'time', group: [{ type: 'time', params: ['5m', 'NULL'] }] }, templateSrv);
|
||||
expect(query.buildTimeColumn()).toBe('$__timeGroup(time,5m,NULL)');
|
||||
});
|
||||
|
||||
@ -114,13 +114,13 @@ describe('PostgresQuery', function() {
|
||||
});
|
||||
|
||||
describe('When generating GROUP BY clause', function() {
|
||||
let query = new PostgresQuery({ groupBy: [], metricColumn: 'none' }, templateSrv);
|
||||
let query = new PostgresQuery({ group: [], metricColumn: 'none' }, templateSrv);
|
||||
|
||||
expect(query.buildGroupByClause()).toBe('');
|
||||
query.target.groupBy = [{ type: 'time', params: ['5m'] }];
|
||||
expect(query.buildGroupByClause()).toBe('\nGROUP BY 1');
|
||||
expect(query.buildGroupClause()).toBe('');
|
||||
query.target.group = [{ type: 'time', params: ['5m'] }];
|
||||
expect(query.buildGroupClause()).toBe('\nGROUP BY 1');
|
||||
query.target.metricColumn = 'm';
|
||||
expect(query.buildGroupByClause()).toBe('\nGROUP BY 1,2');
|
||||
expect(query.buildGroupClause()).toBe('\nGROUP BY 1,2');
|
||||
});
|
||||
|
||||
describe('When generating complete statement', function() {
|
||||
|
Loading…
Reference in New Issue
Block a user