transactionSamplesTable.tsx 7.2 KB

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