queuesTable.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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} from 'sentry/utils/formatters';
  19. import {useLocation} from 'sentry/utils/useLocation';
  20. import useOrganization from 'sentry/utils/useOrganization';
  21. import {useQueuesByDestinationQuery} from 'sentry/views/performance/queues/queries/useQueuesByDestinationQuery';
  22. import {useQueueModuleURL} from 'sentry/views/performance/utils/useModuleURL';
  23. import {renderHeadCell} from 'sentry/views/starfish/components/tableCells/renderHeadCell';
  24. import {
  25. SpanFunction,
  26. SpanIndexedField,
  27. type SpanMetricsResponse,
  28. } from 'sentry/views/starfish/types';
  29. import {QueryParameterNames} from 'sentry/views/starfish/views/queryParameters';
  30. type Row = Pick<
  31. SpanMetricsResponse,
  32. | 'sum(span.duration)'
  33. | 'messaging.destination.name'
  34. | 'avg(messaging.message.receive.latency)'
  35. | `avg_if(${string},${string},${string})`
  36. | `count_op(${string})`
  37. >;
  38. type Column = GridColumnHeader<string>;
  39. const COLUMN_ORDER: Column[] = [
  40. {
  41. key: 'messaging.destination.name',
  42. name: t('Destination'),
  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.duration,span.op,queue.process)',
  52. name: t('Avg Processing Time'),
  53. width: COL_WIDTH_UNDEFINED,
  54. },
  55. {
  56. key: 'trace_status_rate(ok)',
  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: 'time_spent_percentage(app,span.duration)',
  72. name: t('Time Spent'),
  73. width: COL_WIDTH_UNDEFINED,
  74. },
  75. ];
  76. const SORTABLE_FIELDS = [
  77. SpanIndexedField.MESSAGING_MESSAGE_DESTINATION_NAME,
  78. 'count_op(queue.publish)',
  79. 'count_op(queue.process)',
  80. 'avg_if(span.duration,span.op,queue.process)',
  81. 'avg(messaging.message.receive.latency)',
  82. `${SpanFunction.TIME_SPENT_PERCENTAGE}(app,span.duration)`,
  83. ] as const;
  84. type ValidSort = Sort & {
  85. field: (typeof SORTABLE_FIELDS)[number];
  86. };
  87. export function isAValidSort(sort: Sort): sort is ValidSort {
  88. return (SORTABLE_FIELDS as ReadonlyArray<string>).includes(sort.field);
  89. }
  90. interface Props {
  91. sort: ValidSort;
  92. destination?: string;
  93. error?: Error | null;
  94. meta?: EventsMetaType;
  95. }
  96. export function QueuesTable({error, destination, sort}: Props) {
  97. const location = useLocation();
  98. const organization = useOrganization();
  99. const {data, isLoading, meta, pageLinks} = useQueuesByDestinationQuery({
  100. destination,
  101. sort,
  102. });
  103. const handleCursor: CursorHandler = (newCursor, pathname, query) => {
  104. browserHistory.push({
  105. pathname,
  106. query: {...query, [QueryParameterNames.DESTINATIONS_CURSOR]: newCursor},
  107. });
  108. };
  109. return (
  110. <Fragment>
  111. <GridEditable
  112. aria-label={t('Queues')}
  113. isLoading={isLoading}
  114. error={error}
  115. data={data}
  116. columnOrder={COLUMN_ORDER}
  117. columnSortBy={[
  118. {
  119. key: sort.field,
  120. order: sort.kind,
  121. },
  122. ]}
  123. grid={{
  124. renderHeadCell: column =>
  125. renderHeadCell({
  126. column,
  127. sort,
  128. location,
  129. sortParameterName: QueryParameterNames.DESTINATIONS_SORT,
  130. }),
  131. renderBodyCell: (column, row) =>
  132. renderBodyCell(column, row, meta, location, organization),
  133. }}
  134. location={location}
  135. />
  136. <Pagination pageLinks={pageLinks} onCursor={handleCursor} />
  137. </Fragment>
  138. );
  139. }
  140. function renderBodyCell(
  141. column: Column,
  142. row: Row,
  143. meta: EventsMetaType | undefined,
  144. location: Location,
  145. organization: Organization
  146. ) {
  147. const key = column.key;
  148. if (row[key] === undefined) {
  149. return (
  150. <AlignRight>
  151. <NoValue>{' \u2014 '}</NoValue>
  152. </AlignRight>
  153. );
  154. }
  155. if (key === 'messaging.destination.name' && row[key]) {
  156. return <DestinationCell destination={row[key]} />;
  157. }
  158. if (key.startsWith('count')) {
  159. return <AlignRight>{formatAbbreviatedNumber(row[key])}</AlignRight>;
  160. }
  161. if (key.startsWith('avg')) {
  162. const renderer = FIELD_FORMATTERS.duration.renderFunc;
  163. return renderer(key, row);
  164. }
  165. // Need to invert trace_status_rate(ok) to show error rate
  166. if (key === 'trace_status_rate(ok)') {
  167. const formatter = FIELD_FORMATTERS.percentage.renderFunc;
  168. return (
  169. <AlignRight>
  170. {formatter(key, {'trace_status_rate(ok)': 1 - (row[key] ?? 0)})}
  171. </AlignRight>
  172. );
  173. }
  174. if (!meta?.fields) {
  175. return row[column.key];
  176. }
  177. const renderer = getFieldRenderer(column.key, meta.fields, false);
  178. return renderer(row, {
  179. location,
  180. organization,
  181. unit: meta.units?.[column.key],
  182. });
  183. }
  184. function DestinationCell({destination}: {destination: string}) {
  185. const moduleURL = useQueueModuleURL();
  186. const {query} = useLocation();
  187. const queryString = {
  188. ...query,
  189. destination,
  190. };
  191. return (
  192. <NoOverflow>
  193. <Link to={`${moduleURL}/destination/?${qs.stringify(queryString)}`}>
  194. {destination}
  195. </Link>
  196. </NoOverflow>
  197. );
  198. }
  199. const NoOverflow = styled('span')`
  200. overflow: hidden;
  201. `;
  202. const AlignRight = styled('span')`
  203. text-align: right;
  204. `;
  205. const NoValue = styled('span')`
  206. color: ${p => p.theme.gray300};
  207. `;