influx_query.ts 6.1 KB

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