transactionsTable.tsx 4.9 KB

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