mergeBuckets.spec.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import {MonitorBucketData} from 'sentry/views/monitors/components/overviewTimeline/types';
  2. import {CheckInStatus} from 'sentry/views/monitors/types';
  3. import {mergeBuckets} from './mergeBuckets';
  4. type StatusCounts = [
  5. in_progress: number,
  6. ok: number,
  7. missed: number,
  8. timeout: number,
  9. error: number
  10. ];
  11. export function generateEnvMapping(name: string, counts: StatusCounts) {
  12. const [in_progress, ok, missed, timeout, error] = counts;
  13. return {
  14. [name]: {in_progress, ok, missed, timeout, error},
  15. };
  16. }
  17. function generateJobRun(envName: string, jobStatus: CheckInStatus) {
  18. const sortedStatuses = [
  19. CheckInStatus.IN_PROGRESS,
  20. CheckInStatus.OK,
  21. CheckInStatus.MISSED,
  22. CheckInStatus.TIMEOUT,
  23. CheckInStatus.ERROR,
  24. CheckInStatus.IN_PROGRESS,
  25. ];
  26. const counts: StatusCounts = [0, 0, 0, 0, 0];
  27. counts[sortedStatuses.indexOf(jobStatus)] = 1;
  28. return generateEnvMapping(envName, counts);
  29. }
  30. describe('mergeBuckets', function () {
  31. it('does not generate ticks less than 3px width', function () {
  32. const bucketData: MonitorBucketData = [
  33. [1, generateJobRun('prod', CheckInStatus.OK)],
  34. [2, generateJobRun('prod', CheckInStatus.OK)],
  35. [3, generateJobRun('prod', CheckInStatus.OK)],
  36. [4, {}],
  37. [5, generateJobRun('prod', CheckInStatus.OK)],
  38. [6, generateJobRun('prod', CheckInStatus.OK)],
  39. [7, generateJobRun('prod', CheckInStatus.OK)],
  40. [8, generateJobRun('prod', CheckInStatus.OK)],
  41. ];
  42. const mergedData = mergeBuckets(bucketData, 'prod');
  43. const expectedMerged = [
  44. {
  45. startTs: 1,
  46. endTs: 8,
  47. width: 8,
  48. roundedLeft: true,
  49. roundedRight: true,
  50. envMapping: generateEnvMapping('prod', [0, 7, 0, 0, 0]),
  51. },
  52. ];
  53. expect(mergedData).toEqual(expectedMerged);
  54. });
  55. it('generates adjacent ticks without border radius', function () {
  56. const bucketData: MonitorBucketData = [
  57. [1, generateJobRun('prod', CheckInStatus.OK)],
  58. [2, generateJobRun('prod', CheckInStatus.OK)],
  59. [3, generateJobRun('prod', CheckInStatus.OK)],
  60. [4, generateJobRun('prod', CheckInStatus.OK)],
  61. [5, generateJobRun('prod', CheckInStatus.MISSED)],
  62. [6, generateJobRun('prod', CheckInStatus.TIMEOUT)],
  63. [7, generateJobRun('prod', CheckInStatus.MISSED)],
  64. [8, generateJobRun('prod', CheckInStatus.MISSED)],
  65. ];
  66. const mergedData = mergeBuckets(bucketData, 'prod');
  67. const expectedMerged = [
  68. {
  69. startTs: 1,
  70. endTs: 4,
  71. width: 4,
  72. roundedLeft: true,
  73. roundedRight: false,
  74. envMapping: generateEnvMapping('prod', [0, 4, 0, 0, 0]),
  75. },
  76. {
  77. startTs: 5,
  78. endTs: 8,
  79. width: 4,
  80. roundedLeft: false,
  81. roundedRight: true,
  82. envMapping: generateEnvMapping('prod', [0, 0, 3, 1, 0]),
  83. },
  84. ];
  85. expect(mergedData).toEqual(expectedMerged);
  86. });
  87. it('does not generate a separate tick if the next generated tick would be the same status', function () {
  88. const bucketData: MonitorBucketData = [
  89. [1, generateJobRun('prod', CheckInStatus.TIMEOUT)],
  90. [2, generateJobRun('prod', CheckInStatus.TIMEOUT)],
  91. [3, generateJobRun('prod', CheckInStatus.TIMEOUT)],
  92. [4, generateJobRun('prod', CheckInStatus.TIMEOUT)],
  93. [5, generateJobRun('prod', CheckInStatus.MISSED)],
  94. [6, generateJobRun('prod', CheckInStatus.OK)],
  95. [7, generateJobRun('prod', CheckInStatus.MISSED)],
  96. [8, generateJobRun('prod', CheckInStatus.TIMEOUT)],
  97. ];
  98. const mergedData = mergeBuckets(bucketData, 'prod');
  99. const expectedMerged = [
  100. {
  101. startTs: 1,
  102. endTs: 8,
  103. width: 8,
  104. roundedLeft: true,
  105. roundedRight: true,
  106. envMapping: generateEnvMapping('prod', [0, 1, 2, 5, 0]),
  107. },
  108. ];
  109. expect(mergedData).toEqual(expectedMerged);
  110. });
  111. it('filters off environment', function () {
  112. const bucketData: MonitorBucketData = [
  113. [
  114. 1,
  115. {
  116. ...generateJobRun('prod', CheckInStatus.TIMEOUT),
  117. ...generateJobRun('dev', CheckInStatus.OK),
  118. },
  119. ],
  120. [
  121. 2,
  122. {
  123. ...generateJobRun('prod', CheckInStatus.TIMEOUT),
  124. ...generateJobRun('dev', CheckInStatus.MISSED),
  125. },
  126. ],
  127. [
  128. 3,
  129. {
  130. ...generateJobRun('prod', CheckInStatus.TIMEOUT),
  131. ...generateJobRun('dev', CheckInStatus.TIMEOUT),
  132. },
  133. ],
  134. [4, generateJobRun('prod', CheckInStatus.TIMEOUT)],
  135. [5, generateJobRun('prod', CheckInStatus.MISSED)],
  136. [6, generateJobRun('prod', CheckInStatus.OK)],
  137. [7, generateJobRun('prod', CheckInStatus.MISSED)],
  138. [8, generateJobRun('prod', CheckInStatus.TIMEOUT)],
  139. ];
  140. const mergedData = mergeBuckets(bucketData, 'dev');
  141. const expectedMerged = [
  142. {
  143. startTs: 1,
  144. endTs: 4,
  145. width: 4,
  146. roundedLeft: true,
  147. roundedRight: true,
  148. envMapping: generateEnvMapping('dev', [0, 1, 1, 1, 0]),
  149. },
  150. ];
  151. expect(mergedData).toEqual(expectedMerged);
  152. });
  153. });