pageOverviewWebVitalsDetailPanel.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 {t} from 'sentry/locale';
  12. import {defined} from 'sentry/utils';
  13. import {generateEventSlug} from 'sentry/utils/discover/urls';
  14. import {getShortEventId} from 'sentry/utils/events';
  15. import {getDuration} from 'sentry/utils/formatters';
  16. import {PageAlert, PageAlertProvider} from 'sentry/utils/performance/contexts/pageAlert';
  17. import {getTransactionDetailsUrl} from 'sentry/utils/performance/urls';
  18. import {generateProfileFlamechartRoute} from 'sentry/utils/profiling/routes';
  19. import {useLocation} from 'sentry/utils/useLocation';
  20. import useOrganization from 'sentry/utils/useOrganization';
  21. import useProjects from 'sentry/utils/useProjects';
  22. import {useRoutes} from 'sentry/utils/useRoutes';
  23. import {PerformanceBadge} from 'sentry/views/performance/browser/webVitals/components/performanceBadge';
  24. import {WebVitalDetailHeader} from 'sentry/views/performance/browser/webVitals/components/webVitalDescription';
  25. import {WebVitalStatusLineChart} from 'sentry/views/performance/browser/webVitals/components/webVitalStatusLineChart';
  26. import {
  27. calculatePerformanceScoreFromTableDataRow,
  28. PERFORMANCE_SCORE_MEDIANS,
  29. PERFORMANCE_SCORE_P90S,
  30. } from 'sentry/views/performance/browser/webVitals/utils/queries/rawWebVitalsQueries/calculatePerformanceScore';
  31. import {useProjectRawWebVitalsQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/rawWebVitalsQueries/useProjectRawWebVitalsQuery';
  32. import {useProjectRawWebVitalsValuesTimeseriesQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/rawWebVitalsQueries/useProjectRawWebVitalsValuesTimeseriesQuery';
  33. import {calculatePerformanceScoreFromStoredTableDataRow} from 'sentry/views/performance/browser/webVitals/utils/queries/storedScoreQueries/calculatePerformanceScoreFromStored';
  34. import {useProjectWebVitalsScoresQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/storedScoreQueries/useProjectWebVitalsScoresQuery';
  35. import {useTransactionSamplesWebVitalsQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/useTransactionSamplesWebVitalsQuery';
  36. import type {
  37. TransactionSampleRowWithScore,
  38. WebVitals,
  39. } from 'sentry/views/performance/browser/webVitals/utils/types';
  40. import {useStoredScoresSetting} from 'sentry/views/performance/browser/webVitals/utils/useStoredScoresSetting';
  41. import {generateReplayLink} from 'sentry/views/performance/transactionSummary/utils';
  42. import DetailPanel from 'sentry/views/starfish/components/detailPanel';
  43. type Column = GridColumnHeader;
  44. const columnOrder: GridColumnOrder[] = [
  45. {key: 'id', width: COL_WIDTH_UNDEFINED, name: 'Transaction'},
  46. {key: 'replayId', width: COL_WIDTH_UNDEFINED, name: 'Replay'},
  47. {key: 'profile.id', width: COL_WIDTH_UNDEFINED, name: 'Profile'},
  48. {key: 'webVital', width: COL_WIDTH_UNDEFINED, name: 'Web Vital'},
  49. {key: 'score', width: COL_WIDTH_UNDEFINED, name: 'Score'},
  50. ];
  51. const sort: GridColumnSortBy<keyof TransactionSampleRowWithScore> = {
  52. key: 'totalScore',
  53. order: 'desc',
  54. };
  55. export function PageOverviewWebVitalsDetailPanel({
  56. webVital,
  57. onClose,
  58. }: {
  59. onClose: () => void;
  60. webVital: WebVitals | null;
  61. }) {
  62. const location = useLocation();
  63. const {projects} = useProjects();
  64. const organization = useOrganization();
  65. const routes = useRoutes();
  66. const shouldUseStoredScores = useStoredScoresSetting();
  67. const replayLinkGenerator = generateReplayLink(routes);
  68. const project = useMemo(
  69. () => projects.find(p => p.id === String(location.query.project)),
  70. [projects, location.query.project]
  71. );
  72. const transaction = location.query.transaction
  73. ? Array.isArray(location.query.transaction)
  74. ? location.query.transaction[0]
  75. : location.query.transaction
  76. : undefined;
  77. const {data: projectData} = useProjectRawWebVitalsQuery({transaction});
  78. const {data: projectScoresData} = useProjectWebVitalsScoresQuery({
  79. enabled: shouldUseStoredScores,
  80. weightWebVital: webVital ?? 'total',
  81. transaction,
  82. });
  83. const projectScore = shouldUseStoredScores
  84. ? calculatePerformanceScoreFromStoredTableDataRow(projectScoresData?.data?.[0])
  85. : calculatePerformanceScoreFromTableDataRow(projectData?.data?.[0]);
  86. // Do 3 queries filtering on LCP to get a spread of good, meh, and poor events
  87. // We can't query by performance score yet, so we're using LCP as a best estimate
  88. const {data: goodData, isLoading: isGoodTransactionWebVitalsQueryLoading} =
  89. useTransactionSamplesWebVitalsQuery({
  90. limit: 3,
  91. transaction: transaction ?? '',
  92. query: webVital
  93. ? `measurements.${webVital}:<${PERFORMANCE_SCORE_P90S[webVital]}`
  94. : undefined,
  95. enabled: Boolean(webVital),
  96. withProfiles: true,
  97. sortName: 'webVitalSort',
  98. webVital: webVital ?? undefined,
  99. });
  100. const {data: mehData, isLoading: isMehTransactionWebVitalsQueryLoading} =
  101. useTransactionSamplesWebVitalsQuery({
  102. limit: 3,
  103. transaction: transaction ?? '',
  104. query: webVital
  105. ? `measurements.${webVital}:<${PERFORMANCE_SCORE_MEDIANS[webVital]} measurements.${webVital}:>=${PERFORMANCE_SCORE_P90S[webVital]}`
  106. : undefined,
  107. enabled: Boolean(webVital),
  108. withProfiles: true,
  109. sortName: 'webVitalSort',
  110. webVital: webVital ?? undefined,
  111. });
  112. const {data: poorData, isLoading: isPoorTransactionWebVitalsQueryLoading} =
  113. useTransactionSamplesWebVitalsQuery({
  114. limit: 3,
  115. transaction: transaction ?? '',
  116. query: webVital
  117. ? `measurements.${webVital}:>=${PERFORMANCE_SCORE_MEDIANS[webVital]}`
  118. : undefined,
  119. enabled: Boolean(webVital),
  120. withProfiles: true,
  121. sortName: 'webVitalSort',
  122. webVital: webVital ?? undefined,
  123. });
  124. const data = [...goodData, ...mehData, ...poorData];
  125. const isTransactionWebVitalsQueryLoading =
  126. isGoodTransactionWebVitalsQueryLoading ||
  127. isMehTransactionWebVitalsQueryLoading ||
  128. isPoorTransactionWebVitalsQueryLoading;
  129. const tableData: TransactionSampleRowWithScore[] = data.sort(
  130. (a, b) => a[`${webVital}Score`] - b[`${webVital}Score`]
  131. );
  132. const {data: timeseriesData, isLoading: isTimeseriesLoading} =
  133. useProjectRawWebVitalsValuesTimeseriesQuery({transaction});
  134. const webVitalData: LineChartSeries = {
  135. data:
  136. !isTimeseriesLoading && webVital
  137. ? timeseriesData?.[webVital].map(({name, value}) => ({
  138. name,
  139. value,
  140. }))
  141. : [],
  142. seriesName: webVital ?? '',
  143. };
  144. const getProjectSlug = (row: TransactionSampleRowWithScore): string => {
  145. return project && !Array.isArray(location.query.project)
  146. ? project.slug
  147. : row.projectSlug;
  148. };
  149. const renderHeadCell = (col: Column) => {
  150. if (col.key === 'transaction') {
  151. return <NoOverflow>{col.name}</NoOverflow>;
  152. }
  153. if (col.key === 'webVital') {
  154. return <AlignRight>{`${webVital}`}</AlignRight>;
  155. }
  156. if (col.key === 'score') {
  157. return <AlignCenter>{`${webVital} ${col.name}`}</AlignCenter>;
  158. }
  159. if (col.key === 'replayId' || col.key === 'profile.id') {
  160. return <AlignCenter>{col.name}</AlignCenter>;
  161. }
  162. return <NoOverflow>{col.name}</NoOverflow>;
  163. };
  164. const getFormattedDuration = (value: number) => {
  165. if (value === undefined) {
  166. return null;
  167. }
  168. if (value < 1000) {
  169. return getDuration(value / 1000, 0, true);
  170. }
  171. return getDuration(value / 1000, 2, true);
  172. };
  173. const renderBodyCell = (col: Column, row: TransactionSampleRowWithScore) => {
  174. const {key} = col;
  175. const projectSlug = getProjectSlug(row);
  176. if (key === 'score') {
  177. if (row[`measurements.${webVital}`] !== undefined) {
  178. return (
  179. <AlignCenter>
  180. <PerformanceBadge score={row[`${webVital}Score`]} />
  181. </AlignCenter>
  182. );
  183. }
  184. return null;
  185. }
  186. if (col.key === 'webVital') {
  187. const value = row[`measurements.${webVital}`];
  188. if (value === undefined) {
  189. return (
  190. <AlignRight>
  191. <NoValue>{t('(no value)')}</NoValue>
  192. </AlignRight>
  193. );
  194. }
  195. const formattedValue =
  196. webVital === 'cls' ? value?.toFixed(2) : getFormattedDuration(value);
  197. return <AlignRight>{formattedValue}</AlignRight>;
  198. }
  199. if (key === 'id') {
  200. const eventSlug = generateEventSlug({...row, project: projectSlug});
  201. const eventTarget = getTransactionDetailsUrl(organization.slug, eventSlug);
  202. return (
  203. <NoOverflow>
  204. <Link to={eventTarget}>{getShortEventId(row.id)}</Link>
  205. </NoOverflow>
  206. );
  207. }
  208. if (key === 'replayId') {
  209. const replayTarget =
  210. row['transaction.duration'] !== undefined &&
  211. replayLinkGenerator(
  212. organization,
  213. {
  214. replayId: row.replayId,
  215. id: row.id,
  216. 'transaction.duration': row['transaction.duration'],
  217. timestamp: row.timestamp,
  218. },
  219. undefined
  220. );
  221. return row.replayId && replayTarget ? (
  222. <AlignCenter>
  223. <Link to={replayTarget}>{getShortEventId(row.replayId)}</Link>
  224. </AlignCenter>
  225. ) : (
  226. <AlignCenter>
  227. <NoValue>{t('(no value)')}</NoValue>
  228. </AlignCenter>
  229. );
  230. }
  231. if (key === 'profile.id') {
  232. if (!defined(project) || !defined(row['profile.id'])) {
  233. return (
  234. <AlignCenter>
  235. <NoValue>{t('(no value)')}</NoValue>
  236. </AlignCenter>
  237. );
  238. }
  239. const target = generateProfileFlamechartRoute({
  240. orgSlug: organization.slug,
  241. projectSlug,
  242. profileId: String(row['profile.id']),
  243. });
  244. return (
  245. <AlignCenter>
  246. <Link to={target}>{getShortEventId(row['profile.id'])}</Link>
  247. </AlignCenter>
  248. );
  249. }
  250. return <AlignRight>{row[key]}</AlignRight>;
  251. };
  252. const webVitalScore = projectScore[`${webVital}Score`];
  253. const webVitalValue = projectData?.data[0][`p75(measurements.${webVital})`] as
  254. | number
  255. | undefined;
  256. return (
  257. <PageAlertProvider>
  258. <DetailPanel detailKey={webVital ?? undefined} onClose={onClose}>
  259. {webVital && (
  260. <WebVitalDetailHeader
  261. value={
  262. webVitalValue !== undefined
  263. ? webVital !== 'cls'
  264. ? getDuration(webVitalValue / 1000, 2, true)
  265. : (webVitalValue as number)?.toFixed(2)
  266. : undefined
  267. }
  268. webVital={webVital}
  269. score={webVitalScore}
  270. />
  271. )}
  272. <ChartContainer>
  273. {webVital && <WebVitalStatusLineChart webVitalSeries={webVitalData} />}
  274. </ChartContainer>
  275. <TableContainer>
  276. <GridEditable
  277. data={tableData}
  278. isLoading={isTransactionWebVitalsQueryLoading}
  279. columnOrder={columnOrder}
  280. columnSortBy={[sort]}
  281. grid={{
  282. renderHeadCell,
  283. renderBodyCell,
  284. }}
  285. location={location}
  286. />
  287. </TableContainer>
  288. <PageAlert />
  289. </DetailPanel>
  290. </PageAlertProvider>
  291. );
  292. }
  293. const NoOverflow = styled('span')`
  294. overflow: hidden;
  295. text-overflow: ellipsis;
  296. `;
  297. const AlignRight = styled('span')<{color?: string}>`
  298. text-align: right;
  299. width: 100%;
  300. ${p => (p.color ? `color: ${p.color};` : '')}
  301. `;
  302. const AlignCenter = styled('span')`
  303. text-align: center;
  304. width: 100%;
  305. `;
  306. const ChartContainer = styled('div')`
  307. position: relative;
  308. flex: 1;
  309. `;
  310. const NoValue = styled('span')`
  311. color: ${p => p.theme.gray300};
  312. `;
  313. const TableContainer = styled('div')`
  314. margin-bottom: 80px;
  315. `;