transactionsTable.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import {Fragment} from 'react';
  2. import {browserHistory, Link} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import type {Location} from 'history';
  5. import qs from 'qs';
  6. import GridEditable, {
  7. COL_WIDTH_UNDEFINED,
  8. type GridColumnHeader,
  9. } from 'sentry/components/gridEditable';
  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 type {EventsMetaType} from 'sentry/utils/discover/eventView';
  15. import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  16. import {decodeScalar} from 'sentry/utils/queryString';
  17. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  18. import {useLocation} from 'sentry/utils/useLocation';
  19. import useOrganization from 'sentry/utils/useOrganization';
  20. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  21. import {DEFAULT_QUERY_FILTER} from 'sentry/views/performance/queues/settings';
  22. import {renderHeadCell} from 'sentry/views/starfish/components/tableCells/renderHeadCell';
  23. import {useSpanMetrics} from 'sentry/views/starfish/queries/useSpanMetrics';
  24. import type {MetricsResponse} from 'sentry/views/starfish/types';
  25. import {QueryParameterNames} from 'sentry/views/starfish/views/queryParameters';
  26. type Row = Pick<
  27. MetricsResponse,
  28. | 'avg_if(span.self_time,span.op,queue.task.celery)'
  29. | 'count_op(queue.submit.celery)'
  30. | 'count_op(queue.task.celery)'
  31. | 'sum(span.self_time)'
  32. | 'transaction'
  33. >;
  34. type Column = GridColumnHeader<string>;
  35. const COLUMN_ORDER: Column[] = [
  36. {
  37. key: 'transaction',
  38. name: t('Transactions'),
  39. width: COL_WIDTH_UNDEFINED,
  40. },
  41. {
  42. key: '', // TODO
  43. name: t('Type'),
  44. width: COL_WIDTH_UNDEFINED,
  45. },
  46. {
  47. key: '', // TODO
  48. name: t('Avg Time in Queue'),
  49. width: COL_WIDTH_UNDEFINED,
  50. },
  51. {
  52. key: 'avg_if(span.self_time,span.op,queue.task.celery)',
  53. name: t('Avg Processing Time'),
  54. width: COL_WIDTH_UNDEFINED,
  55. },
  56. {
  57. key: '', // TODO
  58. name: t('Error Rate'),
  59. width: COL_WIDTH_UNDEFINED,
  60. },
  61. {
  62. key: 'count_op(queue.submit.celery)',
  63. name: t('Published'),
  64. width: COL_WIDTH_UNDEFINED,
  65. },
  66. {
  67. key: 'count_op(queue.task.celery)',
  68. name: t('Processed'),
  69. width: COL_WIDTH_UNDEFINED,
  70. },
  71. {
  72. key: 'sum(span.self_time)',
  73. name: t('Time Spent'),
  74. width: COL_WIDTH_UNDEFINED,
  75. },
  76. ];
  77. interface Props {
  78. domain?: string;
  79. error?: Error | null;
  80. meta?: EventsMetaType;
  81. pageLinks?: string;
  82. }
  83. export function TransactionsTable({error, pageLinks}: Props) {
  84. const organization = useOrganization();
  85. const location = useLocation();
  86. const destination = decodeScalar(location.query.destination);
  87. const cursor = decodeScalar(location.query?.[QueryParameterNames.DOMAINS_CURSOR]);
  88. const mutableSearch = new MutableSearch(DEFAULT_QUERY_FILTER);
  89. // TODO: This should filter by destination, not transaction.
  90. // We are using transaction for now as a proxy to demo some functionality until destination becomes a filterable tag.
  91. if (destination) {
  92. mutableSearch.addFilterValue('transaction', destination);
  93. }
  94. const {data, isLoading, meta} = useSpanMetrics({
  95. search: mutableSearch,
  96. fields: [
  97. 'transaction',
  98. 'count()',
  99. 'count_op(queue.submit.celery)',
  100. 'count_op(queue.task.celery)',
  101. 'sum(span.self_time)',
  102. 'avg(span.self_time)',
  103. 'avg_if(span.self_time,span.op,queue.submit.celery)',
  104. 'avg_if(span.self_time,span.op,queue.task.celery)',
  105. ],
  106. sorts: [],
  107. limit: 10,
  108. cursor,
  109. referrer: 'api.starfish.http-module-landing-domains-list',
  110. });
  111. const handleCursor: CursorHandler = (newCursor, pathname, query) => {
  112. browserHistory.push({
  113. pathname,
  114. query: {...query, [QueryParameterNames.TRANSACTIONS_CURSOR]: newCursor},
  115. });
  116. };
  117. return (
  118. <Fragment>
  119. <GridEditable
  120. aria-label={t('Transactions')}
  121. isLoading={isLoading}
  122. error={error}
  123. data={data}
  124. columnOrder={COLUMN_ORDER}
  125. columnSortBy={[]}
  126. grid={{
  127. renderHeadCell: col =>
  128. renderHeadCell({
  129. column: col,
  130. location,
  131. }),
  132. renderBodyCell: (column, row) =>
  133. renderBodyCell(column, row, meta, location, organization),
  134. }}
  135. location={location}
  136. />
  137. <Pagination pageLinks={pageLinks} onCursor={handleCursor} />
  138. </Fragment>
  139. );
  140. }
  141. function renderBodyCell(
  142. column: Column,
  143. row: Row,
  144. meta: EventsMetaType | undefined,
  145. location: Location,
  146. organization: Organization
  147. ) {
  148. const key = column.key;
  149. if (row[key] === undefined) {
  150. return (
  151. <AlignRight>
  152. <NoValue>{' \u2014 '}</NoValue>
  153. </AlignRight>
  154. );
  155. }
  156. if (key === 'transaction') {
  157. return <TransactionCell transaction={row[key]} />;
  158. }
  159. if (!meta?.fields) {
  160. return row[column.key];
  161. }
  162. const renderer = getFieldRenderer(column.key, meta.fields, false);
  163. return renderer(row, {
  164. location,
  165. organization,
  166. unit: meta.units?.[column.key],
  167. });
  168. }
  169. function TransactionCell({transaction}: {transaction: string}) {
  170. const organization = useOrganization();
  171. const {query} = useLocation();
  172. const queryString = {
  173. ...query,
  174. transaction,
  175. };
  176. return (
  177. <NoOverflow>
  178. <Link
  179. to={normalizeUrl(
  180. `/organizations/${organization.slug}/performance/queues/destination/?${qs.stringify(queryString)}`
  181. )}
  182. >
  183. {transaction}
  184. </Link>
  185. </NoOverflow>
  186. );
  187. }
  188. const NoOverflow = styled('span')`
  189. overflow: hidden;
  190. `;
  191. const AlignRight = styled('span')`
  192. text-align: right;
  193. `;
  194. const NoValue = styled('span')`
  195. color: ${p => p.theme.gray300};
  196. `;