makeTableFrames.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { ArrayVector, DataFrame, FieldType } from '@grafana/data';
  2. import { makeTableFrames } from './makeTableFrames';
  3. const frame1: DataFrame = {
  4. name: 'frame1',
  5. refId: 'A',
  6. meta: {
  7. executedQueryString: 'something1',
  8. },
  9. fields: [
  10. {
  11. name: 'Time',
  12. type: FieldType.time,
  13. config: {},
  14. values: new ArrayVector([1645029699311]),
  15. },
  16. {
  17. name: 'Value',
  18. type: FieldType.number,
  19. labels: {
  20. level: 'error',
  21. location: 'moon',
  22. protocol: 'http',
  23. },
  24. config: {
  25. displayNameFromDS: '{level="error", location="moon", protocol="http"}',
  26. },
  27. values: new ArrayVector([23]),
  28. },
  29. ],
  30. length: 1,
  31. };
  32. const frame2: DataFrame = {
  33. name: 'frame1',
  34. refId: 'A',
  35. meta: {
  36. executedQueryString: 'something1',
  37. },
  38. fields: [
  39. {
  40. name: 'Time',
  41. type: FieldType.time,
  42. config: {},
  43. values: new ArrayVector([1645029699311]),
  44. },
  45. {
  46. name: 'Value',
  47. type: FieldType.number,
  48. labels: {
  49. level: 'info',
  50. location: 'moon',
  51. protocol: 'http',
  52. },
  53. config: {
  54. displayNameFromDS: '{level="info", location="moon", protocol="http"}',
  55. },
  56. values: new ArrayVector([45]),
  57. },
  58. ],
  59. length: 1,
  60. };
  61. const frame3: DataFrame = {
  62. name: 'frame1',
  63. refId: 'B',
  64. meta: {
  65. executedQueryString: 'something1',
  66. },
  67. fields: [
  68. {
  69. name: 'Time',
  70. type: FieldType.time,
  71. config: {},
  72. values: new ArrayVector([1645029699311]),
  73. },
  74. {
  75. name: 'Value',
  76. type: FieldType.number,
  77. labels: {
  78. level: 'error',
  79. location: 'moon',
  80. protocol: 'http',
  81. },
  82. config: {
  83. displayNameFromDS: '{level="error", location="moon", protocol="http"}',
  84. },
  85. values: new ArrayVector([72]),
  86. },
  87. ],
  88. length: 1,
  89. };
  90. const outputSingle = [
  91. {
  92. fields: [
  93. { config: {}, name: 'Time', type: 'time', values: new ArrayVector([1645029699311]) },
  94. { config: { filterable: true }, name: 'level', type: 'string', values: new ArrayVector(['error']) },
  95. { config: { filterable: true }, name: 'location', type: 'string', values: new ArrayVector(['moon']) },
  96. { config: { filterable: true }, name: 'protocol', type: 'string', values: new ArrayVector(['http']) },
  97. { config: {}, name: 'Value #A', type: 'number', values: new ArrayVector([23]) },
  98. ],
  99. length: 1,
  100. meta: { preferredVisualisationType: 'table' },
  101. refId: 'A',
  102. },
  103. ];
  104. const outputMulti = [
  105. {
  106. fields: [
  107. { config: {}, name: 'Time', type: 'time', values: new ArrayVector([1645029699311, 1645029699311]) },
  108. { config: { filterable: true }, name: 'level', type: 'string', values: new ArrayVector(['error', 'info']) },
  109. { config: { filterable: true }, name: 'location', type: 'string', values: new ArrayVector(['moon', 'moon']) },
  110. { config: { filterable: true }, name: 'protocol', type: 'string', values: new ArrayVector(['http', 'http']) },
  111. { config: {}, name: 'Value #A', type: 'number', values: new ArrayVector([23, 45]) },
  112. ],
  113. length: 2,
  114. meta: { preferredVisualisationType: 'table' },
  115. refId: 'A',
  116. },
  117. {
  118. fields: [
  119. { config: {}, name: 'Time', type: 'time', values: new ArrayVector([1645029699311]) },
  120. { config: { filterable: true }, name: 'level', type: 'string', values: new ArrayVector(['error']) },
  121. { config: { filterable: true }, name: 'location', type: 'string', values: new ArrayVector(['moon']) },
  122. { config: { filterable: true }, name: 'protocol', type: 'string', values: new ArrayVector(['http']) },
  123. { config: {}, name: 'Value #B', type: 'number', values: new ArrayVector([72]) },
  124. ],
  125. length: 1,
  126. meta: { preferredVisualisationType: 'table' },
  127. refId: 'B',
  128. },
  129. ];
  130. describe('loki makeTableFrames', () => {
  131. it('converts a single instant metric dataframe to table dataframe', () => {
  132. const result = makeTableFrames([frame1]);
  133. expect(result).toEqual(outputSingle);
  134. });
  135. it('converts 3 instant metric dataframes into 2 tables', () => {
  136. const result = makeTableFrames([frame1, frame2, frame3]);
  137. expect(result).toEqual(outputMulti);
  138. });
  139. });