transactionBar.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. import {Component, createRef, Fragment} from 'react';
  2. import {Location} from 'history';
  3. import GuideAnchor from 'sentry/components/assistant/guideAnchor';
  4. import Count from 'sentry/components/count';
  5. import * as DividerHandlerManager from 'sentry/components/events/interfaces/spans/dividerHandlerManager';
  6. import * as ScrollbarManager from 'sentry/components/events/interfaces/spans/scrollbarManager';
  7. import {MeasurementMarker} from 'sentry/components/events/interfaces/spans/styles';
  8. import {
  9. getMeasurementBounds,
  10. SpanBoundsType,
  11. SpanGeneratedBoundsType,
  12. transactionTargetHash,
  13. VerticalMark,
  14. } from 'sentry/components/events/interfaces/spans/utils';
  15. import ProjectBadge from 'sentry/components/idBadge/projectBadge';
  16. import {ROW_HEIGHT, SpanBarType} from 'sentry/components/performance/waterfall/constants';
  17. import {
  18. Row,
  19. RowCell,
  20. RowCellContainer,
  21. RowReplayTimeIndicators,
  22. } from 'sentry/components/performance/waterfall/row';
  23. import {DurationPill, RowRectangle} from 'sentry/components/performance/waterfall/rowBar';
  24. import {
  25. DividerContainer,
  26. DividerLine,
  27. DividerLineGhostContainer,
  28. ErrorBadge,
  29. } from 'sentry/components/performance/waterfall/rowDivider';
  30. import {
  31. RowTitle,
  32. RowTitleContainer,
  33. RowTitleContent,
  34. } from 'sentry/components/performance/waterfall/rowTitle';
  35. import {
  36. ConnectorBar,
  37. TOGGLE_BORDER_BOX,
  38. TreeConnector,
  39. TreeToggle,
  40. TreeToggleContainer,
  41. TreeToggleIcon,
  42. } from 'sentry/components/performance/waterfall/treeConnector';
  43. import {
  44. getDurationDisplay,
  45. getHumanDuration,
  46. toPercent,
  47. } from 'sentry/components/performance/waterfall/utils';
  48. import {Tooltip} from 'sentry/components/tooltip';
  49. import {Organization} from 'sentry/types';
  50. import {defined} from 'sentry/utils';
  51. import {TraceFullDetailed} from 'sentry/utils/performance/quickTrace/types';
  52. import {isTraceFullDetailed} from 'sentry/utils/performance/quickTrace/utils';
  53. import Projects from 'sentry/utils/projects';
  54. import {ProjectBadgeContainer} from './styles';
  55. import TransactionDetail from './transactionDetail';
  56. import {TraceInfo, TraceRoot, TreeDepth} from './types';
  57. const MARGIN_LEFT = 0;
  58. type Props = {
  59. addContentSpanBarRef: (instance: HTMLDivElement | null) => void;
  60. continuingDepths: TreeDepth[];
  61. generateBounds: (bounds: SpanBoundsType) => SpanGeneratedBoundsType;
  62. hasGuideAnchor: boolean;
  63. index: number;
  64. isExpanded: boolean;
  65. isLast: boolean;
  66. isOrphan: boolean;
  67. isVisible: boolean;
  68. location: Location;
  69. onWheel: (deltaX: number) => void;
  70. organization: Organization;
  71. removeContentSpanBarRef: (instance: HTMLDivElement | null) => void;
  72. toggleExpandedState: () => void;
  73. traceInfo: TraceInfo;
  74. transaction: TraceRoot | TraceFullDetailed;
  75. barColor?: string;
  76. measurements?: Map<number, VerticalMark>;
  77. };
  78. type State = {
  79. showDetail: boolean;
  80. };
  81. class TransactionBar extends Component<Props, State> {
  82. state: State = {
  83. showDetail: false,
  84. };
  85. componentDidMount() {
  86. const {location, transaction} = this.props;
  87. if (
  88. 'event_id' in transaction &&
  89. transactionTargetHash(transaction.event_id) === location.hash
  90. ) {
  91. this.scrollIntoView();
  92. }
  93. if (this.transactionTitleRef.current) {
  94. this.transactionTitleRef.current.addEventListener('wheel', this.handleWheel, {
  95. passive: false,
  96. });
  97. }
  98. }
  99. componentWillUnmount() {
  100. if (this.transactionTitleRef.current) {
  101. this.transactionTitleRef.current.removeEventListener('wheel', this.handleWheel);
  102. }
  103. }
  104. transactionRowDOMRef = createRef<HTMLDivElement>();
  105. transactionTitleRef = createRef<HTMLDivElement>();
  106. spanContentRef: HTMLDivElement | null = null;
  107. toggleDisplayDetail = () => {
  108. const {transaction} = this.props;
  109. if (isTraceFullDetailed(transaction)) {
  110. this.setState(state => ({
  111. showDetail: !state.showDetail,
  112. }));
  113. }
  114. };
  115. getCurrentOffset() {
  116. const {transaction} = this.props;
  117. const {generation} = transaction;
  118. return getOffset(generation);
  119. }
  120. handleWheel = (event: WheelEvent) => {
  121. // https://stackoverflow.com/q/57358640
  122. // https://github.com/facebook/react/issues/14856
  123. if (Math.abs(event.deltaY) > Math.abs(event.deltaX)) {
  124. return;
  125. }
  126. event.preventDefault();
  127. event.stopPropagation();
  128. if (Math.abs(event.deltaY) === Math.abs(event.deltaX)) {
  129. return;
  130. }
  131. const {onWheel} = this.props;
  132. onWheel(event.deltaX);
  133. };
  134. renderMeasurements() {
  135. const {measurements, generateBounds} = this.props;
  136. if (!measurements) {
  137. return null;
  138. }
  139. return (
  140. <Fragment>
  141. {Array.from(measurements.values()).map(verticalMark => {
  142. const mark = Object.values(verticalMark.marks)[0];
  143. const {timestamp} = mark;
  144. const bounds = getMeasurementBounds(timestamp, generateBounds);
  145. const shouldDisplay = defined(bounds.left) && defined(bounds.width);
  146. if (!shouldDisplay || !bounds.isSpanVisibleInView) {
  147. return null;
  148. }
  149. return (
  150. <MeasurementMarker
  151. key={String(timestamp)}
  152. style={{
  153. left: `clamp(0%, ${toPercent(bounds.left || 0)}, calc(100% - 1px))`,
  154. }}
  155. failedThreshold={verticalMark.failedThreshold}
  156. />
  157. );
  158. })}
  159. </Fragment>
  160. );
  161. }
  162. renderConnector(hasToggle: boolean) {
  163. const {continuingDepths, isExpanded, isOrphan, isLast, transaction} = this.props;
  164. const {generation} = transaction;
  165. const eventId = isTraceFullDetailed(transaction)
  166. ? transaction.event_id
  167. : transaction.traceSlug;
  168. if (generation === 0) {
  169. if (hasToggle) {
  170. return (
  171. <ConnectorBar
  172. style={{right: '15px', height: '10px', bottom: '-5px', top: 'auto'}}
  173. orphanBranch={false}
  174. />
  175. );
  176. }
  177. return null;
  178. }
  179. const connectorBars: Array<React.ReactNode> = continuingDepths.map(
  180. ({depth, isOrphanDepth}) => {
  181. if (generation - depth <= 1) {
  182. // If the difference is less than or equal to 1, then it means that the continued
  183. // bar is from its direct parent. In this case, do not render a connector bar
  184. // because the tree connector below will suffice.
  185. return null;
  186. }
  187. const left = -1 * getOffset(generation - depth - 1) - 2;
  188. return (
  189. <ConnectorBar
  190. style={{left}}
  191. key={`${eventId}-${depth}`}
  192. orphanBranch={isOrphanDepth}
  193. />
  194. );
  195. }
  196. );
  197. if (hasToggle && isExpanded) {
  198. connectorBars.push(
  199. <ConnectorBar
  200. style={{
  201. right: '15px',
  202. height: '10px',
  203. bottom: isLast ? `-${ROW_HEIGHT / 2 + 1}px` : '0',
  204. top: 'auto',
  205. }}
  206. key={`${eventId}-last`}
  207. orphanBranch={false}
  208. />
  209. );
  210. }
  211. return (
  212. <TreeConnector isLast={isLast} hasToggler={hasToggle} orphanBranch={isOrphan}>
  213. {connectorBars}
  214. </TreeConnector>
  215. );
  216. }
  217. renderToggle(errored: boolean) {
  218. const {isExpanded, transaction, toggleExpandedState} = this.props;
  219. const {children, generation} = transaction;
  220. const left = this.getCurrentOffset();
  221. if (children.length <= 0) {
  222. return (
  223. <TreeToggleContainer style={{left: `${left}px`}}>
  224. {this.renderConnector(false)}
  225. </TreeToggleContainer>
  226. );
  227. }
  228. const isRoot = generation === 0;
  229. return (
  230. <TreeToggleContainer style={{left: `${left}px`}} hasToggler>
  231. {this.renderConnector(true)}
  232. <TreeToggle
  233. disabled={isRoot}
  234. isExpanded={isExpanded}
  235. errored={errored}
  236. onClick={event => {
  237. event.stopPropagation();
  238. if (isRoot) {
  239. return;
  240. }
  241. toggleExpandedState();
  242. }}
  243. >
  244. <Count value={children.length} />
  245. {!isRoot && (
  246. <div>
  247. <TreeToggleIcon direction={isExpanded ? 'up' : 'down'} />
  248. </div>
  249. )}
  250. </TreeToggle>
  251. </TreeToggleContainer>
  252. );
  253. }
  254. // TODO: Use ScrollbarManager to bring autoscrolling here
  255. renderTitle(_: ScrollbarManager.ScrollbarManagerChildrenProps) {
  256. const {organization, transaction, addContentSpanBarRef, removeContentSpanBarRef} =
  257. this.props;
  258. const left = this.getCurrentOffset();
  259. const errored = isTraceFullDetailed(transaction)
  260. ? transaction.errors.length + transaction.performance_issues.length > 0
  261. : false;
  262. const content = isTraceFullDetailed(transaction) ? (
  263. <Fragment>
  264. <Projects orgId={organization.slug} slugs={[transaction.project_slug]}>
  265. {({projects}) => {
  266. const project = projects.find(p => p.slug === transaction.project_slug);
  267. return (
  268. <ProjectBadgeContainer>
  269. <Tooltip title={transaction.project_slug}>
  270. <ProjectBadge
  271. project={project ? project : {slug: transaction.project_slug}}
  272. avatarSize={16}
  273. hideName
  274. />
  275. </Tooltip>
  276. </ProjectBadgeContainer>
  277. );
  278. }}
  279. </Projects>
  280. <RowTitleContent errored={errored}>
  281. <strong>
  282. {transaction['transaction.op']}
  283. {' \u2014 '}
  284. </strong>
  285. {transaction.transaction}
  286. </RowTitleContent>
  287. </Fragment>
  288. ) : (
  289. <RowTitleContent errored={false}>
  290. <strong>{'Trace \u2014 '}</strong>
  291. {transaction.traceSlug}
  292. </RowTitleContent>
  293. );
  294. return (
  295. <RowTitleContainer
  296. ref={ref => {
  297. if (!ref) {
  298. removeContentSpanBarRef(this.spanContentRef);
  299. return;
  300. }
  301. addContentSpanBarRef(ref);
  302. this.spanContentRef = ref;
  303. }}
  304. >
  305. {this.renderToggle(errored)}
  306. <RowTitle
  307. style={{
  308. left: `${left}px`,
  309. width: '100%',
  310. }}
  311. >
  312. {content}
  313. </RowTitle>
  314. </RowTitleContainer>
  315. );
  316. }
  317. renderDivider(
  318. dividerHandlerChildrenProps: DividerHandlerManager.DividerHandlerManagerChildrenProps
  319. ) {
  320. if (this.state.showDetail) {
  321. // Mock component to preserve layout spacing
  322. return (
  323. <DividerLine
  324. showDetail
  325. style={{
  326. position: 'absolute',
  327. }}
  328. />
  329. );
  330. }
  331. const {addDividerLineRef} = dividerHandlerChildrenProps;
  332. return (
  333. <DividerLine
  334. ref={addDividerLineRef()}
  335. style={{
  336. position: 'absolute',
  337. }}
  338. onMouseEnter={() => {
  339. dividerHandlerChildrenProps.setHover(true);
  340. }}
  341. onMouseLeave={() => {
  342. dividerHandlerChildrenProps.setHover(false);
  343. }}
  344. onMouseOver={() => {
  345. dividerHandlerChildrenProps.setHover(true);
  346. }}
  347. onMouseDown={dividerHandlerChildrenProps.onDragStart}
  348. onClick={event => {
  349. // we prevent the propagation of the clicks from this component to prevent
  350. // the span detail from being opened.
  351. event.stopPropagation();
  352. }}
  353. />
  354. );
  355. }
  356. renderGhostDivider(
  357. dividerHandlerChildrenProps: DividerHandlerManager.DividerHandlerManagerChildrenProps
  358. ) {
  359. const {dividerPosition, addGhostDividerLineRef} = dividerHandlerChildrenProps;
  360. return (
  361. <DividerLineGhostContainer
  362. style={{
  363. width: `calc(${toPercent(dividerPosition)} + 0.5px)`,
  364. display: 'none',
  365. }}
  366. >
  367. <DividerLine
  368. ref={addGhostDividerLineRef()}
  369. style={{
  370. right: 0,
  371. }}
  372. className="hovering"
  373. onClick={event => {
  374. // the ghost divider line should not be interactive.
  375. // we prevent the propagation of the clicks from this component to prevent
  376. // the span detail from being opened.
  377. event.stopPropagation();
  378. }}
  379. />
  380. </DividerLineGhostContainer>
  381. );
  382. }
  383. renderErrorBadge() {
  384. const {transaction} = this.props;
  385. if (
  386. !isTraceFullDetailed(transaction) ||
  387. !(transaction.errors.length + transaction.performance_issues.length)
  388. ) {
  389. return null;
  390. }
  391. return <ErrorBadge />;
  392. }
  393. renderRectangle() {
  394. const {transaction, traceInfo, barColor} = this.props;
  395. const {showDetail} = this.state;
  396. // Use 1 as the difference in the case that startTimestamp === endTimestamp
  397. const delta = Math.abs(traceInfo.endTimestamp - traceInfo.startTimestamp) || 1;
  398. const startPosition = Math.abs(
  399. transaction.start_timestamp - traceInfo.startTimestamp
  400. );
  401. const startPercentage = startPosition / delta;
  402. const duration = Math.abs(transaction.timestamp - transaction.start_timestamp);
  403. const widthPercentage = duration / delta;
  404. return (
  405. <RowRectangle
  406. style={{
  407. backgroundColor: barColor,
  408. left: `min(${toPercent(startPercentage || 0)}, calc(100% - 1px))`,
  409. width: toPercent(widthPercentage || 0),
  410. }}
  411. >
  412. {this.renderPerformanceIssues()}
  413. <DurationPill
  414. durationDisplay={getDurationDisplay({
  415. left: startPercentage,
  416. width: widthPercentage,
  417. })}
  418. showDetail={showDetail}
  419. >
  420. {getHumanDuration(duration)}
  421. </DurationPill>
  422. </RowRectangle>
  423. );
  424. }
  425. renderPerformanceIssues() {
  426. const {transaction, barColor} = this.props;
  427. if (!isTraceFullDetailed(transaction)) {
  428. return null;
  429. }
  430. const rows: React.ReactElement[] = [];
  431. // Use 1 as the difference in the case that startTimestamp === endTimestamp
  432. const delta = Math.abs(transaction.timestamp - transaction.start_timestamp) || 1;
  433. for (let i = 0; i < transaction.performance_issues.length; i++) {
  434. const issue = transaction.performance_issues[i];
  435. const startPosition = Math.abs(issue.start - transaction.start_timestamp);
  436. const startPercentage = startPosition / delta;
  437. const duration = Math.abs(issue.end - issue.start);
  438. const widthPercentage = duration / delta;
  439. rows.push(
  440. <RowRectangle
  441. style={{
  442. backgroundColor: barColor,
  443. left: `min(${toPercent(startPercentage || 0)}, calc(100% - 1px))`,
  444. width: toPercent(widthPercentage || 0),
  445. }}
  446. spanBarType={SpanBarType.AFFECTED}
  447. />
  448. );
  449. }
  450. return rows;
  451. }
  452. renderHeader({
  453. dividerHandlerChildrenProps,
  454. scrollbarManagerChildrenProps,
  455. }: {
  456. dividerHandlerChildrenProps: DividerHandlerManager.DividerHandlerManagerChildrenProps;
  457. scrollbarManagerChildrenProps: ScrollbarManager.ScrollbarManagerChildrenProps;
  458. }) {
  459. const {hasGuideAnchor, index} = this.props;
  460. const {showDetail} = this.state;
  461. const {dividerPosition} = dividerHandlerChildrenProps;
  462. return (
  463. <RowCellContainer showDetail={showDetail}>
  464. <RowCell
  465. data-test-id="transaction-row-title"
  466. data-type="span-row-cell"
  467. style={{
  468. width: `calc(${toPercent(dividerPosition)} - 0.5px)`,
  469. paddingTop: 0,
  470. }}
  471. showDetail={showDetail}
  472. onClick={this.toggleDisplayDetail}
  473. ref={this.transactionTitleRef}
  474. >
  475. <GuideAnchor target="trace_view_guide_row" disabled={!hasGuideAnchor}>
  476. {this.renderTitle(scrollbarManagerChildrenProps)}
  477. </GuideAnchor>
  478. </RowCell>
  479. <DividerContainer>
  480. {this.renderDivider(dividerHandlerChildrenProps)}
  481. {this.renderErrorBadge()}
  482. </DividerContainer>
  483. <RowCell
  484. data-test-id="transaction-row-duration"
  485. data-type="span-row-cell"
  486. showStriping={index % 2 !== 0}
  487. style={{
  488. width: `calc(${toPercent(1 - dividerPosition)} - 0.5px)`,
  489. paddingTop: 0,
  490. }}
  491. showDetail={showDetail}
  492. onClick={this.toggleDisplayDetail}
  493. >
  494. <RowReplayTimeIndicators />
  495. <GuideAnchor target="trace_view_guide_row_details" disabled={!hasGuideAnchor}>
  496. {this.renderRectangle()}
  497. {this.renderMeasurements()}
  498. </GuideAnchor>
  499. </RowCell>
  500. {!showDetail && this.renderGhostDivider(dividerHandlerChildrenProps)}
  501. </RowCellContainer>
  502. );
  503. }
  504. scrollIntoView = () => {
  505. const element = this.transactionRowDOMRef.current;
  506. if (!element) {
  507. return;
  508. }
  509. const boundingRect = element.getBoundingClientRect();
  510. const offset = boundingRect.top + window.scrollY;
  511. this.setState({showDetail: true}, () => window.scrollTo(0, offset));
  512. };
  513. renderDetail() {
  514. const {location, organization, isVisible, transaction} = this.props;
  515. const {showDetail} = this.state;
  516. if (!isTraceFullDetailed(transaction)) {
  517. return null;
  518. }
  519. if (!isVisible || !showDetail) {
  520. return null;
  521. }
  522. return (
  523. <TransactionDetail
  524. location={location}
  525. organization={organization}
  526. transaction={transaction}
  527. scrollIntoView={this.scrollIntoView}
  528. />
  529. );
  530. }
  531. render() {
  532. const {isVisible, transaction} = this.props;
  533. const {showDetail} = this.state;
  534. return (
  535. <Row
  536. ref={this.transactionRowDOMRef}
  537. visible={isVisible}
  538. showBorder={showDetail}
  539. cursor={isTraceFullDetailed(transaction) ? 'pointer' : 'default'}
  540. >
  541. <ScrollbarManager.Consumer>
  542. {scrollbarManagerChildrenProps => (
  543. <DividerHandlerManager.Consumer>
  544. {dividerHandlerChildrenProps =>
  545. this.renderHeader({
  546. dividerHandlerChildrenProps,
  547. scrollbarManagerChildrenProps,
  548. })
  549. }
  550. </DividerHandlerManager.Consumer>
  551. )}
  552. </ScrollbarManager.Consumer>
  553. {this.renderDetail()}
  554. </Row>
  555. );
  556. }
  557. }
  558. function getOffset(generation) {
  559. return generation * (TOGGLE_BORDER_BOX / 2) + MARGIN_LEFT;
  560. }
  561. export default TransactionBar;