postgres_query.ts 6.7 KB

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