pageOverviewWebVitalsDetailPanel.tsx 14 KB

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