grafana/public/app/plugins/datasource/postgres/postgres_query.ts

247 lines
6.7 KiB
TypeScript
Raw Normal View History

2018-01-30 15:13:55 -06:00
import _ from 'lodash';
import queryPart from './query_part';
import kbn from 'app/core/utils/kbn';
export default class PostgresQuery {
target: any;
selectModels: any[];
queryBuilder: any;
groupByParts: any;
templateSrv: any;
scopedVars: any;
/** @ngInject */
constructor(target, templateSrv?, scopedVars?) {
this.target = target;
this.templateSrv = templateSrv;
this.scopedVars = scopedVars;
target.schema = target.schema || 'public';
target.format = target.format || 'time_series';
target.timeColumn = target.timeColumn || 'time';
2018-02-08 03:25:32 -06:00
target.metricColumn = target.metricColumn || 'None';
2018-01-30 15:13:55 -06:00
2018-02-08 03:25:32 -06:00
target.groupBy = target.groupBy || [];
target.where = target.where || [];
2018-03-03 13:57:00 -06:00
target.select = target.select || [[{ type: 'column', params: ['value'] }]];
2018-01-30 15:13:55 -06:00
this.updateProjection();
}
2018-02-08 03:25:32 -06:00
quoteIdentifier(value) {
return '"' + value + '"';
}
quoteLiteral(value) {
return "'" + value + "'";
2018-01-30 15:57:38 -06:00
}
2018-01-30 15:13:55 -06:00
updateProjection() {
this.selectModels = _.map(this.target.select, function(parts: any) {
return _.map(parts, queryPart.create);
});
this.groupByParts = _.map(this.target.groupBy, queryPart.create);
}
updatePersistedParts() {
this.target.select = _.map(this.selectModels, function(selectParts) {
return _.map(selectParts, function(part: any) {
return { type: part.def.type, params: part.params };
});
});
}
hasGroupByTime() {
return _.find(this.target.groupBy, (g: any) => g.type === 'time');
}
addGroupBy(value) {
2018-03-04 12:35:43 -06:00
var stringParts = value.match(/^(\w+)(\((.*)\))?$/);
2018-01-30 15:13:55 -06:00
var typePart = stringParts[1];
2018-03-04 12:35:43 -06:00
var args = stringParts[3].split(",");
var partModel = queryPart.create({ type: typePart, params: args });
2018-01-30 15:13:55 -06:00
var partCount = this.target.groupBy.length;
if (partCount === 0) {
this.target.groupBy.push(partModel.part);
} else if (typePart === 'time') {
this.target.groupBy.splice(0, 0, partModel.part);
2018-03-04 12:35:43 -06:00
} else if (typePart === 'column') {
2018-01-30 15:13:55 -06:00
if (this.target.groupBy[partCount - 1].type === 'fill') {
this.target.groupBy.splice(partCount - 1, 0, partModel.part);
} else {
this.target.groupBy.push(partModel.part);
}
} else {
this.target.groupBy.push(partModel.part);
}
this.updateProjection();
}
removeGroupByPart(part, index) {
var categories = queryPart.getCategories();
if (part.def.type === 'time') {
// remove aggregations
this.target.select = _.map(this.target.select, (s: any) => {
return _.filter(s, (part: any) => {
var partModel = queryPart.create(part);
if (partModel.def.category === categories.Aggregations) {
return false;
}
return true;
});
});
}
this.target.groupBy.splice(index, 1);
this.updateProjection();
}
removeSelect(index: number) {
this.target.select.splice(index, 1);
this.updateProjection();
}
removeSelectPart(selectParts, part) {
// if we remove the field remove the whole statement
2018-03-03 13:57:00 -06:00
if (part.def.type === 'column') {
2018-01-30 15:13:55 -06:00
if (this.selectModels.length > 1) {
var modelsIndex = _.indexOf(this.selectModels, selectParts);
this.selectModels.splice(modelsIndex, 1);
}
} else {
var partIndex = _.indexOf(selectParts, part);
selectParts.splice(partIndex, 1);
}
this.updatePersistedParts();
}
addSelectPart(selectParts, type) {
var partModel = queryPart.create({ type: type });
partModel.def.addStrategy(selectParts, partModel, this);
this.updatePersistedParts();
}
2018-03-10 13:18:03 -06:00
private renderWhereConstraint(constraint, index, interpolate) {
2018-01-30 15:13:55 -06:00
var str = '';
2018-03-10 13:18:03 -06:00
var operator = constraint.operator;
var value = constraint.value;
2018-01-30 15:13:55 -06:00
if (index > 0) {
2018-03-10 13:18:03 -06:00
str = (constraint.condition || 'AND') + ' ';
2018-01-30 15:13:55 -06:00
}
2018-03-09 11:18:12 -06:00
if (interpolate) {
value = this.templateSrv.replace(value, this.scopedVars);
2018-01-30 15:13:55 -06:00
}
2018-03-13 17:24:26 -05:00
return str + constraint.key + ' ' + operator + ' ' + value;
2018-01-30 15:13:55 -06:00
}
interpolateQueryStr(value, variable, defaultFormatFn) {
// if no multi or include all do not regexEscape
if (!variable.multi && !variable.includeAll) {
return value;
}
if (typeof value === 'string') {
return kbn.regexEscape(value);
}
var escapedValues = _.map(value, kbn.regexEscape);
return '(' + escapedValues.join('|') + ')';
}
render(interpolate?) {
var target = this.target;
if (target.rawQuery) {
if (interpolate) {
return this.templateSrv.replace(target.rawSql, this.scopedVars, this.interpolateQueryStr);
} else {
return target.rawSql;
}
}
var query = 'SELECT ';
2018-03-03 13:57:00 -06:00
2018-03-04 12:35:43 -06:00
var timeGroup = this.hasGroupByTime();
if (timeGroup) {
var args;
if (timeGroup.params.length > 1 && timeGroup.params[1] !== "none") {
args = timeGroup.params.join(",");
} else {
args = timeGroup.params[0];
}
query += '$__timeGroup(' + this.quoteIdentifier(target.timeColumn) + ',' + args + '),';
2018-03-03 13:57:00 -06:00
} else {
query += this.quoteIdentifier(target.timeColumn) + ' AS time,';
}
2018-01-30 15:13:55 -06:00
var i, y;
for (i = 0; i < this.selectModels.length; i++) {
let parts = this.selectModels[i];
var selectText = '';
for (y = 0; y < parts.length; y++) {
let part = parts[y];
selectText = part.render(selectText);
}
if (i > 0) {
query += ', ';
}
query += selectText;
}
2018-03-04 16:32:23 -06:00
if (this.target.metricColumn !== 'None') {
query += "," + this.quoteIdentifier(this.target.metricColumn) + " AS metric";
}
2018-03-09 11:09:59 -06:00
query += ' FROM ' + this.quoteIdentifier(target.schema) + '.' + this.quoteIdentifier(target.table) + ' WHERE ';
2018-02-08 03:25:32 -06:00
var conditions = _.map(target.where, (tag, index) => {
2018-03-10 13:18:03 -06:00
return this.renderWhereConstraint(tag, index, false);
2018-01-30 15:13:55 -06:00
});
if (conditions.length > 0) {
query += '(' + conditions.join(' ') + ') AND ';
}
2018-01-30 15:57:38 -06:00
query += '$__timeFilter(' + this.quoteIdentifier(target.timeColumn) + ')';
2018-01-30 15:13:55 -06:00
var groupBySection = '';
for (i = 0; i < this.groupByParts.length; i++) {
var part = this.groupByParts[i];
if (i > 0) {
2018-03-03 13:57:00 -06:00
groupBySection += ', ';
}
if (part.def.type === 'time') {
2018-03-04 12:35:43 -06:00
groupBySection += '1';
2018-03-03 13:57:00 -06:00
} else {
groupBySection += part.render('');
2018-01-30 15:13:55 -06:00
}
}
if (groupBySection.length) {
query += ' GROUP BY ' + groupBySection;
}
2018-03-04 12:35:43 -06:00
query += ' ORDER BY 1';
2018-01-30 15:13:55 -06:00
this.target.rawSql = query;
2018-03-03 15:11:51 -06:00
if (interpolate) {
query = this.templateSrv.replace(query, this.scopedVars, this.interpolateQueryStr);
}
2018-01-30 15:13:55 -06:00
return query;
}
renderAdhocFilters(filters) {
var conditions = _.map(filters, (tag, index) => {
2018-03-09 11:18:12 -06:00
return this.renderWhereConstraint(tag, index, false);
2018-01-30 15:13:55 -06:00
});
return conditions.join(' ');
}
}