postgres_query.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import _ from 'lodash';
  2. import sqlPart from './sql_part';
  3. export default class PostgresQuery {
  4. target: any;
  5. selectModels: any[];
  6. queryBuilder: any;
  7. groupByParts: any[];
  8. whereParts: any[];
  9. templateSrv: any;
  10. scopedVars: any;
  11. /** @ngInject */
  12. constructor(target, templateSrv?, scopedVars?) {
  13. this.target = target;
  14. this.templateSrv = templateSrv;
  15. this.scopedVars = scopedVars;
  16. target.schema = target.schema || 'public';
  17. target.format = target.format || 'time_series';
  18. target.timeColumn = target.timeColumn || 'time';
  19. target.metricColumn = target.metricColumn || 'None';
  20. target.groupBy = target.groupBy || [];
  21. target.where = target.where || [];
  22. target.select = target.select || [[{ type: 'column', params: ['value'] }]];
  23. // handle pre query gui panels gracefully
  24. if (!('rawQuery' in this.target)) {
  25. if ('rawSql' in target) {
  26. // pre query gui panel
  27. target.rawQuery = true;
  28. } else {
  29. // new panel
  30. target.rawQuery = false;
  31. }
  32. }
  33. // give interpolateQueryStr access to this
  34. this.interpolateQueryStr = this.interpolateQueryStr.bind(this);
  35. this.updateProjection();
  36. }
  37. // remove identifier quoting from identifier to use in metadata queries
  38. unquoteIdentifier(value) {
  39. if (value[0] === '"' && value[value.length - 1] === '"') {
  40. return value.substring(1, value.length - 1).replace('""', '"');
  41. } else {
  42. return value;
  43. }
  44. }
  45. quoteIdentifier(value) {
  46. return '"' + value.replace('"', '""') + '"';
  47. }
  48. quoteLiteral(value) {
  49. return "'" + value.replace("'", "''") + "'";
  50. }
  51. updateProjection() {
  52. this.selectModels = _.map(this.target.select, function(parts: any) {
  53. return _.map(parts, sqlPart.create).filter(n => n);
  54. });
  55. this.whereParts = _.map(this.target.where, sqlPart.create).filter(n => n);
  56. this.groupByParts = _.map(this.target.groupBy, sqlPart.create).filter(n => n);
  57. }
  58. updatePersistedParts() {
  59. this.target.select = _.map(this.selectModels, function(selectParts) {
  60. return _.map(selectParts, function(part: any) {
  61. return { type: part.def.type, params: part.params };
  62. });
  63. });
  64. this.target.where = _.map(this.whereParts, function(part: any) {
  65. return { type: part.def.type, name: part.name, params: part.params };
  66. });
  67. this.target.groupBy = _.map(this.groupByParts, function(part: any) {
  68. return { type: part.def.type, params: part.params };
  69. });
  70. }
  71. hasGroupByTime() {
  72. return _.find(this.target.groupBy, (g: any) => g.type === 'time');
  73. }
  74. addGroupBy(partType, value) {
  75. let params = [value];
  76. if (partType === 'time') {
  77. params = ['1m', 'none'];
  78. }
  79. let partModel = sqlPart.create({ type: partType, params: params });
  80. if (partType === 'time') {
  81. // put timeGroup at start
  82. this.groupByParts.splice(0, 0, partModel);
  83. } else {
  84. this.groupByParts(partModel);
  85. }
  86. // add aggregates when adding group by
  87. for (let i = 0; i < this.selectModels.length; i++) {
  88. var selectParts = this.selectModels[i];
  89. if (!selectParts.some(part => part.def.type === 'aggregate')) {
  90. let aggregate = sqlPart.create({ type: 'aggregate', params: ['avg'] });
  91. selectParts.splice(1, 0, aggregate);
  92. if (!selectParts.some(part => part.def.type === 'alias')) {
  93. let alias = sqlPart.create({ type: 'alias', params: [selectParts[0].part.params[0]] });
  94. selectParts.push(alias);
  95. }
  96. }
  97. }
  98. this.updatePersistedParts();
  99. }
  100. removeGroupByPart(part, index) {
  101. if (part.def.type === 'time') {
  102. // remove aggregations
  103. this.selectModels = _.map(this.selectModels, (s: any) => {
  104. return _.filter(s, (part: any) => {
  105. if (part.def.type === 'aggregate') {
  106. return false;
  107. }
  108. return true;
  109. });
  110. });
  111. }
  112. this.groupByParts.splice(index, 1);
  113. this.updatePersistedParts();
  114. }
  115. removeSelectPart(selectParts, part) {
  116. // if we remove the field remove the whole statement
  117. if (part.def.type === 'column') {
  118. if (this.selectModels.length > 1) {
  119. let modelsIndex = _.indexOf(this.selectModels, selectParts);
  120. this.selectModels.splice(modelsIndex, 1);
  121. }
  122. } else {
  123. let partIndex = _.indexOf(selectParts, part);
  124. selectParts.splice(partIndex, 1);
  125. }
  126. this.updatePersistedParts();
  127. }
  128. addSelectPart(selectParts, type) {
  129. let partModel = sqlPart.create({ type: type });
  130. partModel.def.addStrategy(selectParts, partModel, this);
  131. this.updatePersistedParts();
  132. }
  133. interpolateQueryStr(value, variable, defaultFormatFn) {
  134. // if no multi or include all do not regexEscape
  135. if (!variable.multi && !variable.includeAll) {
  136. return value;
  137. }
  138. if (typeof value === 'string') {
  139. return this.quoteLiteral(value);
  140. }
  141. let escapedValues = _.map(value, this.quoteLiteral);
  142. return '(' + escapedValues.join(',') + ')';
  143. }
  144. render(interpolate?) {
  145. let target = this.target;
  146. let query;
  147. if (target.rawQuery) {
  148. if (interpolate) {
  149. return this.templateSrv.replace(target.rawSql, this.scopedVars, this.interpolateQueryStr);
  150. } else {
  151. return target.rawSql;
  152. }
  153. }
  154. query = this.buildQuery(target);
  155. if (interpolate) {
  156. query = this.templateSrv.replace(query, this.scopedVars, this.interpolateQueryStr);
  157. }
  158. this.target.rawSql = query;
  159. return query;
  160. }
  161. buildTimeColumn(target) {
  162. let timeGroup = this.hasGroupByTime();
  163. let query;
  164. if (timeGroup) {
  165. let args;
  166. if (timeGroup.params.length > 1 && timeGroup.params[1] !== 'none') {
  167. args = timeGroup.params.join(',');
  168. } else {
  169. args = timeGroup.params[0];
  170. }
  171. query = '$__timeGroup(' + target.timeColumn + ',' + args + ')';
  172. } else {
  173. query = target.timeColumn + ' AS "time"';
  174. }
  175. return query;
  176. }
  177. buildMetricColumn(target) {
  178. if (target.metricColumn !== 'None') {
  179. return target.metricColumn + ' AS metric';
  180. }
  181. return '';
  182. }
  183. buildValueColumns(target) {
  184. let query = '';
  185. for (let i = 0; i < target.select.length; i++) {
  186. query += ',\n ' + this.buildValueColumn(target, target.select[i]);
  187. }
  188. return query;
  189. }
  190. buildValueColumn(target, column) {
  191. let query = '';
  192. let columnName = _.find(column, (g: any) => g.type === 'column');
  193. query = columnName.params[0];
  194. let aggregate = _.find(column, (g: any) => g.type === 'aggregate');
  195. if (aggregate) {
  196. query = aggregate.params[0] + '(' + query + ')';
  197. }
  198. let special = _.find(column, (g: any) => g.type === 'special');
  199. if (special) {
  200. let over = '';
  201. if (target.metricColumn !== 'None') {
  202. over = 'PARTITION BY ' + target.metricColumn;
  203. }
  204. switch (special.params[0]) {
  205. case 'increase':
  206. query = query + ' - lag(' + query + ') OVER (' + over + ')';
  207. break;
  208. case 'rate':
  209. let timeColumn = target.timeColumn;
  210. let curr = query;
  211. let prev = 'lag(' + curr + ') OVER (' + over + ')';
  212. query = '(CASE WHEN ' + curr + ' >= ' + prev + ' THEN ' + curr + ' - ' + prev + ' ELSE ' + curr + ' END)';
  213. query += '/extract(epoch from ' + timeColumn + ' - lag(' + timeColumn + ') OVER (' + over + '))';
  214. break;
  215. }
  216. }
  217. let alias = _.find(column, (g: any) => g.type === 'alias');
  218. if (alias) {
  219. query += ' AS ' + this.quoteIdentifier(alias.params[0]);
  220. }
  221. return query;
  222. }
  223. buildWhereClause(target) {
  224. let query = '';
  225. let conditions = _.map(target.where, (tag, index) => {
  226. switch (tag.type) {
  227. case 'macro':
  228. return tag.name + '(' + target.timeColumn + ')';
  229. break;
  230. case 'expression':
  231. return tag.params.join(' ');
  232. break;
  233. }
  234. });
  235. if (conditions.length > 0) {
  236. query = '\nWHERE\n ' + conditions.join(' AND\n ');
  237. }
  238. return query;
  239. }
  240. buildGroupByClause(target) {
  241. let query = '';
  242. let groupBySection = '';
  243. for (let i = 0; i < target.groupBy.length; i++) {
  244. let part = target.groupBy[i];
  245. if (i > 0) {
  246. groupBySection += ', ';
  247. }
  248. if (part.type === 'time') {
  249. groupBySection += '1';
  250. } else {
  251. groupBySection += part.params[0];
  252. }
  253. }
  254. if (groupBySection.length) {
  255. query = '\nGROUP BY ' + groupBySection;
  256. if (target.metricColumn !== 'None') {
  257. query += ',2';
  258. }
  259. }
  260. return query;
  261. }
  262. buildQuery(target) {
  263. let query = 'SELECT';
  264. query += '\n ' + this.buildTimeColumn(target);
  265. if (target.metricColumn !== 'None') {
  266. query += '\n ' + this.buildMetricColumn(target);
  267. }
  268. query += this.buildValueColumns(target);
  269. query += '\nFROM ' + target.schema + '.' + target.table;
  270. query += this.buildWhereClause(target);
  271. query += this.buildGroupByClause(target);
  272. query += '\nORDER BY 1';
  273. return query;
  274. }
  275. }