influx_query.jest.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import InfluxQuery from '../influx_query';
  2. describe('InfluxQuery', function() {
  3. var templateSrv = {replace: val => val};
  4. describe('render series with mesurement only', function() {
  5. it('should generate correct query', function() {
  6. var query = new InfluxQuery({
  7. measurement: 'cpu',
  8. }, templateSrv, {});
  9. var queryText = query.render();
  10. expect(queryText).toBe('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. }, templateSrv, {});
  19. var queryText = query.render();
  20. expect(queryText).toBe('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. }, templateSrv, {});
  36. var queryText = query.render();
  37. expect(queryText).toBe('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. }, templateSrv, {});
  47. var queryText = query.render();
  48. expect(queryText).toBe('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. }, templateSrv, {});
  57. var queryText = query.render();
  58. expect(queryText).toBe('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. }, templateSrv, {});
  68. var queryText = query.render();
  69. expect(queryText).toBe('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. }, templateSrv, {});
  80. var queryText = query.render();
  81. expect(queryText).toBe('SELECT mean("value") FROM "cpu" WHERE ("hostname" = \'server1\' OR "hostname" = \'server2\') AND ' +
  82. '$timeFilter GROUP BY time($__interval)');
  83. });
  84. });
  85. describe('query with value condition', function() {
  86. it('should not quote value', function() {
  87. var query = new InfluxQuery({
  88. measurement: 'cpu',
  89. groupBy: [],
  90. tags: [{key: 'value', value: '5', operator: '>'}]
  91. }, templateSrv, {});
  92. var queryText = query.render();
  93. expect(queryText).toBe('SELECT mean("value") FROM "cpu" WHERE ("value" > 5) AND $timeFilter');
  94. });
  95. });
  96. describe('series with groupByTag', function() {
  97. it('should generate correct query', function() {
  98. var query = new InfluxQuery({
  99. measurement: 'cpu',
  100. tags: [],
  101. groupBy: [{type: 'time', interval: 'auto'}, {type: 'tag', params: ['host']}],
  102. }, templateSrv, {});
  103. var queryText = query.render();
  104. expect(queryText).toBe('SELECT mean("value") FROM "cpu" WHERE $timeFilter GROUP BY time($__interval), "host"');
  105. });
  106. });
  107. describe('render series without group by', function() {
  108. it('should generate correct query', function() {
  109. var query = new InfluxQuery({
  110. measurement: 'cpu',
  111. select: [[{type: 'field', params: ['value']}]],
  112. groupBy: [],
  113. }, templateSrv, {});
  114. var queryText = query.render();
  115. expect(queryText).toBe('SELECT "value" FROM "cpu" WHERE $timeFilter');
  116. });
  117. });
  118. describe('render series without group by and fill', function() {
  119. it('should generate correct query', function() {
  120. var query = new InfluxQuery({
  121. measurement: 'cpu',
  122. select: [[{type: 'field', params: ['value']}]],
  123. groupBy: [{type: 'time'}, {type: 'fill', params: ['0']}],
  124. }, templateSrv, {});
  125. var queryText = query.render();
  126. expect(queryText).toBe('SELECT "value" FROM "cpu" WHERE $timeFilter GROUP BY time($__interval) fill(0)');
  127. });
  128. });
  129. describe('when adding group by part', function() {
  130. it('should add tag before fill', function() {
  131. var query = new InfluxQuery({
  132. measurement: 'cpu',
  133. groupBy: [{type: 'time'}, {type: 'fill'}]
  134. }, templateSrv, {});
  135. query.addGroupBy('tag(host)');
  136. expect(query.target.groupBy.length).toBe(3);
  137. expect(query.target.groupBy[1].type).toBe('tag');
  138. expect(query.target.groupBy[1].params[0]).toBe('host');
  139. expect(query.target.groupBy[2].type).toBe('fill');
  140. });
  141. it('should add tag last if no fill', function() {
  142. var query = new InfluxQuery({
  143. measurement: 'cpu',
  144. groupBy: []
  145. }, templateSrv, {});
  146. query.addGroupBy('tag(host)');
  147. expect(query.target.groupBy.length).toBe(1);
  148. expect(query.target.groupBy[0].type).toBe('tag');
  149. });
  150. });
  151. describe('when adding select part', function() {
  152. it('should add mean after after field', function() {
  153. var query = new InfluxQuery({
  154. measurement: 'cpu',
  155. select: [[{type: 'field', params: ['value']}]]
  156. }, templateSrv, {});
  157. query.addSelectPart(query.selectModels[0], 'mean');
  158. expect(query.target.select[0].length).toBe(2);
  159. expect(query.target.select[0][1].type).toBe('mean');
  160. });
  161. it('should replace sum by mean', function() {
  162. var query = new InfluxQuery({
  163. measurement: 'cpu',
  164. select: [[{type: 'field', params: ['value']}, {type: 'mean'}]]
  165. }, templateSrv, {});
  166. query.addSelectPart(query.selectModels[0], 'sum');
  167. expect(query.target.select[0].length).toBe(2);
  168. expect(query.target.select[0][1].type).toBe('sum');
  169. });
  170. it('should add math before alias', function() {
  171. var query = new InfluxQuery({
  172. measurement: 'cpu',
  173. select: [[{type: 'field', params: ['value']}, {type: 'mean'}, {type: 'alias'}]]
  174. }, templateSrv, {});
  175. query.addSelectPart(query.selectModels[0], 'math');
  176. expect(query.target.select[0].length).toBe(4);
  177. expect(query.target.select[0][2].type).toBe('math');
  178. });
  179. it('should add math last', function() {
  180. var query = new InfluxQuery({
  181. measurement: 'cpu',
  182. select: [[{type: 'field', params: ['value']}, {type: 'mean'}]]
  183. }, templateSrv, {});
  184. query.addSelectPart(query.selectModels[0], 'math');
  185. expect(query.target.select[0].length).toBe(3);
  186. expect(query.target.select[0][2].type).toBe('math');
  187. });
  188. it('should replace math', function() {
  189. var query = new InfluxQuery({
  190. measurement: 'cpu',
  191. select: [[{type: 'field', params: ['value']}, {type: 'mean'}, {type: 'math'}]]
  192. }, templateSrv, {});
  193. query.addSelectPart(query.selectModels[0], 'math');
  194. expect(query.target.select[0].length).toBe(3);
  195. expect(query.target.select[0][2].type).toBe('math');
  196. });
  197. it('should add math when one only query part', function() {
  198. var query = new InfluxQuery({
  199. measurement: 'cpu',
  200. select: [[{type: 'field', params: ['value']}]]
  201. }, templateSrv, {});
  202. query.addSelectPart(query.selectModels[0], 'math');
  203. expect(query.target.select[0].length).toBe(2);
  204. expect(query.target.select[0][1].type).toBe('math');
  205. });
  206. describe('when render adhoc filters', function() {
  207. it('should generate correct query segment', function() {
  208. var query = new InfluxQuery({measurement: 'cpu', }, templateSrv, {});
  209. var queryText = query.renderAdhocFilters([
  210. {key: 'key1', operator: '=', value: 'value1'},
  211. {key: 'key2', operator: '!=', value: 'value2'},
  212. ]);
  213. expect(queryText).toBe('"key1" = \'value1\' AND "key2" != \'value2\'');
  214. });
  215. });
  216. });
  217. });