mapSeriesToChart.spec.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import {mapSeriesToChart} from './mapSeriesToChart';
  2. import type {UsageSeries} from './types';
  3. const mockSeries: UsageSeries = {
  4. start: '2021-01-01T00:00:00Z',
  5. end: '2021-01-07T00:00:00Z',
  6. intervals: ['2021-01-01T00:00:00Z', '2021-01-02T00:00:00Z', '2021-01-03T00:00:00Z'],
  7. groups: [
  8. {
  9. by: {
  10. outcome: 'accepted',
  11. },
  12. totals: {
  13. 'sum(quantity)': 6,
  14. },
  15. series: {
  16. 'sum(quantity)': [1, 2, 3],
  17. },
  18. },
  19. {
  20. by: {
  21. outcome: 'filtered',
  22. reason: 'other',
  23. },
  24. totals: {
  25. 'sum(quantity)': 4,
  26. },
  27. series: {
  28. 'sum(quantity)': [0, 1, 3],
  29. },
  30. },
  31. {
  32. by: {
  33. outcome: 'invalid',
  34. reason: 'invalid_transaction',
  35. },
  36. totals: {
  37. 'sum(quantity)': 6,
  38. },
  39. series: {
  40. 'sum(quantity)': [2, 2, 2],
  41. },
  42. },
  43. {
  44. by: {
  45. outcome: 'invalid',
  46. reason: 'other_reason_a',
  47. },
  48. totals: {
  49. 'sum(quantity)': 6,
  50. },
  51. series: {
  52. 'sum(quantity)': [1, 2, 3],
  53. },
  54. },
  55. {
  56. by: {
  57. outcome: 'invalid',
  58. reason: 'other_reason_b',
  59. },
  60. totals: {
  61. 'sum(quantity)': 3,
  62. },
  63. series: {
  64. 'sum(quantity)': [1, 1, 1],
  65. },
  66. },
  67. ],
  68. };
  69. describe('mapSeriesToChart func', function () {
  70. it("should return correct chart tooltip's reasons", function () {
  71. const mappedSeries = mapSeriesToChart({
  72. orgStats: mockSeries,
  73. chartDateInterval: '1h',
  74. chartDateUtc: true,
  75. dataCategory: 'transactions',
  76. endpointQuery: {},
  77. });
  78. expect(mappedSeries.chartSubLabels).toEqual([
  79. {
  80. parentLabel: 'Filtered',
  81. label: 'Other',
  82. data: [
  83. {name: '2021-01-01T00:00:00Z', value: 0},
  84. {name: '2021-01-02T00:00:00Z', value: 1},
  85. {name: '2021-01-03T00:00:00Z', value: 3},
  86. ],
  87. },
  88. {
  89. parentLabel: 'Invalid',
  90. label: 'Invalid Data',
  91. data: [
  92. {name: '2021-01-01T00:00:00Z', value: 2},
  93. {name: '2021-01-02T00:00:00Z', value: 2},
  94. {name: '2021-01-03T00:00:00Z', value: 2},
  95. ],
  96. },
  97. {
  98. parentLabel: 'Invalid',
  99. label: 'Internal',
  100. data: [
  101. {name: '2021-01-01T00:00:00Z', value: 2},
  102. {name: '2021-01-02T00:00:00Z', value: 3},
  103. {name: '2021-01-03T00:00:00Z', value: 4},
  104. ],
  105. },
  106. ]);
  107. });
  108. it('should correctly sum up the rate limited count', function () {
  109. const mappedSeries = mapSeriesToChart({
  110. orgStats: {
  111. start: '2021-01-01T00:00:00Z',
  112. end: '2021-01-07T00:00:00Z',
  113. intervals: [
  114. '2021-01-01T00:00:00Z',
  115. '2021-01-02T00:00:00Z',
  116. '2021-01-03T00:00:00Z',
  117. ],
  118. groups: [
  119. {
  120. by: {
  121. outcome: 'accepted',
  122. },
  123. totals: {
  124. 'sum(quantity)': 99,
  125. },
  126. series: {
  127. 'sum(quantity)': [99],
  128. },
  129. },
  130. {
  131. by: {
  132. outcome: 'rate_limited',
  133. },
  134. totals: {
  135. 'sum(quantity)': 6,
  136. },
  137. series: {
  138. 'sum(quantity)': [1, 2, 3],
  139. },
  140. },
  141. {
  142. by: {
  143. outcome: 'abuse',
  144. },
  145. totals: {
  146. 'sum(quantity)': 2,
  147. },
  148. series: {
  149. 'sum(quantity)': [1, 1],
  150. },
  151. },
  152. {
  153. by: {
  154. outcome: 'cardinality_limited',
  155. },
  156. totals: {
  157. 'sum(quantity)': 3,
  158. },
  159. series: {
  160. 'sum(quantity)': [1, 2],
  161. },
  162. },
  163. ],
  164. },
  165. chartDateInterval: '1h',
  166. chartDateUtc: true,
  167. dataCategory: 'transactions',
  168. endpointQuery: {},
  169. });
  170. // sums up rate limited, abuse, and cardinality limited
  171. expect(mappedSeries.cardStats.rateLimited).toEqual('11');
  172. });
  173. });