influx_query.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. ///<reference path="../../../headers/common.d.ts" />
  2. ///<amd-dependency path="./query_builder" name="InfluxQueryBuilder" />
  3. import _ = require('lodash');
  4. import queryPart = require('./query_part');
  5. declare var InfluxQueryBuilder: any;
  6. class InfluxQuery {
  7. target: any;
  8. selectModels: any[];
  9. groupByParts: any;
  10. queryBuilder: any;
  11. constructor(target) {
  12. this.target = target;
  13. target.tags = target.tags || [];
  14. target.groupBy = target.groupBy || [{type: 'time', params: ['$interval']}];
  15. target.select = target.select || [[
  16. {type: 'field', params: ['value']},
  17. {type: 'mean', params: []},
  18. ]];
  19. this.updateProjection();
  20. }
  21. updateProjection() {
  22. this.selectModels = _.map(this.target.select, function(parts: any) {
  23. return _.map(parts, queryPart.create);
  24. });
  25. this.groupByParts = _.map(this.target.groupBy, queryPart.create);
  26. }
  27. updatePersistedParts() {
  28. this.target.select = _.map(this.selectModels, function(selectParts) {
  29. return _.map(selectParts, function(part: any) {
  30. return {type: part.def.type, params: part.params};
  31. });
  32. });
  33. }
  34. hasGroupByTime() {
  35. return false;
  36. }
  37. hasFill() {
  38. return false;
  39. }
  40. addGroupBy(value) {
  41. var stringParts = value.match(/^(\w+)\((.*)\)$/);
  42. var typePart = stringParts[1];
  43. var arg = stringParts[2];
  44. console.log(value, stringParts);
  45. var partModel = queryPart.create({type: typePart, params: [arg]});
  46. this.target.groupBy.push(partModel.part);
  47. this.updateProjection();
  48. }
  49. removeGroupByPart(part, index) {
  50. this.target.groupBy.splice(index, 1);
  51. this.updateProjection();
  52. }
  53. removeSelect(index: number) {
  54. this.target.select.splice(index, 1);
  55. this.updateProjection();
  56. }
  57. removeSelectPart(selectParts, part) {
  58. // if we remove the field remove the whole statement
  59. if (part.def.type === 'field') {
  60. if (this.selectModels.length > 1) {
  61. var modelsIndex = _.indexOf(this.selectModels, selectParts);
  62. this.selectModels.splice(modelsIndex, 1);
  63. }
  64. } else {
  65. var partIndex = _.indexOf(selectParts, part);
  66. selectParts.splice(partIndex, 1);
  67. }
  68. this.updatePersistedParts();
  69. }
  70. addSelectPart(selectParts, type) {
  71. var partModel = queryPart.create({type: type});
  72. partModel.def.addStrategy(selectParts, partModel, this);
  73. this.updatePersistedParts();
  74. }
  75. private renderTagCondition(tag, index) {
  76. var str = "";
  77. var operator = tag.operator;
  78. var value = tag.value;
  79. if (index > 0) {
  80. str = (tag.condition || 'AND') + ' ';
  81. }
  82. if (!operator) {
  83. if (/^\/.*\/$/.test(tag.value)) {
  84. operator = '=~';
  85. } else {
  86. operator = '=';
  87. }
  88. }
  89. // quote value unless regex
  90. if (operator !== '=~' && operator !== '!~') {
  91. value = "'" + value + "'";
  92. }
  93. return str + '"' + tag.key + '" ' + operator + ' ' + value;
  94. }
  95. private getGroupByTimeInterval(interval) {
  96. if (interval === 'auto') {
  97. return '$interval';
  98. }
  99. return interval;
  100. }
  101. render() {
  102. var target = this.target;
  103. if (target.rawQuery) {
  104. return target.query;
  105. }
  106. if (!target.measurement) {
  107. throw "Metric measurement is missing";
  108. }
  109. var query = 'SELECT ';
  110. var i, y;
  111. for (i = 0; i < this.selectModels.length; i++) {
  112. let parts = this.selectModels[i];
  113. var selectText = "";
  114. for (y = 0; y < parts.length; y++) {
  115. let part = parts[y];
  116. selectText = part.render(selectText);
  117. }
  118. if (i > 0) {
  119. query += ', ';
  120. }
  121. query += selectText;
  122. }
  123. var measurement = target.measurement;
  124. if (!measurement.match('^/.*/') && !measurement.match(/^merge\(.*\)/)) {
  125. measurement = '"' + measurement+ '"';
  126. }
  127. query += ' FROM ' + measurement + ' WHERE ';
  128. var conditions = _.map(target.tags, (tag, index) => {
  129. return this.renderTagCondition(tag, index);
  130. });
  131. query += conditions.join(' ');
  132. query += (conditions.length > 0 ? ' AND ' : '') + '$timeFilter';
  133. query += ' GROUP BY ';
  134. for (i = 0; i < this.groupByParts.length; i++) {
  135. var part = this.groupByParts[i];
  136. if (i > 0) {
  137. query += ', ';
  138. }
  139. query += part.render('');
  140. }
  141. if (target.fill) {
  142. query += ' fill(' + target.fill + ')';
  143. }
  144. target.query = query;
  145. return query;
  146. }
  147. }
  148. export = InfluxQuery;