influx_query.ts 7.3 KB

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