pageOverviewWebVitalsDetailPanel.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. import {useMemo} from 'react';
  2. import {Link} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import type {LineChartSeries} from 'sentry/components/charts/lineChart';
  5. import type {
  6. GridColumnHeader,
  7. GridColumnOrder,
  8. GridColumnSortBy,
  9. } from 'sentry/components/gridEditable';
  10. import GridEditable, {COL_WIDTH_UNDEFINED} from 'sentry/components/gridEditable';
  11. import {Tooltip} from 'sentry/components/tooltip';
  12. import {t} from 'sentry/locale';
  13. import {defined} from 'sentry/utils';
  14. import EventView from 'sentry/utils/discover/eventView';
  15. import {
  16. generateEventSlug,
  17. generateLinkToEventInTraceView,
  18. } from 'sentry/utils/discover/urls';
  19. import {getShortEventId} from 'sentry/utils/events';
  20. import {getDuration} from 'sentry/utils/formatters';
  21. import {PageAlert, PageAlertProvider} from 'sentry/utils/performance/contexts/pageAlert';
  22. import {generateProfileFlamechartRoute} from 'sentry/utils/profiling/routes';
  23. import useReplayExists from 'sentry/utils/replayCount/useReplayExists';
  24. import {useLocation} from 'sentry/utils/useLocation';
  25. import useOrganization from 'sentry/utils/useOrganization';
  26. import useProjects from 'sentry/utils/useProjects';
  27. import {useRoutes} from 'sentry/utils/useRoutes';
  28. import {PerformanceBadge} from 'sentry/views/performance/browser/webVitals/components/performanceBadge';
  29. import {WebVitalDetailHeader} from 'sentry/views/performance/browser/webVitals/components/webVitalDescription';
  30. import {WebVitalStatusLineChart} from 'sentry/views/performance/browser/webVitals/components/webVitalStatusLineChart';
  31. import useProfileExists from 'sentry/views/performance/browser/webVitals/utils/profiling/useProfileExists';
  32. import {useProjectRawWebVitalsQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/rawWebVitalsQueries/useProjectRawWebVitalsQuery';
  33. import {useProjectRawWebVitalsValuesTimeseriesQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/rawWebVitalsQueries/useProjectRawWebVitalsValuesTimeseriesQuery';
  34. import {calculatePerformanceScoreFromStoredTableDataRow} from 'sentry/views/performance/browser/webVitals/utils/queries/storedScoreQueries/calculatePerformanceScoreFromStored';
  35. import {useProjectWebVitalsScoresQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/storedScoreQueries/useProjectWebVitalsScoresQuery';
  36. import {useInteractionsCategorizedSamplesQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/useInteractionsCategorizedSamplesQuery';
  37. import {useTransactionsCategorizedSamplesQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/useTransactionsCategorizedSamplesQuery';
  38. import type {
  39. InteractionSpanSampleRowWithScore,
  40. TransactionSampleRowWithScore,
  41. WebVitals,
  42. } from 'sentry/views/performance/browser/webVitals/utils/types';
  43. import {generateReplayLink} from 'sentry/views/performance/transactionSummary/utils';
  44. import DetailPanel from 'sentry/views/starfish/components/detailPanel';
  45. import {SpanIndexedField} from 'sentry/views/starfish/types';
  46. type Column = GridColumnHeader;
  47. const columnOrder: GridColumnOrder[] = [
  48. {key: 'id', width: COL_WIDTH_UNDEFINED, name: t('Transaction')},
  49. {key: 'replayId', width: COL_WIDTH_UNDEFINED, name: t('Replay')},
  50. {key: 'profile.id', width: COL_WIDTH_UNDEFINED, name: t('Profile')},
  51. {key: 'webVital', width: COL_WIDTH_UNDEFINED, name: t('Web Vital')},
  52. {key: 'score', width: COL_WIDTH_UNDEFINED, name: t('Score')},
  53. ];
  54. const inpColumnOrder: GridColumnOrder[] = [
  55. {
  56. key: SpanIndexedField.SPAN_DESCRIPTION,
  57. width: COL_WIDTH_UNDEFINED,
  58. name: t('Interaction Target'),
  59. },
  60. {key: 'profile.id', width: COL_WIDTH_UNDEFINED, name: t('Profile')},
  61. {key: 'replayId', width: COL_WIDTH_UNDEFINED, name: t('Replay')},
  62. {key: 'webVital', width: COL_WIDTH_UNDEFINED, name: t('Inp')},
  63. {key: 'score', width: COL_WIDTH_UNDEFINED, name: t('Score')},
  64. ];
  65. const sort: GridColumnSortBy<keyof TransactionSampleRowWithScore> = {
  66. key: 'totalScore',
  67. order: 'desc',
  68. };
  69. const inpSort: GridColumnSortBy<keyof InteractionSpanSampleRowWithScore> = {
  70. key: 'inpScore',
  71. order: 'desc',
  72. };
  73. export function PageOverviewWebVitalsDetailPanel({
  74. webVital,
  75. onClose,
  76. }: {
  77. onClose: () => void;
  78. webVital: WebVitals | null;
  79. }) {
  80. const location = useLocation();
  81. const {projects} = useProjects();
  82. const organization = useOrganization();
  83. const routes = useRoutes();
  84. const {replayExists} = useReplayExists();
  85. const isInp = webVital === 'inp';
  86. const replayLinkGenerator = generateReplayLink(routes);
  87. const project = useMemo(
  88. () => projects.find(p => p.id === String(location.query.project)),
  89. [projects, location.query.project]
  90. );
  91. const transaction = location.query.transaction
  92. ? Array.isArray(location.query.transaction)
  93. ? location.query.transaction[0]
  94. : location.query.transaction
  95. : undefined;
  96. const {data: projectData} = useProjectRawWebVitalsQuery({transaction});
  97. const {data: projectScoresData} = useProjectWebVitalsScoresQuery({
  98. weightWebVital: webVital ?? 'total',
  99. transaction,
  100. });
  101. const projectScore = calculatePerformanceScoreFromStoredTableDataRow(
  102. projectScoresData?.data?.[0]
  103. );
  104. const {data: transactionsTableData, isLoading: isTransactionWebVitalsQueryLoading} =
  105. useTransactionsCategorizedSamplesQuery({
  106. transaction: transaction ?? '',
  107. webVital,
  108. enabled: Boolean(webVital) && !isInp,
  109. });
  110. const {data: inpTableData, isLoading: isInteractionsLoading} =
  111. useInteractionsCategorizedSamplesQuery({
  112. transaction: transaction ?? '',
  113. enabled: Boolean(webVital) && isInp,
  114. });
  115. const {profileExists} = useProfileExists(
  116. inpTableData.filter(row => row['profile.id']).map(row => row['profile.id'])
  117. );
  118. const {data: timeseriesData, isLoading: isTimeseriesLoading} =
  119. useProjectRawWebVitalsValuesTimeseriesQuery({transaction});
  120. const webVitalData: LineChartSeries = {
  121. data:
  122. !isTimeseriesLoading && webVital
  123. ? timeseriesData?.[webVital].map(({name, value}) => ({
  124. name,
  125. value,
  126. }))
  127. : [],
  128. seriesName: webVital ?? '',
  129. };
  130. const getProjectSlug = (row: TransactionSampleRowWithScore): string => {
  131. return project && !Array.isArray(location.query.project)
  132. ? project.slug
  133. : row.projectSlug;
  134. };
  135. const renderHeadCell = (col: Column) => {
  136. if (col.key === 'transaction') {
  137. return <NoOverflow>{col.name}</NoOverflow>;
  138. }
  139. if (col.key === 'webVital') {
  140. return <AlignRight>{`${webVital}`}</AlignRight>;
  141. }
  142. if (col.key === 'score' || col.key === 'measurements.score.inp') {
  143. return <AlignCenter>{`${webVital} ${col.name}`}</AlignCenter>;
  144. }
  145. if (col.key === 'replayId' || col.key === 'profile.id') {
  146. return <AlignCenter>{col.name}</AlignCenter>;
  147. }
  148. return <NoOverflow>{col.name}</NoOverflow>;
  149. };
  150. const getFormattedDuration = (value: number) => {
  151. if (value === undefined) {
  152. return null;
  153. }
  154. if (value < 1000) {
  155. return getDuration(value / 1000, 0, true);
  156. }
  157. return getDuration(value / 1000, 2, true);
  158. };
  159. const renderBodyCell = (col: Column, row: TransactionSampleRowWithScore) => {
  160. const {key} = col;
  161. const projectSlug = getProjectSlug(row);
  162. if (key === 'score') {
  163. if (row[`measurements.${webVital}`] !== undefined) {
  164. return (
  165. <AlignCenter>
  166. <PerformanceBadge score={row[`${webVital}Score`]} />
  167. </AlignCenter>
  168. );
  169. }
  170. return null;
  171. }
  172. if (col.key === 'webVital') {
  173. const value = row[`measurements.${webVital}`];
  174. if (value === undefined) {
  175. return (
  176. <AlignRight>
  177. <NoValue>{t('(no value)')}</NoValue>
  178. </AlignRight>
  179. );
  180. }
  181. const formattedValue =
  182. webVital === 'cls' ? value?.toFixed(2) : getFormattedDuration(value);
  183. return <AlignRight>{formattedValue}</AlignRight>;
  184. }
  185. if (key === 'id') {
  186. const eventTarget = generateLinkToEventInTraceView({
  187. eventSlug: generateEventSlug({id: row.id, project: projectSlug}),
  188. dataRow: row,
  189. organization,
  190. eventView: EventView.fromLocation(location),
  191. location,
  192. });
  193. return (
  194. <NoOverflow>
  195. <Link to={eventTarget}>{getShortEventId(row.id)}</Link>
  196. </NoOverflow>
  197. );
  198. }
  199. if (key === 'replayId') {
  200. const replayTarget =
  201. row['transaction.duration'] !== undefined &&
  202. replayLinkGenerator(
  203. organization,
  204. {
  205. replayId: row.replayId,
  206. id: row.id,
  207. 'transaction.duration': row['transaction.duration'],
  208. timestamp: row.timestamp,
  209. },
  210. undefined
  211. );
  212. return row.replayId && replayTarget && replayExists(row[key]) ? (
  213. <AlignCenter>
  214. <Link to={replayTarget}>{getShortEventId(row.replayId)}</Link>
  215. </AlignCenter>
  216. ) : (
  217. <AlignCenter>
  218. <NoValue>{t('(no value)')}</NoValue>
  219. </AlignCenter>
  220. );
  221. }
  222. if (key === 'profile.id') {
  223. if (!defined(project) || !defined(row['profile.id'])) {
  224. return (
  225. <AlignCenter>
  226. <NoValue>{t('(no value)')}</NoValue>
  227. </AlignCenter>
  228. );
  229. }
  230. const target = generateProfileFlamechartRoute({
  231. orgSlug: organization.slug,
  232. projectSlug,
  233. profileId: String(row['profile.id']),
  234. });
  235. return (
  236. <AlignCenter>
  237. <Link to={target}>{getShortEventId(row['profile.id'])}</Link>
  238. </AlignCenter>
  239. );
  240. }
  241. return <AlignRight>{row[key]}</AlignRight>;
  242. };
  243. const renderInpBodyCell = (col: Column, row: InteractionSpanSampleRowWithScore) => {
  244. const {key} = col;
  245. if (key === 'score') {
  246. if (row[`measurements.${webVital}`] !== undefined) {
  247. return (
  248. <AlignCenter>
  249. <PerformanceBadge score={row[`${webVital}Score`]} />
  250. </AlignCenter>
  251. );
  252. }
  253. return null;
  254. }
  255. if (col.key === 'webVital') {
  256. const value = row[`measurements.${webVital}`];
  257. if (value === undefined) {
  258. return (
  259. <AlignRight>
  260. <NoValue>{t('(no value)')}</NoValue>
  261. </AlignRight>
  262. );
  263. }
  264. const formattedValue =
  265. webVital === 'cls' ? value?.toFixed(2) : getFormattedDuration(value);
  266. return <AlignRight>{formattedValue}</AlignRight>;
  267. }
  268. if (key === 'replayId') {
  269. const replayTarget = replayLinkGenerator(
  270. organization,
  271. {
  272. replayId: row.replayId,
  273. id: '', // id doesn't actually matter here. Just to satisfy type.
  274. 'transaction.duration': isInp
  275. ? row[SpanIndexedField.SPAN_SELF_TIME]
  276. : row['transaction.duration'],
  277. timestamp: row.timestamp,
  278. },
  279. undefined
  280. );
  281. return row.replayId && replayTarget && replayExists(row[key]) ? (
  282. <AlignCenter>
  283. <Link to={replayTarget}>{getShortEventId(row.replayId)}</Link>
  284. </AlignCenter>
  285. ) : (
  286. <AlignCenter>
  287. <NoValue>{t('(no value)')}</NoValue>
  288. </AlignCenter>
  289. );
  290. }
  291. if (key === 'profile.id') {
  292. if (
  293. !defined(project) ||
  294. !defined(row['profile.id']) ||
  295. !profileExists(row['profile.id'])
  296. ) {
  297. return (
  298. <AlignCenter>
  299. <NoValue>{t('(no value)')}</NoValue>
  300. </AlignCenter>
  301. );
  302. }
  303. const target = generateProfileFlamechartRoute({
  304. orgSlug: organization.slug,
  305. projectSlug: project.slug,
  306. profileId: String(row['profile.id']),
  307. });
  308. return (
  309. <AlignCenter>
  310. <Link to={target}>{getShortEventId(row['profile.id'])}</Link>
  311. </AlignCenter>
  312. );
  313. }
  314. if (key === SpanIndexedField.SPAN_DESCRIPTION) {
  315. return (
  316. <NoOverflow>
  317. <Tooltip title={row[key]}>{row[key]}</Tooltip>
  318. </NoOverflow>
  319. );
  320. }
  321. return <AlignRight>{row[key]}</AlignRight>;
  322. };
  323. const webVitalScore = projectScore[`${webVital}Score`];
  324. const webVitalValue = projectData?.data[0]?.[`p75(measurements.${webVital})`] as
  325. | number
  326. | undefined;
  327. return (
  328. <PageAlertProvider>
  329. <DetailPanel detailKey={webVital ?? undefined} onClose={onClose}>
  330. {webVital && (
  331. <WebVitalDetailHeader
  332. value={
  333. webVitalValue !== undefined
  334. ? webVital !== 'cls'
  335. ? getDuration(webVitalValue / 1000, 2, true)
  336. : (webVitalValue as number)?.toFixed(2)
  337. : undefined
  338. }
  339. webVital={webVital}
  340. score={webVitalScore}
  341. />
  342. )}
  343. <ChartContainer>
  344. {webVital && <WebVitalStatusLineChart webVitalSeries={webVitalData} />}
  345. </ChartContainer>
  346. <TableContainer>
  347. {isInp ? (
  348. <GridEditable
  349. data={inpTableData}
  350. isLoading={isInteractionsLoading}
  351. columnOrder={inpColumnOrder}
  352. columnSortBy={[inpSort]}
  353. grid={{
  354. renderHeadCell,
  355. renderBodyCell: renderInpBodyCell,
  356. }}
  357. location={location}
  358. />
  359. ) : (
  360. <GridEditable
  361. data={transactionsTableData}
  362. isLoading={isTransactionWebVitalsQueryLoading}
  363. columnOrder={columnOrder}
  364. columnSortBy={[sort]}
  365. grid={{
  366. renderHeadCell,
  367. renderBodyCell,
  368. }}
  369. location={location}
  370. />
  371. )}
  372. </TableContainer>
  373. <PageAlert />
  374. </DetailPanel>
  375. </PageAlertProvider>
  376. );
  377. }
  378. const NoOverflow = styled('span')`
  379. overflow: hidden;
  380. text-overflow: ellipsis;
  381. white-space: nowrap;
  382. `;
  383. const AlignRight = styled('span')<{color?: string}>`
  384. text-align: right;
  385. width: 100%;
  386. ${p => (p.color ? `color: ${p.color};` : '')}
  387. `;
  388. const AlignCenter = styled('span')`
  389. text-align: center;
  390. width: 100%;
  391. `;
  392. const ChartContainer = styled('div')`
  393. position: relative;
  394. flex: 1;
  395. `;
  396. const NoValue = styled('span')`
  397. color: ${p => p.theme.gray300};
  398. `;
  399. const TableContainer = styled('div')`
  400. margin-bottom: 80px;
  401. `;