influx_query_specs.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import {describe, beforeEach, it, sinon, expect} from 'test/lib/common';
  2. import InfluxQuery from '../influx_query';
  3. describe('InfluxQuery', function() {
  4. describe('render series with mesurement only', function() {
  5. it('should generate correct query', function() {
  6. var query = new InfluxQuery({
  7. measurement: 'cpu',
  8. });
  9. var queryText = query.render();
  10. expect(queryText).to.be('SELECT mean("value") FROM "cpu" WHERE $timeFilter GROUP BY time($interval) fill(null)');
  11. });
  12. });
  13. describe('render series with policy only', function() {
  14. it('should generate correct query', function() {
  15. var query = new InfluxQuery({
  16. measurement: 'cpu',
  17. policy: '5m_avg'
  18. });
  19. var queryText = query.render();
  20. expect(queryText).to.be('SELECT mean("value") FROM "5m_avg"."cpu" WHERE $timeFilter GROUP BY time($interval) fill(null)');
  21. });
  22. });
  23. describe('render series with math and alias', function() {
  24. it('should generate correct query', function() {
  25. var query = new InfluxQuery({
  26. measurement: 'cpu',
  27. select: [
  28. [
  29. {type: 'field', params: ['value']},
  30. {type: 'mean', params: []},
  31. {type: 'math', params: ['/100']},
  32. {type: 'alias', params: ['text']},
  33. ]
  34. ]
  35. });
  36. var queryText = query.render();
  37. expect(queryText).to.be('SELECT mean("value") /100 AS "text" FROM "cpu" WHERE $timeFilter GROUP BY time($interval) fill(null)');
  38. });
  39. });
  40. describe('series with single tag only', function() {
  41. it('should generate correct query', function() {
  42. var query = new InfluxQuery({
  43. measurement: 'cpu',
  44. groupBy: [{type: 'time', params: ['auto']}],
  45. tags: [{key: 'hostname', value: 'server\\1'}]
  46. });
  47. var queryText = query.render();
  48. expect(queryText).to.be('SELECT mean("value") FROM "cpu" WHERE "hostname" = \'server\\\\1\' AND $timeFilter'
  49. + ' GROUP BY time($interval)');
  50. });
  51. it('should switch regex operator with tag value is regex', function() {
  52. var query = new InfluxQuery({
  53. measurement: 'cpu',
  54. groupBy: [{type: 'time', params: ['auto']}],
  55. tags: [{key: 'app', value: '/e.*/'}]
  56. });
  57. var queryText = query.render();
  58. expect(queryText).to.be('SELECT mean("value") FROM "cpu" WHERE "app" =~ /e.*/ AND $timeFilter GROUP BY time($interval)');
  59. });
  60. });
  61. describe('series with multiple tags only', function() {
  62. it('should generate correct query', function() {
  63. var query = new InfluxQuery({
  64. measurement: 'cpu',
  65. groupBy: [{type: 'time', params: ['auto']}],
  66. tags: [{key: 'hostname', value: 'server1'}, {key: 'app', value: 'email', condition: "AND"}]
  67. });
  68. var queryText = query.render();
  69. expect(queryText).to.be('SELECT mean("value") FROM "cpu" WHERE "hostname" = \'server1\' AND "app" = \'email\' AND ' +
  70. '$timeFilter GROUP BY time($interval)');
  71. });
  72. });
  73. describe('series with tags OR condition', function() {
  74. it('should generate correct query', function() {
  75. var query = new InfluxQuery({
  76. measurement: 'cpu',
  77. groupBy: [{type: 'time', params: ['auto']}],
  78. tags: [{key: 'hostname', value: 'server1'}, {key: 'hostname', value: 'server2', condition: "OR"}]
  79. });
  80. var queryText = query.render();
  81. expect(queryText).to.be('SELECT mean("value") FROM "cpu" WHERE "hostname" = \'server1\' OR "hostname" = \'server2\' AND ' +
  82. '$timeFilter GROUP BY time($interval)');
  83. });
  84. });
  85. describe('series with groupByTag', function() {
  86. it('should generate correct query', function() {
  87. var query = new InfluxQuery({
  88. measurement: 'cpu',
  89. tags: [],
  90. groupBy: [{type: 'time', interval: 'auto'}, {type: 'tag', params: ['host']}],
  91. });
  92. var queryText = query.render();
  93. expect(queryText).to.be('SELECT mean("value") FROM "cpu" WHERE $timeFilter ' +
  94. 'GROUP BY time($interval), "host"');
  95. });
  96. });
  97. describe('render series without group by', function() {
  98. it('should generate correct query', function() {
  99. var query = new InfluxQuery({
  100. measurement: 'cpu',
  101. select: [[{type: 'field', params: ['value']}]],
  102. groupBy: [],
  103. });
  104. var queryText = query.render();
  105. expect(queryText).to.be('SELECT "value" FROM "cpu" WHERE $timeFilter');
  106. });
  107. });
  108. describe('render series without group by and fill', function() {
  109. it('should generate correct query', function() {
  110. var query = new InfluxQuery({
  111. measurement: 'cpu',
  112. select: [[{type: 'field', params: ['value']}]],
  113. groupBy: [{type: 'time'}, {type: 'fill', params: ['0']}],
  114. });
  115. var queryText = query.render();
  116. expect(queryText).to.be('SELECT "value" FROM "cpu" WHERE $timeFilter GROUP BY time($interval) fill(0)');
  117. });
  118. });
  119. describe('when adding group by part', function() {
  120. it('should add tag before fill', function() {
  121. var query = new InfluxQuery({
  122. measurement: 'cpu',
  123. groupBy: [{type: 'time'}, {type: 'fill'}]
  124. });
  125. query.addGroupBy('tag(host)');
  126. expect(query.target.groupBy.length).to.be(3);
  127. expect(query.target.groupBy[1].type).to.be('tag');
  128. expect(query.target.groupBy[1].params[0]).to.be('host');
  129. expect(query.target.groupBy[2].type).to.be('fill');
  130. });
  131. it('should add tag last if no fill', function() {
  132. var query = new InfluxQuery({
  133. measurement: 'cpu',
  134. groupBy: []
  135. });
  136. query.addGroupBy('tag(host)');
  137. expect(query.target.groupBy.length).to.be(1);
  138. expect(query.target.groupBy[0].type).to.be('tag');
  139. });
  140. });
  141. describe('when adding select part', function() {
  142. it('should add mean after after field', function() {
  143. var query = new InfluxQuery({
  144. measurement: 'cpu',
  145. select: [[{type: 'field', params: ['value']}]]
  146. });
  147. query.addSelectPart(query.selectModels[0], 'mean');
  148. expect(query.target.select[0].length).to.be(2);
  149. expect(query.target.select[0][1].type).to.be('mean');
  150. });
  151. it('should replace sum by mean', function() {
  152. var query = new InfluxQuery({
  153. measurement: 'cpu',
  154. select: [[{type: 'field', params: ['value']}, {type: 'mean'}]]
  155. });
  156. query.addSelectPart(query.selectModels[0], 'sum');
  157. expect(query.target.select[0].length).to.be(2);
  158. expect(query.target.select[0][1].type).to.be('sum');
  159. });
  160. it('should add math before alias', function() {
  161. var query = new InfluxQuery({
  162. measurement: 'cpu',
  163. select: [[{type: 'field', params: ['value']}, {type: 'mean'}, {type: 'alias'}]]
  164. });
  165. query.addSelectPart(query.selectModels[0], 'math');
  166. expect(query.target.select[0].length).to.be(4);
  167. expect(query.target.select[0][2].type).to.be('math');
  168. });
  169. it('should add math last', function() {
  170. var query = new InfluxQuery({
  171. measurement: 'cpu',
  172. select: [[{type: 'field', params: ['value']}, {type: 'mean'}]]
  173. });
  174. query.addSelectPart(query.selectModels[0], 'math');
  175. expect(query.target.select[0].length).to.be(3);
  176. expect(query.target.select[0][2].type).to.be('math');
  177. });
  178. it('should replace math', function() {
  179. var query = new InfluxQuery({
  180. measurement: 'cpu',
  181. select: [[{type: 'field', params: ['value']}, {type: 'mean'}, {type: 'math'}]]
  182. });
  183. query.addSelectPart(query.selectModels[0], 'math');
  184. expect(query.target.select[0].length).to.be(3);
  185. expect(query.target.select[0][2].type).to.be('math');
  186. });
  187. });
  188. });