spanSummaryTable.tsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. import {Fragment} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import type {Location} from 'history';
  5. import type {GridColumnHeader} from 'sentry/components/gridEditable';
  6. import GridEditable, {COL_WIDTH_UNDEFINED} from 'sentry/components/gridEditable';
  7. import Pagination, {type CursorHandler} from 'sentry/components/pagination';
  8. import {ROW_HEIGHT, ROW_PADDING} from 'sentry/components/performance/waterfall/constants';
  9. import PerformanceDuration from 'sentry/components/performanceDuration';
  10. import {Tooltip} from 'sentry/components/tooltip';
  11. import {t} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import type {Organization} from 'sentry/types/organization';
  14. import type {Project} from 'sentry/types/project';
  15. import {defined} from 'sentry/utils';
  16. import EventView, {type MetaType} from 'sentry/utils/discover/eventView';
  17. import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  18. import type {ColumnType} from 'sentry/utils/discover/fields';
  19. import {
  20. type DiscoverQueryProps,
  21. useGenericDiscoverQuery,
  22. } from 'sentry/utils/discover/genericDiscoverQuery';
  23. import {VisuallyCompleteWithData} from 'sentry/utils/performanceForSentry';
  24. import {decodeScalar} from 'sentry/utils/queryString';
  25. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  26. import {useLocation} from 'sentry/utils/useLocation';
  27. import useOrganization from 'sentry/utils/useOrganization';
  28. import {useParams} from 'sentry/utils/useParams';
  29. import {SpanDurationBar} from 'sentry/views/performance/transactionSummary/transactionSpans/spanDetails/spanDetailsTable';
  30. import {SpanSummaryReferrer} from 'sentry/views/performance/transactionSummary/transactionSpans/spanSummary/referrers';
  31. import {useSpanSummarySort} from 'sentry/views/performance/transactionSummary/transactionSpans/spanSummary/useSpanSummarySort';
  32. import {renderHeadCell} from 'sentry/views/starfish/components/tableCells/renderHeadCell';
  33. import {SpanIdCell} from 'sentry/views/starfish/components/tableCells/spanIdCell';
  34. import {useSpansIndexed} from 'sentry/views/starfish/queries/useDiscover';
  35. import {
  36. ModuleName,
  37. SpanIndexedField,
  38. type SpanIndexedResponse,
  39. type SpanMetricsQueryFilters,
  40. } from 'sentry/views/starfish/types';
  41. import {QueryParameterNames} from 'sentry/views/starfish/views/queryParameters';
  42. type DataRowKeys =
  43. | SpanIndexedField.ID
  44. | SpanIndexedField.TIMESTAMP
  45. | SpanIndexedField.SPAN_DURATION
  46. | SpanIndexedField.TRANSACTION_ID
  47. | SpanIndexedField.TRACE
  48. | SpanIndexedField.PROJECT;
  49. type ColumnKeys =
  50. | SpanIndexedField.ID
  51. | SpanIndexedField.TIMESTAMP
  52. | SpanIndexedField.SPAN_DURATION;
  53. type DataRow = Pick<SpanIndexedResponse, DataRowKeys> & {'transaction.duration': number};
  54. type Column = GridColumnHeader<ColumnKeys>;
  55. const COLUMN_ORDER: Column[] = [
  56. {
  57. key: SpanIndexedField.ID,
  58. name: t('Span ID'),
  59. width: COL_WIDTH_UNDEFINED,
  60. },
  61. {
  62. key: SpanIndexedField.TIMESTAMP,
  63. name: t('Timestamp'),
  64. width: COL_WIDTH_UNDEFINED,
  65. },
  66. {
  67. key: SpanIndexedField.SPAN_DURATION,
  68. name: t('Span Duration'),
  69. width: COL_WIDTH_UNDEFINED,
  70. },
  71. ];
  72. const COLUMN_TYPE: Omit<
  73. Record<ColumnKeys, ColumnType>,
  74. 'spans' | 'transactionDuration'
  75. > = {
  76. span_id: 'string',
  77. timestamp: 'date',
  78. 'span.duration': 'duration',
  79. };
  80. const LIMIT = 8;
  81. type Props = {
  82. project: Project | undefined;
  83. };
  84. export default function SpanSummaryTable(props: Props) {
  85. const {project} = props;
  86. const organization = useOrganization();
  87. const {spanSlug} = useParams();
  88. const [spanOp, groupId] = spanSlug.split(':');
  89. const location = useLocation();
  90. const {transaction} = location.query;
  91. const spansCursor = decodeScalar(location.query?.[QueryParameterNames.SPANS_CURSOR]);
  92. const filters: SpanMetricsQueryFilters = {
  93. 'span.group': groupId,
  94. 'span.op': spanOp,
  95. transaction: transaction as string,
  96. };
  97. const sort = useSpanSummarySort();
  98. const {
  99. data: rowData,
  100. pageLinks,
  101. isLoading: isRowDataLoading,
  102. } = useSpansIndexed(
  103. {
  104. fields: [
  105. SpanIndexedField.ID,
  106. SpanIndexedField.TRANSACTION_ID,
  107. SpanIndexedField.TIMESTAMP,
  108. SpanIndexedField.SPAN_DURATION,
  109. SpanIndexedField.TRACE,
  110. SpanIndexedField.PROJECT,
  111. ],
  112. search: MutableSearch.fromQueryObject(filters),
  113. limit: LIMIT,
  114. sorts: [sort],
  115. cursor: spansCursor,
  116. },
  117. SpanSummaryReferrer.SPAN_SUMMARY_TABLE
  118. );
  119. const transactionIds = rowData?.map(row => row[SpanIndexedField.TRANSACTION_ID]);
  120. const eventView = EventView.fromNewQueryWithLocation(
  121. {
  122. name: 'Transaction Durations',
  123. query: MutableSearch.fromQueryObject({
  124. project: project?.slug,
  125. id: `[${transactionIds?.join() ?? ''}]`,
  126. }).formatString(),
  127. fields: ['id', 'transaction.duration'],
  128. version: 2,
  129. },
  130. location
  131. );
  132. const {
  133. isLoading: isTxnDurationDataLoading,
  134. data: txnDurationData,
  135. isError: isTxnDurationError,
  136. } = useGenericDiscoverQuery<
  137. {
  138. data: any[];
  139. meta: MetaType;
  140. },
  141. DiscoverQueryProps
  142. >({
  143. route: 'events',
  144. eventView,
  145. location,
  146. orgSlug: organization.slug,
  147. getRequestPayload: () => ({
  148. ...eventView.getEventsAPIPayload(location),
  149. interval: eventView.interval,
  150. }),
  151. limit: LIMIT,
  152. options: {
  153. refetchOnWindowFocus: false,
  154. enabled: Boolean(rowData && rowData.length > 0),
  155. },
  156. referrer: SpanSummaryReferrer.SPAN_SUMMARY_TABLE,
  157. });
  158. // Restructure the transaction durations into a map for faster lookup
  159. const transactionDurationMap = {};
  160. txnDurationData?.data.forEach(datum => {
  161. transactionDurationMap[datum.id] = datum['transaction.duration'];
  162. });
  163. const mergedData: DataRow[] =
  164. rowData?.map((row: Pick<SpanIndexedResponse, DataRowKeys>) => {
  165. const transactionId = row[SpanIndexedField.TRANSACTION_ID];
  166. const newRow = {
  167. ...row,
  168. 'transaction.duration': transactionDurationMap[transactionId],
  169. };
  170. return newRow;
  171. }) ?? [];
  172. const handleCursor: CursorHandler = (cursor, pathname, query) => {
  173. browserHistory.push({
  174. pathname,
  175. query: {...query, [QueryParameterNames.SPANS_CURSOR]: cursor},
  176. });
  177. };
  178. return (
  179. <Fragment>
  180. <VisuallyCompleteWithData
  181. id="SpanDetails-SpanDetailsTable"
  182. hasData={!!mergedData?.length}
  183. isLoading={isRowDataLoading}
  184. >
  185. <GridEditable
  186. isLoading={isRowDataLoading}
  187. data={mergedData}
  188. columnOrder={COLUMN_ORDER}
  189. columnSortBy={[
  190. {
  191. key: sort.field,
  192. order: sort.kind,
  193. },
  194. ]}
  195. grid={{
  196. renderHeadCell: column =>
  197. renderHeadCell({
  198. column,
  199. location,
  200. sort,
  201. }),
  202. renderBodyCell: renderBodyCell(
  203. location,
  204. organization,
  205. spanOp,
  206. isTxnDurationDataLoading || isTxnDurationError
  207. ),
  208. }}
  209. />
  210. </VisuallyCompleteWithData>
  211. <Pagination pageLinks={pageLinks} onCursor={handleCursor} />
  212. </Fragment>
  213. );
  214. }
  215. function renderBodyCell(
  216. location: Location,
  217. organization: Organization,
  218. spanOp: string = '',
  219. isTxnDurationDataLoading: boolean
  220. ) {
  221. return function (column: Column, dataRow: DataRow): React.ReactNode {
  222. const {timestamp, span_id, trace, project} = dataRow;
  223. const spanDuration = dataRow[SpanIndexedField.SPAN_DURATION];
  224. const transactionId = dataRow[SpanIndexedField.TRANSACTION_ID];
  225. const transactionDuration = dataRow['transaction.duration'];
  226. if (column.key === SpanIndexedField.SPAN_DURATION) {
  227. if (isTxnDurationDataLoading) {
  228. return <EmptySpanDurationBar />;
  229. }
  230. if (!transactionDuration) {
  231. return (
  232. <EmptySpanDurationBar>
  233. <Tooltip
  234. title={t('Transaction duration unknown')}
  235. containerDisplayMode="block"
  236. >
  237. <PerformanceDuration abbreviation milliseconds={spanDuration} />
  238. </Tooltip>
  239. </EmptySpanDurationBar>
  240. );
  241. }
  242. return (
  243. <SpanDurationBar
  244. spanOp={spanOp}
  245. spanDuration={spanDuration}
  246. transactionDuration={transactionDuration}
  247. />
  248. );
  249. }
  250. if (column.key === SpanIndexedField.ID) {
  251. if (!defined(span_id)) {
  252. return null;
  253. }
  254. return (
  255. <SpanIdCell
  256. moduleName={ModuleName.OTHER}
  257. projectSlug={project}
  258. spanId={span_id}
  259. timestamp={timestamp}
  260. traceId={trace}
  261. transactionId={transactionId}
  262. />
  263. );
  264. }
  265. const fieldRenderer = getFieldRenderer(column.key, COLUMN_TYPE);
  266. const rendered = fieldRenderer(dataRow, {location, organization});
  267. return rendered;
  268. };
  269. }
  270. const EmptySpanDurationBar = styled('div')`
  271. height: ${ROW_HEIGHT - 2 * ROW_PADDING}px;
  272. width: 100%;
  273. position: relative;
  274. display: flex;
  275. align-items: center;
  276. top: ${space(0.5)};
  277. background-color: ${p => p.theme.gray100};
  278. padding-left: ${space(1)};
  279. color: ${p => p.theme.gray300};
  280. font-size: ${p => p.theme.fontSizeExtraSmall};
  281. font-variant-numeric: tabular-nums;
  282. line-height: 1;
  283. `;