mergeBuckets.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import type {JobTickData, MonitorBucket} from '../types';
  2. import {filterMonitorStatsBucketByEnv} from './filterMonitorStatsBucketByEnv';
  3. import {getAggregateStatus} from './getAggregateStatus';
  4. import {getAggregateStatusFromMultipleBuckets} from './getAggregateStatusFromMultipleBuckets';
  5. import {isEnvMappingEmpty} from './isEnvMappingEmpty';
  6. import {mergeEnvMappings} from './mergeEnvMappings';
  7. function generateJobTickFromBucket(
  8. bucket: MonitorBucket,
  9. options?: Partial<JobTickData>
  10. ) {
  11. const [timestamp, envMapping] = bucket;
  12. return {
  13. endTs: timestamp,
  14. startTs: timestamp,
  15. width: 1,
  16. envMapping,
  17. roundedLeft: false,
  18. roundedRight: false,
  19. ...options,
  20. };
  21. }
  22. export function mergeBuckets(data: MonitorBucket[], environment: string) {
  23. const minTickWidth = 4;
  24. const jobTicks: JobTickData[] = [];
  25. data.reduce(
  26. (currentJobTick, bucket, i) => {
  27. const filteredBucket = filterMonitorStatsBucketByEnv(bucket, environment);
  28. const [timestamp, envMapping] = filteredBucket;
  29. const envMappingEmpty = isEnvMappingEmpty(envMapping);
  30. if (!currentJobTick) {
  31. return envMappingEmpty
  32. ? currentJobTick
  33. : generateJobTickFromBucket(filteredBucket, {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(filteredBucket);
  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. },
  71. null as JobTickData | null
  72. );
  73. return jobTicks;
  74. }