transactionsTable.tsx 5.4 KB

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