spanSummaryTable.tsx 8.4 KB

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