traceTimelineEvents.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import DateTime from 'sentry/components/dateTime';
  4. import {Tooltip} from 'sentry/components/tooltip';
  5. import {t} from 'sentry/locale';
  6. import {space} from 'sentry/styles/space';
  7. import type {Event} from 'sentry/types';
  8. import {TraceTimelineTooltip} from 'sentry/views/issueDetails/traceTimeline/traceTimelineTooltip';
  9. import type {TimelineEvent} from './useTraceTimelineEvents';
  10. import {useTraceTimelineEvents} from './useTraceTimelineEvents';
  11. import {getChunkTimeRange, getEventsByColumn} from './utils';
  12. // Adjusting this will change the number of tooltip groups
  13. const PARENT_WIDTH = 12;
  14. // Adjusting subwidth changes how many dots to render
  15. const CHILD_WIDTH = 4;
  16. interface TraceTimelineEventsProps {
  17. event: Event;
  18. width: number;
  19. }
  20. export function TraceTimelineEvents({event, width}: TraceTimelineEventsProps) {
  21. const {startTimestamp, endTimestamp, data} = useTraceTimelineEvents({event});
  22. let durationMs = endTimestamp - startTimestamp;
  23. const paddedStartTime = startTimestamp - 200;
  24. let paddedEndTime = endTimestamp + 100;
  25. // Will need to figure out padding
  26. if (durationMs === 0) {
  27. durationMs = 1000;
  28. // If the duration is 0, we need to pad the end time
  29. paddedEndTime = startTimestamp + 1000;
  30. }
  31. const totalColumns = Math.floor(width / PARENT_WIDTH);
  32. const eventsByColumn = getEventsByColumn(
  33. durationMs,
  34. data,
  35. totalColumns,
  36. paddedStartTime
  37. );
  38. const columnSize = width / totalColumns;
  39. return (
  40. <Fragment>
  41. <TimelineColumns totalColumns={totalColumns}>
  42. {Array.from(eventsByColumn.entries()).map(([column, colEvents]) => {
  43. // Calculate the timestamp range that this column represents
  44. const timeRange = getChunkTimeRange(
  45. paddedStartTime,
  46. column - 1,
  47. durationMs / totalColumns
  48. );
  49. return (
  50. <EventColumn
  51. key={column}
  52. style={{gridColumn: Math.floor(column), width: columnSize}}
  53. >
  54. <NodeGroup
  55. event={event}
  56. colEvents={colEvents}
  57. columnSize={columnSize}
  58. timeRange={timeRange}
  59. currentEventId={event.id}
  60. />
  61. </EventColumn>
  62. );
  63. })}
  64. </TimelineColumns>
  65. <TimestampColumns>
  66. <TimestampItem style={{textAlign: 'left'}}>
  67. <DateTime date={paddedStartTime} timeOnly />
  68. </TimestampItem>
  69. <TimestampItem style={{textAlign: 'center'}}>
  70. <DateTime date={paddedStartTime + Math.floor(durationMs / 2)} timeOnly />
  71. </TimestampItem>
  72. <TimestampItem style={{textAlign: 'right'}}>
  73. <DateTime date={paddedEndTime} timeOnly />
  74. </TimestampItem>
  75. </TimestampColumns>
  76. </Fragment>
  77. );
  78. }
  79. /**
  80. * Use grid to create columns that we can place child nodes into.
  81. * Leveraging grid for alignment means we don't need to calculate percent offset
  82. * nor use position:absolute to lay out items.
  83. *
  84. * <Columns>
  85. * <Col>...</Col>
  86. * <Col>...</Col>
  87. * </Columns>
  88. */
  89. const TimelineColumns = styled('ul')<{totalColumns: number}>`
  90. /* Reset defaults for <ul> */
  91. list-style: none;
  92. margin: 0;
  93. padding: 0;
  94. /* Layout of the lines */
  95. display: grid;
  96. grid-template-columns: repeat(${p => p.totalColumns}, 1fr);
  97. margin-top: -1px;
  98. `;
  99. const TimestampColumns = styled('div')`
  100. display: grid;
  101. grid-template-columns: repeat(3, 1fr);
  102. margin-top: ${space(1)};
  103. `;
  104. const TimestampItem = styled('div')`
  105. place-items: stretch;
  106. display: grid;
  107. align-items: center;
  108. position: relative;
  109. color: ${p => p.theme.subText};
  110. font-size: ${p => p.theme.fontSizeSmall};
  111. `;
  112. function NodeGroup({
  113. event,
  114. timeRange,
  115. colEvents,
  116. columnSize,
  117. currentEventId,
  118. }: {
  119. colEvents: TimelineEvent[];
  120. columnSize: number;
  121. currentEventId: string;
  122. event: Event;
  123. timeRange: [number, number];
  124. }) {
  125. const totalSubColumns = Math.floor(columnSize / CHILD_WIDTH);
  126. const durationMs = timeRange[1] - timeRange[0];
  127. const eventsByColumn = getEventsByColumn(
  128. durationMs,
  129. colEvents,
  130. totalSubColumns,
  131. timeRange[0]
  132. );
  133. return (
  134. <Tooltip
  135. title={<TraceTimelineTooltip event={event} timelineEvents={colEvents} />}
  136. overlayStyle={{
  137. padding: `0 !important`,
  138. maxWidth: '250px !important',
  139. width: '250px',
  140. }}
  141. offset={10}
  142. position="bottom"
  143. isHoverable
  144. skipWrapper
  145. >
  146. <TimelineColumns totalColumns={totalSubColumns}>
  147. {Array.from(eventsByColumn.entries()).map(([column, groupEvents]) => (
  148. <EventColumn key={column} style={{gridColumn: Math.floor(column)}}>
  149. {groupEvents.map(groupEvent => (
  150. // TODO: use sentry colors and add the other styles
  151. <IconNode
  152. key={groupEvent.id}
  153. aria-label={
  154. groupEvent.id === currentEventId ? t('Current Event') : undefined
  155. }
  156. style={
  157. groupEvent.id === currentEventId
  158. ? {
  159. backgroundColor: 'rgb(181, 19, 7, 1)',
  160. outline: '1px solid rgb(181, 19, 7, 0.5)',
  161. outlineOffset: '3px',
  162. }
  163. : undefined
  164. }
  165. />
  166. ))}
  167. </EventColumn>
  168. ))}
  169. </TimelineColumns>
  170. </Tooltip>
  171. );
  172. }
  173. const EventColumn = styled('li')`
  174. place-items: stretch;
  175. display: grid;
  176. align-items: center;
  177. position: relative;
  178. &:hover {
  179. z-index: ${p => p.theme.zIndex.initial};
  180. }
  181. `;
  182. const IconNode = styled('div')`
  183. position: absolute;
  184. grid-column: 1;
  185. grid-row: 1;
  186. width: 8px;
  187. height: 8px;
  188. border-radius: 50%;
  189. color: ${p => p.theme.white};
  190. box-shadow: ${p => p.theme.dropShadowLight};
  191. user-select: none;
  192. background-color: rgb(181, 19, 7, 0.2);
  193. `;