transformers.test.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. import { transformers, transformDataToTable } from '../transformers';
  2. describe('when transforming time series table', () => {
  3. let table;
  4. describe('given 2 time series', () => {
  5. const time = new Date().getTime();
  6. const timeSeries = [
  7. {
  8. target: 'series1',
  9. datapoints: [[12.12, time], [14.44, time + 1]],
  10. },
  11. {
  12. target: 'series2',
  13. datapoints: [[16.12, time]],
  14. },
  15. ];
  16. describe('timeseries_to_rows', () => {
  17. const panel = {
  18. transform: 'timeseries_to_rows',
  19. sort: { col: 0, desc: true },
  20. };
  21. beforeEach(() => {
  22. table = transformDataToTable(timeSeries, panel);
  23. });
  24. it('should return 3 rows', () => {
  25. expect(table.rows.length).toBe(3);
  26. expect(table.rows[0][1]).toBe('series1');
  27. expect(table.rows[1][1]).toBe('series1');
  28. expect(table.rows[2][1]).toBe('series2');
  29. expect(table.rows[0][2]).toBe(12.12);
  30. });
  31. it('should return 3 rows', () => {
  32. expect(table.columns.length).toBe(3);
  33. expect(table.columns[0].text).toBe('Time');
  34. expect(table.columns[1].text).toBe('Metric');
  35. expect(table.columns[2].text).toBe('Value');
  36. });
  37. });
  38. describe('timeseries_to_columns', () => {
  39. const panel = {
  40. transform: 'timeseries_to_columns',
  41. };
  42. beforeEach(() => {
  43. table = transformDataToTable(timeSeries, panel);
  44. });
  45. it('should return 3 columns', () => {
  46. expect(table.columns.length).toBe(3);
  47. expect(table.columns[0].text).toBe('Time');
  48. expect(table.columns[1].text).toBe('series1');
  49. expect(table.columns[2].text).toBe('series2');
  50. });
  51. it('should return 2 rows', () => {
  52. expect(table.rows.length).toBe(2);
  53. expect(table.rows[0][1]).toBe(12.12);
  54. expect(table.rows[0][2]).toBe(16.12);
  55. });
  56. it('should be undefined when no value for timestamp', () => {
  57. expect(table.rows[1][2]).toBe(undefined);
  58. });
  59. });
  60. describe('timeseries_aggregations', () => {
  61. const panel = {
  62. transform: 'timeseries_aggregations',
  63. sort: { col: 0, desc: true },
  64. columns: [{ text: 'Max', value: 'max' }, { text: 'Min', value: 'min' }],
  65. };
  66. beforeEach(() => {
  67. table = transformDataToTable(timeSeries, panel);
  68. });
  69. it('should return 2 rows', () => {
  70. expect(table.rows.length).toBe(2);
  71. expect(table.rows[0][0]).toBe('series1');
  72. expect(table.rows[0][1]).toBe(14.44);
  73. expect(table.rows[0][2]).toBe(12.12);
  74. });
  75. it('should return 2 columns', () => {
  76. expect(table.columns.length).toBe(3);
  77. expect(table.columns[0].text).toBe('Metric');
  78. expect(table.columns[1].text).toBe('Max');
  79. expect(table.columns[2].text).toBe('Min');
  80. });
  81. });
  82. });
  83. describe('table data sets', () => {
  84. describe('Table', () => {
  85. const transform = 'table';
  86. const panel = {
  87. transform,
  88. };
  89. const time = new Date().getTime();
  90. const nonTableData = [
  91. {
  92. type: 'foo',
  93. columns: [{ text: 'Time' }, { text: 'Label Key 1' }, { text: 'Value' }],
  94. rows: [[time, 'Label Value 1', 42]],
  95. },
  96. ];
  97. const singleQueryData = [
  98. {
  99. type: 'table',
  100. columns: [{ text: 'Time' }, { text: 'Label Key 1' }, { text: 'Value' }],
  101. rows: [[time, 'Label Value 1', 42]],
  102. },
  103. ];
  104. const multipleQueriesDataSameLabels = [
  105. {
  106. type: 'table',
  107. columns: [{ text: 'Time' }, { text: 'Label Key 1' }, { text: 'Label Key 2' }, { text: 'Value #A' }],
  108. rows: [[time, 'Label Value 1', 'Label Value 2', 42]],
  109. },
  110. {
  111. type: 'table',
  112. columns: [{ text: 'Time' }, { text: 'Label Key 1' }, { text: 'Label Key 2' }, { text: 'Value #B' }],
  113. rows: [[time, 'Label Value 1', 'Label Value 2', 13]],
  114. },
  115. {
  116. type: 'table',
  117. columns: [{ text: 'Time' }, { text: 'Label Key 1' }, { text: 'Label Key 2' }, { text: 'Value #C' }],
  118. rows: [[time, 'Label Value 1', 'Label Value 2', 4]],
  119. },
  120. {
  121. type: 'table',
  122. columns: [{ text: 'Time' }, { text: 'Label Key 1' }, { text: 'Label Key 2' }, { text: 'Value #C' }],
  123. rows: [[time, 'Label Value 1', 'Label Value 2', 7]],
  124. },
  125. ];
  126. describe('getColumns', () => {
  127. it('should return data columns given a single query', () => {
  128. const columns = transformers[transform].getColumns(singleQueryData);
  129. expect(columns[0].text).toBe('Time');
  130. expect(columns[1].text).toBe('Label Key 1');
  131. expect(columns[2].text).toBe('Value');
  132. });
  133. it('should return the union of data columns given a multiple queries', () => {
  134. const columns = transformers[transform].getColumns(multipleQueriesDataSameLabels);
  135. expect(columns[0].text).toBe('Time');
  136. expect(columns[1].text).toBe('Label Key 1');
  137. expect(columns[2].text).toBe('Label Key 2');
  138. expect(columns[3].text).toBe('Value #A');
  139. expect(columns[4].text).toBe('Value #B');
  140. });
  141. });
  142. describe('transform', () => {
  143. it('should throw an error with non-table data', () => {
  144. expect(() => transformDataToTable(nonTableData, panel)).toThrow();
  145. });
  146. it('should return 3 columns for single queries', () => {
  147. table = transformDataToTable(singleQueryData, panel);
  148. expect(table.columns.length).toBe(3);
  149. expect(table.columns[0].text).toBe('Time');
  150. expect(table.columns[1].text).toBe('Label Key 1');
  151. expect(table.columns[2].text).toBe('Value');
  152. });
  153. it('should return the union of columns for multiple queries', () => {
  154. table = transformDataToTable(multipleQueriesDataSameLabels, panel);
  155. expect(table.columns.length).toBe(6);
  156. expect(table.columns[0].text).toBe('Time');
  157. expect(table.columns[1].text).toBe('Label Key 1');
  158. expect(table.columns[2].text).toBe('Label Key 2');
  159. expect(table.columns[3].text).toBe('Value #A');
  160. expect(table.columns[4].text).toBe('Value #B');
  161. expect(table.columns[5].text).toBe('Value #C');
  162. });
  163. it('should return 1 row for a single query', () => {
  164. table = transformDataToTable(singleQueryData, panel);
  165. expect(table.rows.length).toBe(1);
  166. expect(table.rows[0][0]).toBe(time);
  167. expect(table.rows[0][1]).toBe('Label Value 1');
  168. expect(table.rows[0][2]).toBe(42);
  169. });
  170. it('should return 2 rows for a multiple queries with same label values plus one extra row', () => {
  171. table = transformDataToTable(multipleQueriesDataSameLabels, panel);
  172. expect(table.rows.length).toBe(2);
  173. expect(table.rows[0][0]).toBe(time);
  174. expect(table.rows[0][1]).toBe('Label Value 1');
  175. expect(table.rows[0][2]).toBe('Label Value 2');
  176. expect(table.rows[0][3]).toBe(42);
  177. expect(table.rows[0][4]).toBe(13);
  178. expect(table.rows[0][5]).toBe(4);
  179. expect(table.rows[1][0]).toBe(time);
  180. expect(table.rows[1][1]).toBe('Label Value 1');
  181. expect(table.rows[1][2]).toBe('Label Value 2');
  182. expect(table.rows[1][3]).toBeUndefined();
  183. expect(table.rows[1][4]).toBeUndefined();
  184. expect(table.rows[1][5]).toBe(7);
  185. });
  186. });
  187. });
  188. });
  189. describe('doc data sets', () => {
  190. describe('JSON Data', () => {
  191. const panel = {
  192. transform: 'json',
  193. columns: [
  194. { text: 'Timestamp', value: 'timestamp' },
  195. { text: 'Message', value: 'message' },
  196. { text: 'nested.level2', value: 'nested.level2' },
  197. ],
  198. };
  199. const rawData = [
  200. {
  201. type: 'docs',
  202. datapoints: [
  203. {
  204. timestamp: 'time',
  205. message: 'message',
  206. nested: {
  207. level2: 'level2-value',
  208. },
  209. },
  210. ],
  211. },
  212. ];
  213. describe('getColumns', () => {
  214. it('should return nested properties', () => {
  215. const columns = transformers['json'].getColumns(rawData);
  216. expect(columns[0].text).toBe('timestamp');
  217. expect(columns[1].text).toBe('message');
  218. expect(columns[2].text).toBe('nested.level2');
  219. });
  220. });
  221. describe('transform', () => {
  222. beforeEach(() => {
  223. table = transformDataToTable(rawData, panel);
  224. });
  225. it('should return 2 columns', () => {
  226. expect(table.columns.length).toBe(3);
  227. expect(table.columns[0].text).toBe('Timestamp');
  228. expect(table.columns[1].text).toBe('Message');
  229. expect(table.columns[2].text).toBe('nested.level2');
  230. });
  231. it('should return 2 rows', () => {
  232. expect(table.rows.length).toBe(1);
  233. expect(table.rows[0][0]).toBe('time');
  234. expect(table.rows[0][1]).toBe('message');
  235. expect(table.rows[0][2]).toBe('level2-value');
  236. });
  237. });
  238. });
  239. });
  240. describe('annotation data', () => {
  241. describe('Annnotations', () => {
  242. const panel = { transform: 'annotations' };
  243. const rawData = {
  244. annotations: [
  245. {
  246. time: 1000,
  247. text: 'hej',
  248. tags: ['tags', 'asd'],
  249. title: 'title',
  250. },
  251. ],
  252. };
  253. beforeEach(() => {
  254. table = transformDataToTable(rawData, panel);
  255. });
  256. it('should return 4 columns', () => {
  257. expect(table.columns.length).toBe(4);
  258. expect(table.columns[0].text).toBe('Time');
  259. expect(table.columns[1].text).toBe('Title');
  260. expect(table.columns[2].text).toBe('Text');
  261. expect(table.columns[3].text).toBe('Tags');
  262. });
  263. it('should return 1 rows', () => {
  264. expect(table.rows.length).toBe(1);
  265. expect(table.rows[0][0]).toBe(1000);
  266. });
  267. });
  268. });
  269. });