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

299 lines
8.0 KiB
TypeScript
Raw Normal View History

import { find, map } from 'lodash';
import { ScopedVars } from '@grafana/data';
import { TemplateSrv } from '@grafana/runtime';
2018-01-30 15:13:55 -06:00
export default class PostgresQueryModel {
2018-01-30 15:13:55 -06:00
target: any;
templateSrv: any;
scopedVars: any;
/** @ngInject */
constructor(target: any, templateSrv?: TemplateSrv, scopedVars?: ScopedVars) {
2018-01-30 15:13:55 -06:00
this.target = target;
this.templateSrv = templateSrv;
this.scopedVars = scopedVars;
target.format = target.format || 'time_series';
target.timeColumn = target.timeColumn || 'time';
2018-07-15 02:36:49 -05:00
target.metricColumn = target.metricColumn || 'none';
2018-01-30 15:13:55 -06:00
target.group = target.group || [];
2018-07-15 15:58:25 -05:00
target.where = target.where || [{ type: 'macro', name: '$__timeFilter', params: [] }];
2018-03-03 13:57:00 -06:00
target.select = target.select || [[{ type: 'column', params: ['value'] }]];
2018-01-30 15:13:55 -06:00
// handle pre query gui panels gracefully
if (!('rawQuery' in this.target)) {
if ('rawSql' in target) {
// pre query gui panel
target.rawQuery = true;
} else {
// new panel
target.rawQuery = false;
}
}
2018-03-14 12:09:47 -05:00
// give interpolateQueryStr access to this
this.interpolateQueryStr = this.interpolateQueryStr.bind(this);
2018-01-30 15:13:55 -06:00
}
2018-07-06 02:28:34 -05:00
// remove identifier quoting from identifier to use in metadata queries
unquoteIdentifier(value: string) {
2018-07-06 02:28:34 -05:00
if (value[0] === '"' && value[value.length - 1] === '"') {
2018-08-29 05:24:28 -05:00
return value.substring(1, value.length - 1).replace(/""/g, '"');
} else {
return value;
}
}
quoteIdentifier(value: any) {
return '"' + String(value).replace(/"/g, '""') + '"';
2018-02-08 03:25:32 -06:00
}
quoteLiteral(value: any) {
return "'" + String(value).replace(/'/g, "''") + "'";
2018-08-15 04:37:30 -05:00
}
escapeLiteral(value: any) {
return String(value).replace(/'/g, "''");
2018-01-30 15:57:38 -06:00
}
hasTimeGroup() {
return find(this.target.group, (g: any) => g.type === 'time');
2018-01-30 15:13:55 -06:00
}
2018-07-15 02:36:49 -05:00
hasMetricColumn() {
return this.target.metricColumn !== 'none';
}
interpolateQueryStr(value: any, variable: { multi: any; includeAll: any }, defaultFormatFn: any) {
2018-01-30 15:13:55 -06:00
// if no multi or include all do not regexEscape
if (!variable.multi && !variable.includeAll) {
2018-08-15 04:37:30 -05:00
return this.escapeLiteral(value);
2018-01-30 15:13:55 -06:00
}
if (typeof value === 'string') {
2018-03-14 12:09:47 -05:00
return this.quoteLiteral(value);
2018-01-30 15:13:55 -06:00
}
const escapedValues = map(value, this.quoteLiteral);
return escapedValues.join(',');
2018-01-30 15:13:55 -06:00
}
render(interpolate?: any) {
const target = this.target;
2018-01-30 15:13:55 -06:00
2018-07-21 02:57:42 -05:00
// new query with no table set yet
if (!this.target.rawQuery && !('table' in this.target)) {
return '';
}
2018-07-15 10:02:26 -05:00
if (!target.rawQuery) {
target.rawSql = this.buildQuery();
2018-01-30 15:13:55 -06:00
}
if (interpolate) {
2018-07-15 10:02:26 -05:00
return this.templateSrv.replace(target.rawSql, this.scopedVars, this.interpolateQueryStr);
} else {
return target.rawSql;
}
}
hasUnixEpochTimecolumn() {
return ['int4', 'int8', 'float4', 'float8', 'numeric'].indexOf(this.target.timeColumnType) > -1;
}
2018-08-02 14:40:15 -05:00
buildTimeColumn(alias = true) {
const timeGroup = this.hasTimeGroup();
2018-07-11 04:00:12 -05:00
let query;
let macro = '$__timeGroup';
2018-03-04 12:35:43 -06:00
if (timeGroup) {
2018-07-11 06:03:06 -05:00
let args;
if (timeGroup.params.length > 1 && timeGroup.params[1] !== 'none') {
args = timeGroup.params.join(',');
2018-03-04 12:35:43 -06:00
} else {
args = timeGroup.params[0];
}
if (this.hasUnixEpochTimecolumn()) {
macro = '$__unixEpochGroup';
}
2018-08-02 14:40:15 -05:00
if (alias) {
macro += 'Alias';
2018-08-02 14:40:15 -05:00
}
query = macro + '(' + this.target.timeColumn + ',' + args + ')';
2018-03-03 13:57:00 -06:00
} else {
2018-08-02 14:40:15 -05:00
query = this.target.timeColumn;
if (alias) {
query += ' AS "time"';
}
2018-05-21 05:10:18 -05:00
}
2018-07-13 15:29:10 -05:00
return query;
2018-07-11 04:00:12 -05:00
}
2018-07-15 02:36:49 -05:00
buildMetricColumn() {
if (this.hasMetricColumn()) {
return this.target.metricColumn + ' AS metric';
2018-07-11 05:33:37 -05:00
}
2018-07-13 15:29:10 -05:00
return '';
2018-07-11 05:33:37 -05:00
}
2018-07-15 02:36:49 -05:00
buildValueColumns() {
let query = '';
for (const column of this.target.select) {
2018-07-15 02:36:49 -05:00
query += ',\n ' + this.buildValueColumn(column);
}
return query;
}
buildValueColumn(column: any) {
let query = '';
const columnName: any = find(column, (g: any) => g.type === 'column');
query = columnName.params[0];
const aggregate: any = find(column, (g: any) => g.type === 'aggregate' || g.type === 'percentile');
const windows: any = find(column, (g: any) => g.type === 'window' || g.type === 'moving_window');
if (aggregate) {
const func = aggregate.params[0];
switch (aggregate.type) {
case 'aggregate':
2018-07-29 08:00:13 -05:00
if (func === 'first' || func === 'last') {
query = func + '(' + query + ',' + this.target.timeColumn + ')';
} else {
2018-08-03 03:15:28 -05:00
query = func + '(' + query + ')';
}
break;
case 'percentile':
2018-07-29 08:00:13 -05:00
query = func + '(' + aggregate.params[1] + ') WITHIN GROUP (ORDER BY ' + query + ')';
break;
}
2018-07-11 06:03:06 -05:00
}
2018-07-29 08:56:22 -05:00
if (windows) {
const overParts = [];
2018-07-15 02:36:49 -05:00
if (this.hasMetricColumn()) {
overParts.push('PARTITION BY ' + this.target.metricColumn);
}
2018-08-02 14:40:15 -05:00
overParts.push('ORDER BY ' + this.buildTimeColumn(false));
const over = overParts.join(' ');
2018-07-28 14:30:08 -05:00
let curr: string;
let prev: string;
2018-08-03 00:44:36 -05:00
switch (windows.type) {
case 'window':
switch (windows.params[0]) {
case 'delta':
curr = query;
prev = 'lag(' + curr + ') OVER (' + over + ')';
query = curr + ' - ' + prev;
break;
2018-08-03 00:44:36 -05:00
case 'increase':
curr = query;
prev = 'lag(' + curr + ') OVER (' + over + ')';
query = '(CASE WHEN ' + curr + ' >= ' + prev + ' THEN ' + curr + ' - ' + prev;
query += ' WHEN ' + prev + ' IS NULL THEN NULL ELSE ' + curr + ' END)';
2018-08-03 00:44:36 -05:00
break;
case 'rate':
let timeColumn = this.target.timeColumn;
if (aggregate) {
timeColumn = 'min(' + timeColumn + ')';
}
curr = query;
prev = 'lag(' + curr + ') OVER (' + over + ')';
query = '(CASE WHEN ' + curr + ' >= ' + prev + ' THEN ' + curr + ' - ' + prev;
query += ' WHEN ' + prev + ' IS NULL THEN NULL ELSE ' + curr + ' END)';
2018-08-03 00:44:36 -05:00
query += '/extract(epoch from ' + timeColumn + ' - lag(' + timeColumn + ') OVER (' + over + '))';
break;
default:
query = windows.params[0] + '(' + query + ') OVER (' + over + ')';
break;
}
break;
2018-08-03 00:44:36 -05:00
case 'moving_window':
query = windows.params[0] + '(' + query + ') OVER (' + over + ' ROWS ' + windows.params[1] + ' PRECEDING)';
2018-07-28 14:30:08 -05:00
break;
}
}
const alias: any = find(column, (g: any) => g.type === 'alias');
if (alias) {
query += ' AS ' + this.quoteIdentifier(alias.params[0]);
}
return query;
2018-07-11 06:03:06 -05:00
}
2018-07-15 02:36:49 -05:00
buildWhereClause() {
2018-07-11 04:50:02 -05:00
let query = '';
const conditions = map(this.target.where, (tag, index) => {
switch (tag.type) {
case 'macro':
2018-07-15 02:36:49 -05:00
return tag.name + '(' + this.target.timeColumn + ')';
break;
case 'expression':
return tag.params.join(' ');
break;
}
2018-01-30 15:13:55 -06:00
});
if (conditions.length > 0) {
2018-07-12 15:36:41 -05:00
query = '\nWHERE\n ' + conditions.join(' AND\n ');
2018-07-11 04:50:02 -05:00
}
return query;
}
buildGroupClause() {
2018-07-11 05:20:24 -05:00
let query = '';
let groupSection = '';
2018-07-11 04:50:02 -05:00
for (let i = 0; i < this.target.group.length; i++) {
const part = this.target.group[i];
2018-01-30 15:13:55 -06:00
if (i > 0) {
groupSection += ', ';
2018-03-03 13:57:00 -06:00
}
2018-07-13 15:29:10 -05:00
if (part.type === 'time') {
groupSection += '1';
2018-03-03 13:57:00 -06:00
} else {
groupSection += part.params[0];
2018-01-30 15:13:55 -06:00
}
}
if (groupSection.length) {
query = '\nGROUP BY ' + groupSection;
2018-07-15 02:36:49 -05:00
if (this.hasMetricColumn()) {
query += ',2';
2018-05-21 05:10:18 -05:00
}
2018-01-30 15:13:55 -06:00
}
2018-07-11 05:20:24 -05:00
return query;
}
2018-07-15 02:36:49 -05:00
buildQuery() {
2018-07-13 15:29:10 -05:00
let query = 'SELECT';
2018-07-11 05:20:24 -05:00
2018-07-15 02:36:49 -05:00
query += '\n ' + this.buildTimeColumn();
if (this.hasMetricColumn()) {
query += ',\n ' + this.buildMetricColumn();
2018-07-13 15:29:10 -05:00
}
2018-07-15 02:36:49 -05:00
query += this.buildValueColumns();
2018-07-11 05:20:24 -05:00
query += '\nFROM ' + this.target.table;
2018-07-11 05:20:24 -05:00
2018-07-15 02:36:49 -05:00
query += this.buildWhereClause();
query += this.buildGroupClause();
2018-01-30 15:13:55 -06:00
2018-07-12 15:36:41 -05:00
query += '\nORDER BY 1';
if (this.hasMetricColumn()) {
query += ',2';
}
2018-01-30 15:13:55 -06:00
return query;
}
}