influx_query.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. import InfluxQuery from '../influx_query';
  2. describe('InfluxQuery', () => {
  3. const templateSrv = { replace: val => val };
  4. describe('render series with mesurement only', () => {
  5. it('should generate correct query', () => {
  6. const query = new InfluxQuery(
  7. {
  8. measurement: 'cpu',
  9. },
  10. templateSrv,
  11. {}
  12. );
  13. const queryText = query.render();
  14. expect(queryText).toBe('SELECT mean("value") FROM "cpu" WHERE $timeFilter GROUP BY time($__interval) fill(null)');
  15. });
  16. });
  17. describe('render series with policy only', () => {
  18. it('should generate correct query', () => {
  19. const query = new InfluxQuery(
  20. {
  21. measurement: 'cpu',
  22. policy: '5m_avg',
  23. },
  24. templateSrv,
  25. {}
  26. );
  27. const queryText = query.render();
  28. expect(queryText).toBe(
  29. 'SELECT mean("value") FROM "5m_avg"."cpu" WHERE $timeFilter GROUP BY time($__interval) fill(null)'
  30. );
  31. });
  32. });
  33. describe('render series with math and alias', () => {
  34. it('should generate correct query', () => {
  35. const query = new InfluxQuery(
  36. {
  37. measurement: 'cpu',
  38. select: [
  39. [
  40. { type: 'field', params: ['value'] },
  41. { type: 'mean', params: [] },
  42. { type: 'math', params: ['/100'] },
  43. { type: 'alias', params: ['text'] },
  44. ],
  45. ],
  46. },
  47. templateSrv,
  48. {}
  49. );
  50. const queryText = query.render();
  51. expect(queryText).toBe(
  52. 'SELECT mean("value") /100 AS "text" FROM "cpu" WHERE $timeFilter GROUP BY time($__interval) fill(null)'
  53. );
  54. });
  55. });
  56. describe('series with single tag only', () => {
  57. it('should generate correct query', () => {
  58. const query = new InfluxQuery(
  59. {
  60. measurement: 'cpu',
  61. groupBy: [{ type: 'time', params: ['auto'] }],
  62. tags: [{ key: 'hostname', value: 'server\\1' }],
  63. },
  64. templateSrv,
  65. {}
  66. );
  67. const queryText = query.render();
  68. expect(queryText).toBe(
  69. 'SELECT mean("value") FROM "cpu" WHERE ("hostname" = \'server\\\\1\') AND $timeFilter' +
  70. ' GROUP BY time($__interval)'
  71. );
  72. });
  73. it('should switch regex operator with tag value is regex', () => {
  74. const query = new InfluxQuery(
  75. {
  76. measurement: 'cpu',
  77. groupBy: [{ type: 'time', params: ['auto'] }],
  78. tags: [{ key: 'app', value: '/e.*/' }],
  79. },
  80. templateSrv,
  81. {}
  82. );
  83. const queryText = query.render();
  84. expect(queryText).toBe(
  85. 'SELECT mean("value") FROM "cpu" WHERE ("app" =~ /e.*/) AND $timeFilter GROUP BY time($__interval)'
  86. );
  87. });
  88. });
  89. describe('series with multiple tags only', () => {
  90. it('should generate correct query', () => {
  91. const query = new InfluxQuery(
  92. {
  93. measurement: 'cpu',
  94. groupBy: [{ type: 'time', params: ['auto'] }],
  95. tags: [{ key: 'hostname', value: 'server1' }, { key: 'app', value: 'email', condition: 'AND' }],
  96. },
  97. templateSrv,
  98. {}
  99. );
  100. const queryText = query.render();
  101. expect(queryText).toBe(
  102. 'SELECT mean("value") FROM "cpu" WHERE ("hostname" = \'server1\' AND "app" = \'email\') AND ' +
  103. '$timeFilter GROUP BY time($__interval)'
  104. );
  105. });
  106. });
  107. describe('series with tags OR condition', () => {
  108. it('should generate correct query', () => {
  109. const query = new InfluxQuery(
  110. {
  111. measurement: 'cpu',
  112. groupBy: [{ type: 'time', params: ['auto'] }],
  113. tags: [{ key: 'hostname', value: 'server1' }, { key: 'hostname', value: 'server2', condition: 'OR' }],
  114. },
  115. templateSrv,
  116. {}
  117. );
  118. const queryText = query.render();
  119. expect(queryText).toBe(
  120. 'SELECT mean("value") FROM "cpu" WHERE ("hostname" = \'server1\' OR "hostname" = \'server2\') AND ' +
  121. '$timeFilter GROUP BY time($__interval)'
  122. );
  123. });
  124. });
  125. describe('field name with single quote should be escaped and', () => {
  126. it('should generate correct query', () => {
  127. const query = new InfluxQuery(
  128. {
  129. measurement: 'cpu',
  130. groupBy: [{ type: 'time', params: ['auto'] }],
  131. tags: [{ key: 'name', value: "Let's encrypt." }, { key: 'hostname', value: 'server2', condition: 'OR' }],
  132. },
  133. templateSrv,
  134. {}
  135. );
  136. const queryText = query.render();
  137. expect(queryText).toBe(
  138. 'SELECT mean("value") FROM "cpu" WHERE ("name" = \'Let\\\'s encrypt.\' OR "hostname" = \'server2\') AND ' +
  139. '$timeFilter GROUP BY time($__interval)'
  140. );
  141. });
  142. });
  143. describe('query with value condition', () => {
  144. it('should not quote value', () => {
  145. const query = new InfluxQuery(
  146. {
  147. measurement: 'cpu',
  148. groupBy: [],
  149. tags: [{ key: 'value', value: '5', operator: '>' }],
  150. },
  151. templateSrv,
  152. {}
  153. );
  154. const queryText = query.render();
  155. expect(queryText).toBe('SELECT mean("value") FROM "cpu" WHERE ("value" > 5) AND $timeFilter');
  156. });
  157. });
  158. describe('series with groupByTag', () => {
  159. it('should generate correct query', () => {
  160. const query = new InfluxQuery(
  161. {
  162. measurement: 'cpu',
  163. tags: [],
  164. groupBy: [{ type: 'time', interval: 'auto' }, { type: 'tag', params: ['host'] }],
  165. },
  166. templateSrv,
  167. {}
  168. );
  169. const queryText = query.render();
  170. expect(queryText).toBe('SELECT mean("value") FROM "cpu" WHERE $timeFilter GROUP BY time($__interval), "host"');
  171. });
  172. });
  173. describe('render series without group by', () => {
  174. it('should generate correct query', () => {
  175. const query = new InfluxQuery(
  176. {
  177. measurement: 'cpu',
  178. select: [[{ type: 'field', params: ['value'] }]],
  179. groupBy: [],
  180. },
  181. templateSrv,
  182. {}
  183. );
  184. const queryText = query.render();
  185. expect(queryText).toBe('SELECT "value" FROM "cpu" WHERE $timeFilter');
  186. });
  187. });
  188. describe('render series without group by and fill', () => {
  189. it('should generate correct query', () => {
  190. const query = new InfluxQuery(
  191. {
  192. measurement: 'cpu',
  193. select: [[{ type: 'field', params: ['value'] }]],
  194. groupBy: [{ type: 'time' }, { type: 'fill', params: ['0'] }],
  195. },
  196. templateSrv,
  197. {}
  198. );
  199. const queryText = query.render();
  200. expect(queryText).toBe('SELECT "value" FROM "cpu" WHERE $timeFilter GROUP BY time($__interval) fill(0)');
  201. });
  202. });
  203. describe('when adding group by part', () => {
  204. it('should add tag before fill', () => {
  205. const query = new InfluxQuery(
  206. {
  207. measurement: 'cpu',
  208. groupBy: [{ type: 'time' }, { type: 'fill' }],
  209. },
  210. templateSrv,
  211. {}
  212. );
  213. query.addGroupBy('tag(host)');
  214. expect(query.target.groupBy.length).toBe(3);
  215. expect(query.target.groupBy[1].type).toBe('tag');
  216. expect(query.target.groupBy[1].params[0]).toBe('host');
  217. expect(query.target.groupBy[2].type).toBe('fill');
  218. });
  219. it('should add tag last if no fill', () => {
  220. const query = new InfluxQuery(
  221. {
  222. measurement: 'cpu',
  223. groupBy: [],
  224. },
  225. templateSrv,
  226. {}
  227. );
  228. query.addGroupBy('tag(host)');
  229. expect(query.target.groupBy.length).toBe(1);
  230. expect(query.target.groupBy[0].type).toBe('tag');
  231. });
  232. });
  233. describe('when adding select part', () => {
  234. it('should add mean after after field', () => {
  235. const query = new InfluxQuery(
  236. {
  237. measurement: 'cpu',
  238. select: [[{ type: 'field', params: ['value'] }]],
  239. },
  240. templateSrv,
  241. {}
  242. );
  243. query.addSelectPart(query.selectModels[0], 'mean');
  244. expect(query.target.select[0].length).toBe(2);
  245. expect(query.target.select[0][1].type).toBe('mean');
  246. });
  247. it('should replace sum by mean', () => {
  248. const query = new InfluxQuery(
  249. {
  250. measurement: 'cpu',
  251. select: [[{ type: 'field', params: ['value'] }, { type: 'mean' }]],
  252. },
  253. templateSrv,
  254. {}
  255. );
  256. query.addSelectPart(query.selectModels[0], 'sum');
  257. expect(query.target.select[0].length).toBe(2);
  258. expect(query.target.select[0][1].type).toBe('sum');
  259. });
  260. it('should add math before alias', () => {
  261. const query = new InfluxQuery(
  262. {
  263. measurement: 'cpu',
  264. select: [[{ type: 'field', params: ['value'] }, { type: 'mean' }, { type: 'alias' }]],
  265. },
  266. templateSrv,
  267. {}
  268. );
  269. query.addSelectPart(query.selectModels[0], 'math');
  270. expect(query.target.select[0].length).toBe(4);
  271. expect(query.target.select[0][2].type).toBe('math');
  272. });
  273. it('should add math last', () => {
  274. const query = new InfluxQuery(
  275. {
  276. measurement: 'cpu',
  277. select: [[{ type: 'field', params: ['value'] }, { type: 'mean' }]],
  278. },
  279. templateSrv,
  280. {}
  281. );
  282. query.addSelectPart(query.selectModels[0], 'math');
  283. expect(query.target.select[0].length).toBe(3);
  284. expect(query.target.select[0][2].type).toBe('math');
  285. });
  286. it('should replace math', () => {
  287. const query = new InfluxQuery(
  288. {
  289. measurement: 'cpu',
  290. select: [[{ type: 'field', params: ['value'] }, { type: 'mean' }, { type: 'math' }]],
  291. },
  292. templateSrv,
  293. {}
  294. );
  295. query.addSelectPart(query.selectModels[0], 'math');
  296. expect(query.target.select[0].length).toBe(3);
  297. expect(query.target.select[0][2].type).toBe('math');
  298. });
  299. it('should add math when one only query part', () => {
  300. const query = new InfluxQuery(
  301. {
  302. measurement: 'cpu',
  303. select: [[{ type: 'field', params: ['value'] }]],
  304. },
  305. templateSrv,
  306. {}
  307. );
  308. query.addSelectPart(query.selectModels[0], 'math');
  309. expect(query.target.select[0].length).toBe(2);
  310. expect(query.target.select[0][1].type).toBe('math');
  311. });
  312. describe('when render adhoc filters', () => {
  313. it('should generate correct query segment', () => {
  314. const query = new InfluxQuery({ measurement: 'cpu' }, templateSrv, {});
  315. const queryText = query.renderAdhocFilters([
  316. { key: 'key1', operator: '=', value: 'value1' },
  317. { key: 'key2', operator: '!=', value: 'value2' },
  318. ]);
  319. expect(queryText).toBe('"key1" = \'value1\' AND "key2" != \'value2\'');
  320. });
  321. });
  322. });
  323. });