query_builder.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { InfluxQueryBuilder } from '../query_builder';
  2. describe('InfluxQueryBuilder', function() {
  3. describe('when building explore queries', function() {
  4. it('should only have measurement condition in tag keys query given query with measurement', function() {
  5. const builder = new InfluxQueryBuilder({ measurement: 'cpu', tags: [] });
  6. const query = builder.buildExploreQuery('TAG_KEYS');
  7. expect(query).toBe('SHOW TAG KEYS FROM "cpu"');
  8. });
  9. it('should handle regex measurement in tag keys query', function() {
  10. const builder = new InfluxQueryBuilder({
  11. measurement: '/.*/',
  12. tags: [],
  13. });
  14. const query = builder.buildExploreQuery('TAG_KEYS');
  15. expect(query).toBe('SHOW TAG KEYS FROM /.*/');
  16. });
  17. it('should have no conditions in tags keys query given query with no measurement or tag', function() {
  18. const builder = new InfluxQueryBuilder({ measurement: '', tags: [] });
  19. const query = builder.buildExploreQuery('TAG_KEYS');
  20. expect(query).toBe('SHOW TAG KEYS');
  21. });
  22. it('should have where condition in tag keys query with tags', function() {
  23. const builder = new InfluxQueryBuilder({
  24. measurement: '',
  25. tags: [{ key: 'host', value: 'se1' }],
  26. });
  27. const query = builder.buildExploreQuery('TAG_KEYS');
  28. expect(query).toBe('SHOW TAG KEYS WHERE "host" = \'se1\'');
  29. });
  30. it('should have no conditions in measurement query for query with no tags', function() {
  31. const builder = new InfluxQueryBuilder({ measurement: '', tags: [] });
  32. const query = builder.buildExploreQuery('MEASUREMENTS');
  33. expect(query).toBe('SHOW MEASUREMENTS LIMIT 100');
  34. });
  35. it('should have no conditions in measurement query for query with no tags and empty query', function() {
  36. const builder = new InfluxQueryBuilder({ measurement: '', tags: [] });
  37. const query = builder.buildExploreQuery('MEASUREMENTS', undefined, '');
  38. expect(query).toBe('SHOW MEASUREMENTS LIMIT 100');
  39. });
  40. it('should have WITH MEASUREMENT in measurement query for non-empty query with no tags', function() {
  41. const builder = new InfluxQueryBuilder({ measurement: '', tags: [] });
  42. const query = builder.buildExploreQuery('MEASUREMENTS', undefined, 'something');
  43. expect(query).toBe('SHOW MEASUREMENTS WITH MEASUREMENT =~ /something/ LIMIT 100');
  44. });
  45. it('should have WITH MEASUREMENT WHERE in measurement query for non-empty query with tags', function() {
  46. const builder = new InfluxQueryBuilder({
  47. measurement: '',
  48. tags: [{ key: 'app', value: 'email' }],
  49. });
  50. const query = builder.buildExploreQuery('MEASUREMENTS', undefined, 'something');
  51. expect(query).toBe('SHOW MEASUREMENTS WITH MEASUREMENT =~ /something/ WHERE "app" = \'email\' LIMIT 100');
  52. });
  53. it('should have where condition in measurement query for query with tags', function() {
  54. const builder = new InfluxQueryBuilder({
  55. measurement: '',
  56. tags: [{ key: 'app', value: 'email' }],
  57. });
  58. const query = builder.buildExploreQuery('MEASUREMENTS');
  59. expect(query).toBe('SHOW MEASUREMENTS WHERE "app" = \'email\' LIMIT 100');
  60. });
  61. it('should have where tag name IN filter in tag values query for query with one tag', function() {
  62. const builder = new InfluxQueryBuilder({
  63. measurement: '',
  64. tags: [{ key: 'app', value: 'asdsadsad' }],
  65. });
  66. const query = builder.buildExploreQuery('TAG_VALUES', 'app');
  67. expect(query).toBe('SHOW TAG VALUES WITH KEY = "app"');
  68. });
  69. it('should have measurement tag condition and tag name IN filter in tag values query', function() {
  70. const builder = new InfluxQueryBuilder({
  71. measurement: 'cpu',
  72. tags: [{ key: 'app', value: 'email' }, { key: 'host', value: 'server1' }],
  73. });
  74. const query = builder.buildExploreQuery('TAG_VALUES', 'app');
  75. expect(query).toBe('SHOW TAG VALUES FROM "cpu" WITH KEY = "app" WHERE "host" = \'server1\'');
  76. });
  77. it('should select from policy correctly if policy is specified', function() {
  78. const builder = new InfluxQueryBuilder({
  79. measurement: 'cpu',
  80. policy: 'one_week',
  81. tags: [{ key: 'app', value: 'email' }, { key: 'host', value: 'server1' }],
  82. });
  83. const query = builder.buildExploreQuery('TAG_VALUES', 'app');
  84. expect(query).toBe('SHOW TAG VALUES FROM "one_week"."cpu" WITH KEY = "app" WHERE "host" = \'server1\'');
  85. });
  86. it('should not include policy when policy is default', function() {
  87. const builder = new InfluxQueryBuilder({
  88. measurement: 'cpu',
  89. policy: 'default',
  90. tags: [],
  91. });
  92. const query = builder.buildExploreQuery('TAG_VALUES', 'app');
  93. expect(query).toBe('SHOW TAG VALUES FROM "cpu" WITH KEY = "app"');
  94. });
  95. it('should switch to regex operator in tag condition', function() {
  96. const builder = new InfluxQueryBuilder({
  97. measurement: 'cpu',
  98. tags: [{ key: 'host', value: '/server.*/' }],
  99. });
  100. const query = builder.buildExploreQuery('TAG_VALUES', 'app');
  101. expect(query).toBe('SHOW TAG VALUES FROM "cpu" WITH KEY = "app" WHERE "host" =~ /server.*/');
  102. });
  103. it('should build show field query', function() {
  104. const builder = new InfluxQueryBuilder({
  105. measurement: 'cpu',
  106. tags: [{ key: 'app', value: 'email' }],
  107. });
  108. const query = builder.buildExploreQuery('FIELDS');
  109. expect(query).toBe('SHOW FIELD KEYS FROM "cpu"');
  110. });
  111. it('should build show field query with regexp', function() {
  112. const builder = new InfluxQueryBuilder({
  113. measurement: '/$var/',
  114. tags: [{ key: 'app', value: 'email' }],
  115. });
  116. const query = builder.buildExploreQuery('FIELDS');
  117. expect(query).toBe('SHOW FIELD KEYS FROM /$var/');
  118. });
  119. it('should build show retention policies query', function() {
  120. const builder = new InfluxQueryBuilder({ measurement: 'cpu', tags: [] }, 'site');
  121. const query = builder.buildExploreQuery('RETENTION POLICIES');
  122. expect(query).toBe('SHOW RETENTION POLICIES on "site"');
  123. });
  124. });
  125. });