mergeBuckets.spec.tsx 4.9 KB

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