transformers.jest.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. import { transformers, transformDataToTable } from "../transformers";
  2. describe("when transforming time series table", () => {
  3. var table;
  4. describe("given 2 time series", () => {
  5. var time = new Date().getTime();
  6. var 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. var 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. var 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. var 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. var panel = {
  87. transform
  88. };
  89. var time = new Date().getTime();
  90. var nonTableData = [
  91. {
  92. type: "foo",
  93. columns: [
  94. { text: "Time" },
  95. { text: "Label Key 1" },
  96. { text: "Value" }
  97. ],
  98. rows: [[time, "Label Value 1", 42]]
  99. }
  100. ];
  101. var singleQueryData = [
  102. {
  103. type: "table",
  104. columns: [
  105. { text: "Time" },
  106. { text: "Label Key 1" },
  107. { text: "Value" }
  108. ],
  109. rows: [[time, "Label Value 1", 42]]
  110. }
  111. ];
  112. var multipleQueriesDataSameLabels = [
  113. {
  114. type: "table",
  115. columns: [
  116. { text: "Time" },
  117. { text: "Label Key 1" },
  118. { text: "Label Key 2" },
  119. { text: "Value #A" }
  120. ],
  121. rows: [[time, "Label Value 1", "Label Value 2", 42]]
  122. },
  123. {
  124. type: "table",
  125. columns: [
  126. { text: "Time" },
  127. { text: "Label Key 1" },
  128. { text: "Label Key 2" },
  129. { text: "Value #B" }
  130. ],
  131. rows: [[time, "Label Value 1", "Label Value 2", 13]]
  132. },
  133. {
  134. type: "table",
  135. columns: [
  136. { text: "Time" },
  137. { text: "Label Key 1" },
  138. { text: "Label Key 2" },
  139. { text: "Value #C" }
  140. ],
  141. rows: [[time, "Label Value 1", "Label Value 2", 4]]
  142. },
  143. {
  144. type: "table",
  145. columns: [
  146. { text: "Time" },
  147. { text: "Label Key 1" },
  148. { text: "Label Key 2" },
  149. { text: "Value #C" }
  150. ],
  151. rows: [[time, "Label Value 1", "Label Value 2", 7]]
  152. }
  153. ];
  154. var multipleQueriesDataDifferentLabels = [
  155. {
  156. type: "table",
  157. columns: [
  158. { text: "Time" },
  159. { text: "Label Key 1" },
  160. { text: "Value #A" }
  161. ],
  162. rows: [[time, "Label Value 1", 42]]
  163. },
  164. {
  165. type: "table",
  166. columns: [
  167. { text: "Time" },
  168. { text: "Label Key 2" },
  169. { text: "Value #B" }
  170. ],
  171. rows: [[time, "Label Value 2", 13]]
  172. },
  173. {
  174. type: "table",
  175. columns: [
  176. { text: "Time" },
  177. { text: "Label Key 1" },
  178. { text: "Value #C" }
  179. ],
  180. rows: [[time, "Label Value 3", 7]]
  181. }
  182. ];
  183. describe("getColumns", function() {
  184. it("should return data columns given a single query", function() {
  185. var columns = transformers[transform].getColumns(singleQueryData);
  186. expect(columns[0].text).toBe("Time");
  187. expect(columns[1].text).toBe("Label Key 1");
  188. expect(columns[2].text).toBe("Value");
  189. });
  190. it("should return the union of data columns given a multiple queries", function() {
  191. var columns = transformers[transform].getColumns(
  192. multipleQueriesDataSameLabels
  193. );
  194. expect(columns[0].text).toBe("Time");
  195. expect(columns[1].text).toBe("Label Key 1");
  196. expect(columns[2].text).toBe("Label Key 2");
  197. expect(columns[3].text).toBe("Value #A");
  198. expect(columns[4].text).toBe("Value #B");
  199. });
  200. it("should return the union of data columns given a multiple queries with different labels", function() {
  201. var columns = transformers[transform].getColumns(
  202. multipleQueriesDataDifferentLabels
  203. );
  204. expect(columns[0].text).toBe("Time");
  205. expect(columns[1].text).toBe("Label Key 1");
  206. expect(columns[2].text).toBe("Value #A");
  207. expect(columns[3].text).toBe("Label Key 2");
  208. expect(columns[4].text).toBe("Value #B");
  209. expect(columns[5].text).toBe("Value #C");
  210. });
  211. });
  212. describe("transform", function() {
  213. it("should throw an error with non-table data", () => {
  214. expect(() => transformDataToTable(nonTableData, panel)).toThrow();
  215. });
  216. it("should return 3 columns for single queries", () => {
  217. table = transformDataToTable(singleQueryData, panel);
  218. expect(table.columns.length).toBe(3);
  219. expect(table.columns[0].text).toBe("Time");
  220. expect(table.columns[1].text).toBe("Label Key 1");
  221. expect(table.columns[2].text).toBe("Value");
  222. });
  223. it("should return the union of columns for multiple queries", () => {
  224. table = transformDataToTable(multipleQueriesDataSameLabels, panel);
  225. expect(table.columns.length).toBe(6);
  226. expect(table.columns[0].text).toBe("Time");
  227. expect(table.columns[1].text).toBe("Label Key 1");
  228. expect(table.columns[2].text).toBe("Label Key 2");
  229. expect(table.columns[3].text).toBe("Value #A");
  230. expect(table.columns[4].text).toBe("Value #B");
  231. expect(table.columns[5].text).toBe("Value #C");
  232. });
  233. it("should return 1 row for a single query", () => {
  234. table = transformDataToTable(singleQueryData, panel);
  235. expect(table.rows.length).toBe(1);
  236. expect(table.rows[0][0]).toBe(time);
  237. expect(table.rows[0][1]).toBe("Label Value 1");
  238. expect(table.rows[0][2]).toBe(42);
  239. });
  240. it("should return 2 rows for a mulitple queries with same label values plus one extra row", () => {
  241. table = transformDataToTable(multipleQueriesDataSameLabels, panel);
  242. expect(table.rows.length).toBe(2);
  243. expect(table.rows[0][0]).toBe(time);
  244. expect(table.rows[0][1]).toBe("Label Value 1");
  245. expect(table.rows[0][2]).toBe("Label Value 2");
  246. expect(table.rows[0][3]).toBe(42);
  247. expect(table.rows[0][4]).toBe(13);
  248. expect(table.rows[0][5]).toBe(4);
  249. expect(table.rows[1][0]).toBe(time);
  250. expect(table.rows[1][1]).toBe("Label Value 1");
  251. expect(table.rows[1][2]).toBe("Label Value 2");
  252. expect(table.rows[1][3]).toBeUndefined();
  253. expect(table.rows[1][4]).toBeUndefined();
  254. expect(table.rows[1][5]).toBe(7);
  255. });
  256. it("should return 2 rows for mulitple queries with different label values", () => {
  257. table = transformDataToTable(
  258. multipleQueriesDataDifferentLabels,
  259. panel
  260. );
  261. expect(table.rows.length).toBe(2);
  262. expect(table.columns.length).toBe(6);
  263. expect(table.rows[0][0]).toBe(time);
  264. expect(table.rows[0][1]).toBe("Label Value 1");
  265. expect(table.rows[0][2]).toBe(42);
  266. expect(table.rows[0][3]).toBe("Label Value 2");
  267. expect(table.rows[0][4]).toBe(13);
  268. expect(table.rows[0][5]).toBeUndefined();
  269. expect(table.rows[1][0]).toBe(time);
  270. expect(table.rows[1][1]).toBe("Label Value 3");
  271. expect(table.rows[1][2]).toBeUndefined();
  272. expect(table.rows[1][3]).toBeUndefined();
  273. expect(table.rows[1][4]).toBeUndefined();
  274. expect(table.rows[1][5]).toBe(7);
  275. });
  276. });
  277. });
  278. });
  279. describe("doc data sets", () => {
  280. describe("JSON Data", () => {
  281. var panel = {
  282. transform: "json",
  283. columns: [
  284. { text: "Timestamp", value: "timestamp" },
  285. { text: "Message", value: "message" },
  286. { text: "nested.level2", value: "nested.level2" }
  287. ]
  288. };
  289. var rawData = [
  290. {
  291. type: "docs",
  292. datapoints: [
  293. {
  294. timestamp: "time",
  295. message: "message",
  296. nested: {
  297. level2: "level2-value"
  298. }
  299. }
  300. ]
  301. }
  302. ];
  303. describe("getColumns", function() {
  304. it("should return nested properties", function() {
  305. var columns = transformers["json"].getColumns(rawData);
  306. expect(columns[0].text).toBe("timestamp");
  307. expect(columns[1].text).toBe("message");
  308. expect(columns[2].text).toBe("nested.level2");
  309. });
  310. });
  311. describe("transform", function() {
  312. beforeEach(() => {
  313. table = transformDataToTable(rawData, panel);
  314. });
  315. it("should return 2 columns", () => {
  316. expect(table.columns.length).toBe(3);
  317. expect(table.columns[0].text).toBe("Timestamp");
  318. expect(table.columns[1].text).toBe("Message");
  319. expect(table.columns[2].text).toBe("nested.level2");
  320. });
  321. it("should return 2 rows", () => {
  322. expect(table.rows.length).toBe(1);
  323. expect(table.rows[0][0]).toBe("time");
  324. expect(table.rows[0][1]).toBe("message");
  325. expect(table.rows[0][2]).toBe("level2-value");
  326. });
  327. });
  328. });
  329. });
  330. describe("annotation data", () => {
  331. describe("Annnotations", () => {
  332. var panel = { transform: "annotations" };
  333. var rawData = {
  334. annotations: [
  335. {
  336. time: 1000,
  337. text: "hej",
  338. tags: ["tags", "asd"],
  339. title: "title"
  340. }
  341. ]
  342. };
  343. beforeEach(() => {
  344. table = transformDataToTable(rawData, panel);
  345. });
  346. it("should return 4 columns", () => {
  347. expect(table.columns.length).toBe(4);
  348. expect(table.columns[0].text).toBe("Time");
  349. expect(table.columns[1].text).toBe("Title");
  350. expect(table.columns[2].text).toBe("Text");
  351. expect(table.columns[3].text).toBe("Tags");
  352. });
  353. it("should return 1 rows", () => {
  354. expect(table.rows.length).toBe(1);
  355. expect(table.rows[0][0]).toBe(1000);
  356. });
  357. });
  358. });
  359. });