postgres_query.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import _ from 'lodash';
  2. import queryPart from './query_part';
  3. import kbn from 'app/core/utils/kbn';
  4. export default class PostgresQuery {
  5. target: any;
  6. selectModels: any[];
  7. queryBuilder: any;
  8. groupByParts: 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.orderByTime = target.orderByTime || 'ASC';
  21. target.groupBy = target.groupBy || [];
  22. target.where = target.where || [];
  23. target.select = target.select || [[{ type: 'field', params: ['value'] }]];
  24. this.updateProjection();
  25. }
  26. quoteIdentifier(value) {
  27. return '"' + value + '"';
  28. }
  29. quoteLiteral(value) {
  30. return "'" + value + "'";
  31. }
  32. updateProjection() {
  33. this.selectModels = _.map(this.target.select, function(parts: any) {
  34. return _.map(parts, queryPart.create);
  35. });
  36. this.groupByParts = _.map(this.target.groupBy, queryPart.create);
  37. }
  38. updatePersistedParts() {
  39. this.target.select = _.map(this.selectModels, function(selectParts) {
  40. return _.map(selectParts, function(part: any) {
  41. return { type: part.def.type, params: part.params };
  42. });
  43. });
  44. }
  45. hasGroupByTime() {
  46. return _.find(this.target.groupBy, (g: any) => g.type === 'time');
  47. }
  48. hasFill() {
  49. return _.find(this.target.groupBy, (g: any) => g.type === 'fill');
  50. }
  51. addGroupBy(value) {
  52. var stringParts = value.match(/^(\w+)\((.*)\)$/);
  53. var typePart = stringParts[1];
  54. var arg = stringParts[2];
  55. var partModel = queryPart.create({ type: typePart, params: [arg] });
  56. var partCount = this.target.groupBy.length;
  57. if (partCount === 0) {
  58. this.target.groupBy.push(partModel.part);
  59. } else if (typePart === 'time') {
  60. this.target.groupBy.splice(0, 0, partModel.part);
  61. } else if (typePart === 'tag') {
  62. if (this.target.groupBy[partCount - 1].type === 'fill') {
  63. this.target.groupBy.splice(partCount - 1, 0, partModel.part);
  64. } else {
  65. this.target.groupBy.push(partModel.part);
  66. }
  67. } else {
  68. this.target.groupBy.push(partModel.part);
  69. }
  70. this.updateProjection();
  71. }
  72. removeGroupByPart(part, index) {
  73. var categories = queryPart.getCategories();
  74. if (part.def.type === 'time') {
  75. // remove fill
  76. this.target.groupBy = _.filter(this.target.groupBy, (g: any) => g.type !== 'fill');
  77. // remove aggregations
  78. this.target.select = _.map(this.target.select, (s: any) => {
  79. return _.filter(s, (part: any) => {
  80. var partModel = queryPart.create(part);
  81. if (partModel.def.category === categories.Aggregations) {
  82. return false;
  83. }
  84. return true;
  85. });
  86. });
  87. }
  88. this.target.groupBy.splice(index, 1);
  89. this.updateProjection();
  90. }
  91. removeSelect(index: number) {
  92. this.target.select.splice(index, 1);
  93. this.updateProjection();
  94. }
  95. removeSelectPart(selectParts, part) {
  96. // if we remove the field remove the whole statement
  97. if (part.def.type === 'field') {
  98. if (this.selectModels.length > 1) {
  99. var modelsIndex = _.indexOf(this.selectModels, selectParts);
  100. this.selectModels.splice(modelsIndex, 1);
  101. }
  102. } else {
  103. var partIndex = _.indexOf(selectParts, part);
  104. selectParts.splice(partIndex, 1);
  105. }
  106. this.updatePersistedParts();
  107. }
  108. addSelectPart(selectParts, type) {
  109. var partModel = queryPart.create({ type: type });
  110. partModel.def.addStrategy(selectParts, partModel, this);
  111. this.updatePersistedParts();
  112. }
  113. private renderTagCondition(tag, index, interpolate) {
  114. var str = '';
  115. var operator = tag.operator;
  116. var value = tag.value;
  117. if (index > 0) {
  118. str = (tag.condition || 'AND') + ' ';
  119. }
  120. if (!operator) {
  121. if (/^\/.*\/$/.test(value)) {
  122. operator = '=~';
  123. } else {
  124. operator = '=';
  125. }
  126. }
  127. // quote value unless regex
  128. if (operator !== '=~' && operator !== '!~') {
  129. if (interpolate) {
  130. value = this.templateSrv.replace(value, this.scopedVars);
  131. }
  132. if (operator !== '>' && operator !== '<') {
  133. value = "'" + value.replace(/\\/g, '\\\\') + "'";
  134. }
  135. } else if (interpolate) {
  136. value = this.templateSrv.replace(value, this.scopedVars, 'regex');
  137. }
  138. return str + '"' + tag.key + '" ' + operator + ' ' + value;
  139. }
  140. interpolateQueryStr(value, variable, defaultFormatFn) {
  141. // if no multi or include all do not regexEscape
  142. if (!variable.multi && !variable.includeAll) {
  143. return value;
  144. }
  145. if (typeof value === 'string') {
  146. return kbn.regexEscape(value);
  147. }
  148. var escapedValues = _.map(value, kbn.regexEscape);
  149. return '(' + escapedValues.join('|') + ')';
  150. }
  151. render(interpolate?) {
  152. var target = this.target;
  153. if (target.rawQuery) {
  154. if (interpolate) {
  155. return this.templateSrv.replace(target.rawSql, this.scopedVars, this.interpolateQueryStr);
  156. } else {
  157. return target.rawSql;
  158. }
  159. }
  160. var query = 'SELECT ';
  161. query += this.quoteIdentifier(target.timeColumn) + ' AS time,';
  162. var i, y;
  163. for (i = 0; i < this.selectModels.length; i++) {
  164. let parts = this.selectModels[i];
  165. var selectText = '';
  166. for (y = 0; y < parts.length; y++) {
  167. let part = parts[y];
  168. selectText = part.render(selectText);
  169. }
  170. if (i > 0) {
  171. query += ', ';
  172. }
  173. query += selectText;
  174. }
  175. query += ' FROM ' + target.schema + '.' + target.table + ' WHERE ';
  176. var conditions = _.map(target.where, (tag, index) => {
  177. return this.renderTagCondition(tag, index, interpolate);
  178. });
  179. if (conditions.length > 0) {
  180. query += '(' + conditions.join(' ') + ') AND ';
  181. }
  182. query += '$__timeFilter(' + this.quoteIdentifier(target.timeColumn) + ')';
  183. var groupBySection = '';
  184. for (i = 0; i < this.groupByParts.length; i++) {
  185. var part = this.groupByParts[i];
  186. if (i > 0) {
  187. // for some reason fill has no seperator
  188. groupBySection += part.def.type === 'fill' ? ' ' : ', ';
  189. }
  190. groupBySection += part.render('');
  191. }
  192. if (groupBySection.length) {
  193. query += ' GROUP BY ' + groupBySection;
  194. }
  195. query += ' ORDER BY time';
  196. return query;
  197. }
  198. renderAdhocFilters(filters) {
  199. var conditions = _.map(filters, (tag, index) => {
  200. return this.renderTagCondition(tag, index, false);
  201. });
  202. return conditions.join(' ');
  203. }
  204. }