transactionBar.tsx 15 KB

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