checkInTimeline.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import styled from '@emotion/styled';
  2. import {DateTime} from 'sentry/components/dateTime';
  3. import {Tooltip} from 'sentry/components/tooltip';
  4. import {CheckInStatus} from 'sentry/views/monitors/types';
  5. import {getTickStyle} from 'sentry/views/monitors/utils';
  6. import {getAggregateStatus} from './utils/getAggregateStatus';
  7. import {mergeBuckets} from './utils/mergeBuckets';
  8. import {CheckInTooltip} from './checkInTooltip';
  9. import type {MonitorBucketData, TimeWindowConfig} from './types';
  10. interface TimelineProps {
  11. timeWindowConfig: TimeWindowConfig;
  12. width: number;
  13. }
  14. export interface CheckInTimelineProps extends TimelineProps {
  15. bucketedData: MonitorBucketData;
  16. environment: string;
  17. }
  18. function getBucketedCheckInsPosition(
  19. timestamp: number,
  20. timelineStart: Date,
  21. msPerPixel: number
  22. ) {
  23. const elapsedSinceStart = new Date(timestamp).getTime() - timelineStart.getTime();
  24. return elapsedSinceStart / msPerPixel;
  25. }
  26. export function CheckInTimeline(props: CheckInTimelineProps) {
  27. const {bucketedData, timeWindowConfig, width, environment} = props;
  28. const {start, end} = timeWindowConfig;
  29. const elapsedMs = end.getTime() - start.getTime();
  30. const msPerPixel = elapsedMs / width;
  31. const jobTicks = mergeBuckets(bucketedData, environment);
  32. return (
  33. <TimelineContainer>
  34. {jobTicks.map(jobTick => {
  35. const {
  36. startTs,
  37. width: tickWidth,
  38. envMapping,
  39. roundedLeft,
  40. roundedRight,
  41. } = jobTick;
  42. const timestampMs = startTs * 1000;
  43. const left = getBucketedCheckInsPosition(timestampMs, start, msPerPixel);
  44. return (
  45. <CheckInTooltip
  46. jobTick={jobTick}
  47. timeWindowConfig={timeWindowConfig}
  48. skipWrapper
  49. key={startTs}
  50. >
  51. <JobTick
  52. style={{left, width: tickWidth}}
  53. status={getAggregateStatus(envMapping)}
  54. roundedLeft={roundedLeft}
  55. roundedRight={roundedRight}
  56. data-test-id="monitor-checkin-tick"
  57. />
  58. </CheckInTooltip>
  59. );
  60. })}
  61. </TimelineContainer>
  62. );
  63. }
  64. export interface MockCheckInTimelineProps extends TimelineProps {
  65. mockTimestamps: Date[];
  66. }
  67. export function MockCheckInTimeline({
  68. mockTimestamps,
  69. timeWindowConfig,
  70. width,
  71. }: MockCheckInTimelineProps) {
  72. const {start, end} = timeWindowConfig;
  73. const elapsedMs = end.getTime() - start.getTime();
  74. const msPerPixel = elapsedMs / width;
  75. return (
  76. <TimelineContainer>
  77. {mockTimestamps.map(ts => {
  78. const timestampMs = ts.getTime();
  79. const left = getBucketedCheckInsPosition(timestampMs, start, msPerPixel);
  80. return (
  81. <Tooltip
  82. key={left}
  83. title={
  84. <DateTime date={timestampMs} format={timeWindowConfig.dateLabelFormat} />
  85. }
  86. skipWrapper
  87. >
  88. <JobTick
  89. style={{left}}
  90. status={CheckInStatus.IN_PROGRESS}
  91. roundedLeft
  92. roundedRight
  93. data-test-id="monitor-checkin-tick"
  94. />
  95. </Tooltip>
  96. );
  97. })}
  98. </TimelineContainer>
  99. );
  100. }
  101. const TimelineContainer = styled('div')`
  102. position: relative;
  103. height: 100%;
  104. `;
  105. const JobTick = styled('div')<{
  106. roundedLeft: boolean;
  107. roundedRight: boolean;
  108. status: CheckInStatus;
  109. }>`
  110. position: absolute;
  111. top: calc(50% + 1px);
  112. width: 4px;
  113. height: 14px;
  114. transform: translateY(-50%);
  115. opacity: 0.7;
  116. ${p => getTickStyle(p.status, p.theme)};
  117. ${p =>
  118. p.roundedLeft &&
  119. `
  120. border-top-left-radius: 2px;
  121. border-bottom-left-radius: 2px;
  122. `};
  123. ${p =>
  124. p.roundedRight &&
  125. `
  126. border-top-right-radius: 2px;
  127. border-bottom-right-radius: 2px;
  128. `}
  129. ${p => !p.roundedLeft && 'border-left-width: 0'};
  130. ${p => !p.roundedRight && 'border-right-width: 0'};
  131. `;