influx_query_specs.ts 8.3 KB

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