styles.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import {Fragment, type PropsWithChildren, useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import * as qs from 'query-string';
  4. import {Button as CommonButton, LinkButton} from 'sentry/components/button';
  5. import {DataSection} from 'sentry/components/events/styles';
  6. import {Tooltip} from 'sentry/components/tooltip';
  7. import {t, tct} from 'sentry/locale';
  8. import {space} from 'sentry/styles/space';
  9. import type {Organization} from 'sentry/types/organization';
  10. import {trackAnalytics} from 'sentry/utils/analytics';
  11. import {getDuration} from 'sentry/utils/formatters';
  12. import type {ColorOrAlias} from 'sentry/utils/theme';
  13. import {
  14. isAutogroupedNode,
  15. isSpanNode,
  16. } from 'sentry/views/performance/newTraceDetails/guards';
  17. import type {
  18. TraceTree,
  19. TraceTreeNode,
  20. } from 'sentry/views/performance/newTraceDetails/traceModels/traceTree';
  21. const DetailContainer = styled('div')`
  22. display: flex;
  23. flex-direction: column;
  24. gap: ${space(2)};
  25. padding: ${space(1)};
  26. ${DataSection} {
  27. padding: 0;
  28. }
  29. `;
  30. const FlexBox = styled('div')`
  31. display: flex;
  32. align-items: center;
  33. `;
  34. const Actions = styled(FlexBox)`
  35. gap: ${space(0.5)};
  36. flex-wrap: wrap;
  37. justify-content: end;
  38. `;
  39. const Title = styled(FlexBox)`
  40. gap: ${space(1)};
  41. flex: none;
  42. width: 50%;
  43. `;
  44. const TitleText = styled('div')`
  45. ${p => p.theme.overflowEllipsis}
  46. `;
  47. function TitleWithTestId(props: PropsWithChildren<{}>) {
  48. return <Title data-test-id="trace-drawer-title">{props.children}</Title>;
  49. }
  50. const Type = styled('div')`
  51. font-size: ${p => p.theme.fontSizeSmall};
  52. `;
  53. const TitleOp = styled('div')`
  54. font-size: 15px;
  55. font-weight: bold;
  56. ${p => p.theme.overflowEllipsis}
  57. `;
  58. const Table = styled('table')`
  59. margin-bottom: 0 !important;
  60. td {
  61. overflow: hidden;
  62. }
  63. `;
  64. const IconTitleWrapper = styled(FlexBox)`
  65. gap: ${space(1)};
  66. `;
  67. const IconBorder = styled('div')<{backgroundColor: string; errored?: boolean}>`
  68. background-color: ${p => p.backgroundColor};
  69. border-radius: ${p => p.theme.borderRadius};
  70. padding: 0;
  71. display: flex;
  72. align-items: center;
  73. justify-content: center;
  74. width: 30px;
  75. height: 30px;
  76. svg {
  77. fill: ${p => p.theme.white};
  78. width: 14px;
  79. height: 14px;
  80. }
  81. `;
  82. const Button = styled(CommonButton)`
  83. position: absolute;
  84. top: ${space(0.75)};
  85. right: ${space(0.5)};
  86. `;
  87. const HeaderContainer = styled(Title)`
  88. justify-content: space-between;
  89. overflow: hidden;
  90. width: 100%;
  91. `;
  92. interface EventDetailsLinkProps {
  93. node: TraceTreeNode<TraceTree.NodeValue>;
  94. organization: Organization;
  95. }
  96. function EventDetailsLink(props: EventDetailsLinkProps) {
  97. const params = useMemo((): {
  98. eventId: string | undefined;
  99. projectSlug: string | undefined;
  100. } => {
  101. const eventId = props.node.metadata.event_id;
  102. const projectSlug = props.node.metadata.project_slug;
  103. if (eventId && projectSlug) {
  104. return {eventId, projectSlug};
  105. }
  106. if (isSpanNode(props.node) || isAutogroupedNode(props.node)) {
  107. const parent = props.node.parent_transaction;
  108. if (parent?.metadata.event_id && parent?.metadata.project_slug) {
  109. return {
  110. eventId: parent.metadata.event_id,
  111. projectSlug: parent.metadata.project_slug,
  112. };
  113. }
  114. }
  115. return {eventId: undefined, projectSlug: undefined};
  116. }, [props.node]);
  117. const locationDescriptor = useMemo(() => {
  118. const query = {...qs.parse(location.search), legacy: 1};
  119. return {
  120. query: query,
  121. pathname: `/performance/${params.projectSlug}:${params.eventId}/`,
  122. hash: isSpanNode(props.node) ? `#span-${props.node.value.span_id}` : undefined,
  123. };
  124. }, [params.eventId, params.projectSlug, props.node]);
  125. return (
  126. <LinkButton
  127. disabled={!params.eventId || !params.projectSlug}
  128. title={
  129. !params.eventId || !params.projectSlug
  130. ? t('Event ID or Project Slug missing')
  131. : undefined
  132. }
  133. size="xs"
  134. to={locationDescriptor}
  135. onClick={() => {
  136. trackAnalytics('performance_views.trace_details.view_event_details', {
  137. organization: props.organization,
  138. });
  139. }}
  140. >
  141. {t('View Event Details')}
  142. </LinkButton>
  143. );
  144. }
  145. const DURATION_COMPARISON_STATUS_COLORS: {
  146. equal: {light: ColorOrAlias; normal: ColorOrAlias};
  147. faster: {light: ColorOrAlias; normal: ColorOrAlias};
  148. slower: {light: ColorOrAlias; normal: ColorOrAlias};
  149. } = {
  150. faster: {
  151. light: 'green100',
  152. normal: 'green300',
  153. },
  154. slower: {
  155. light: 'red100',
  156. normal: 'red300',
  157. },
  158. equal: {
  159. light: 'gray100',
  160. normal: 'gray300',
  161. },
  162. };
  163. const MIN_PCT_DURATION_DIFFERENCE = 10;
  164. type DurationProps = {
  165. baseline: number | undefined;
  166. duration: number;
  167. baseDescription?: string;
  168. ratio?: number;
  169. };
  170. function Duration(props: DurationProps) {
  171. if (typeof props.duration !== 'number' || Number.isNaN(props.duration)) {
  172. return <DurationContainer>{t('unknown')}</DurationContainer>;
  173. }
  174. if (props.baseline === undefined || props.baseline === 0) {
  175. return <DurationContainer>{getDuration(props.duration, 2, true)}</DurationContainer>;
  176. }
  177. const delta = props.duration - props.baseline;
  178. const deltaPct = Math.round(Math.abs((delta / props.baseline) * 100));
  179. const status = delta > 0 ? 'slower' : delta < 0 ? 'faster' : 'equal';
  180. const formattedBaseDuration = (
  181. <Tooltip
  182. title={props.baseDescription}
  183. showUnderline
  184. underlineColor={DURATION_COMPARISON_STATUS_COLORS[status].normal}
  185. >
  186. {getDuration(props.baseline, 2, true)}
  187. </Tooltip>
  188. );
  189. const deltaText =
  190. status === 'equal'
  191. ? tct(`equal to the avg of [formattedBaseDuration]`, {
  192. formattedBaseDuration,
  193. })
  194. : status === 'faster'
  195. ? tct(`[deltaPct] faster than the avg of [formattedBaseDuration]`, {
  196. formattedBaseDuration,
  197. deltaPct: `${deltaPct}%`,
  198. })
  199. : tct(`[deltaPct] slower than the avg of [formattedBaseDuration]`, {
  200. formattedBaseDuration,
  201. deltaPct: `${deltaPct}%`,
  202. });
  203. return (
  204. <Fragment>
  205. <DurationContainer>
  206. {getDuration(props.duration, 2, true)}{' '}
  207. {props.ratio ? `(${(props.ratio * 100).toFixed()}%)` : null}
  208. </DurationContainer>
  209. {deltaPct >= MIN_PCT_DURATION_DIFFERENCE ? (
  210. <Comparison status={status}>{deltaText}</Comparison>
  211. ) : null}
  212. </Fragment>
  213. );
  214. }
  215. const DurationContainer = styled('span')`
  216. font-weight: bold;
  217. margin-right: ${space(1)};
  218. `;
  219. const Comparison = styled('span')<{status: 'faster' | 'slower' | 'equal'}>`
  220. color: ${p => p.theme[DURATION_COMPARISON_STATUS_COLORS[p.status].normal]};
  221. `;
  222. const TraceDrawerComponents = {
  223. DetailContainer,
  224. FlexBox,
  225. Title: TitleWithTestId,
  226. Type,
  227. TitleOp,
  228. HeaderContainer,
  229. Actions,
  230. Table,
  231. IconTitleWrapper,
  232. IconBorder,
  233. EventDetailsLink,
  234. Button,
  235. TitleText,
  236. Duration,
  237. };
  238. export {TraceDrawerComponents};