postgres_query.ts 6.5 KB

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