mergeBuckets.spec.tsx 5.0 KB

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