influx_query.ts 7.3 KB

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