transformers_specs.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import {describe, beforeEach, it, sinon, expect} from 'test/lib/common';
  2. import {transformers, transformDataToTable} from '../transformers';
  3. describe('when transforming time series table', () => {
  4. var table;
  5. describe('given 2 time series', () => {
  6. var time = new Date().getTime();
  7. var timeSeries = [
  8. {
  9. target: 'series1',
  10. datapoints: [[12.12, time], [14.44, time+1]],
  11. },
  12. {
  13. target: 'series2',
  14. datapoints: [[16.12, time]],
  15. }
  16. ];
  17. describe('timeseries_to_rows', () => {
  18. var panel = {
  19. transform: 'timeseries_to_rows',
  20. sort: {col: 0, desc: true},
  21. };
  22. beforeEach(() => {
  23. table = transformDataToTable(timeSeries, panel);
  24. });
  25. it('should return 3 rows', () => {
  26. expect(table.rows.length).to.be(3);
  27. expect(table.rows[0][1]).to.be('series1');
  28. expect(table.rows[1][1]).to.be('series1');
  29. expect(table.rows[2][1]).to.be('series2');
  30. expect(table.rows[0][2]).to.be(12.12);
  31. });
  32. it('should return 3 rows', () => {
  33. expect(table.columns.length).to.be(3);
  34. expect(table.columns[0].text).to.be('Time');
  35. expect(table.columns[1].text).to.be('Metric');
  36. expect(table.columns[2].text).to.be('Value');
  37. });
  38. });
  39. describe('timeseries_to_columns', () => {
  40. var panel = {
  41. transform: 'timeseries_to_columns'
  42. };
  43. beforeEach(() => {
  44. table = transformDataToTable(timeSeries, panel);
  45. });
  46. it ('should return 3 columns', () => {
  47. expect(table.columns.length).to.be(3);
  48. expect(table.columns[0].text).to.be('Time');
  49. expect(table.columns[1].text).to.be('series1');
  50. expect(table.columns[2].text).to.be('series2');
  51. });
  52. it ('should return 2 rows', () => {
  53. expect(table.rows.length).to.be(2);
  54. expect(table.rows[0][1]).to.be(12.12);
  55. expect(table.rows[0][2]).to.be(16.12);
  56. });
  57. it ('should be undefined when no value for timestamp', () => {
  58. expect(table.rows[1][2]).to.be(undefined);
  59. });
  60. });
  61. describe('timeseries_aggregations', () => {
  62. var panel = {
  63. transform: 'timeseries_aggregations',
  64. sort: {col: 0, desc: true},
  65. columns: [{text: 'Max', value: 'max'}, {text: 'Min', value: 'min'}]
  66. };
  67. beforeEach(() => {
  68. table = transformDataToTable(timeSeries, panel);
  69. });
  70. it('should return 2 rows', () => {
  71. expect(table.rows.length).to.be(2);
  72. expect(table.rows[0][0]).to.be('series1');
  73. expect(table.rows[0][1]).to.be(14.44);
  74. expect(table.rows[0][2]).to.be(12.12);
  75. });
  76. it('should return 2 columns', () => {
  77. expect(table.columns.length).to.be(3);
  78. expect(table.columns[0].text).to.be('Metric');
  79. expect(table.columns[1].text).to.be('Max');
  80. expect(table.columns[2].text).to.be('Min');
  81. });
  82. });
  83. describe('JSON Data', () => {
  84. var panel = {
  85. transform: 'json',
  86. columns: [
  87. {text: 'Timestamp', value: 'timestamp'},
  88. {text: 'Message', value: 'message'},
  89. {text: 'nested.level2', value: 'nested.level2'},
  90. ]
  91. };
  92. var rawData = [
  93. {
  94. type: 'docs',
  95. datapoints: [
  96. {
  97. timestamp: 'time',
  98. message: 'message',
  99. nested: {
  100. level2: 'level2-value'
  101. }
  102. }
  103. ]
  104. }
  105. ];
  106. describe('getColumns', function() {
  107. it('should return nested properties', function() {
  108. var columns = transformers['json'].getColumns(rawData);
  109. expect(columns[0].text).to.be('timestamp');
  110. expect(columns[1].text).to.be('message');
  111. expect(columns[2].text).to.be('nested.level2');
  112. });
  113. });
  114. describe('transform', function() {
  115. beforeEach(() => {
  116. table = transformDataToTable(rawData, panel);
  117. });
  118. it ('should return 2 columns', () => {
  119. expect(table.columns.length).to.be(3);
  120. expect(table.columns[0].text).to.be('Timestamp');
  121. expect(table.columns[1].text).to.be('Message');
  122. expect(table.columns[2].text).to.be('nested.level2');
  123. });
  124. it ('should return 2 rows', () => {
  125. expect(table.rows.length).to.be(1);
  126. expect(table.rows[0][0]).to.be('time');
  127. expect(table.rows[0][1]).to.be('message');
  128. expect(table.rows[0][2]).to.be('level2-value');
  129. });
  130. });
  131. });
  132. describe('Annnotations', () => {
  133. var panel = {transform: 'annotations'};
  134. var rawData = [
  135. {
  136. min: 1000,
  137. text: 'hej',
  138. tags: ['tags', 'asd'],
  139. title: 'title',
  140. }
  141. ];
  142. beforeEach(() => {
  143. table = transformDataToTable(rawData, panel);
  144. });
  145. it ('should return 4 columns', () => {
  146. expect(table.columns.length).to.be(4);
  147. expect(table.columns[0].text).to.be('Time');
  148. expect(table.columns[1].text).to.be('Title');
  149. expect(table.columns[2].text).to.be('Text');
  150. expect(table.columns[3].text).to.be('Tags');
  151. });
  152. it ('should return 1 rows', () => {
  153. expect(table.rows.length).to.be(1);
  154. expect(table.rows[0][0]).to.be(1000);
  155. });
  156. });
  157. });
  158. });