transactionSamplesTable.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import {CSSProperties, Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import DateTime from 'sentry/components/dateTime';
  4. import GridEditable, {
  5. COL_WIDTH_UNDEFINED,
  6. GridColumnHeader,
  7. } from 'sentry/components/gridEditable';
  8. import Link from 'sentry/components/links/link';
  9. import QuestionTooltip from 'sentry/components/questionTooltip';
  10. import {t} from 'sentry/locale';
  11. import {NewQuery} from 'sentry/types';
  12. import EventView from 'sentry/utils/discover/eventView';
  13. import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  14. import {SPAN_OP_RELATIVE_BREAKDOWN_FIELD} from 'sentry/utils/discover/fields';
  15. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  16. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  17. import {useLocation} from 'sentry/utils/useLocation';
  18. import useOrganization from 'sentry/utils/useOrganization';
  19. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  20. import {DurationComparisonCell} from 'sentry/views/starfish/components/samplesTable/common';
  21. import useErrorSamples from 'sentry/views/starfish/components/samplesTable/useErrorSamples';
  22. import {DurationCell} from 'sentry/views/starfish/components/tableCells/durationCell';
  23. import {
  24. OverflowEllipsisTextContainer,
  25. TextAlignLeft,
  26. TextAlignRight,
  27. } from 'sentry/views/starfish/components/textAlign';
  28. import {DataTitles} from 'sentry/views/starfish/views/spans/types';
  29. import {SampleFilter} from 'sentry/views/starfish/views/webServiceView/endpointOverview';
  30. type Keys =
  31. | 'id'
  32. | 'profile_id'
  33. | 'timestamp'
  34. | 'transaction.duration'
  35. | 'avg_comparison'
  36. | 'span_ops_breakdown.relative'
  37. | 'http.status_code'
  38. | 'transaction.status';
  39. type TableColumnHeader = GridColumnHeader<Keys>;
  40. type Props = {
  41. data: DataRow[];
  42. isLoading: boolean;
  43. queryConditions: string[];
  44. sampleFilter: SampleFilter;
  45. setHighlightedId: (string) => void;
  46. averageDuration?: number;
  47. highlightedId?: string;
  48. };
  49. export type DataRow = {
  50. 'http.status_code': number;
  51. id: string;
  52. profile_id: string;
  53. 'spans.browser': number;
  54. 'spans.db': number;
  55. 'spans.http': number;
  56. 'spans.resource': number;
  57. 'spans.ui': number;
  58. timestamp: string;
  59. 'transaction.duration': number;
  60. 'transaction.status': string;
  61. };
  62. export function TransactionSamplesTable({
  63. queryConditions,
  64. sampleFilter,
  65. data,
  66. isLoading,
  67. averageDuration,
  68. setHighlightedId,
  69. highlightedId,
  70. }: Props) {
  71. const location = useLocation();
  72. const organization = useOrganization();
  73. const query = new MutableSearch(queryConditions);
  74. const savedQuery: NewQuery = {
  75. id: undefined,
  76. name: 'Endpoint Overview Samples',
  77. query: query.formatString(),
  78. fields: [],
  79. dataset: DiscoverDatasets.DISCOVER,
  80. version: 2,
  81. };
  82. const columnOrder: TableColumnHeader[] = [
  83. {
  84. key: 'id',
  85. name: 'Event ID',
  86. width: 100,
  87. },
  88. ...(sampleFilter === 'ALL'
  89. ? [
  90. {
  91. key: 'profile_id',
  92. name: 'Profile ID',
  93. width: 140,
  94. } as TableColumnHeader,
  95. {
  96. key: SPAN_OP_RELATIVE_BREAKDOWN_FIELD,
  97. name: 'Operation Duration',
  98. width: 180,
  99. } as TableColumnHeader,
  100. {
  101. key: 'timestamp',
  102. name: 'Timestamp',
  103. width: 230,
  104. } as TableColumnHeader,
  105. {
  106. key: 'transaction.duration',
  107. name: DataTitles.duration,
  108. width: 100,
  109. } as TableColumnHeader,
  110. {
  111. key: 'avg_comparison',
  112. name: 'Compared to Average',
  113. width: 100,
  114. } as TableColumnHeader,
  115. ]
  116. : [
  117. {
  118. key: 'http.status_code',
  119. name: 'Response Code',
  120. width: COL_WIDTH_UNDEFINED,
  121. } as TableColumnHeader,
  122. {
  123. key: 'transaction.status',
  124. name: 'Status',
  125. width: COL_WIDTH_UNDEFINED,
  126. } as TableColumnHeader,
  127. {
  128. key: 'timestamp',
  129. name: 'Timestamp',
  130. width: 230,
  131. } as TableColumnHeader,
  132. ]),
  133. ];
  134. const eventView = EventView.fromNewQueryWithLocation(savedQuery, location);
  135. const {isLoading: isErrorSamplesLoading, data: errorSamples} =
  136. useErrorSamples(eventView);
  137. function renderHeadCell(column: GridColumnHeader): React.ReactNode {
  138. if (column.key === 'avg_comparison' || column.key === 'transaction.duration') {
  139. return (
  140. <TextAlignRight>
  141. <OverflowEllipsisTextContainer>{column.name}</OverflowEllipsisTextContainer>
  142. </TextAlignRight>
  143. );
  144. }
  145. if (column.key === SPAN_OP_RELATIVE_BREAKDOWN_FIELD) {
  146. return (
  147. <Fragment>
  148. {column.name}
  149. <StyledIconQuestion
  150. size="xs"
  151. position="top"
  152. title={t(
  153. `Span durations are summed over the course of an entire transaction. Any overlapping spans are only counted once.`
  154. )}
  155. />
  156. </Fragment>
  157. );
  158. }
  159. return <OverflowEllipsisTextContainer>{column.name}</OverflowEllipsisTextContainer>;
  160. }
  161. function renderBodyCell(column: TableColumnHeader, row: DataRow): React.ReactNode {
  162. const commonProps = {
  163. style: (row.id === highlightedId
  164. ? {fontWeight: 'bold'}
  165. : {}) satisfies CSSProperties,
  166. onMouseEnter: () => setHighlightedId(row.id),
  167. };
  168. if (column.key === 'id') {
  169. return (
  170. <Link
  171. {...commonProps}
  172. to={normalizeUrl(
  173. `/organizations/${organization.slug}/performance/${row['project.name']}:${row.id}`
  174. )}
  175. >
  176. {row.id.slice(0, 8)}
  177. </Link>
  178. );
  179. }
  180. if (column.key === 'profile_id') {
  181. return row.profile_id ? (
  182. <Link
  183. {...commonProps}
  184. to={normalizeUrl(
  185. `/organizations/${organization.slug}/profiling/profile/${row['project.name']}/${row.profile_id}/flamechart/`
  186. )}
  187. >
  188. {row.profile_id.slice(0, 8)}
  189. </Link>
  190. ) : (
  191. <div {...commonProps}>(no value)</div>
  192. );
  193. }
  194. if (column.key === 'transaction.duration') {
  195. return <DurationCell milliseconds={row['transaction.duration']} />;
  196. }
  197. if (column.key === 'timestamp') {
  198. return <DateTime {...commonProps} date={row[column.key]} year timeZone seconds />;
  199. }
  200. if (column.key === 'avg_comparison') {
  201. return (
  202. <DurationComparisonContainer {...commonProps}>
  203. <DurationComparisonCell
  204. duration={row['transaction.duration']}
  205. compareToDuration={averageDuration ?? 0}
  206. />
  207. </DurationComparisonContainer>
  208. );
  209. }
  210. if (column.key === SPAN_OP_RELATIVE_BREAKDOWN_FIELD) {
  211. return getFieldRenderer(column.key, {})(row, {
  212. location,
  213. organization,
  214. eventView,
  215. });
  216. }
  217. return <TextAlignLeft {...commonProps}>{row[column.key]}</TextAlignLeft>;
  218. }
  219. return (
  220. <div onMouseLeave={() => setHighlightedId(undefined)}>
  221. <GridEditable
  222. isLoading={sampleFilter === 'ALL' ? isLoading : isErrorSamplesLoading}
  223. data={sampleFilter === 'ALL' ? (data as DataRow[]) : (errorSamples as DataRow[])}
  224. columnOrder={columnOrder}
  225. columnSortBy={[]}
  226. location={location}
  227. grid={{
  228. renderHeadCell,
  229. renderBodyCell,
  230. }}
  231. />
  232. </div>
  233. );
  234. }
  235. const DurationComparisonContainer = styled('div')`
  236. text-align: right;
  237. width: 100%;
  238. display: inline-block;
  239. `;
  240. const StyledIconQuestion = styled(QuestionTooltip)`
  241. position: relative;
  242. left: 4px;
  243. `;