query_builder.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. define([
  2. 'lodash'
  3. ],
  4. function (_) {
  5. 'use strict';
  6. function InfluxQueryBuilder(target, database) {
  7. this.target = target;
  8. this.database = database;
  9. }
  10. function renderTagCondition (tag, index) {
  11. var str = "";
  12. var operator = tag.operator;
  13. var value = tag.value;
  14. if (index > 0) {
  15. str = (tag.condition || 'AND') + ' ';
  16. }
  17. if (!operator) {
  18. if (/^\/.*\/$/.test(tag.value)) {
  19. operator = '=~';
  20. } else {
  21. operator = '=';
  22. }
  23. }
  24. // quote value unless regex or number
  25. if (operator !== '=~' && operator !== '!~' && isNaN(+value)) {
  26. value = "'" + value + "'";
  27. }
  28. return str + '"' + tag.key + '" ' + operator + ' ' + value;
  29. }
  30. var p = InfluxQueryBuilder.prototype;
  31. p.build = function() {
  32. return this.target.rawQuery ? this._modifyRawQuery() : this._buildQuery();
  33. };
  34. p.buildExploreQuery = function(type, withKey, withMeasurementFilter) {
  35. var query;
  36. var measurement;
  37. var policy;
  38. if (type === 'TAG_KEYS') {
  39. query = 'SHOW TAG KEYS';
  40. measurement = this.target.measurement;
  41. policy = this.target.policy;
  42. } else if (type === 'TAG_VALUES') {
  43. query = 'SHOW TAG VALUES';
  44. measurement = this.target.measurement;
  45. policy = this.target.policy;
  46. } else if (type === 'MEASUREMENTS') {
  47. query = 'SHOW MEASUREMENTS';
  48. if (withMeasurementFilter)
  49. {
  50. query += ' WITH MEASUREMENT =~ /' + withMeasurementFilter +'/';
  51. }
  52. } else if (type === 'FIELDS') {
  53. measurement = this.target.measurement;
  54. policy = this.target.policy;
  55. if (!measurement.match('^/.*/')) {
  56. measurement = '"' + measurement + '"';
  57. if (policy) {
  58. if (!policy.match('^/.*/')) {
  59. policy = '"' + policy + '"';
  60. }
  61. measurement = policy + '.' + measurement;
  62. }
  63. }
  64. return 'SHOW FIELD KEYS FROM ' + measurement;
  65. } else if (type === 'RETENTION POLICIES') {
  66. query = 'SHOW RETENTION POLICIES on "' + this.database + '"';
  67. return query;
  68. }
  69. if (measurement) {
  70. if (!measurement.match('^/.*/') && !measurement.match(/^merge\(.*\)/)) {
  71. measurement = '"' + measurement+ '"';
  72. }
  73. if (policy) {
  74. if (!policy.match('^/.*/') && !policy.match(/^merge\(.*\)/)) {
  75. policy = '"' + policy + '"';
  76. }
  77. measurement = policy + '.' + measurement;
  78. }
  79. query += ' FROM ' + measurement;
  80. }
  81. if (withKey) {
  82. query += ' WITH KEY = "' + withKey + '"';
  83. }
  84. if (this.target.tags && this.target.tags.length > 0) {
  85. var whereConditions = _.reduce(this.target.tags, function(memo, tag) {
  86. // do not add a condition for the key we want to explore for
  87. if (tag.key === withKey) {
  88. return memo;
  89. }
  90. memo.push(renderTagCondition(tag, memo.length));
  91. return memo;
  92. }, []);
  93. if (whereConditions.length > 0) {
  94. query += ' WHERE ' + whereConditions.join(' ');
  95. }
  96. }
  97. if (type === 'MEASUREMENTS')
  98. {
  99. query += ' LIMIT 100';
  100. //Solve issue #2524 by limiting the number of measurements returned
  101. //LIMIT must be after WITH MEASUREMENT and WHERE clauses
  102. //This also could be used for TAG KEYS and TAG VALUES, if desired
  103. }
  104. return query;
  105. };
  106. return InfluxQueryBuilder;
  107. });