influx_query.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. return true;
  75. });
  76. });
  77. }
  78. this.target.groupBy.splice(index, 1);
  79. this.updateProjection();
  80. }
  81. removeSelect(index: number) {
  82. this.target.select.splice(index, 1);
  83. this.updateProjection();
  84. }
  85. removeSelectPart(selectParts, part) {
  86. // if we remove the field remove the whole statement
  87. if (part.def.type === 'field') {
  88. if (this.selectModels.length > 1) {
  89. var modelsIndex = _.indexOf(this.selectModels, selectParts);
  90. this.selectModels.splice(modelsIndex, 1);
  91. }
  92. } else {
  93. var partIndex = _.indexOf(selectParts, part);
  94. selectParts.splice(partIndex, 1);
  95. }
  96. this.updatePersistedParts();
  97. }
  98. addSelectPart(selectParts, type) {
  99. var partModel = queryPart.create({type: type});
  100. partModel.def.addStrategy(selectParts, partModel, this);
  101. this.updatePersistedParts();
  102. }
  103. private renderTagCondition(tag, index) {
  104. var str = "";
  105. var operator = tag.operator;
  106. var value = tag.value;
  107. if (index > 0) {
  108. str = (tag.condition || 'AND') + ' ';
  109. }
  110. if (!operator) {
  111. if (/^\/.*\/$/.test(tag.value)) {
  112. operator = '=~';
  113. } else {
  114. operator = '=';
  115. }
  116. }
  117. // quote value unless regex
  118. if (operator !== '=~' && operator !== '!~') {
  119. value = "'" + value + "'";
  120. }
  121. return str + '"' + tag.key + '" ' + operator + ' ' + value;
  122. }
  123. render() {
  124. var target = this.target;
  125. if (target.rawQuery) {
  126. return target.query;
  127. }
  128. if (!target.measurement) {
  129. throw "Metric measurement is missing";
  130. }
  131. var query = 'SELECT ';
  132. var i, y;
  133. for (i = 0; i < this.selectModels.length; i++) {
  134. let parts = this.selectModels[i];
  135. var selectText = "";
  136. for (y = 0; y < parts.length; y++) {
  137. let part = parts[y];
  138. selectText = part.render(selectText);
  139. }
  140. if (i > 0) {
  141. query += ', ';
  142. }
  143. query += selectText;
  144. }
  145. var measurement = target.measurement;
  146. if (!measurement.match('^/.*/') && !measurement.match(/^merge\(.*\)/)) {
  147. measurement = '"' + measurement+ '"';
  148. }
  149. query += ' FROM ' + measurement + ' WHERE ';
  150. var conditions = _.map(target.tags, (tag, index) => {
  151. return this.renderTagCondition(tag, index);
  152. });
  153. query += conditions.join(' ');
  154. query += (conditions.length > 0 ? ' AND ' : '') + '$timeFilter';
  155. var groupBySection = "";
  156. for (i = 0; i < this.groupByParts.length; i++) {
  157. var part = this.groupByParts[i];
  158. if (i > 0) {
  159. // for some reason fill has no seperator
  160. groupBySection += part.def.type === 'fill' ? ' ' : ', ';
  161. }
  162. groupBySection += part.render('');
  163. }
  164. if (groupBySection.length) {
  165. query += ' GROUP BY ' + groupBySection;
  166. }
  167. if (target.fill) {
  168. query += ' fill(' + target.fill + ')';
  169. }
  170. target.query = query;
  171. return query;
  172. }
  173. }
  174. export = InfluxQuery;