transformers.test.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import { transformers, transformDataToTable } from '../transformers';
  2. describe('when transforming time series table', () => {
  3. let table: any;
  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. },
  95. ];
  96. const singleQueryData = [
  97. {
  98. type: 'table',
  99. columns: [{ text: 'Time' }, { text: 'Label Key 1' }, { text: 'Value' }],
  100. rows: [[time, 'Label Value 1', 42]],
  101. },
  102. ];
  103. const multipleQueriesDataSameLabels = [
  104. {
  105. type: 'table',
  106. columns: [{ text: 'Time' }, { text: 'Label Key 1' }, { text: 'Label Key 2' }, { text: 'Value #A' }],
  107. rows: [[time, 'Label Value 1', 'Label Value 2', 42]],
  108. },
  109. {
  110. type: 'table',
  111. columns: [{ text: 'Time' }, { text: 'Label Key 1' }, { text: 'Label Key 2' }, { text: 'Value #B' }],
  112. rows: [[time, 'Label Value 1', 'Label Value 2', 13]],
  113. },
  114. {
  115. type: 'table',
  116. columns: [{ text: 'Time' }, { text: 'Label Key 1' }, { text: 'Label Key 2' }, { text: 'Value #C' }],
  117. rows: [[time, 'Label Value 1', 'Label Value 2', 4]],
  118. },
  119. {
  120. type: 'table',
  121. columns: [{ text: 'Time' }, { text: 'Label Key 1' }, { text: 'Label Key 2' }, { text: 'Value #C' }],
  122. rows: [[time, 'Label Value 1', 'Label Value 2', 7]],
  123. },
  124. ];
  125. describe('getColumns', () => {
  126. it('should return data columns given a single query', () => {
  127. const columns = transformers[transform].getColumns(singleQueryData);
  128. expect(columns[0].text).toBe('Time');
  129. expect(columns[1].text).toBe('Label Key 1');
  130. expect(columns[2].text).toBe('Value');
  131. });
  132. it('should return the union of data columns given a multiple queries', () => {
  133. const columns = transformers[transform].getColumns(multipleQueriesDataSameLabels);
  134. expect(columns[0].text).toBe('Time');
  135. expect(columns[1].text).toBe('Label Key 1');
  136. expect(columns[2].text).toBe('Label Key 2');
  137. expect(columns[3].text).toBe('Value #A');
  138. expect(columns[4].text).toBe('Value #B');
  139. });
  140. });
  141. describe('transform', () => {
  142. it('should throw an error with non-table data', () => {
  143. expect(() => transformDataToTable(nonTableData, panel)).toThrow();
  144. });
  145. it('should return 3 columns for single queries', () => {
  146. table = transformDataToTable(singleQueryData, panel);
  147. expect(table.columns.length).toBe(3);
  148. expect(table.columns[0].text).toBe('Time');
  149. expect(table.columns[1].text).toBe('Label Key 1');
  150. expect(table.columns[2].text).toBe('Value');
  151. });
  152. it('should return the union of columns for multiple queries', () => {
  153. table = transformDataToTable(multipleQueriesDataSameLabels, panel);
  154. expect(table.columns.length).toBe(6);
  155. expect(table.columns[0].text).toBe('Time');
  156. expect(table.columns[1].text).toBe('Label Key 1');
  157. expect(table.columns[2].text).toBe('Label Key 2');
  158. expect(table.columns[3].text).toBe('Value #A');
  159. expect(table.columns[4].text).toBe('Value #B');
  160. expect(table.columns[5].text).toBe('Value #C');
  161. });
  162. it('should return 1 row for a single query', () => {
  163. table = transformDataToTable(singleQueryData, panel);
  164. expect(table.rows.length).toBe(1);
  165. expect(table.rows[0][0]).toBe(time);
  166. expect(table.rows[0][1]).toBe('Label Value 1');
  167. expect(table.rows[0][2]).toBe(42);
  168. });
  169. it('should return 2 rows for a multiple queries with same label values plus one extra row', () => {
  170. table = transformDataToTable(multipleQueriesDataSameLabels, panel);
  171. expect(table.rows.length).toBe(2);
  172. expect(table.rows[0][0]).toBe(time);
  173. expect(table.rows[0][1]).toBe('Label Value 1');
  174. expect(table.rows[0][2]).toBe('Label Value 2');
  175. expect(table.rows[0][3]).toBe(42);
  176. expect(table.rows[0][4]).toBe(13);
  177. expect(table.rows[0][5]).toBe(4);
  178. expect(table.rows[1][0]).toBe(time);
  179. expect(table.rows[1][1]).toBe('Label Value 1');
  180. expect(table.rows[1][2]).toBe('Label Value 2');
  181. expect(table.rows[1][3]).toBeUndefined();
  182. expect(table.rows[1][4]).toBeUndefined();
  183. expect(table.rows[1][5]).toBe(7);
  184. });
  185. });
  186. });
  187. });
  188. describe('doc data sets', () => {
  189. describe('JSON Data', () => {
  190. const panel = {
  191. transform: 'json',
  192. columns: [
  193. { text: 'Timestamp', value: 'timestamp' },
  194. { text: 'Message', value: 'message' },
  195. { text: 'nested.level2', value: 'nested.level2' },
  196. ],
  197. };
  198. const rawData = [
  199. {
  200. type: 'docs',
  201. datapoints: [
  202. {
  203. timestamp: 'time',
  204. message: 'message',
  205. nested: {
  206. level2: 'level2-value',
  207. },
  208. },
  209. ],
  210. },
  211. ];
  212. describe('getColumns', () => {
  213. it('should return nested properties', () => {
  214. const columns = transformers['json'].getColumns(rawData);
  215. expect(columns[0].text).toBe('timestamp');
  216. expect(columns[1].text).toBe('message');
  217. expect(columns[2].text).toBe('nested.level2');
  218. });
  219. });
  220. describe('transform', () => {
  221. beforeEach(() => {
  222. table = transformDataToTable(rawData, panel);
  223. });
  224. it('should return 2 columns', () => {
  225. expect(table.columns.length).toBe(3);
  226. expect(table.columns[0].text).toBe('Timestamp');
  227. expect(table.columns[1].text).toBe('Message');
  228. expect(table.columns[2].text).toBe('nested.level2');
  229. });
  230. it('should return 2 rows', () => {
  231. expect(table.rows.length).toBe(1);
  232. expect(table.rows[0][0]).toBe('time');
  233. expect(table.rows[0][1]).toBe('message');
  234. expect(table.rows[0][2]).toBe('level2-value');
  235. });
  236. });
  237. });
  238. });
  239. describe('annotation data', () => {
  240. describe('Annnotations', () => {
  241. const panel = { transform: 'annotations' };
  242. const rawData = {
  243. annotations: [
  244. {
  245. time: 1000,
  246. text: 'hej',
  247. tags: ['tags', 'asd'],
  248. title: 'title',
  249. },
  250. ],
  251. };
  252. beforeEach(() => {
  253. table = transformDataToTable(rawData, panel);
  254. });
  255. it('should return 4 columns', () => {
  256. expect(table.columns.length).toBe(4);
  257. expect(table.columns[0].text).toBe('Time');
  258. expect(table.columns[1].text).toBe('Title');
  259. expect(table.columns[2].text).toBe('Text');
  260. expect(table.columns[3].text).toBe('Tags');
  261. });
  262. it('should return 1 rows', () => {
  263. expect(table.rows.length).toBe(1);
  264. expect(table.rows[0][0]).toBe(1000);
  265. });
  266. });
  267. });
  268. });