123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- import {Component, createRef, Fragment} from 'react';
- import {Location} from 'history';
- import GuideAnchor from 'sentry/components/assistant/guideAnchor';
- import Count from 'sentry/components/count';
- import * as AnchorLinkManager from 'sentry/components/events/interfaces/spans/anchorLinkManager';
- import * as DividerHandlerManager from 'sentry/components/events/interfaces/spans/dividerHandlerManager';
- import * as ScrollbarManager from 'sentry/components/events/interfaces/spans/scrollbarManager';
- import ProjectBadge from 'sentry/components/idBadge/projectBadge';
- import {ROW_HEIGHT} from 'sentry/components/performance/waterfall/constants';
- import {
- Row,
- RowCell,
- RowCellContainer,
- } from 'sentry/components/performance/waterfall/row';
- import {DurationPill, RowRectangle} from 'sentry/components/performance/waterfall/rowBar';
- import {
- DividerContainer,
- DividerLine,
- DividerLineGhostContainer,
- ErrorBadge,
- } from 'sentry/components/performance/waterfall/rowDivider';
- import {
- RowTitle,
- RowTitleContainer,
- RowTitleContent,
- } from 'sentry/components/performance/waterfall/rowTitle';
- import {
- ConnectorBar,
- TOGGLE_BORDER_BOX,
- TreeConnector,
- TreeToggle,
- TreeToggleContainer,
- TreeToggleIcon,
- } from 'sentry/components/performance/waterfall/treeConnector';
- import {
- getDurationDisplay,
- getHumanDuration,
- toPercent,
- } from 'sentry/components/performance/waterfall/utils';
- import Tooltip from 'sentry/components/tooltip';
- import {Organization} from 'sentry/types';
- import {TraceFullDetailed} from 'sentry/utils/performance/quickTrace/types';
- import {isTraceFullDetailed} from 'sentry/utils/performance/quickTrace/utils';
- import Projects from 'sentry/utils/projects';
- import {ProjectBadgeContainer} from './styles';
- import TransactionDetail from './transactionDetail';
- import {TraceInfo, TraceRoot, TreeDepth} from './types';
- const MARGIN_LEFT = 0;
- type Props = {
- continuingDepths: TreeDepth[];
- hasGuideAnchor: boolean;
- index: number;
- isExpanded: boolean;
- isLast: boolean;
- isOrphan: boolean;
- isVisible: boolean;
- location: Location;
- organization: Organization;
- toggleExpandedState: () => void;
- traceInfo: TraceInfo;
- transaction: TraceRoot | TraceFullDetailed;
- barColor?: string;
- };
- type State = {
- showDetail: boolean;
- };
- class TransactionBar extends Component<Props, State> {
- state: State = {
- showDetail: false,
- };
- transactionRowDOMRef = createRef<HTMLDivElement>();
- toggleDisplayDetail = () => {
- const {transaction} = this.props;
- if (isTraceFullDetailed(transaction)) {
- this.setState(state => ({
- showDetail: !state.showDetail,
- }));
- }
- };
- getCurrentOffset() {
- const {transaction} = this.props;
- const {generation} = transaction;
- return getOffset(generation);
- }
- renderConnector(hasToggle: boolean) {
- const {continuingDepths, isExpanded, isOrphan, isLast, transaction} = this.props;
- const {generation} = transaction;
- const eventId = isTraceFullDetailed(transaction)
- ? transaction.event_id
- : transaction.traceSlug;
- if (generation === 0) {
- if (hasToggle) {
- return (
- <ConnectorBar
- style={{right: '15px', height: '10px', bottom: '-5px', top: 'auto'}}
- orphanBranch={false}
- />
- );
- }
- return null;
- }
- const connectorBars: Array<React.ReactNode> = continuingDepths.map(
- ({depth, isOrphanDepth}) => {
- if (generation - depth <= 1) {
- // If the difference is less than or equal to 1, then it means that the continued
- // bar is from its direct parent. In this case, do not render a connector bar
- // because the tree connector below will suffice.
- return null;
- }
- const left = -1 * getOffset(generation - depth - 1) - 2;
- return (
- <ConnectorBar
- style={{left}}
- key={`${eventId}-${depth}`}
- orphanBranch={isOrphanDepth}
- />
- );
- }
- );
- if (hasToggle && isExpanded) {
- connectorBars.push(
- <ConnectorBar
- style={{
- right: '15px',
- height: '10px',
- bottom: isLast ? `-${ROW_HEIGHT / 2 + 1}px` : '0',
- top: 'auto',
- }}
- key={`${eventId}-last`}
- orphanBranch={false}
- />
- );
- }
- return (
- <TreeConnector isLast={isLast} hasToggler={hasToggle} orphanBranch={isOrphan}>
- {connectorBars}
- </TreeConnector>
- );
- }
- renderToggle(errored: boolean) {
- const {isExpanded, transaction, toggleExpandedState} = this.props;
- const {children, generation} = transaction;
- const left = this.getCurrentOffset();
- if (children.length <= 0) {
- return (
- <TreeToggleContainer style={{left: `${left}px`}}>
- {this.renderConnector(false)}
- </TreeToggleContainer>
- );
- }
- const isRoot = generation === 0;
- return (
- <TreeToggleContainer style={{left: `${left}px`}} hasToggler>
- {this.renderConnector(true)}
- <TreeToggle
- disabled={isRoot}
- isExpanded={isExpanded}
- errored={errored}
- onClick={event => {
- event.stopPropagation();
- if (isRoot) {
- return;
- }
- toggleExpandedState();
- }}
- >
- <Count value={children.length} />
- {!isRoot && (
- <div>
- <TreeToggleIcon direction={isExpanded ? 'up' : 'down'} />
- </div>
- )}
- </TreeToggle>
- </TreeToggleContainer>
- );
- }
- renderTitle(
- scrollbarManagerChildrenProps: ScrollbarManager.ScrollbarManagerChildrenProps
- ) {
- const {generateContentSpanBarRef} = scrollbarManagerChildrenProps;
- const {organization, transaction} = this.props;
- const left = this.getCurrentOffset();
- const errored = isTraceFullDetailed(transaction)
- ? transaction.errors.length > 0
- : false;
- const content = isTraceFullDetailed(transaction) ? (
- <Fragment>
- <Projects orgId={organization.slug} slugs={[transaction.project_slug]}>
- {({projects}) => {
- const project = projects.find(p => p.slug === transaction.project_slug);
- return (
- <ProjectBadgeContainer>
- <Tooltip title={transaction.project_slug}>
- <ProjectBadge
- project={project ? project : {slug: transaction.project_slug}}
- avatarSize={16}
- hideName
- />
- </Tooltip>
- </ProjectBadgeContainer>
- );
- }}
- </Projects>
- <RowTitleContent errored={errored}>
- <strong>
- {transaction['transaction.op']}
- {' \u2014 '}
- </strong>
- {transaction.transaction}
- </RowTitleContent>
- </Fragment>
- ) : (
- <RowTitleContent errored={false}>
- <strong>{'Trace \u2014 '}</strong>
- {transaction.traceSlug}
- </RowTitleContent>
- );
- return (
- <RowTitleContainer ref={generateContentSpanBarRef()}>
- {this.renderToggle(errored)}
- <RowTitle
- style={{
- left: `${left}px`,
- width: '100%',
- }}
- >
- {content}
- </RowTitle>
- </RowTitleContainer>
- );
- }
- renderDivider(
- dividerHandlerChildrenProps: DividerHandlerManager.DividerHandlerManagerChildrenProps
- ) {
- if (this.state.showDetail) {
- // Mock component to preserve layout spacing
- return (
- <DividerLine
- showDetail
- style={{
- position: 'absolute',
- }}
- />
- );
- }
- const {addDividerLineRef} = dividerHandlerChildrenProps;
- return (
- <DividerLine
- ref={addDividerLineRef()}
- style={{
- position: 'absolute',
- }}
- onMouseEnter={() => {
- dividerHandlerChildrenProps.setHover(true);
- }}
- onMouseLeave={() => {
- dividerHandlerChildrenProps.setHover(false);
- }}
- onMouseOver={() => {
- dividerHandlerChildrenProps.setHover(true);
- }}
- onMouseDown={dividerHandlerChildrenProps.onDragStart}
- onClick={event => {
- // we prevent the propagation of the clicks from this component to prevent
- // the span detail from being opened.
- event.stopPropagation();
- }}
- />
- );
- }
- renderGhostDivider(
- dividerHandlerChildrenProps: DividerHandlerManager.DividerHandlerManagerChildrenProps
- ) {
- const {dividerPosition, addGhostDividerLineRef} = dividerHandlerChildrenProps;
- return (
- <DividerLineGhostContainer
- style={{
- width: `calc(${toPercent(dividerPosition)} + 0.5px)`,
- display: 'none',
- }}
- >
- <DividerLine
- ref={addGhostDividerLineRef()}
- style={{
- right: 0,
- }}
- className="hovering"
- onClick={event => {
- // the ghost divider line should not be interactive.
- // we prevent the propagation of the clicks from this component to prevent
- // the span detail from being opened.
- event.stopPropagation();
- }}
- />
- </DividerLineGhostContainer>
- );
- }
- renderErrorBadge() {
- const {transaction} = this.props;
- if (!isTraceFullDetailed(transaction) || !transaction.errors.length) {
- return null;
- }
- return <ErrorBadge />;
- }
- renderRectangle() {
- const {transaction, traceInfo, barColor} = this.props;
- const {showDetail} = this.state;
- // Use 1 as the difference in the event that startTimestamp === endTimestamp
- const delta = Math.abs(traceInfo.endTimestamp - traceInfo.startTimestamp) || 1;
- const startPosition = Math.abs(
- transaction.start_timestamp - traceInfo.startTimestamp
- );
- const startPercentage = startPosition / delta;
- const duration = Math.abs(transaction.timestamp - transaction.start_timestamp);
- const widthPercentage = duration / delta;
- return (
- <RowRectangle
- style={{
- backgroundColor: barColor,
- left: `min(${toPercent(startPercentage || 0)}, calc(100% - 1px))`,
- width: toPercent(widthPercentage || 0),
- }}
- >
- <DurationPill
- durationDisplay={getDurationDisplay({
- left: startPercentage,
- width: widthPercentage,
- })}
- showDetail={showDetail}
- >
- {getHumanDuration(duration)}
- </DurationPill>
- </RowRectangle>
- );
- }
- renderHeader({
- dividerHandlerChildrenProps,
- scrollbarManagerChildrenProps,
- }: {
- dividerHandlerChildrenProps: DividerHandlerManager.DividerHandlerManagerChildrenProps;
- scrollbarManagerChildrenProps: ScrollbarManager.ScrollbarManagerChildrenProps;
- }) {
- const {hasGuideAnchor, index} = this.props;
- const {showDetail} = this.state;
- const {dividerPosition} = dividerHandlerChildrenProps;
- return (
- <RowCellContainer showDetail={showDetail}>
- <RowCell
- data-test-id="transaction-row-title"
- data-type="span-row-cell"
- style={{
- width: `calc(${toPercent(dividerPosition)} - 0.5px)`,
- paddingTop: 0,
- }}
- showDetail={showDetail}
- onClick={this.toggleDisplayDetail}
- >
- <GuideAnchor target="trace_view_guide_row" disabled={!hasGuideAnchor}>
- {this.renderTitle(scrollbarManagerChildrenProps)}
- </GuideAnchor>
- </RowCell>
- <DividerContainer>
- {this.renderDivider(dividerHandlerChildrenProps)}
- {this.renderErrorBadge()}
- </DividerContainer>
- <RowCell
- data-test-id="transaction-row-duration"
- data-type="span-row-cell"
- showStriping={index % 2 !== 0}
- style={{
- width: `calc(${toPercent(1 - dividerPosition)} - 0.5px)`,
- paddingTop: 0,
- }}
- showDetail={showDetail}
- onClick={this.toggleDisplayDetail}
- >
- <GuideAnchor target="trace_view_guide_row_details" disabled={!hasGuideAnchor}>
- {this.renderRectangle()}
- </GuideAnchor>
- </RowCell>
- {!showDetail && this.renderGhostDivider(dividerHandlerChildrenProps)}
- </RowCellContainer>
- );
- }
- scrollIntoView = () => {
- const element = this.transactionRowDOMRef.current;
- if (!element) {
- return;
- }
- const boundingRect = element.getBoundingClientRect();
- const offset = boundingRect.top + window.scrollY;
- this.setState({showDetail: true}, () => window.scrollTo(0, offset));
- };
- renderDetail() {
- const {location, organization, isVisible, transaction} = this.props;
- const {showDetail} = this.state;
- return (
- <AnchorLinkManager.Consumer>
- {({registerScrollFn, scrollToHash}) => {
- if (!isTraceFullDetailed(transaction)) {
- return null;
- }
- registerScrollFn(`#txn-${transaction.event_id}`, this.scrollIntoView, false);
- if (!isVisible || !showDetail) {
- return null;
- }
- return (
- <TransactionDetail
- location={location}
- organization={organization}
- transaction={transaction}
- scrollToHash={scrollToHash}
- />
- );
- }}
- </AnchorLinkManager.Consumer>
- );
- }
- render() {
- const {isVisible, transaction} = this.props;
- const {showDetail} = this.state;
- return (
- <Row
- ref={this.transactionRowDOMRef}
- visible={isVisible}
- showBorder={showDetail}
- cursor={isTraceFullDetailed(transaction) ? 'pointer' : 'default'}
- >
- <ScrollbarManager.Consumer>
- {scrollbarManagerChildrenProps => (
- <DividerHandlerManager.Consumer>
- {dividerHandlerChildrenProps =>
- this.renderHeader({
- dividerHandlerChildrenProps,
- scrollbarManagerChildrenProps,
- })
- }
- </DividerHandlerManager.Consumer>
- )}
- </ScrollbarManager.Consumer>
- {this.renderDetail()}
- </Row>
- );
- }
- }
- function getOffset(generation) {
- return generation * (TOGGLE_BORDER_BOX / 2) + MARGIN_LEFT;
- }
- export default TransactionBar;
|