transformers_specs.ts 5.3 KB

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