influx_query.ts 6.3 KB

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