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