influx_query.ts 7.3 KB

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