pageOverviewWebVitalsDetailPanel.tsx 14 KB

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