influx_query.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ = require('lodash');
  3. import queryPart = require('./query_part');
  4. class InfluxQuery {
  5. target: any;
  6. selectModels: any[];
  7. groupByParts: any;
  8. queryBuilder: any;
  9. constructor(target) {
  10. this.target = target;
  11. target.tags = target.tags || [];
  12. target.groupBy = target.groupBy || [
  13. {type: 'time', params: ['$interval']},
  14. {type: 'fill', params: ['null']},
  15. ];
  16. target.select = target.select || [[
  17. {type: 'field', params: ['value']},
  18. {type: 'mean', params: []},
  19. ]];
  20. this.updateProjection();
  21. }
  22. updateProjection() {
  23. this.selectModels = _.map(this.target.select, function(parts: any) {
  24. return _.map(parts, queryPart.create);
  25. });
  26. this.groupByParts = _.map(this.target.groupBy, queryPart.create);
  27. }
  28. updatePersistedParts() {
  29. this.target.select = _.map(this.selectModels, function(selectParts) {
  30. return _.map(selectParts, function(part: any) {
  31. return {type: part.def.type, params: part.params};
  32. });
  33. });
  34. }
  35. hasGroupByTime() {
  36. return _.find(this.target.groupBy, (g: any) => g.type === 'time');
  37. }
  38. hasFill() {
  39. return _.find(this.target.groupBy, (g: any) => g.type === 'fill');
  40. }
  41. addGroupBy(value) {
  42. var stringParts = value.match(/^(\w+)\((.*)\)$/);
  43. var typePart = stringParts[1];
  44. var arg = stringParts[2];
  45. var partModel = queryPart.create({type: typePart, params: [arg]});
  46. var partCount = this.target.groupBy.length;
  47. if (partCount === 0) {
  48. this.target.groupBy.push(partModel.part);
  49. } else if (typePart === 'time') {
  50. this.target.groupBy.splice(0, 0, partModel.part);
  51. } else if (typePart === 'tag') {
  52. if (this.target.groupBy[partCount-1].type === 'fill') {
  53. this.target.groupBy.splice(partCount-1, 0, partModel.part);
  54. } else {
  55. this.target.groupBy.push(partModel.part);
  56. }
  57. } else {
  58. this.target.groupBy.push(partModel.part);
  59. }
  60. this.updateProjection();
  61. }
  62. removeGroupByPart(part, index) {
  63. var categories = queryPart.getCategories();
  64. if (part.def.type === 'time') {
  65. // remove fill
  66. this.target.groupBy = _.filter(this.target.groupBy, (g: any) => g.type !== 'fill');
  67. // remove aggregations
  68. this.target.select = _.map(this.target.select, (s: any) => {
  69. return _.filter(s, (part: any) => {
  70. var partModel = queryPart.create(part);
  71. if (partModel.def.category === categories.Aggregations) {
  72. return false;
  73. }
  74. if (partModel.def.category === categories.Selectors) {
  75. return false;
  76. }
  77. return true;
  78. });
  79. });
  80. }
  81. this.target.groupBy.splice(index, 1);
  82. this.updateProjection();
  83. }
  84. removeSelect(index: number) {
  85. this.target.select.splice(index, 1);
  86. this.updateProjection();
  87. }
  88. removeSelectPart(selectParts, part) {
  89. // if we remove the field remove the whole statement
  90. if (part.def.type === 'field') {
  91. if (this.selectModels.length > 1) {
  92. var modelsIndex = _.indexOf(this.selectModels, selectParts);
  93. this.selectModels.splice(modelsIndex, 1);
  94. }
  95. } else {
  96. var partIndex = _.indexOf(selectParts, part);
  97. selectParts.splice(partIndex, 1);
  98. }
  99. this.updatePersistedParts();
  100. }
  101. addSelectPart(selectParts, type) {
  102. var partModel = queryPart.create({type: type});
  103. partModel.def.addStrategy(selectParts, partModel, this);
  104. this.updatePersistedParts();
  105. }
  106. private renderTagCondition(tag, index) {
  107. var str = "";
  108. var operator = tag.operator;
  109. var value = tag.value;
  110. if (index > 0) {
  111. str = (tag.condition || 'AND') + ' ';
  112. }
  113. if (!operator) {
  114. if (/^\/.*\/$/.test(tag.value)) {
  115. operator = '=~';
  116. } else {
  117. operator = '=';
  118. }
  119. }
  120. // quote value unless regex
  121. if (operator !== '=~' && operator !== '!~') {
  122. value = "'" + value + "'";
  123. }
  124. return str + '"' + tag.key + '" ' + operator + ' ' + value;
  125. }
  126. render() {
  127. var target = this.target;
  128. if (target.rawQuery) {
  129. return target.query;
  130. }
  131. if (!target.measurement) {
  132. throw "Metric measurement is missing";
  133. }
  134. var query = 'SELECT ';
  135. var i, y;
  136. for (i = 0; i < this.selectModels.length; i++) {
  137. let parts = this.selectModels[i];
  138. var selectText = "";
  139. for (y = 0; y < parts.length; y++) {
  140. let part = parts[y];
  141. selectText = part.render(selectText);
  142. }
  143. if (i > 0) {
  144. query += ', ';
  145. }
  146. query += selectText;
  147. }
  148. var measurement = target.measurement;
  149. if (!measurement.match('^/.*/') && !measurement.match(/^merge\(.*\)/)) {
  150. measurement = '"' + measurement+ '"';
  151. }
  152. query += ' FROM ' + measurement + ' WHERE ';
  153. var conditions = _.map(target.tags, (tag, index) => {
  154. return this.renderTagCondition(tag, index);
  155. });
  156. query += conditions.join(' ');
  157. query += (conditions.length > 0 ? ' AND ' : '') + '$timeFilter';
  158. var groupBySection = "";
  159. for (i = 0; i < this.groupByParts.length; i++) {
  160. var part = this.groupByParts[i];
  161. if (i > 0) {
  162. // for some reason fill has no seperator
  163. groupBySection += part.def.type === 'fill' ? ' ' : ', ';
  164. }
  165. groupBySection += part.render('');
  166. }
  167. if (groupBySection.length) {
  168. query += ' GROUP BY ' + groupBySection;
  169. }
  170. if (target.fill) {
  171. query += ' fill(' + target.fill + ')';
  172. }
  173. target.query = query;
  174. return query;
  175. }
  176. }
  177. export = InfluxQuery;