queryBuilder.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. define([
  2. 'lodash'
  3. ],
  4. function (_) {
  5. 'use strict';
  6. function InfluxQueryBuilder(target) {
  7. this.target = target;
  8. }
  9. function renderTagCondition (tag, index) {
  10. var str = "";
  11. var operator = tag.operator;
  12. var value = tag.value;
  13. if (index > 0) {
  14. str = (tag.condition || 'AND') + ' ';
  15. }
  16. if (!operator) {
  17. if (/^\/.*\/$/.test(tag.value)) {
  18. operator = '=~';
  19. } else {
  20. operator = '=';
  21. }
  22. }
  23. // quote value unless regex
  24. if (operator !== '=~' && operator !== '!~') {
  25. value = "'" + value + "'";
  26. }
  27. return str + '"' + tag.key + '" ' + operator + ' ' + value;
  28. }
  29. var p = InfluxQueryBuilder.prototype;
  30. p.build = function() {
  31. return this.target.rawQuery ? this._modifyRawQuery() : this._buildQuery();
  32. };
  33. p.buildExploreQuery = function(type, withKey) {
  34. var query;
  35. var measurement;
  36. if (type === 'TAG_KEYS') {
  37. query = 'SHOW TAG KEYS';
  38. measurement = this.target.measurement;
  39. } else if (type === 'TAG_VALUES') {
  40. query = 'SHOW TAG VALUES';
  41. measurement = this.target.measurement;
  42. } else if (type === 'MEASUREMENTS') {
  43. query = 'SHOW MEASUREMENTS';
  44. } else if (type === 'FIELDS') {
  45. query = 'SHOW FIELD KEYS FROM "' + this.target.measurement + '"';
  46. return query;
  47. }
  48. if (measurement) {
  49. if (!measurement.match('^/.*/') && !measurement.match(/^merge\(.*\)/)) {
  50. measurement = '"' + measurement+ '"';
  51. }
  52. query += ' FROM ' + measurement;
  53. }
  54. if (withKey) {
  55. query += ' WITH KEY = "' + withKey + '"';
  56. }
  57. if (this.target.tags && this.target.tags.length > 0) {
  58. var whereConditions = _.reduce(this.target.tags, function(memo, tag) {
  59. // do not add a condition for the key we want to explore for
  60. if (tag.key === withKey) {
  61. return memo;
  62. }
  63. memo.push(renderTagCondition(tag, memo.length));
  64. return memo;
  65. }, []);
  66. if (whereConditions.length > 0) {
  67. query += ' WHERE ' + whereConditions.join(' ');
  68. }
  69. }
  70. return query;
  71. };
  72. p._buildQuery = function() {
  73. var target = this.target;
  74. if (!target.measurement) {
  75. throw "Metric measurement is missing";
  76. }
  77. if (!target.fields) {
  78. target.fields = [{name: 'value', func: target.function || 'mean'}];
  79. }
  80. var query = 'SELECT ';
  81. var i;
  82. for (i = 0; i < target.fields.length; i++) {
  83. var field = target.fields[i];
  84. if (i > 0) {
  85. query += ', ';
  86. }
  87. query += field.func + '(' + field.name + ')';
  88. }
  89. var measurement = target.measurement;
  90. if (!measurement.match('^/.*/') && !measurement.match(/^merge\(.*\)/)) {
  91. measurement = '"' + measurement+ '"';
  92. }
  93. query += ' FROM ' + measurement + ' WHERE ';
  94. var conditions = _.map(target.tags, function(tag, index) {
  95. return renderTagCondition(tag, index);
  96. });
  97. query += conditions.join(' ');
  98. query += (conditions.length > 0 ? ' AND ' : '') + '$timeFilter';
  99. query += ' GROUP BY time($interval)';
  100. if (target.groupByTags && target.groupByTags.length > 0) {
  101. query += ', "' + target.groupByTags.join('", "') + '"';
  102. }
  103. if (target.fill) {
  104. query += ' fill(' + target.fill + ')';
  105. }
  106. target.query = query;
  107. return query;
  108. };
  109. p._modifyRawQuery = function () {
  110. var query = this.target.query.replace(";", "");
  111. return query;
  112. };
  113. return InfluxQueryBuilder;
  114. });