sampleTable.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import {Fragment} from 'react';
  2. import {Link} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {PlatformIcon} from 'platformicons';
  5. import * as qs from 'query-string';
  6. import {LinkButton} from 'sentry/components/button';
  7. import Duration from 'sentry/components/duration';
  8. import GridEditable, {
  9. COL_WIDTH_UNDEFINED,
  10. GridColumnHeader,
  11. GridColumnOrder,
  12. } from 'sentry/components/gridEditable';
  13. import {extractSelectionParameters} from 'sentry/components/organizations/pageFilters/utils';
  14. import TextOverflow from 'sentry/components/textOverflow';
  15. import {Tooltip} from 'sentry/components/tooltip';
  16. import {CHART_PALETTE} from 'sentry/constants/chartPalette';
  17. import {IconArrow, IconProfiling} from 'sentry/icons';
  18. import {t} from 'sentry/locale';
  19. import {space} from 'sentry/styles/space';
  20. import {MRI} from 'sentry/types';
  21. import {generateEventSlug} from 'sentry/utils/discover/urls';
  22. import {getDuration} from 'sentry/utils/formatters';
  23. import {useCorrelatedSamples} from 'sentry/utils/metrics/useMetricsCodeLocations';
  24. import {getTransactionDetailsUrl} from 'sentry/utils/performance/urls';
  25. import {useLocation} from 'sentry/utils/useLocation';
  26. import useOrganization from 'sentry/utils/useOrganization';
  27. import useProjects from 'sentry/utils/useProjects';
  28. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  29. import ColorBar from 'sentry/views/performance/vitalDetail/colorBar';
  30. import {MetricCorrelation, MetricRange} from '../../utils/metrics/index';
  31. /**
  32. * Limits the number of spans to the top n + an "other" entry
  33. */
  34. function sortAndLimitSpans(samples: MetricCorrelation['spansSummary'], limit: number) {
  35. if (!samples) {
  36. return [];
  37. }
  38. const sortedSpans = [...samples].sort((a, b) => b.spanDuration - a.spanDuration);
  39. return sortedSpans.slice(0, limit).concat([
  40. {
  41. spanDuration: sortedSpans
  42. .slice(limit)
  43. .reduce((acc, span) => acc + span.spanDuration, 0),
  44. spanOp: `+${sortedSpans.length - limit} more`,
  45. },
  46. ]);
  47. }
  48. export type SamplesTableProps = MetricRange & {
  49. mri?: MRI;
  50. query?: string;
  51. };
  52. type Column = GridColumnHeader<keyof MetricCorrelation>;
  53. const columnOrder: GridColumnOrder<keyof MetricCorrelation>[] = [
  54. {key: 'transactionId', width: COL_WIDTH_UNDEFINED, name: 'Event ID'},
  55. {key: 'segmentName', width: COL_WIDTH_UNDEFINED, name: 'Transaction'},
  56. {key: 'spansNumber', width: COL_WIDTH_UNDEFINED, name: 'Number of Spans'},
  57. {key: 'spansSummary', width: COL_WIDTH_UNDEFINED, name: 'Spans Summary'},
  58. {key: 'duration', width: COL_WIDTH_UNDEFINED, name: 'Duration'},
  59. {key: 'traceId', width: COL_WIDTH_UNDEFINED, name: 'Trace ID'},
  60. {key: 'profileId', width: COL_WIDTH_UNDEFINED, name: 'Profile'},
  61. ];
  62. export function SampleTable({mri, ...metricMetaOptions}: SamplesTableProps) {
  63. const location = useLocation();
  64. const organization = useOrganization();
  65. const {projects} = useProjects();
  66. const {data, isFetching} = useCorrelatedSamples(mri, metricMetaOptions);
  67. const rows = data?.metrics
  68. .map(m => m.metricSpans)
  69. .flat()
  70. .filter(Boolean)
  71. // We only want to show the first 10 correlations
  72. .slice(0, 10) as MetricCorrelation[];
  73. function renderHeadCell(col: Column) {
  74. if (col.key === 'profileId') {
  75. return <AlignCenter>{col.name}</AlignCenter>;
  76. }
  77. if (col.key === 'duration') {
  78. return (
  79. <DurationHeadCell>
  80. {col.name}
  81. <IconArrow size="xs" direction="down" />
  82. </DurationHeadCell>
  83. );
  84. }
  85. return <span>{col.name}</span>;
  86. }
  87. function renderBodyCell(col: Column, row: MetricCorrelation) {
  88. const {key} = col;
  89. if (!row[key]) {
  90. return <AlignCenter>{'\u2014'}</AlignCenter>;
  91. }
  92. const project = projects.find(p => parseInt(p.id, 10) === row.projectId);
  93. const eventSlug = generateEventSlug({
  94. id: row.transactionId,
  95. project: project?.slug,
  96. });
  97. if (key === 'transactionId') {
  98. return (
  99. <span>
  100. <Link
  101. to={getTransactionDetailsUrl(
  102. organization.slug,
  103. eventSlug,
  104. undefined,
  105. {referrer: 'metrics'},
  106. row.spanId
  107. )}
  108. target="_blank"
  109. >
  110. {row.transactionId.slice(0, 8)}
  111. </Link>
  112. </span>
  113. );
  114. }
  115. if (key === 'segmentName') {
  116. return (
  117. <TextOverflow>
  118. <Tooltip title={project?.slug}>
  119. <StyledPlatformIcon platform={project?.platform || 'default'} />
  120. </Tooltip>
  121. <Link
  122. to={normalizeUrl(
  123. `/organizations/${organization.slug}/performance/summary/?${qs.stringify({
  124. ...extractSelectionParameters(location.query),
  125. project: project?.id,
  126. transaction: row.segmentName,
  127. referrer: 'metrics',
  128. })}`
  129. )}
  130. >
  131. {row.segmentName}
  132. </Link>
  133. </TextOverflow>
  134. );
  135. }
  136. if (key === 'duration') {
  137. // We get duration in miliseconds, but getDuration expects seconds
  138. return <span>{getDuration(row.duration / 1000, 2, true)}</span>;
  139. }
  140. if (key === 'traceId') {
  141. return (
  142. <span>
  143. <Link
  144. to={normalizeUrl(
  145. `/organizations/${organization.slug}/performance/trace/${row.traceId}/`
  146. )}
  147. >
  148. {row.traceId.slice(0, 8)}
  149. </Link>
  150. </span>
  151. );
  152. }
  153. if (key === 'spansSummary') {
  154. const totalDuration =
  155. row.spansSummary?.reduce(
  156. (acc, spanSummary) => acc + spanSummary.spanDuration,
  157. 0
  158. ) ?? 0;
  159. if (totalDuration === 0) {
  160. return <NoValue>{t('(no value)')}</NoValue>;
  161. }
  162. const preparedSpans = sortAndLimitSpans(row.spansSummary, 5);
  163. return (
  164. <StyledColorBar
  165. colorStops={preparedSpans.map((spanSummary, i) => {
  166. return {
  167. color: CHART_PALETTE[4][i % CHART_PALETTE.length],
  168. percent: (spanSummary.spanDuration / totalDuration) * 100,
  169. renderBarStatus: (barStatus, barKey) => (
  170. <Tooltip
  171. title={
  172. <Fragment>
  173. <div>{spanSummary.spanOp}</div>
  174. <div>
  175. <Duration
  176. seconds={spanSummary.spanDuration / 1000}
  177. fixedDigits={2}
  178. abbreviation
  179. />
  180. </div>
  181. </Fragment>
  182. }
  183. key={barKey}
  184. skipWrapper
  185. >
  186. {barStatus}
  187. </Tooltip>
  188. ),
  189. };
  190. })}
  191. />
  192. );
  193. }
  194. if (key === 'profileId') {
  195. return (
  196. <AlignCenter>
  197. <Tooltip title={t('View Profile')}>
  198. <LinkButton
  199. to={normalizeUrl(
  200. `/organizations/${organization.slug}/profiling/profile/${project?.slug}/${row.profileId}/flamegraph/`
  201. )}
  202. size="xs"
  203. >
  204. <IconProfiling size="xs" />
  205. </LinkButton>
  206. </Tooltip>
  207. </AlignCenter>
  208. );
  209. }
  210. return <span>{row[col.key]}</span>;
  211. }
  212. return (
  213. <GridEditable
  214. isLoading={isFetching}
  215. columnOrder={columnOrder}
  216. columnSortBy={[]}
  217. data={rows}
  218. grid={{
  219. renderHeadCell,
  220. renderBodyCell,
  221. }}
  222. emptyMessage={mri ? t('No samples found') : t('Choose a metric to display data.')}
  223. location={location}
  224. />
  225. );
  226. }
  227. const AlignCenter = styled('span')`
  228. display: block;
  229. margin: auto;
  230. text-align: center;
  231. width: 100%;
  232. `;
  233. const DurationHeadCell = styled('span')`
  234. display: flex;
  235. gap: ${space(0.25)};
  236. `;
  237. const StyledPlatformIcon = styled(PlatformIcon)`
  238. margin-right: ${space(1)};
  239. height: ${space(3)};
  240. `;
  241. const StyledColorBar = styled(ColorBar)`
  242. margin-bottom: 0px;
  243. `;
  244. const NoValue = styled('span')`
  245. color: ${p => p.theme.gray300};
  246. `;