transactionsTable.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import qs from 'qs';
  5. import GridEditable, {
  6. COL_WIDTH_UNDEFINED,
  7. type GridColumnHeader,
  8. } from 'sentry/components/gridEditable';
  9. import Link from 'sentry/components/links/link';
  10. import type {CursorHandler} from 'sentry/components/pagination';
  11. import Pagination from 'sentry/components/pagination';
  12. import {t} from 'sentry/locale';
  13. import type {Organization} from 'sentry/types';
  14. import {browserHistory} from 'sentry/utils/browserHistory';
  15. import type {EventsMetaType} from 'sentry/utils/discover/eventView';
  16. import {FIELD_FORMATTERS, getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  17. import {decodeScalar} from 'sentry/utils/queryString';
  18. import {useLocation} from 'sentry/utils/useLocation';
  19. import useOrganization from 'sentry/utils/useOrganization';
  20. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  21. import {useQueuesByTransactionQuery} from 'sentry/views/performance/queues/queries/useQueuesByTransactionQuery';
  22. import {renderHeadCell} from 'sentry/views/starfish/components/tableCells/renderHeadCell';
  23. import type {SpanMetricsResponse} from 'sentry/views/starfish/types';
  24. import {QueryParameterNames} from 'sentry/views/starfish/views/queryParameters';
  25. type Row = Pick<
  26. SpanMetricsResponse,
  27. | 'sum(span.duration)'
  28. | 'transaction'
  29. | `avg_if(${string},${string},${string})`
  30. | `count_op(${string})`
  31. >;
  32. type Column = GridColumnHeader<string>;
  33. const COLUMN_ORDER: Column[] = [
  34. {
  35. key: 'transaction',
  36. name: t('Transactions'),
  37. width: COL_WIDTH_UNDEFINED,
  38. },
  39. {
  40. key: 'span.op',
  41. name: t('Type'),
  42. width: COL_WIDTH_UNDEFINED,
  43. },
  44. {
  45. key: 'avg(messaging.message.receive.latency)',
  46. name: t('Avg Time in Queue'),
  47. width: COL_WIDTH_UNDEFINED,
  48. },
  49. {
  50. key: 'avg_if(span.duration,span.op,queue.process)',
  51. name: t('Avg Processing Time'),
  52. width: COL_WIDTH_UNDEFINED,
  53. },
  54. {
  55. key: '', // TODO
  56. name: t('Error Rate'),
  57. width: COL_WIDTH_UNDEFINED,
  58. },
  59. {
  60. key: 'count_op(queue.publish)',
  61. name: t('Published'),
  62. width: COL_WIDTH_UNDEFINED,
  63. },
  64. {
  65. key: 'count_op(queue.process)',
  66. name: t('Processed'),
  67. width: COL_WIDTH_UNDEFINED,
  68. },
  69. {
  70. key: 'sum(span.duration)',
  71. name: t('Time Spent'),
  72. width: COL_WIDTH_UNDEFINED,
  73. },
  74. ];
  75. export function TransactionsTable() {
  76. const organization = useOrganization();
  77. const location = useLocation();
  78. const destination = decodeScalar(location.query.destination);
  79. const {data, isLoading, meta, pageLinks, error} = useQueuesByTransactionQuery({
  80. destination,
  81. });
  82. const handleCursor: CursorHandler = (newCursor, pathname, query) => {
  83. browserHistory.push({
  84. pathname,
  85. query: {...query, [QueryParameterNames.TRANSACTIONS_CURSOR]: newCursor},
  86. });
  87. };
  88. return (
  89. <Fragment>
  90. <GridEditable
  91. aria-label={t('Transactions')}
  92. isLoading={isLoading}
  93. error={error}
  94. data={data}
  95. columnOrder={COLUMN_ORDER}
  96. columnSortBy={[]}
  97. grid={{
  98. renderHeadCell: col =>
  99. renderHeadCell({
  100. column: col,
  101. location,
  102. }),
  103. renderBodyCell: (column, row) =>
  104. renderBodyCell(column, row, meta, location, organization),
  105. }}
  106. location={location}
  107. />
  108. <Pagination pageLinks={pageLinks} onCursor={handleCursor} />
  109. </Fragment>
  110. );
  111. }
  112. function renderBodyCell(
  113. column: Column,
  114. row: Row,
  115. meta: EventsMetaType | undefined,
  116. location: Location,
  117. organization: Organization
  118. ) {
  119. const op = row['span.op'];
  120. const isProducer = op === 'queue.publish';
  121. const isConsumer = op === 'queue.process';
  122. const key = column.key;
  123. if (
  124. row[key] === undefined ||
  125. (isConsumer && ['count_op(queue.publish)'].includes(key)) ||
  126. (isProducer &&
  127. [
  128. 'count_op(queue.process)',
  129. 'avg(messaging.message.receive.latency)',
  130. 'avg_if(span.duration,span.op,queue.process)',
  131. ].includes(key))
  132. ) {
  133. return (
  134. <AlignRight>
  135. <NoValue>{' \u2014 '}</NoValue>
  136. </AlignRight>
  137. );
  138. }
  139. if (key === 'transaction') {
  140. return <TransactionCell transaction={row[key]} />;
  141. }
  142. if (!meta?.fields) {
  143. return row[column.key];
  144. }
  145. if (key.startsWith('avg')) {
  146. const renderer = FIELD_FORMATTERS.duration.renderFunc;
  147. return renderer(key, row);
  148. }
  149. if (key === 'span.op') {
  150. switch (row[key]) {
  151. case 'queue.publish':
  152. return t('Producer');
  153. case 'queue.process':
  154. return t('Consumer');
  155. default:
  156. return row[key];
  157. }
  158. }
  159. const renderer = getFieldRenderer(column.key, meta.fields, false);
  160. return renderer(row, {
  161. location,
  162. organization,
  163. unit: meta.units?.[column.key],
  164. });
  165. }
  166. function TransactionCell({transaction}: {transaction: string}) {
  167. const organization = useOrganization();
  168. const {query} = useLocation();
  169. const queryString = {
  170. ...query,
  171. transaction,
  172. };
  173. return (
  174. <NoOverflow>
  175. <Link
  176. to={normalizeUrl(
  177. `/organizations/${organization.slug}/performance/queues/destination/?${qs.stringify(queryString)}`
  178. )}
  179. >
  180. {transaction}
  181. </Link>
  182. </NoOverflow>
  183. );
  184. }
  185. const NoOverflow = styled('span')`
  186. overflow: hidden;
  187. `;
  188. const AlignRight = styled('span')`
  189. text-align: right;
  190. `;
  191. const NoValue = styled('span')`
  192. color: ${p => p.theme.gray300};
  193. `;