influx_query_model.test.ts 11 KB

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