pageOverviewWebVitalsDetailPanel.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 {calculatePerformanceScoreFromStoredTableDataRow} from 'sentry/views/insights/browser/webVitals/queries/storedScoreQueries/calculatePerformanceScoreFromStored';
  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 = calculatePerformanceScoreFromStoredTableDataRow(
  110. projectScoresData?.data?.[0]
  111. );
  112. const {data: transactionsTableData, isLoading: isTransactionWebVitalsQueryLoading} =
  113. useTransactionsCategorizedSamplesQuery({
  114. transaction: transaction ?? '',
  115. webVital,
  116. enabled: Boolean(webVital) && !isInp,
  117. browserTypes,
  118. subregions,
  119. });
  120. const {data: inpTableData, isLoading: isInteractionsLoading} =
  121. useInteractionsCategorizedSamplesQuery({
  122. transaction: transaction ?? '',
  123. enabled: Boolean(webVital) && isInp,
  124. browserTypes,
  125. subregions,
  126. });
  127. const {profileExists} = useProfileExists(
  128. inpTableData.filter(row => row['profile.id']).map(row => row['profile.id'])
  129. );
  130. const {data: timeseriesData, isLoading: isTimeseriesLoading} =
  131. useProjectRawWebVitalsValuesTimeseriesQuery({transaction, browserTypes, subregions});
  132. const webVitalData: LineChartSeries = {
  133. data:
  134. !isTimeseriesLoading && webVital
  135. ? timeseriesData?.[webVital].map(({name, value}) => ({
  136. name,
  137. value,
  138. }))
  139. : [],
  140. seriesName: webVital ?? '',
  141. };
  142. const getProjectSlug = (row: TransactionSampleRowWithScore): string => {
  143. return project && !Array.isArray(location.query.project)
  144. ? project.slug
  145. : row.projectSlug;
  146. };
  147. const renderHeadCell = (col: Column) => {
  148. if (col.key === 'transaction') {
  149. return <NoOverflow>{col.name}</NoOverflow>;
  150. }
  151. if (col.key === 'webVital') {
  152. return <AlignRight>{`${webVital}`}</AlignRight>;
  153. }
  154. if (col.key === 'score' || col.key === 'measurements.score.inp') {
  155. return <AlignCenter>{`${webVital} ${col.name}`}</AlignCenter>;
  156. }
  157. if (col.key === 'replayId' || col.key === 'profile.id') {
  158. return <AlignCenter>{col.name}</AlignCenter>;
  159. }
  160. return <NoOverflow>{col.name}</NoOverflow>;
  161. };
  162. const getFormattedDuration = (value: number) => {
  163. if (value === undefined) {
  164. return null;
  165. }
  166. if (value < 1000) {
  167. return getDuration(value / 1000, 0, true);
  168. }
  169. return getDuration(value / 1000, 2, true);
  170. };
  171. const renderBodyCell = (col: Column, row: TransactionSampleRowWithScore) => {
  172. const {key} = col;
  173. const projectSlug = getProjectSlug(row);
  174. if (key === 'score') {
  175. if (row[`measurements.${webVital}`] !== undefined) {
  176. return (
  177. <AlignCenter>
  178. <PerformanceBadge score={row[`${webVital}Score`]} />
  179. </AlignCenter>
  180. );
  181. }
  182. return null;
  183. }
  184. if (col.key === 'webVital') {
  185. const value = row[`measurements.${webVital}`];
  186. if (value === undefined) {
  187. return (
  188. <AlignRight>
  189. <NoValue>{t('(no value)')}</NoValue>
  190. </AlignRight>
  191. );
  192. }
  193. const formattedValue =
  194. webVital === 'cls' ? value?.toFixed(2) : getFormattedDuration(value);
  195. return <AlignRight>{formattedValue}</AlignRight>;
  196. }
  197. if (key === 'id') {
  198. const eventTarget = generateLinkToEventInTraceView({
  199. eventId: row.id,
  200. traceSlug: row.trace,
  201. timestamp: row.timestamp,
  202. projectSlug,
  203. organization,
  204. location,
  205. source: TraceViewSources.WEB_VITALS_MODULE,
  206. });
  207. return (
  208. <NoOverflow>
  209. <Link to={eventTarget}>{getShortEventId(row.id)}</Link>
  210. </NoOverflow>
  211. );
  212. }
  213. if (key === 'replayId') {
  214. const replayTarget =
  215. row['transaction.duration'] !== undefined &&
  216. replayLinkGenerator(
  217. organization,
  218. {
  219. replayId: row.replayId,
  220. id: row.id,
  221. 'transaction.duration': row['transaction.duration'],
  222. timestamp: row.timestamp,
  223. },
  224. undefined
  225. );
  226. return row.replayId && replayTarget && replayExists(row[key]) ? (
  227. <AlignCenter>
  228. <Link to={replayTarget}>{getShortEventId(row.replayId)}</Link>
  229. </AlignCenter>
  230. ) : (
  231. <AlignCenter>
  232. <NoValue>{t('(no value)')}</NoValue>
  233. </AlignCenter>
  234. );
  235. }
  236. if (key === 'profile.id') {
  237. if (!defined(project) || !defined(row['profile.id'])) {
  238. return (
  239. <AlignCenter>
  240. <NoValue>{t('(no value)')}</NoValue>
  241. </AlignCenter>
  242. );
  243. }
  244. const target = generateProfileFlamechartRoute({
  245. orgSlug: organization.slug,
  246. projectSlug,
  247. profileId: String(row['profile.id']),
  248. });
  249. return (
  250. <AlignCenter>
  251. <Link to={target}>{getShortEventId(row['profile.id'])}</Link>
  252. </AlignCenter>
  253. );
  254. }
  255. return <AlignRight>{row[key]}</AlignRight>;
  256. };
  257. const renderInpBodyCell = (col: Column, row: InteractionSpanSampleRowWithScore) => {
  258. const {key} = col;
  259. if (key === 'score') {
  260. if (row[`measurements.${webVital}`] !== undefined) {
  261. return (
  262. <AlignCenter>
  263. <PerformanceBadge score={row[`${webVital}Score`]} />
  264. </AlignCenter>
  265. );
  266. }
  267. return null;
  268. }
  269. if (col.key === 'webVital') {
  270. const value = row[`measurements.${webVital}`];
  271. if (value === undefined) {
  272. return (
  273. <AlignRight>
  274. <NoValue>{t('(no value)')}</NoValue>
  275. </AlignRight>
  276. );
  277. }
  278. const formattedValue =
  279. webVital === 'cls' ? value?.toFixed(2) : getFormattedDuration(value);
  280. return <AlignRight>{formattedValue}</AlignRight>;
  281. }
  282. if (key === 'replayId') {
  283. const replayTarget = replayLinkGenerator(
  284. organization,
  285. {
  286. replayId: row.replayId,
  287. id: '', // id doesn't actually matter here. Just to satisfy type.
  288. 'transaction.duration': isInp
  289. ? row[SpanIndexedField.SPAN_SELF_TIME]
  290. : row['transaction.duration'],
  291. timestamp: row.timestamp,
  292. },
  293. undefined
  294. );
  295. return row.replayId && replayTarget && replayExists(row[key]) ? (
  296. <AlignCenter>
  297. <Link to={replayTarget}>{getShortEventId(row.replayId)}</Link>
  298. </AlignCenter>
  299. ) : (
  300. <AlignCenter>
  301. <NoValue>{t('(no value)')}</NoValue>
  302. </AlignCenter>
  303. );
  304. }
  305. if (key === 'profile.id') {
  306. if (
  307. !defined(project) ||
  308. !defined(row['profile.id']) ||
  309. !profileExists(row['profile.id'])
  310. ) {
  311. return (
  312. <AlignCenter>
  313. <NoValue>{t('(no value)')}</NoValue>
  314. </AlignCenter>
  315. );
  316. }
  317. const target = generateProfileFlamechartRoute({
  318. orgSlug: organization.slug,
  319. projectSlug: project.slug,
  320. profileId: String(row['profile.id']),
  321. });
  322. return (
  323. <AlignCenter>
  324. <Link to={target}>{getShortEventId(row['profile.id'])}</Link>
  325. </AlignCenter>
  326. );
  327. }
  328. if (key === SpanIndexedField.SPAN_DESCRIPTION) {
  329. return (
  330. <NoOverflow>
  331. <Tooltip title={row[key]}>{row[key]}</Tooltip>
  332. </NoOverflow>
  333. );
  334. }
  335. return <AlignRight>{row[key]}</AlignRight>;
  336. };
  337. const webVitalScore = projectScore[`${webVital}Score`];
  338. const webVitalValue = projectData?.data[0]?.[`p75(measurements.${webVital})`] as
  339. | number
  340. | undefined;
  341. return (
  342. <PageAlertProvider>
  343. <DetailPanel detailKey={webVital ?? undefined} onClose={onClose}>
  344. {webVital && (
  345. <WebVitalDetailHeader
  346. value={
  347. webVitalValue !== undefined
  348. ? webVital !== 'cls'
  349. ? getDuration(webVitalValue / 1000, 2, true)
  350. : (webVitalValue as number)?.toFixed(2)
  351. : undefined
  352. }
  353. webVital={webVital}
  354. score={webVitalScore}
  355. />
  356. )}
  357. <ChartContainer>
  358. {webVital && <WebVitalStatusLineChart webVitalSeries={webVitalData} />}
  359. </ChartContainer>
  360. <TableContainer>
  361. {isInp ? (
  362. <GridEditable
  363. data={inpTableData}
  364. isLoading={isInteractionsLoading}
  365. columnOrder={inpColumnOrder}
  366. columnSortBy={[inpSort]}
  367. grid={{
  368. renderHeadCell,
  369. renderBodyCell: renderInpBodyCell,
  370. }}
  371. />
  372. ) : (
  373. <GridEditable
  374. data={transactionsTableData}
  375. isLoading={isTransactionWebVitalsQueryLoading}
  376. columnOrder={columnOrder}
  377. columnSortBy={[sort]}
  378. grid={{
  379. renderHeadCell,
  380. renderBodyCell,
  381. }}
  382. />
  383. )}
  384. </TableContainer>
  385. <PageAlert />
  386. </DetailPanel>
  387. </PageAlertProvider>
  388. );
  389. }
  390. const NoOverflow = styled('span')`
  391. overflow: hidden;
  392. text-overflow: ellipsis;
  393. white-space: nowrap;
  394. `;
  395. const AlignRight = styled('span')<{color?: string}>`
  396. text-align: right;
  397. width: 100%;
  398. ${p => (p.color ? `color: ${p.color};` : '')}
  399. `;
  400. const AlignCenter = styled('span')`
  401. text-align: center;
  402. width: 100%;
  403. `;
  404. const ChartContainer = styled('div')`
  405. position: relative;
  406. flex: 1;
  407. `;
  408. const NoValue = styled('span')`
  409. color: ${p => p.theme.gray300};
  410. `;
  411. const TableContainer = styled('div')`
  412. margin-bottom: 80px;
  413. `;