influx_query.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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', interval: 'auto'}];
  15. target.select = target.select || [[
  16. {type: 'field', params: ['value']},
  17. {type: 'mean', params: []},
  18. ]];
  19. this.updateSelectParts();
  20. this.groupByParts = [
  21. queryPart.create({type: 'time', params: ['$interval']})
  22. ];
  23. }
  24. updateSelectParts() {
  25. this.selectModels = _.map(this.target.select, function(parts: any) {
  26. return _.map(parts, queryPart.create);
  27. });
  28. }
  29. updatePersistedParts() {
  30. this.target.select = _.map(this.selectModels, function(selectParts) {
  31. return _.map(selectParts, function(part: any) {
  32. return {type: part.def.type, params: part.params};
  33. });
  34. });
  35. }
  36. removeSelect(index: number) {
  37. this.target.select.splice(index, 1);
  38. this.updateSelectParts();
  39. }
  40. removeSelectPart(selectParts, part) {
  41. // if we remove the field remove the whole statement
  42. if (part.def.type === 'field') {
  43. if (this.selectModels.length > 1) {
  44. var modelsIndex = _.indexOf(this.selectModels, selectParts);
  45. this.selectModels.splice(modelsIndex, 1);
  46. }
  47. } else {
  48. var partIndex = _.indexOf(selectParts, part);
  49. selectParts.splice(partIndex, 1);
  50. }
  51. this.updatePersistedParts();
  52. }
  53. addSelectPart(selectParts, type) {
  54. var partModel = queryPart.create({type: type});
  55. partModel.def.addStrategy(selectParts, partModel, this);
  56. this.updatePersistedParts();
  57. }
  58. private renderTagCondition(tag, index) {
  59. var str = "";
  60. var operator = tag.operator;
  61. var value = tag.value;
  62. if (index > 0) {
  63. str = (tag.condition || 'AND') + ' ';
  64. }
  65. if (!operator) {
  66. if (/^\/.*\/$/.test(tag.value)) {
  67. operator = '=~';
  68. } else {
  69. operator = '=';
  70. }
  71. }
  72. // quote value unless regex
  73. if (operator !== '=~' && operator !== '!~') {
  74. value = "'" + value + "'";
  75. }
  76. return str + '"' + tag.key + '" ' + operator + ' ' + value;
  77. }
  78. private getGroupByTimeInterval(interval) {
  79. if (interval === 'auto') {
  80. return '$interval';
  81. }
  82. return interval;
  83. }
  84. render() {
  85. var target = this.target;
  86. if (target.rawQuery) {
  87. return target.query;
  88. }
  89. if (!target.measurement) {
  90. throw "Metric measurement is missing";
  91. }
  92. var query = 'SELECT ';
  93. var i, y;
  94. for (i = 0; i < this.selectModels.length; i++) {
  95. let parts = this.selectModels[i];
  96. var selectText = "";
  97. for (y = 0; y < parts.length; y++) {
  98. let part = parts[y];
  99. selectText = part.render(selectText);
  100. }
  101. if (i > 0) {
  102. query += ', ';
  103. }
  104. query += selectText;
  105. }
  106. var measurement = target.measurement;
  107. if (!measurement.match('^/.*/') && !measurement.match(/^merge\(.*\)/)) {
  108. measurement = '"' + measurement+ '"';
  109. }
  110. query += ' FROM ' + measurement + ' WHERE ';
  111. var conditions = _.map(target.tags, (tag, index) => {
  112. return this.renderTagCondition(tag, index);
  113. });
  114. query += conditions.join(' ');
  115. query += (conditions.length > 0 ? ' AND ' : '') + '$timeFilter';
  116. query += ' GROUP BY';
  117. for (i = 0; i < target.groupBy.length; i++) {
  118. var group = target.groupBy[i];
  119. if (group.type === 'time') {
  120. query += ' time(' + this.getGroupByTimeInterval(group.interval) + ')';
  121. } else {
  122. query += ', "' + group.key + '"';
  123. }
  124. }
  125. if (target.fill) {
  126. query += ' fill(' + target.fill + ')';
  127. }
  128. target.query = query;
  129. return query;
  130. }
  131. }
  132. export = InfluxQuery;