domainTransactionsTable.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import {Fragment} from 'react';
  2. import type {Location} from 'history';
  3. import GridEditable, {
  4. COL_WIDTH_UNDEFINED,
  5. type GridColumnHeader,
  6. } from 'sentry/components/gridEditable';
  7. import type {CursorHandler} from 'sentry/components/pagination';
  8. import Pagination from 'sentry/components/pagination';
  9. import {t} from 'sentry/locale';
  10. import type {Organization} from 'sentry/types/organization';
  11. import {browserHistory} from 'sentry/utils/browserHistory';
  12. import type {EventsMetaType} from 'sentry/utils/discover/eventView';
  13. import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  14. import {RATE_UNIT_TITLE, RateUnit, type Sort} from 'sentry/utils/discover/fields';
  15. import {useLocation} from 'sentry/utils/useLocation';
  16. import useOrganization from 'sentry/utils/useOrganization';
  17. import {renderHeadCell} from 'sentry/views/insights/common/components/tableCells/renderHeadCell';
  18. import {QueryParameterNames} from 'sentry/views/insights/common/views/queryParameters';
  19. import {DataTitles} from 'sentry/views/insights/common/views/spans/types';
  20. import {TransactionCell} from 'sentry/views/insights/http/components/tables/transactionCell';
  21. import type {SpanMetricsResponse} from 'sentry/views/insights/types';
  22. type Row = Pick<
  23. SpanMetricsResponse,
  24. | 'project.id'
  25. | 'transaction'
  26. | 'transaction.method'
  27. | 'spm()'
  28. | 'http_response_rate(3)'
  29. | 'http_response_rate(4)'
  30. | 'http_response_rate(5)'
  31. | 'avg(span.self_time)'
  32. | 'sum(span.self_time)'
  33. | 'time_spent_percentage()'
  34. >;
  35. type Column = GridColumnHeader<
  36. | 'transaction'
  37. | 'spm()'
  38. | 'http_response_rate(3)'
  39. | 'http_response_rate(4)'
  40. | 'http_response_rate(5)'
  41. | 'avg(span.self_time)'
  42. | 'time_spent_percentage()'
  43. >;
  44. const COLUMN_ORDER: Column[] = [
  45. {
  46. key: 'transaction',
  47. name: t('Found In'),
  48. width: COL_WIDTH_UNDEFINED,
  49. },
  50. {
  51. key: 'spm()',
  52. name: `${t('Requests')} ${RATE_UNIT_TITLE[RateUnit.PER_MINUTE]}`,
  53. width: COL_WIDTH_UNDEFINED,
  54. },
  55. {
  56. key: `http_response_rate(3)`,
  57. name: t('3XXs'),
  58. width: 50,
  59. },
  60. {
  61. key: `http_response_rate(4)`,
  62. name: t('4XXs'),
  63. width: 50,
  64. },
  65. {
  66. key: `http_response_rate(5)`,
  67. name: t('5XXs'),
  68. width: 50,
  69. },
  70. {
  71. key: `avg(span.self_time)`,
  72. name: DataTitles.avg,
  73. width: COL_WIDTH_UNDEFINED,
  74. },
  75. {
  76. key: 'time_spent_percentage()',
  77. name: DataTitles.timeSpent,
  78. width: COL_WIDTH_UNDEFINED,
  79. },
  80. ];
  81. const SORTABLE_FIELDS = [
  82. 'avg(span.self_time)',
  83. 'spm()',
  84. 'http_response_rate(3)',
  85. 'http_response_rate(4)',
  86. 'http_response_rate(5)',
  87. 'time_spent_percentage()',
  88. ] as const;
  89. type ValidSort = Sort & {
  90. field: (typeof SORTABLE_FIELDS)[number];
  91. };
  92. export function isAValidSort(sort: Sort): sort is ValidSort {
  93. return (SORTABLE_FIELDS as unknown as string[]).includes(sort.field);
  94. }
  95. interface Props {
  96. data: Row[];
  97. isLoading: boolean;
  98. sort: ValidSort;
  99. domain?: string;
  100. error?: Error | null;
  101. meta?: EventsMetaType;
  102. pageLinks?: string;
  103. }
  104. export function DomainTransactionsTable({
  105. data,
  106. isLoading,
  107. error,
  108. meta,
  109. pageLinks,
  110. sort,
  111. domain,
  112. }: Props) {
  113. const location = useLocation();
  114. const organization = useOrganization();
  115. const handleCursor: CursorHandler = (newCursor, pathname, query) => {
  116. browserHistory.push({
  117. pathname,
  118. query: {...query, [QueryParameterNames.TRANSACTIONS_CURSOR]: newCursor},
  119. });
  120. };
  121. return (
  122. <Fragment>
  123. <GridEditable
  124. aria-label={t('Transactions')}
  125. isLoading={isLoading}
  126. error={error}
  127. data={data}
  128. columnOrder={COLUMN_ORDER}
  129. columnSortBy={[
  130. {
  131. key: sort.field,
  132. order: sort.kind,
  133. },
  134. ]}
  135. grid={{
  136. renderHeadCell: col =>
  137. renderHeadCell({
  138. column: col,
  139. sort,
  140. location,
  141. sortParameterName: QueryParameterNames.TRANSACTIONS_SORT,
  142. }),
  143. renderBodyCell: (column, row) =>
  144. renderBodyCell(column, row, meta, domain, location, organization),
  145. }}
  146. />
  147. <Pagination pageLinks={pageLinks} onCursor={handleCursor} />
  148. </Fragment>
  149. );
  150. }
  151. function renderBodyCell(
  152. column: Column,
  153. row: Row,
  154. meta: EventsMetaType | undefined,
  155. domain: string | undefined,
  156. location: Location,
  157. organization: Organization
  158. ) {
  159. if (column.key === 'transaction') {
  160. return (
  161. <TransactionCell
  162. domain={domain}
  163. project={String(row['project.id'])}
  164. transaction={row.transaction}
  165. transactionMethod={row['transaction.method']}
  166. />
  167. );
  168. }
  169. if (!meta?.fields) {
  170. return row[column.key];
  171. }
  172. const renderer = getFieldRenderer(column.key, meta.fields, false);
  173. return renderer(
  174. {...row, 'span.op': 'http.client'},
  175. {
  176. location,
  177. organization,
  178. unit: meta.units?.[column.key],
  179. }
  180. );
  181. }