query_builder.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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(type: string, withKey?: string, withMeasurementFilter?: string) {
  25. var query;
  26. var measurement;
  27. var policy;
  28. if (type === 'TAG_KEYS') {
  29. query = 'SHOW TAG KEYS';
  30. measurement = this.target.measurement;
  31. policy = this.target.policy;
  32. } else if (type === 'TAG_VALUES') {
  33. query = 'SHOW TAG VALUES';
  34. measurement = this.target.measurement;
  35. policy = this.target.policy;
  36. } else if (type === 'MEASUREMENTS') {
  37. query = 'SHOW MEASUREMENTS';
  38. if (withMeasurementFilter) {
  39. query += ' WITH MEASUREMENT =~ /' + withMeasurementFilter + '/';
  40. }
  41. } else if (type === 'FIELDS') {
  42. measurement = this.target.measurement;
  43. policy = this.target.policy;
  44. if (!measurement.match('^/.*/')) {
  45. measurement = '"' + measurement + '"';
  46. if (policy) {
  47. if (!policy.match('^/.*/')) {
  48. policy = '"' + policy + '"';
  49. }
  50. measurement = policy + '.' + measurement;
  51. }
  52. }
  53. return 'SHOW FIELD KEYS FROM ' + measurement;
  54. } else if (type === 'RETENTION POLICIES') {
  55. query = 'SHOW RETENTION POLICIES on "' + this.database + '"';
  56. return query;
  57. }
  58. if (measurement) {
  59. if (!measurement.match('^/.*/') && !measurement.match(/^merge\(.*\)/)) {
  60. measurement = '"' + measurement + '"';
  61. }
  62. if (policy && policy !== 'default') {
  63. if (!policy.match('^/.*/') && !policy.match(/^merge\(.*\)/)) {
  64. policy = '"' + policy + '"';
  65. }
  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. }