mergeBuckets.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import {
  2. JobTickData,
  3. MonitorBucketData,
  4. } from 'sentry/views/monitors/components/overviewTimeline/types';
  5. import {getAggregateStatus} from './getAggregateStatus';
  6. import {getAggregateStatusFromMultipleBuckets} from './getAggregateStatusFromMultipleBuckets';
  7. import {isEnvMappingEmpty} from './isEnvMappingEmpty';
  8. import {mergeEnvMappings} from './mergeEnvMappings';
  9. function generateJobTickFromBucket(
  10. bucket: MonitorBucketData[number],
  11. options?: Partial<JobTickData>
  12. ) {
  13. const [timestamp, envMapping] = bucket;
  14. return {
  15. endTs: timestamp,
  16. startTs: timestamp,
  17. width: 1,
  18. envMapping,
  19. roundedLeft: false,
  20. roundedRight: false,
  21. ...options,
  22. };
  23. }
  24. export function mergeBuckets(data: MonitorBucketData) {
  25. const minTickWidth = 4;
  26. const jobTicks: JobTickData[] = [];
  27. data.reduce((currentJobTick, bucket, i) => {
  28. const [timestamp, envMapping] = bucket;
  29. const envMappingEmpty = isEnvMappingEmpty(envMapping);
  30. if (!currentJobTick) {
  31. return envMappingEmpty
  32. ? currentJobTick
  33. : generateJobTickFromBucket(bucket, {roundedLeft: true});
  34. }
  35. const bucketStatus = getAggregateStatus(envMapping);
  36. const currJobTickStatus = getAggregateStatus(currentJobTick.envMapping);
  37. // If the current bucket is empty and our job tick has reached a min width
  38. if (envMappingEmpty && currentJobTick.width >= minTickWidth) {
  39. // Then add our current tick to the running list of job ticks to render
  40. currentJobTick.roundedRight = true;
  41. jobTicks.push(currentJobTick);
  42. return null;
  43. }
  44. const nextTickAggregateStatus = getAggregateStatusFromMultipleBuckets(
  45. data.slice(i, i + minTickWidth).map(([_, envData]) => envData)
  46. );
  47. // If the next buckets have a different status from our current job tick
  48. if (
  49. bucketStatus !== currJobTickStatus &&
  50. nextTickAggregateStatus !== currJobTickStatus &&
  51. currentJobTick.width >= minTickWidth
  52. ) {
  53. // Then add our current tick to the running list of job ticks to render
  54. jobTicks.push(currentJobTick);
  55. return generateJobTickFromBucket(bucket);
  56. }
  57. // Merge our current tick with the current bucket data
  58. currentJobTick = {
  59. ...currentJobTick,
  60. endTs: timestamp,
  61. envMapping: mergeEnvMappings(currentJobTick.envMapping, envMapping),
  62. width: currentJobTick.width + 1,
  63. };
  64. // Ensure we render the last tick
  65. if (i === data.length - 1) {
  66. currentJobTick.roundedRight = true;
  67. jobTicks.push(currentJobTick);
  68. }
  69. return currentJobTick;
  70. }, null as JobTickData | null);
  71. return jobTicks;
  72. }