influx_query.ts 6.4 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. if (isNaN(+value)) {
  134. value = "'" + value.replace('\\', '\\\\') + "'";
  135. }
  136. } else if (interpolate){
  137. value = this.templateSrv.replace(value, this.scopedVars, 'regex');
  138. }
  139. return str + '"' + tag.key + '" ' + operator + ' ' + value;
  140. }
  141. getMeasurementAndPolicy(interpolate) {
  142. var policy = this.target.policy;
  143. var measurement = this.target.measurement || 'measurement';
  144. if (!measurement.match('^/.*/')) {
  145. measurement = '"' + measurement+ '"';
  146. } else if (interpolate) {
  147. measurement = this.templateSrv.replace(measurement, this.scopedVars, 'regex');
  148. }
  149. if (policy !== 'default') {
  150. policy = '"' + this.target.policy + '".';
  151. } else {
  152. policy = "";
  153. }
  154. return policy + measurement;
  155. }
  156. render(interpolate?) {
  157. var target = this.target;
  158. if (target.rawQuery) {
  159. if (interpolate) {
  160. return this.templateSrv.replace(target.query, this.scopedVars, 'regex');
  161. } else {
  162. return target.query;
  163. }
  164. }
  165. var query = 'SELECT ';
  166. var i, y;
  167. for (i = 0; i < this.selectModels.length; i++) {
  168. let parts = this.selectModels[i];
  169. var selectText = "";
  170. for (y = 0; y < parts.length; y++) {
  171. let part = parts[y];
  172. selectText = part.render(selectText);
  173. }
  174. if (i > 0) {
  175. query += ', ';
  176. }
  177. query += selectText;
  178. }
  179. query += ' FROM ' + this.getMeasurementAndPolicy(interpolate) + ' WHERE ';
  180. var conditions = _.map(target.tags, (tag, index) => {
  181. return this.renderTagCondition(tag, index, interpolate);
  182. });
  183. query += conditions.join(' ');
  184. query += (conditions.length > 0 ? ' AND ' : '') + '$timeFilter';
  185. var groupBySection = "";
  186. for (i = 0; i < this.groupByParts.length; i++) {
  187. var part = this.groupByParts[i];
  188. if (i > 0) {
  189. // for some reason fill has no seperator
  190. groupBySection += part.def.type === 'fill' ? ' ' : ', ';
  191. }
  192. groupBySection += part.render('');
  193. }
  194. if (groupBySection.length) {
  195. query += ' GROUP BY ' + groupBySection;
  196. }
  197. if (target.fill) {
  198. query += ' fill(' + target.fill + ')';
  199. }
  200. return query;
  201. }
  202. }