influx_series.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. import produce from 'immer';
  2. import InfluxSeries from '../influx_series';
  3. describe('when generating timeseries from influxdb response', () => {
  4. describe('given multiple fields for series', () => {
  5. const options = {
  6. alias: '',
  7. series: [
  8. {
  9. name: 'cpu',
  10. tags: { app: 'test', server: 'server1' },
  11. columns: ['time', 'mean', 'max', 'min'],
  12. values: [
  13. [1431946625000, 10, 11, 9],
  14. [1431946626000, 20, 21, 19],
  15. ],
  16. },
  17. ],
  18. };
  19. describe('and no alias', () => {
  20. it('should generate multiple datapoints for each column', () => {
  21. const series = new InfluxSeries(options);
  22. const result = series.getTimeSeries();
  23. expect(result.length).toBe(3);
  24. expect(result[0].target).toBe('cpu.mean {app: test, server: server1}');
  25. expect(result[0].datapoints[0][0]).toBe(10);
  26. expect(result[0].datapoints[0][1]).toBe(1431946625000);
  27. expect(result[0].datapoints[1][0]).toBe(20);
  28. expect(result[0].datapoints[1][1]).toBe(1431946626000);
  29. expect(result[1].target).toBe('cpu.max {app: test, server: server1}');
  30. expect(result[1].datapoints[0][0]).toBe(11);
  31. expect(result[1].datapoints[0][1]).toBe(1431946625000);
  32. expect(result[1].datapoints[1][0]).toBe(21);
  33. expect(result[1].datapoints[1][1]).toBe(1431946626000);
  34. expect(result[2].target).toBe('cpu.min {app: test, server: server1}');
  35. expect(result[2].datapoints[0][0]).toBe(9);
  36. expect(result[2].datapoints[0][1]).toBe(1431946625000);
  37. expect(result[2].datapoints[1][0]).toBe(19);
  38. expect(result[2].datapoints[1][1]).toBe(1431946626000);
  39. });
  40. });
  41. describe('and simple alias', () => {
  42. it('should use alias', () => {
  43. options.alias = 'new series';
  44. const series = new InfluxSeries(options);
  45. const result = series.getTimeSeries();
  46. expect(result[0].target).toBe('new series');
  47. expect(result[1].target).toBe('new series');
  48. expect(result[2].target).toBe('new series');
  49. });
  50. });
  51. describe('and alias patterns', () => {
  52. it('should replace patterns', () => {
  53. options.alias = 'alias: $m -> $tag_server ([[measurement]])';
  54. const series = new InfluxSeries(options);
  55. const result = series.getTimeSeries();
  56. expect(result[0].target).toBe('alias: cpu -> server1 (cpu)');
  57. expect(result[1].target).toBe('alias: cpu -> server1 (cpu)');
  58. expect(result[2].target).toBe('alias: cpu -> server1 (cpu)');
  59. });
  60. });
  61. });
  62. describe('given measurement with default fieldname', () => {
  63. const options = {
  64. series: [
  65. {
  66. name: 'cpu',
  67. tags: { app: 'test', server: 'server1' },
  68. columns: ['time', 'value'],
  69. values: [
  70. ['2015-05-18T10:57:05Z', 10],
  71. ['2015-05-18T10:57:06Z', 12],
  72. ],
  73. },
  74. {
  75. name: 'cpu',
  76. tags: { app: 'test2', server: 'server2' },
  77. columns: ['time', 'value'],
  78. values: [
  79. ['2015-05-18T10:57:05Z', 15],
  80. ['2015-05-18T10:57:06Z', 16],
  81. ],
  82. },
  83. ],
  84. };
  85. describe('and no alias', () => {
  86. it('should generate label with no field', () => {
  87. const series = new InfluxSeries(options);
  88. const result = series.getTimeSeries();
  89. expect(result[0].target).toBe('cpu {app: test, server: server1}');
  90. expect(result[1].target).toBe('cpu {app: test2, server: server2}');
  91. });
  92. });
  93. });
  94. describe('given two series', () => {
  95. const options = {
  96. alias: '',
  97. series: [
  98. {
  99. name: 'cpu',
  100. tags: { app: 'test', server: 'server1' },
  101. columns: ['time', 'mean'],
  102. values: [
  103. [1431946625000, 10],
  104. [1431946626000, 12],
  105. ],
  106. },
  107. {
  108. name: 'cpu',
  109. tags: { app: 'test2', server: 'server2' },
  110. columns: ['time', 'mean'],
  111. values: [
  112. [1431946625000, 15],
  113. [1431946626000, 16],
  114. ],
  115. },
  116. ],
  117. };
  118. describe('and no alias', () => {
  119. it('should generate two time series', () => {
  120. const series = new InfluxSeries(options);
  121. const result = series.getTimeSeries();
  122. expect(result.length).toBe(2);
  123. expect(result[0].target).toBe('cpu.mean {app: test, server: server1}');
  124. expect(result[0].datapoints[0][0]).toBe(10);
  125. expect(result[0].datapoints[0][1]).toBe(1431946625000);
  126. expect(result[0].datapoints[1][0]).toBe(12);
  127. expect(result[0].datapoints[1][1]).toBe(1431946626000);
  128. expect(result[0].tags).toMatchObject({
  129. app: 'test',
  130. server: 'server1',
  131. });
  132. expect(result[1].target).toBe('cpu.mean {app: test2, server: server2}');
  133. expect(result[1].datapoints[0][0]).toBe(15);
  134. expect(result[1].datapoints[0][1]).toBe(1431946625000);
  135. expect(result[1].datapoints[1][0]).toBe(16);
  136. expect(result[1].datapoints[1][1]).toBe(1431946626000);
  137. expect(result[1].tags).toMatchObject({
  138. app: 'test2',
  139. server: 'server2',
  140. });
  141. });
  142. });
  143. describe('and simple alias', () => {
  144. it('should use alias', () => {
  145. options.alias = 'new series';
  146. const series = new InfluxSeries(options);
  147. const result = series.getTimeSeries();
  148. expect(result[0].target).toBe('new series');
  149. });
  150. });
  151. describe('and alias patterns', () => {
  152. it('should replace patterns', () => {
  153. options.alias = 'alias: $m -> $tag_server ([[measurement]])';
  154. const series = new InfluxSeries(options);
  155. const result = series.getTimeSeries();
  156. expect(result[0].target).toBe('alias: cpu -> server1 (cpu)');
  157. expect(result[1].target).toBe('alias: cpu -> server2 (cpu)');
  158. });
  159. });
  160. });
  161. describe('given measurement with dots', () => {
  162. const options = {
  163. alias: '',
  164. series: [
  165. {
  166. name: 'app.prod.server1.count',
  167. tags: {},
  168. columns: ['time', 'mean'],
  169. values: [
  170. [1431946625000, 10],
  171. [1431946626000, 12],
  172. ],
  173. },
  174. ],
  175. };
  176. it('should replace patterns', () => {
  177. options.alias = 'alias: $1 -> [[3]]';
  178. const series = new InfluxSeries(options);
  179. const result = series.getTimeSeries();
  180. expect(result[0].target).toBe('alias: prod -> count');
  181. });
  182. it('should handle too large indexes', () => {
  183. options.alias = 'alias: $0 $1 $2 $3 $4 $5';
  184. const series = new InfluxSeries(options);
  185. const result = series.getTimeSeries();
  186. expect(result[0].target).toBe('alias: app prod server1 count $4 $5');
  187. });
  188. });
  189. describe('given table response', () => {
  190. const options = {
  191. alias: '',
  192. series: [
  193. {
  194. name: 'app.prod.server1.count',
  195. tags: { datacenter: 'Africa', server: 'server2' },
  196. columns: ['time', 'value2', 'value'],
  197. values: [
  198. [1431946625000, 23, 10],
  199. [1431946626000, 25, 12],
  200. ],
  201. },
  202. ],
  203. };
  204. it('should return table', () => {
  205. const series = new InfluxSeries(options);
  206. const table = series.getTable();
  207. expect(table.type).toBe('table');
  208. expect(table.columns.length).toBe(5);
  209. expect(table.columns[0].text).toEqual('Time');
  210. expect(table.rows[0]).toEqual([1431946625000, 'Africa', 'server2', 23, 10]);
  211. });
  212. });
  213. describe('given table response from SHOW CARDINALITY', () => {
  214. const options = {
  215. alias: '',
  216. series: [
  217. {
  218. name: 'cpu',
  219. columns: ['count'],
  220. values: [[37]],
  221. },
  222. ],
  223. };
  224. it('should return table', () => {
  225. const series = new InfluxSeries(options);
  226. const table = series.getTable();
  227. expect(table.type).toBe('table');
  228. expect(table.columns.length).toBe(1);
  229. expect(table.columns[0].text).toEqual('count');
  230. expect(table.rows[0]).toEqual([37]);
  231. });
  232. });
  233. describe('given annotation response', () => {
  234. describe('with empty tagsColumn', () => {
  235. const options = {
  236. alias: '',
  237. annotation: {},
  238. series: [
  239. {
  240. name: 'logins.count',
  241. tags: { datacenter: 'Africa', server: 'server2' },
  242. columns: ['time', 'datacenter', 'hostname', 'source', 'value'],
  243. values: [[1481549440372, 'America', '10.1.100.10', 'backend', 215.7432653659507]],
  244. },
  245. ],
  246. };
  247. it('should multiple tags', () => {
  248. const series = new InfluxSeries(options);
  249. const annotations = series.getAnnotations();
  250. expect(annotations[0].tags.length).toBe(0);
  251. });
  252. });
  253. describe('given annotation response', () => {
  254. const options = {
  255. alias: '',
  256. annotation: {
  257. tagsColumn: 'datacenter, source',
  258. },
  259. series: [
  260. {
  261. name: 'logins.count',
  262. tags: { datacenter: 'Africa', server: 'server2' },
  263. columns: ['time', 'datacenter', 'hostname', 'source', 'value'],
  264. values: [[1481549440372, 'America', '10.1.100.10', 'backend', 215.7432653659507]],
  265. },
  266. ],
  267. };
  268. it('should multiple tags', () => {
  269. const series = new InfluxSeries(options);
  270. const annotations = series.getAnnotations();
  271. expect(annotations[0].tags.length).toBe(2);
  272. expect(annotations[0].tags[0]).toBe('America');
  273. expect(annotations[0].tags[1]).toBe('backend');
  274. });
  275. });
  276. describe('given a time-column in the json-response', () => {
  277. const options = {
  278. alias: '',
  279. series: [
  280. {
  281. name: 'cpu',
  282. tags: { cpu: 'cpu1' },
  283. columns: ['time', 'usage_idle'],
  284. values: [[1481549440372, 42]],
  285. },
  286. ],
  287. };
  288. it('the column-names should be correct if the time-column is not renamed', () => {
  289. const series = new InfluxSeries(options);
  290. const table = series.getTable();
  291. expect(table.columns).toStrictEqual([
  292. {
  293. text: 'Time',
  294. type: 'time',
  295. },
  296. {
  297. text: 'cpu',
  298. },
  299. {
  300. text: 'usage_idle',
  301. },
  302. ]);
  303. expect(table.rows).toStrictEqual([[1481549440372, 'cpu1', 42]]);
  304. });
  305. it('the column-names should be correct if the time-column is renamed', () => {
  306. const renamedOptions = produce(options, (draft) => {
  307. // we rename the time-column to `zeit`
  308. draft.series[0].columns[0] = 'zeit';
  309. });
  310. const series = new InfluxSeries(renamedOptions);
  311. const table = series.getTable();
  312. expect(table.columns).toStrictEqual([
  313. {
  314. text: 'zeit',
  315. },
  316. {
  317. text: 'cpu',
  318. },
  319. {
  320. text: 'usage_idle',
  321. },
  322. ]);
  323. expect(table.rows).toStrictEqual([[1481549440372, 'cpu1', 42]]);
  324. });
  325. });
  326. });
  327. });