influx_query.ts 5.6 KB

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