spanDetailsTable.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Location} from 'history';
  4. import GridEditable, {
  5. COL_WIDTH_UNDEFINED,
  6. GridColumnOrder,
  7. } from 'sentry/components/gridEditable';
  8. import SortLink from 'sentry/components/gridEditable/sortLink';
  9. import Link from 'sentry/components/links/link';
  10. import Pagination from 'sentry/components/pagination';
  11. import {DurationPill, RowRectangle} from 'sentry/components/performance/waterfall/rowBar';
  12. import {pickBarColor, toPercent} from 'sentry/components/performance/waterfall/utils';
  13. import PerformanceDuration from 'sentry/components/performanceDuration';
  14. import {Tooltip} from 'sentry/components/tooltip';
  15. import {t, tct} from 'sentry/locale';
  16. import {space} from 'sentry/styles/space';
  17. import {Organization, Project} from 'sentry/types';
  18. import {defined} from 'sentry/utils';
  19. import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  20. import {ColumnType, fieldAlignment} from 'sentry/utils/discover/fields';
  21. import {formatPercentage} from 'sentry/utils/formatters';
  22. import {
  23. ExampleTransaction,
  24. SuspectSpan,
  25. } from 'sentry/utils/performance/suspectSpans/types';
  26. import {VisuallyCompleteWithData} from 'sentry/utils/performanceForSentry';
  27. import {generateTransactionLink} from '../../utils';
  28. type TableColumnKeys =
  29. | 'id'
  30. | 'timestamp'
  31. | 'transactionDuration'
  32. | 'spanDuration'
  33. | 'occurrences'
  34. | 'cumulativeDuration'
  35. | 'spans';
  36. type TableColumn = GridColumnOrder<TableColumnKeys>;
  37. type TableDataRow = Record<TableColumnKeys, any>;
  38. type Props = {
  39. examples: ExampleTransaction[];
  40. isLoading: boolean;
  41. location: Location;
  42. organization: Organization;
  43. transactionName: string;
  44. pageLinks?: string | null;
  45. project?: Project;
  46. suspectSpan?: SuspectSpan;
  47. };
  48. export default function SpanTable(props: Props) {
  49. const {
  50. location,
  51. organization,
  52. project,
  53. examples,
  54. suspectSpan,
  55. transactionName,
  56. isLoading,
  57. pageLinks,
  58. } = props;
  59. if (!defined(examples)) {
  60. return null;
  61. }
  62. const data = examples
  63. // we assume that the span appears in each example at least once,
  64. // if this assumption is broken, nothing onwards will work so
  65. // filter out such examples
  66. .filter(example => example.spans.length > 0)
  67. .map(example => ({
  68. id: example.id,
  69. project: project?.slug,
  70. // timestamps are in seconds but want them in milliseconds
  71. timestamp: example.finishTimestamp * 1000,
  72. transactionDuration: (example.finishTimestamp - example.startTimestamp) * 1000,
  73. spanDuration: example.nonOverlappingExclusiveTime,
  74. occurrences: example.spans.length,
  75. cumulativeDuration: example.spans.reduce(
  76. (duration, span) => duration + span.exclusiveTime,
  77. 0
  78. ),
  79. spans: example.spans,
  80. }));
  81. return (
  82. <Fragment>
  83. <VisuallyCompleteWithData id="SpanDetails-SpanDetailsTable" hasData={!!data.length}>
  84. <GridEditable
  85. isLoading={isLoading}
  86. data={data}
  87. columnOrder={SPANS_TABLE_COLUMN_ORDER}
  88. columnSortBy={[]}
  89. grid={{
  90. renderHeadCell,
  91. renderBodyCell: renderBodyCellWithMeta(
  92. location,
  93. organization,
  94. transactionName,
  95. suspectSpan
  96. ),
  97. }}
  98. location={location}
  99. />
  100. </VisuallyCompleteWithData>
  101. <Pagination pageLinks={pageLinks ?? null} />
  102. </Fragment>
  103. );
  104. }
  105. function renderHeadCell(column: TableColumn, _index: number): React.ReactNode {
  106. const align = fieldAlignment(column.key, COLUMN_TYPE[column.key]);
  107. return (
  108. <SortLink
  109. title={column.name}
  110. align={align}
  111. direction={undefined}
  112. canSort={false}
  113. generateSortLink={() => undefined}
  114. />
  115. );
  116. }
  117. function renderBodyCellWithMeta(
  118. location: Location,
  119. organization: Organization,
  120. transactionName: string,
  121. suspectSpan?: SuspectSpan
  122. ) {
  123. return (column: TableColumn, dataRow: TableDataRow): React.ReactNode => {
  124. // if the transaction duration is falsey, then just render the span duration on its own
  125. if (column.key === 'spanDuration' && dataRow.transactionDuration) {
  126. return (
  127. <SpanDurationBar
  128. spanOp={suspectSpan?.op ?? ''}
  129. spanDuration={dataRow.spanDuration}
  130. transactionDuration={dataRow.transactionDuration}
  131. />
  132. );
  133. }
  134. const fieldRenderer = getFieldRenderer(column.key, COLUMN_TYPE);
  135. let rendered = fieldRenderer(dataRow, {location, organization});
  136. if (column.key === 'id') {
  137. const worstSpan = dataRow.spans.length
  138. ? dataRow.spans.reduce((worst, span) =>
  139. worst.exclusiveTime >= span.exclusiveTime ? worst : span
  140. )
  141. : null;
  142. const target = generateTransactionLink(transactionName)(
  143. organization,
  144. dataRow,
  145. location.query,
  146. worstSpan.id
  147. );
  148. rendered = <Link to={target}>{rendered}</Link>;
  149. }
  150. return rendered;
  151. };
  152. }
  153. const COLUMN_TYPE: Omit<
  154. Record<TableColumnKeys, ColumnType>,
  155. 'spans' | 'transactionDuration'
  156. > = {
  157. id: 'string',
  158. timestamp: 'date',
  159. spanDuration: 'duration',
  160. occurrences: 'integer',
  161. cumulativeDuration: 'duration',
  162. };
  163. const SPANS_TABLE_COLUMN_ORDER: TableColumn[] = [
  164. {
  165. key: 'id',
  166. name: t('Event ID'),
  167. width: COL_WIDTH_UNDEFINED,
  168. },
  169. {
  170. key: 'timestamp',
  171. name: t('Timestamp'),
  172. width: COL_WIDTH_UNDEFINED,
  173. },
  174. {
  175. key: 'spanDuration',
  176. name: t('Span Duration'),
  177. width: COL_WIDTH_UNDEFINED,
  178. },
  179. {
  180. key: 'occurrences',
  181. name: t('Count'),
  182. width: COL_WIDTH_UNDEFINED,
  183. },
  184. {
  185. key: 'cumulativeDuration',
  186. name: t('Cumulative Duration'),
  187. width: COL_WIDTH_UNDEFINED,
  188. },
  189. ];
  190. const DurationBar = styled('div')`
  191. position: relative;
  192. display: flex;
  193. top: ${space(0.5)};
  194. background-color: ${p => p.theme.gray100};
  195. `;
  196. const DurationBarSection = styled(RowRectangle)`
  197. position: relative;
  198. width: 100%;
  199. top: 0;
  200. `;
  201. type SpanDurationBarProps = {
  202. spanDuration: number;
  203. spanOp: string;
  204. transactionDuration: number;
  205. };
  206. function SpanDurationBar(props: SpanDurationBarProps) {
  207. const {spanOp, spanDuration, transactionDuration} = props;
  208. const widthPercentage = spanDuration / transactionDuration;
  209. const position = widthPercentage < 0.7 ? 'right' : 'inset';
  210. return (
  211. <DurationBar>
  212. <div style={{width: toPercent(widthPercentage)}}>
  213. <Tooltip
  214. title={tct('[percentage] of the transaction', {
  215. percentage: formatPercentage(widthPercentage),
  216. })}
  217. containerDisplayMode="block"
  218. >
  219. <DurationBarSection style={{backgroundColor: pickBarColor(spanOp)}}>
  220. <DurationPill durationDisplay={position} showDetail={false}>
  221. <PerformanceDuration abbreviation milliseconds={spanDuration} />
  222. </DurationPill>
  223. </DurationBarSection>
  224. </Tooltip>
  225. </div>
  226. </DurationBar>
  227. );
  228. }