influx_query_model.ts 7.4 KB

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