influx_query.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.resultFormat = target.resultFormat || 'time_series';
  19. target.orderByTime = target.orderByTime || 'ASC';
  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'] }, { 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(
  75. this.target.groupBy,
  76. (g: any) => g.type !== 'fill'
  77. );
  78. // remove aggregations
  79. this.target.select = _.map(this.target.select, (s: any) => {
  80. return _.filter(s, (part: any) => {
  81. var partModel = queryPart.create(part);
  82. if (partModel.def.category === categories.Aggregations) {
  83. return false;
  84. }
  85. if (partModel.def.category === categories.Selectors) {
  86. return false;
  87. }
  88. return true;
  89. });
  90. });
  91. }
  92. this.target.groupBy.splice(index, 1);
  93. this.updateProjection();
  94. }
  95. removeSelect(index: number) {
  96. this.target.select.splice(index, 1);
  97. this.updateProjection();
  98. }
  99. removeSelectPart(selectParts, part) {
  100. // if we remove the field remove the whole statement
  101. if (part.def.type === 'field') {
  102. if (this.selectModels.length > 1) {
  103. var modelsIndex = _.indexOf(this.selectModels, selectParts);
  104. this.selectModels.splice(modelsIndex, 1);
  105. }
  106. } else {
  107. var partIndex = _.indexOf(selectParts, part);
  108. selectParts.splice(partIndex, 1);
  109. }
  110. this.updatePersistedParts();
  111. }
  112. addSelectPart(selectParts, type) {
  113. var partModel = queryPart.create({ type: type });
  114. partModel.def.addStrategy(selectParts, partModel, this);
  115. this.updatePersistedParts();
  116. }
  117. private renderTagCondition(tag, index, interpolate) {
  118. var str = '';
  119. var operator = tag.operator;
  120. var value = tag.value;
  121. if (index > 0) {
  122. str = (tag.condition || 'AND') + ' ';
  123. }
  124. if (!operator) {
  125. if (/^\/.*\/$/.test(value)) {
  126. operator = '=~';
  127. } else {
  128. operator = '=';
  129. }
  130. }
  131. // quote value unless regex
  132. if (operator !== '=~' && operator !== '!~') {
  133. if (interpolate) {
  134. value = this.templateSrv.replace(value, this.scopedVars);
  135. }
  136. if (operator !== '>' && operator !== '<') {
  137. value = "'" + value.replace(/\\/g, '\\\\') + "'";
  138. }
  139. } else if (interpolate) {
  140. value = this.templateSrv.replace(value, this.scopedVars, 'regex');
  141. }
  142. return str + '"' + tag.key + '" ' + operator + ' ' + value;
  143. }
  144. getMeasurementAndPolicy(interpolate) {
  145. var policy = this.target.policy;
  146. var measurement = this.target.measurement || 'measurement';
  147. if (!measurement.match('^/.*/$')) {
  148. measurement = '"' + measurement + '"';
  149. } else if (interpolate) {
  150. measurement = this.templateSrv.replace(
  151. measurement,
  152. this.scopedVars,
  153. 'regex'
  154. );
  155. }
  156. if (policy !== 'default') {
  157. policy = '"' + this.target.policy + '".';
  158. } else {
  159. policy = '';
  160. }
  161. return policy + measurement;
  162. }
  163. interpolateQueryStr(value, variable, defaultFormatFn) {
  164. // if no multi or include all do not regexEscape
  165. if (!variable.multi && !variable.includeAll) {
  166. return value;
  167. }
  168. if (typeof value === 'string') {
  169. return kbn.regexEscape(value);
  170. }
  171. var escapedValues = _.map(value, kbn.regexEscape);
  172. return '(' + escapedValues.join('|') + ')';
  173. }
  174. render(interpolate?) {
  175. var target = this.target;
  176. if (target.rawQuery) {
  177. if (interpolate) {
  178. return this.templateSrv.replace(
  179. target.query,
  180. this.scopedVars,
  181. this.interpolateQueryStr
  182. );
  183. } else {
  184. return target.query;
  185. }
  186. }
  187. var query = 'SELECT ';
  188. var i, y;
  189. for (i = 0; i < this.selectModels.length; i++) {
  190. let parts = this.selectModels[i];
  191. var selectText = '';
  192. for (y = 0; y < parts.length; y++) {
  193. let part = parts[y];
  194. selectText = part.render(selectText);
  195. }
  196. if (i > 0) {
  197. query += ', ';
  198. }
  199. query += selectText;
  200. }
  201. query += ' FROM ' + this.getMeasurementAndPolicy(interpolate) + ' WHERE ';
  202. var conditions = _.map(target.tags, (tag, index) => {
  203. return this.renderTagCondition(tag, index, interpolate);
  204. });
  205. if (conditions.length > 0) {
  206. query += '(' + conditions.join(' ') + ') AND ';
  207. }
  208. query += '$timeFilter';
  209. var groupBySection = '';
  210. for (i = 0; i < this.groupByParts.length; i++) {
  211. var part = this.groupByParts[i];
  212. if (i > 0) {
  213. // for some reason fill has no seperator
  214. groupBySection += part.def.type === 'fill' ? ' ' : ', ';
  215. }
  216. groupBySection += part.render('');
  217. }
  218. if (groupBySection.length) {
  219. query += ' GROUP BY ' + groupBySection;
  220. }
  221. if (target.fill) {
  222. query += ' fill(' + target.fill + ')';
  223. }
  224. if (target.orderByTime === 'DESC') {
  225. query += ' ORDER BY time DESC';
  226. }
  227. if (target.limit) {
  228. query += ' LIMIT ' + target.limit;
  229. }
  230. if (target.slimit) {
  231. query += ' SLIMIT ' + target.slimit;
  232. }
  233. return query;
  234. }
  235. renderAdhocFilters(filters) {
  236. var conditions = _.map(filters, (tag, index) => {
  237. return this.renderTagCondition(tag, index, false);
  238. });
  239. return conditions.join(' ');
  240. }
  241. }