pageOverviewWebVitalsDetailPanel.tsx 14 KB

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