query_builder.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import _ from 'lodash';
  2. function renderTagCondition(tag, index) {
  3. let str = '';
  4. let operator = tag.operator;
  5. let 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. let query;
  26. let measurement;
  27. let 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 && policy !== 'default') {
  47. policy = '"' + policy + '"';
  48. measurement = policy + '.' + measurement;
  49. }
  50. }
  51. return 'SHOW FIELD KEYS FROM ' + measurement;
  52. } else if (type === 'RETENTION POLICIES') {
  53. query = 'SHOW RETENTION POLICIES on "' + this.database + '"';
  54. return query;
  55. }
  56. if (measurement) {
  57. if (!measurement.match('^/.*/') && !measurement.match(/^merge\(.*\)/)) {
  58. measurement = '"' + measurement + '"';
  59. }
  60. if (policy && policy !== 'default') {
  61. policy = '"' + policy + '"';
  62. measurement = policy + '.' + measurement;
  63. }
  64. query += ' FROM ' + measurement;
  65. }
  66. if (withKey) {
  67. query += ' WITH KEY = "' + withKey + '"';
  68. }
  69. if (this.target.tags && this.target.tags.length > 0) {
  70. const whereConditions = _.reduce(
  71. this.target.tags,
  72. function(memo, tag) {
  73. // do not add a condition for the key we want to explore for
  74. if (tag.key === withKey) {
  75. return memo;
  76. }
  77. memo.push(renderTagCondition(tag, memo.length));
  78. return memo;
  79. },
  80. []
  81. );
  82. if (whereConditions.length > 0) {
  83. query += ' WHERE ' + whereConditions.join(' ');
  84. }
  85. }
  86. if (type === 'MEASUREMENTS') {
  87. query += ' LIMIT 100';
  88. //Solve issue #2524 by limiting the number of measurements returned
  89. //LIMIT must be after WITH MEASUREMENT and WHERE clauses
  90. //This also could be used for TAG KEYS and TAG VALUES, if desired
  91. }
  92. return query;
  93. }
  94. }