influx_query.ts 5.1 KB

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