pageOverviewWebVitalsDetailPanel.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 {calculatePerformanceScoreFromTableDataRow} from 'sentry/views/performance/browser/webVitals/utils/queries/rawWebVitalsQueries/calculatePerformanceScore';
  33. import {useProjectRawWebVitalsQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/rawWebVitalsQueries/useProjectRawWebVitalsQuery';
  34. import {useProjectRawWebVitalsValuesTimeseriesQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/rawWebVitalsQueries/useProjectRawWebVitalsValuesTimeseriesQuery';
  35. import {calculatePerformanceScoreFromStoredTableDataRow} from 'sentry/views/performance/browser/webVitals/utils/queries/storedScoreQueries/calculatePerformanceScoreFromStored';
  36. import {useProjectWebVitalsScoresQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/storedScoreQueries/useProjectWebVitalsScoresQuery';
  37. import {useInteractionsCategorizedSamplesQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/useInteractionsCategorizedSamplesQuery';
  38. import {useTransactionsCategorizedSamplesQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/useTransactionsCategorizedSamplesQuery';
  39. import type {
  40. InteractionSpanSampleRowWithScore,
  41. TransactionSampleRowWithScore,
  42. WebVitals,
  43. } from 'sentry/views/performance/browser/webVitals/utils/types';
  44. import {useStoredScoresSetting} from 'sentry/views/performance/browser/webVitals/utils/useStoredScoresSetting';
  45. import {generateReplayLink} from 'sentry/views/performance/transactionSummary/utils';
  46. import DetailPanel from 'sentry/views/starfish/components/detailPanel';
  47. import {SpanIndexedField} from 'sentry/views/starfish/types';
  48. type Column = GridColumnHeader;
  49. const columnOrder: GridColumnOrder[] = [
  50. {key: 'id', width: COL_WIDTH_UNDEFINED, name: t('Transaction')},
  51. {key: 'replayId', width: COL_WIDTH_UNDEFINED, name: t('Replay')},
  52. {key: 'profile.id', width: COL_WIDTH_UNDEFINED, name: t('Profile')},
  53. {key: 'webVital', width: COL_WIDTH_UNDEFINED, name: t('Web Vital')},
  54. {key: 'score', width: COL_WIDTH_UNDEFINED, name: t('Score')},
  55. ];
  56. const inpColumnOrder: GridColumnOrder[] = [
  57. {
  58. key: SpanIndexedField.SPAN_DESCRIPTION,
  59. width: COL_WIDTH_UNDEFINED,
  60. name: t('Interaction Target'),
  61. },
  62. {key: 'profile.id', width: COL_WIDTH_UNDEFINED, name: t('Profile')},
  63. {key: 'replayId', width: COL_WIDTH_UNDEFINED, name: t('Replay')},
  64. {key: 'webVital', width: COL_WIDTH_UNDEFINED, name: t('Inp')},
  65. {key: 'score', width: COL_WIDTH_UNDEFINED, name: t('Score')},
  66. ];
  67. const sort: GridColumnSortBy<keyof TransactionSampleRowWithScore> = {
  68. key: 'totalScore',
  69. order: 'desc',
  70. };
  71. const inpSort: GridColumnSortBy<keyof InteractionSpanSampleRowWithScore> = {
  72. key: 'inpScore',
  73. order: 'desc',
  74. };
  75. export function PageOverviewWebVitalsDetailPanel({
  76. webVital,
  77. onClose,
  78. }: {
  79. onClose: () => void;
  80. webVital: WebVitals | null;
  81. }) {
  82. const location = useLocation();
  83. const {projects} = useProjects();
  84. const organization = useOrganization();
  85. const routes = useRoutes();
  86. const {replayExists} = useReplayExists();
  87. const shouldUseStoredScores = useStoredScoresSetting();
  88. const isInp = webVital === 'inp';
  89. const replayLinkGenerator = generateReplayLink(routes);
  90. const project = useMemo(
  91. () => projects.find(p => p.id === String(location.query.project)),
  92. [projects, location.query.project]
  93. );
  94. const transaction = location.query.transaction
  95. ? Array.isArray(location.query.transaction)
  96. ? location.query.transaction[0]
  97. : location.query.transaction
  98. : undefined;
  99. const {data: projectData} = useProjectRawWebVitalsQuery({transaction});
  100. const {data: projectScoresData} = useProjectWebVitalsScoresQuery({
  101. enabled: shouldUseStoredScores,
  102. weightWebVital: webVital ?? 'total',
  103. transaction,
  104. });
  105. const projectScore = shouldUseStoredScores
  106. ? calculatePerformanceScoreFromStoredTableDataRow(projectScoresData?.data?.[0])
  107. : calculatePerformanceScoreFromTableDataRow(projectData?.data?.[0]);
  108. const {data: transactionsTableData, isLoading: isTransactionWebVitalsQueryLoading} =
  109. useTransactionsCategorizedSamplesQuery({
  110. transaction: transaction ?? '',
  111. webVital,
  112. enabled: Boolean(webVital) && !isInp,
  113. });
  114. const {data: inpTableData, isLoading: isInteractionsLoading} =
  115. useInteractionsCategorizedSamplesQuery({
  116. transaction: transaction ?? '',
  117. enabled: Boolean(webVital) && isInp,
  118. });
  119. const {profileExists} = useProfileExists(
  120. inpTableData.filter(row => row['profile.id']).map(row => row['profile.id'])
  121. );
  122. const {data: timeseriesData, isLoading: isTimeseriesLoading} =
  123. useProjectRawWebVitalsValuesTimeseriesQuery({transaction});
  124. const webVitalData: LineChartSeries = {
  125. data:
  126. !isTimeseriesLoading && webVital
  127. ? timeseriesData?.[webVital].map(({name, value}) => ({
  128. name,
  129. value,
  130. }))
  131. : [],
  132. seriesName: webVital ?? '',
  133. };
  134. const getProjectSlug = (row: TransactionSampleRowWithScore): string => {
  135. return project && !Array.isArray(location.query.project)
  136. ? project.slug
  137. : row.projectSlug;
  138. };
  139. const renderHeadCell = (col: Column) => {
  140. if (col.key === 'transaction') {
  141. return <NoOverflow>{col.name}</NoOverflow>;
  142. }
  143. if (col.key === 'webVital') {
  144. return <AlignRight>{`${webVital}`}</AlignRight>;
  145. }
  146. if (col.key === 'score' || col.key === 'measurements.score.inp') {
  147. return <AlignCenter>{`${webVital} ${col.name}`}</AlignCenter>;
  148. }
  149. if (col.key === 'replayId' || col.key === 'profile.id') {
  150. return <AlignCenter>{col.name}</AlignCenter>;
  151. }
  152. return <NoOverflow>{col.name}</NoOverflow>;
  153. };
  154. const getFormattedDuration = (value: number) => {
  155. if (value === undefined) {
  156. return null;
  157. }
  158. if (value < 1000) {
  159. return getDuration(value / 1000, 0, true);
  160. }
  161. return getDuration(value / 1000, 2, true);
  162. };
  163. const renderBodyCell = (col: Column, row: TransactionSampleRowWithScore) => {
  164. const {key} = col;
  165. const projectSlug = getProjectSlug(row);
  166. if (key === 'score') {
  167. if (row[`measurements.${webVital}`] !== undefined) {
  168. return (
  169. <AlignCenter>
  170. <PerformanceBadge score={row[`${webVital}Score`]} />
  171. </AlignCenter>
  172. );
  173. }
  174. return null;
  175. }
  176. if (col.key === 'webVital') {
  177. const value = row[`measurements.${webVital}`];
  178. if (value === undefined) {
  179. return (
  180. <AlignRight>
  181. <NoValue>{t('(no value)')}</NoValue>
  182. </AlignRight>
  183. );
  184. }
  185. const formattedValue =
  186. webVital === 'cls' ? value?.toFixed(2) : getFormattedDuration(value);
  187. return <AlignRight>{formattedValue}</AlignRight>;
  188. }
  189. if (key === 'id') {
  190. const eventTarget = generateLinkToEventInTraceView({
  191. eventSlug: generateEventSlug({id: row.id, project: projectSlug}),
  192. dataRow: row,
  193. organization,
  194. eventView: EventView.fromLocation(location),
  195. location,
  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. location={location}
  362. />
  363. ) : (
  364. <GridEditable
  365. data={transactionsTableData}
  366. isLoading={isTransactionWebVitalsQueryLoading}
  367. columnOrder={columnOrder}
  368. columnSortBy={[sort]}
  369. grid={{
  370. renderHeadCell,
  371. renderBodyCell,
  372. }}
  373. location={location}
  374. />
  375. )}
  376. </TableContainer>
  377. <PageAlert />
  378. </DetailPanel>
  379. </PageAlertProvider>
  380. );
  381. }
  382. const NoOverflow = styled('span')`
  383. overflow: hidden;
  384. text-overflow: ellipsis;
  385. white-space: nowrap;
  386. `;
  387. const AlignRight = styled('span')<{color?: string}>`
  388. text-align: right;
  389. width: 100%;
  390. ${p => (p.color ? `color: ${p.color};` : '')}
  391. `;
  392. const AlignCenter = styled('span')`
  393. text-align: center;
  394. width: 100%;
  395. `;
  396. const ChartContainer = styled('div')`
  397. position: relative;
  398. flex: 1;
  399. `;
  400. const NoValue = styled('span')`
  401. color: ${p => p.theme.gray300};
  402. `;
  403. const TableContainer = styled('div')`
  404. margin-bottom: 80px;
  405. `;