influx_query.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import queryPart from './query_part';
  4. export default class InfluxQuery {
  5. target: any;
  6. selectModels: any[];
  7. queryBuilder: any;
  8. groupByParts: any;
  9. constructor(target) {
  10. this.target = target;
  11. target.policy = target.policy || 'default';
  12. target.dsType = 'influxdb';
  13. target.resultFormat = target.resultFormat || 'time_series';
  14. target.tags = target.tags || [];
  15. target.groupBy = target.groupBy || [
  16. {type: 'time', params: ['$interval']},
  17. {type: 'fill', params: ['null']},
  18. ];
  19. target.select = target.select || [[
  20. {type: 'field', params: ['value']},
  21. {type: 'mean', params: []},
  22. ]];
  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) {
  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(tag.value)) {
  118. operator = '=~';
  119. } else {
  120. operator = '=';
  121. }
  122. }
  123. // quote value unless regex
  124. if (operator !== '=~' && operator !== '!~') {
  125. value = "'" + value.replace('\\', '\\\\') + "'";
  126. }
  127. return str + '"' + tag.key + '" ' + operator + ' ' + value;
  128. }
  129. getMeasurementAndPolicy() {
  130. var policy = this.target.policy;
  131. var measurement = this.target.measurement;
  132. if (!measurement.match('^/.*/')) {
  133. measurement = '"' + measurement+ '"';
  134. }
  135. if (policy !== 'default') {
  136. policy = '"' + this.target.policy + '".';
  137. } else {
  138. policy = "";
  139. }
  140. return policy + measurement;
  141. }
  142. render() {
  143. var target = this.target;
  144. if (target.rawQuery) {
  145. return target.query;
  146. }
  147. if (!target.measurement) {
  148. throw {message: "Metric measurement is missing"};
  149. }
  150. var query = 'SELECT ';
  151. var i, y;
  152. for (i = 0; i < this.selectModels.length; i++) {
  153. let parts = this.selectModels[i];
  154. var selectText = "";
  155. for (y = 0; y < parts.length; y++) {
  156. let part = parts[y];
  157. selectText = part.render(selectText);
  158. }
  159. if (i > 0) {
  160. query += ', ';
  161. }
  162. query += selectText;
  163. }
  164. query += ' FROM ' + this.getMeasurementAndPolicy() + ' WHERE ';
  165. var conditions = _.map(target.tags, (tag, index) => {
  166. return this.renderTagCondition(tag, index);
  167. });
  168. query += conditions.join(' ');
  169. query += (conditions.length > 0 ? ' AND ' : '') + '$timeFilter';
  170. var groupBySection = "";
  171. for (i = 0; i < this.groupByParts.length; i++) {
  172. var part = this.groupByParts[i];
  173. if (i > 0) {
  174. // for some reason fill has no seperator
  175. groupBySection += part.def.type === 'fill' ? ' ' : ', ';
  176. }
  177. groupBySection += part.render('');
  178. }
  179. if (groupBySection.length) {
  180. query += ' GROUP BY ' + groupBySection;
  181. }
  182. if (target.fill) {
  183. query += ' fill(' + target.fill + ')';
  184. }
  185. target.query = query;
  186. return query;
  187. }
  188. }