influx_query_specs.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 GROUP BY time($__interval), "host"');
  106. });
  107. });
  108. describe('render series without group by', function() {
  109. it('should generate correct query', function() {
  110. var query = new InfluxQuery({
  111. measurement: 'cpu',
  112. select: [[{type: 'field', params: ['value']}]],
  113. groupBy: [],
  114. }, templateSrv, {});
  115. var queryText = query.render();
  116. expect(queryText).to.be('SELECT "value" FROM "cpu" WHERE $timeFilter');
  117. });
  118. });
  119. describe('render series without group by and fill', function() {
  120. it('should generate correct query', function() {
  121. var query = new InfluxQuery({
  122. measurement: 'cpu',
  123. select: [[{type: 'field', params: ['value']}]],
  124. groupBy: [{type: 'time'}, {type: 'fill', params: ['0']}],
  125. }, templateSrv, {});
  126. var queryText = query.render();
  127. expect(queryText).to.be('SELECT "value" FROM "cpu" WHERE $timeFilter GROUP BY time($__interval) fill(0)');
  128. });
  129. });
  130. describe('when adding group by part', function() {
  131. it('should add tag before fill', function() {
  132. var query = new InfluxQuery({
  133. measurement: 'cpu',
  134. groupBy: [{type: 'time'}, {type: 'fill'}]
  135. }, templateSrv, {});
  136. query.addGroupBy('tag(host)');
  137. expect(query.target.groupBy.length).to.be(3);
  138. expect(query.target.groupBy[1].type).to.be('tag');
  139. expect(query.target.groupBy[1].params[0]).to.be('host');
  140. expect(query.target.groupBy[2].type).to.be('fill');
  141. });
  142. it('should add tag last if no fill', function() {
  143. var query = new InfluxQuery({
  144. measurement: 'cpu',
  145. groupBy: []
  146. }, templateSrv, {});
  147. query.addGroupBy('tag(host)');
  148. expect(query.target.groupBy.length).to.be(1);
  149. expect(query.target.groupBy[0].type).to.be('tag');
  150. });
  151. });
  152. describe('when adding select part', function() {
  153. it('should add mean after after field', function() {
  154. var query = new InfluxQuery({
  155. measurement: 'cpu',
  156. select: [[{type: 'field', params: ['value']}]]
  157. }, templateSrv, {});
  158. query.addSelectPart(query.selectModels[0], 'mean');
  159. expect(query.target.select[0].length).to.be(2);
  160. expect(query.target.select[0][1].type).to.be('mean');
  161. });
  162. it('should replace sum by mean', function() {
  163. var query = new InfluxQuery({
  164. measurement: 'cpu',
  165. select: [[{type: 'field', params: ['value']}, {type: 'mean'}]]
  166. }, templateSrv, {});
  167. query.addSelectPart(query.selectModels[0], 'sum');
  168. expect(query.target.select[0].length).to.be(2);
  169. expect(query.target.select[0][1].type).to.be('sum');
  170. });
  171. it('should add math before alias', function() {
  172. var query = new InfluxQuery({
  173. measurement: 'cpu',
  174. select: [[{type: 'field', params: ['value']}, {type: 'mean'}, {type: 'alias'}]]
  175. }, templateSrv, {});
  176. query.addSelectPart(query.selectModels[0], 'math');
  177. expect(query.target.select[0].length).to.be(4);
  178. expect(query.target.select[0][2].type).to.be('math');
  179. });
  180. it('should add math last', function() {
  181. var query = new InfluxQuery({
  182. measurement: 'cpu',
  183. select: [[{type: 'field', params: ['value']}, {type: 'mean'}]]
  184. }, templateSrv, {});
  185. query.addSelectPart(query.selectModels[0], 'math');
  186. expect(query.target.select[0].length).to.be(3);
  187. expect(query.target.select[0][2].type).to.be('math');
  188. });
  189. it('should replace math', function() {
  190. var query = new InfluxQuery({
  191. measurement: 'cpu',
  192. select: [[{type: 'field', params: ['value']}, {type: 'mean'}, {type: 'math'}]]
  193. }, templateSrv, {});
  194. query.addSelectPart(query.selectModels[0], 'math');
  195. expect(query.target.select[0].length).to.be(3);
  196. expect(query.target.select[0][2].type).to.be('math');
  197. });
  198. describe('when render adhoc filters', function() {
  199. it('should generate correct query segment', function() {
  200. var query = new InfluxQuery({measurement: 'cpu', }, templateSrv, {});
  201. var queryText = query.renderAdhocFilters([
  202. {key: 'key1', operator: '=', value: 'value1'},
  203. {key: 'key2', operator: '!=', value: 'value2'},
  204. ]);
  205. expect(queryText).to.be('"key1" = \'value1\' AND "key2" != \'value2\'');
  206. });
  207. });
  208. });
  209. });