queuesTable.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 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 {useNavigate} from 'sentry/utils/useNavigate';
  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 readonly 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 navigate = useNavigate();
  101. const location = useLocation();
  102. const organization = useOrganization();
  103. const {data, isPending, meta, pageLinks} = useQueuesByDestinationQuery({
  104. destination,
  105. sort,
  106. referrer: Referrer.QUEUES_LANDING_DESTINATIONS_TABLE,
  107. });
  108. const handleCursor: CursorHandler = (newCursor, pathname, query) => {
  109. navigate({
  110. pathname,
  111. query: {...query, [QueryParameterNames.DESTINATIONS_CURSOR]: newCursor},
  112. });
  113. };
  114. return (
  115. <Fragment>
  116. <GridEditable
  117. aria-label={t('Queues')}
  118. isLoading={isPending}
  119. error={error}
  120. data={data}
  121. columnOrder={COLUMN_ORDER}
  122. columnSortBy={[
  123. {
  124. key: sort.field,
  125. order: sort.kind,
  126. },
  127. ]}
  128. grid={{
  129. renderHeadCell: column =>
  130. renderHeadCell({
  131. column,
  132. sort,
  133. location,
  134. sortParameterName: QueryParameterNames.DESTINATIONS_SORT,
  135. }),
  136. renderBodyCell: (column, row) =>
  137. renderBodyCell(column, row, meta, location, organization),
  138. }}
  139. />
  140. <Pagination
  141. pageLinks={pageLinks}
  142. onCursor={handleCursor}
  143. paginationAnalyticsEvent={(direction: string) => {
  144. trackAnalytics('insight.general.table_paginate', {
  145. organization,
  146. source: ModuleName.QUEUE,
  147. direction,
  148. });
  149. }}
  150. />
  151. </Fragment>
  152. );
  153. }
  154. function renderBodyCell(
  155. column: Column,
  156. row: Row,
  157. meta: EventsMetaType | undefined,
  158. location: Location,
  159. organization: Organization
  160. ) {
  161. const key = column.key;
  162. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  163. if (row[key] === undefined) {
  164. return (
  165. <AlignRight>
  166. <NoValue>{' \u2014 '}</NoValue>
  167. </AlignRight>
  168. );
  169. }
  170. if (key === 'messaging.destination.name' && row[key]) {
  171. return <DestinationCell destination={row[key]} />;
  172. }
  173. if (key.startsWith('count')) {
  174. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  175. return <AlignRight>{formatAbbreviatedNumber(row[key])}</AlignRight>;
  176. }
  177. if (key.startsWith('avg')) {
  178. const renderer = FIELD_FORMATTERS.duration.renderFunc;
  179. return renderer(key, row);
  180. }
  181. // Need to invert trace_status_rate(ok) to show error rate
  182. if (key === 'trace_status_rate(ok)') {
  183. const formatter = FIELD_FORMATTERS.percentage.renderFunc;
  184. return (
  185. <AlignRight>
  186. {/* @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message */}
  187. {formatter(key, {'trace_status_rate(ok)': 1 - (row[key] ?? 0)})}
  188. </AlignRight>
  189. );
  190. }
  191. if (!meta?.fields) {
  192. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  193. return row[column.key];
  194. }
  195. const renderer = getFieldRenderer(column.key, meta.fields, false);
  196. return renderer(row, {
  197. location,
  198. organization,
  199. unit: meta.units?.[column.key],
  200. });
  201. }
  202. function DestinationCell({destination}: {destination: string}) {
  203. const moduleURL = useModuleURL('queue');
  204. const {query} = useLocation();
  205. const queryString = {
  206. ...query,
  207. destination,
  208. };
  209. return (
  210. <NoOverflow>
  211. <Link to={`${moduleURL}/destination/?${qs.stringify(queryString)}`}>
  212. {destination}
  213. </Link>
  214. </NoOverflow>
  215. );
  216. }
  217. const NoOverflow = styled('span')`
  218. overflow: hidden;
  219. `;
  220. const AlignRight = styled('span')`
  221. text-align: right;
  222. `;
  223. const NoValue = styled('span')`
  224. color: ${p => p.theme.gray300};
  225. `;