influx_query.ts 7.0 KB

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