query_builder.ts 3.0 KB

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