queuesTable.tsx 6.4 KB

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