spanMetricsTable.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import {Fragment} from 'react';
  2. import type {Location} from 'history';
  3. import type {GridColumnHeader} from 'sentry/components/gridEditable';
  4. import GridEditable, {COL_WIDTH_UNDEFINED} from 'sentry/components/gridEditable';
  5. import Link from 'sentry/components/links/link';
  6. import Pagination, {type CursorHandler} from 'sentry/components/pagination';
  7. import {t} from 'sentry/locale';
  8. import type {Organization} from 'sentry/types/organization';
  9. import type {Project} from 'sentry/types/project';
  10. import {browserHistory} from 'sentry/utils/browserHistory';
  11. import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  12. import type {ColumnType} from 'sentry/utils/discover/fields';
  13. import {Container as TableCellContainer} from 'sentry/utils/discover/styles';
  14. import {VisuallyCompleteWithData} from 'sentry/utils/performanceForSentry';
  15. import {decodeScalar} from 'sentry/utils/queryString';
  16. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  17. import useLocationQuery from 'sentry/utils/url/useLocationQuery';
  18. import {useLocation} from 'sentry/utils/useLocation';
  19. import useOrganization from 'sentry/utils/useOrganization';
  20. import {renderHeadCell} from 'sentry/views/insights/common/components/tableCells/renderHeadCell';
  21. import {useSpanMetrics} from 'sentry/views/insights/common/queries/useDiscover';
  22. import {QueryParameterNames} from 'sentry/views/insights/common/views/queryParameters';
  23. import {
  24. type DomainView,
  25. useDomainViewFilters,
  26. } from 'sentry/views/insights/pages/useFilters';
  27. import {
  28. SpanMetricsField,
  29. type SpanMetricsQueryFilters,
  30. } from 'sentry/views/insights/types';
  31. import {spanDetailsRouteWithQuery} from 'sentry/views/performance/transactionSummary/transactionSpans/spanDetails/utils';
  32. import {useSpansTabTableSort} from 'sentry/views/performance/transactionSummary/transactionSpans/useSpansTabTableSort';
  33. type DataRow = {
  34. [SpanMetricsField.SPAN_OP]: string;
  35. [SpanMetricsField.SPAN_DESCRIPTION]: string;
  36. [SpanMetricsField.SPAN_GROUP]: string;
  37. 'avg(span.duration)': number;
  38. 'spm()': number;
  39. 'sum(span.duration)': number;
  40. };
  41. type ColumnKeys =
  42. | SpanMetricsField.SPAN_OP
  43. | SpanMetricsField.SPAN_DESCRIPTION
  44. | 'spm()'
  45. | `avg(${SpanMetricsField.SPAN_DURATION})`
  46. | `sum(${SpanMetricsField.SPAN_DURATION})`;
  47. type Column = GridColumnHeader<ColumnKeys>;
  48. const COLUMN_ORDER: Column[] = [
  49. {
  50. key: SpanMetricsField.SPAN_OP,
  51. name: t('Span Operation'),
  52. width: COL_WIDTH_UNDEFINED,
  53. },
  54. {
  55. key: SpanMetricsField.SPAN_DESCRIPTION,
  56. name: t('Span Description'),
  57. width: COL_WIDTH_UNDEFINED,
  58. },
  59. {
  60. key: 'spm()',
  61. name: t('Throughput'),
  62. width: COL_WIDTH_UNDEFINED,
  63. },
  64. {
  65. key: `avg(${SpanMetricsField.SPAN_DURATION})`,
  66. name: t('Avg Duration'),
  67. width: COL_WIDTH_UNDEFINED,
  68. },
  69. {
  70. key: `sum(${SpanMetricsField.SPAN_DURATION})`,
  71. name: t('Time Spent'),
  72. width: COL_WIDTH_UNDEFINED,
  73. },
  74. ];
  75. const COLUMN_TYPE: Record<ColumnKeys, ColumnType> = {
  76. [SpanMetricsField.SPAN_OP]: 'string',
  77. [SpanMetricsField.SPAN_DESCRIPTION]: 'string',
  78. ['spm()']: 'rate',
  79. [`avg(${SpanMetricsField.SPAN_DURATION})`]: 'duration',
  80. [`sum(${SpanMetricsField.SPAN_DURATION})`]: 'duration',
  81. };
  82. const LIMIT = 12;
  83. type Props = {
  84. project: Project | undefined;
  85. query: string;
  86. transactionName: string;
  87. };
  88. export default function SpanMetricsTable(props: Props) {
  89. const {project, transactionName, query: search} = props;
  90. const organization = useOrganization();
  91. const location = useLocation();
  92. const sort = useSpansTabTableSort();
  93. const domainViewFilters = useDomainViewFilters();
  94. const query = useLocationQuery({
  95. fields: {
  96. spansCursor: decodeScalar,
  97. spanOp: decodeScalar,
  98. },
  99. });
  100. const {spansCursor, spanOp} = query;
  101. const filters: SpanMetricsQueryFilters = {
  102. transaction: transactionName,
  103. ['span.op']: spanOp,
  104. };
  105. const handleCursor: CursorHandler = (cursor, pathname, q) => {
  106. browserHistory.push({
  107. pathname,
  108. query: {...q, [QueryParameterNames.SPANS_CURSOR]: cursor},
  109. });
  110. };
  111. const mutableSearch = MutableSearch.fromQueryObject(filters);
  112. mutableSearch.addStringMultiFilter(search);
  113. const {data, isPending, pageLinks} = useSpanMetrics(
  114. {
  115. search: mutableSearch,
  116. fields: [
  117. SpanMetricsField.SPAN_OP,
  118. SpanMetricsField.SPAN_DESCRIPTION,
  119. SpanMetricsField.SPAN_GROUP,
  120. `spm()`,
  121. `avg(${SpanMetricsField.SPAN_DURATION})`,
  122. `sum(${SpanMetricsField.SPAN_DURATION})`,
  123. ],
  124. sorts: [sort],
  125. cursor: spansCursor,
  126. limit: LIMIT,
  127. },
  128. 'api.performance.transaction-spans'
  129. );
  130. return (
  131. <Fragment>
  132. <VisuallyCompleteWithData
  133. id="TransactionSpans-SpanMetricsTable"
  134. hasData={!!data?.length}
  135. isLoading={isPending}
  136. >
  137. <GridEditable
  138. isLoading={isPending}
  139. data={data}
  140. columnOrder={COLUMN_ORDER}
  141. columnSortBy={[
  142. {
  143. key: sort.field,
  144. order: sort.kind,
  145. },
  146. ]}
  147. grid={{
  148. renderHeadCell: column =>
  149. renderHeadCell({
  150. column,
  151. location,
  152. sort,
  153. }),
  154. renderBodyCell: renderBodyCell(
  155. location,
  156. organization,
  157. transactionName,
  158. project,
  159. domainViewFilters?.view
  160. ),
  161. }}
  162. />
  163. </VisuallyCompleteWithData>
  164. <Pagination pageLinks={pageLinks} onCursor={handleCursor} />
  165. </Fragment>
  166. );
  167. }
  168. function renderBodyCell(
  169. location: Location,
  170. organization: Organization,
  171. transactionName: string,
  172. project?: Project,
  173. view?: DomainView
  174. ) {
  175. return function (column: Column, dataRow: DataRow): React.ReactNode {
  176. if (column.key === SpanMetricsField.SPAN_OP) {
  177. const target = spanDetailsRouteWithQuery({
  178. orgSlug: organization.slug,
  179. transaction: transactionName,
  180. query: location.query,
  181. spanSlug: {op: dataRow['span.op'], group: ''},
  182. projectID: project?.id,
  183. view,
  184. });
  185. return (
  186. <TableCellContainer>
  187. <Link to={target}>{dataRow[column.key]}</Link>
  188. </TableCellContainer>
  189. );
  190. }
  191. if (column.key === SpanMetricsField.SPAN_DESCRIPTION) {
  192. if (!dataRow['span.group']) {
  193. return <TableCellContainer>{'\u2014'}</TableCellContainer>;
  194. }
  195. const target = spanDetailsRouteWithQuery({
  196. orgSlug: organization.slug,
  197. transaction: transactionName,
  198. query: location.query,
  199. spanSlug: {op: dataRow['span.op'], group: dataRow['span.group']},
  200. projectID: project?.id,
  201. view,
  202. });
  203. return (
  204. <TableCellContainer>
  205. <Link to={target}>{dataRow[column.key]}</Link>
  206. </TableCellContainer>
  207. );
  208. }
  209. const fieldRenderer = getFieldRenderer(column.key, COLUMN_TYPE, false);
  210. const rendered = fieldRenderer(dataRow, {location, organization});
  211. return rendered;
  212. };
  213. }