domainTransactionsTable.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import {Fragment} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import type {Location} from 'history';
  4. import GridEditable, {
  5. COL_WIDTH_UNDEFINED,
  6. type GridColumnHeader,
  7. } from 'sentry/components/gridEditable';
  8. import type {CursorHandler} from 'sentry/components/pagination';
  9. import Pagination from 'sentry/components/pagination';
  10. import {t} from 'sentry/locale';
  11. import type {Organization} from 'sentry/types';
  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 {TransactionCell} from 'sentry/views/performance/http/transactionCell';
  18. import {renderHeadCell} from 'sentry/views/starfish/components/tableCells/renderHeadCell';
  19. import type {MetricsResponse} from 'sentry/views/starfish/types';
  20. import {QueryParameterNames} from 'sentry/views/starfish/views/queryParameters';
  21. import {DataTitles} from 'sentry/views/starfish/views/spans/types';
  22. type Row = Pick<
  23. MetricsResponse,
  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. location={location}
  147. />
  148. <Pagination pageLinks={pageLinks} onCursor={handleCursor} />
  149. </Fragment>
  150. );
  151. }
  152. function renderBodyCell(
  153. column: Column,
  154. row: Row,
  155. meta: EventsMetaType | undefined,
  156. domain: string | undefined,
  157. location: Location,
  158. organization: Organization
  159. ) {
  160. if (column.key === 'transaction') {
  161. return (
  162. <TransactionCell
  163. domain={domain}
  164. project={String(row['project.id'])}
  165. transaction={row.transaction}
  166. transactionMethod={row['transaction.method']}
  167. />
  168. );
  169. }
  170. if (!meta?.fields) {
  171. return row[column.key];
  172. }
  173. const renderer = getFieldRenderer(column.key, meta.fields, false);
  174. return renderer(row, {
  175. location,
  176. organization,
  177. unit: meta.units?.[column.key],
  178. });
  179. }