queuesTable.tsx 5.4 KB

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