pageOverviewWebVitalsDetailPanel.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. import {useMemo} from 'react';
  2. import {Link} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {LineChartSeries} from 'sentry/components/charts/lineChart';
  5. import GridEditable, {
  6. COL_WIDTH_UNDEFINED,
  7. GridColumnHeader,
  8. GridColumnOrder,
  9. GridColumnSortBy,
  10. } 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 {
  17. PageErrorAlert,
  18. PageErrorProvider,
  19. } from 'sentry/utils/performance/contexts/pageError';
  20. import {getTransactionDetailsUrl} from 'sentry/utils/performance/urls';
  21. import {generateProfileFlamechartRoute} from 'sentry/utils/profiling/routes';
  22. import {useLocation} from 'sentry/utils/useLocation';
  23. import useOrganization from 'sentry/utils/useOrganization';
  24. import useProjects from 'sentry/utils/useProjects';
  25. import {useRoutes} from 'sentry/utils/useRoutes';
  26. import {PerformanceBadge} from 'sentry/views/performance/browser/webVitals/components/performanceBadge';
  27. import {WebVitalDetailHeader} from 'sentry/views/performance/browser/webVitals/components/webVitalDescription';
  28. import {WebVitalStatusLineChart} from 'sentry/views/performance/browser/webVitals/components/webVitalStatusLineChart';
  29. import {
  30. calculatePerformanceScore,
  31. PERFORMANCE_SCORE_MEDIANS,
  32. PERFORMANCE_SCORE_P90S,
  33. } from 'sentry/views/performance/browser/webVitals/utils/calculatePerformanceScore';
  34. import {
  35. TransactionSampleRowWithScore,
  36. WebVitals,
  37. } from 'sentry/views/performance/browser/webVitals/utils/types';
  38. import {useProjectWebVitalsQuery} from 'sentry/views/performance/browser/webVitals/utils/useProjectWebVitalsQuery';
  39. import {useProjectWebVitalsValuesTimeseriesQuery} from 'sentry/views/performance/browser/webVitals/utils/useProjectWebVitalsValuesTimeseriesQuery';
  40. import {useTransactionSamplesWebVitalsQuery} from 'sentry/views/performance/browser/webVitals/utils/useTransactionSamplesWebVitalsQuery';
  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: 'score',
  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 replayLinkGenerator = generateReplayLink(routes);
  67. const project = useMemo(
  68. () => projects.find(p => p.id === String(location.query.project)),
  69. [projects, location.query.project]
  70. );
  71. const transaction = location.query.transaction
  72. ? Array.isArray(location.query.transaction)
  73. ? location.query.transaction[0]
  74. : location.query.transaction
  75. : undefined;
  76. const {data: projectData} = useProjectWebVitalsQuery({transaction});
  77. const projectScore = calculatePerformanceScore({
  78. lcp: projectData?.data[0]['p75(measurements.lcp)'] as number,
  79. fcp: projectData?.data[0]['p75(measurements.fcp)'] as number,
  80. cls: projectData?.data[0]['p75(measurements.cls)'] as number,
  81. ttfb: projectData?.data[0]['p75(measurements.ttfb)'] as number,
  82. fid: projectData?.data[0]['p75(measurements.fid)'] as number,
  83. });
  84. // Do 3 queries filtering on LCP to get a spread of good, meh, and poor events
  85. // We can't query by performance score yet, so we're using LCP as a best estimate
  86. const {data: goodData, isLoading: isGoodTransactionWebVitalsQueryLoading} =
  87. useTransactionSamplesWebVitalsQuery({
  88. limit: 3,
  89. transaction: transaction ?? '',
  90. query: webVital
  91. ? `measurements.${webVital}:<${PERFORMANCE_SCORE_P90S[webVital]}`
  92. : undefined,
  93. enabled: Boolean(webVital),
  94. withProfiles: true,
  95. });
  96. const {data: mehData, isLoading: isMehTransactionWebVitalsQueryLoading} =
  97. useTransactionSamplesWebVitalsQuery({
  98. limit: 3,
  99. transaction: transaction ?? '',
  100. query: webVital
  101. ? `measurements.${webVital}:<${PERFORMANCE_SCORE_MEDIANS[webVital]} measurements.${webVital}:>=${PERFORMANCE_SCORE_P90S[webVital]}`
  102. : undefined,
  103. enabled: Boolean(webVital),
  104. withProfiles: true,
  105. });
  106. const {data: poorData, isLoading: isPoorTransactionWebVitalsQueryLoading} =
  107. useTransactionSamplesWebVitalsQuery({
  108. limit: 3,
  109. transaction: transaction ?? '',
  110. query: webVital
  111. ? `measurements.${webVital}:>=${PERFORMANCE_SCORE_MEDIANS[webVital]}`
  112. : undefined,
  113. enabled: Boolean(webVital),
  114. withProfiles: true,
  115. });
  116. const data = [...goodData, ...mehData, ...poorData];
  117. const isTransactionWebVitalsQueryLoading =
  118. isGoodTransactionWebVitalsQueryLoading ||
  119. isMehTransactionWebVitalsQueryLoading ||
  120. isPoorTransactionWebVitalsQueryLoading;
  121. const tableData: TransactionSampleRowWithScore[] = data.sort(
  122. (a, b) => a[`${webVital}Score`] - b[`${webVital}Score`]
  123. );
  124. const {data: timeseriesData, isLoading: isTimeseriesLoading} =
  125. useProjectWebVitalsValuesTimeseriesQuery({transaction});
  126. const webVitalData: LineChartSeries = {
  127. data:
  128. !isTimeseriesLoading && webVital
  129. ? timeseriesData?.[webVital].map(({name, value}) => ({
  130. name,
  131. value,
  132. }))
  133. : [],
  134. seriesName: webVital ?? '',
  135. };
  136. const getProjectSlug = (row: TransactionSampleRowWithScore): string => {
  137. return project && !Array.isArray(location.query.project)
  138. ? project.slug
  139. : row.projectSlug;
  140. };
  141. const renderHeadCell = (col: Column) => {
  142. if (col.key === 'transaction') {
  143. return <NoOverflow>{col.name}</NoOverflow>;
  144. }
  145. if (col.key === 'webVital') {
  146. return <AlignRight>{`${webVital}`}</AlignRight>;
  147. }
  148. if (col.key === 'score') {
  149. return <AlignCenter>{`${webVital} ${col.name}`}</AlignCenter>;
  150. }
  151. if (col.key === 'replayId' || col.key === 'profile.id') {
  152. return <AlignCenter>{col.name}</AlignCenter>;
  153. }
  154. return <NoOverflow>{col.name}</NoOverflow>;
  155. };
  156. const getFormattedDuration = (value: number | null) => {
  157. if (value === null) {
  158. return null;
  159. }
  160. if (value < 1000) {
  161. return getDuration(value / 1000, 0, true);
  162. }
  163. return getDuration(value / 1000, 2, true);
  164. };
  165. const renderBodyCell = (col: Column, row: TransactionSampleRowWithScore) => {
  166. const {key} = col;
  167. const projectSlug = getProjectSlug(row);
  168. if (key === 'score') {
  169. if (row[`measurements.${webVital}`] !== null) {
  170. return (
  171. <AlignCenter>
  172. <PerformanceBadge score={row[`${webVital}Score`]} />
  173. </AlignCenter>
  174. );
  175. }
  176. return null;
  177. }
  178. if (col.key === 'webVital') {
  179. if (row[key] === null) {
  180. return <NoValue>{t('(no value)')}</NoValue>;
  181. }
  182. const value = row[`measurements.${webVital}`];
  183. const formattedValue =
  184. webVital === 'cls' ? value?.toFixed(2) : getFormattedDuration(value);
  185. return <AlignRight>{formattedValue}</AlignRight>;
  186. }
  187. if (key === 'id') {
  188. const eventSlug = generateEventSlug({...row, project: projectSlug});
  189. const eventTarget = getTransactionDetailsUrl(organization.slug, eventSlug);
  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'] !== null &&
  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 ? (
  210. <AlignCenter>
  211. <Link to={replayTarget}>{getShortEventId(row.replayId)}</Link>
  212. </AlignCenter>
  213. ) : (
  214. <AlignCenter>{' \u2014 '}</AlignCenter>
  215. );
  216. }
  217. if (key === 'profile.id') {
  218. if (!defined(project) || !defined(row['profile.id'])) {
  219. return <AlignCenter>{' \u2014 '}</AlignCenter>;
  220. }
  221. const target = generateProfileFlamechartRoute({
  222. orgSlug: organization.slug,
  223. projectSlug,
  224. profileId: String(row['profile.id']),
  225. });
  226. return (
  227. <AlignCenter>
  228. <Link to={target}>{getShortEventId(row['profile.id'])}</Link>
  229. </AlignCenter>
  230. );
  231. }
  232. return <AlignRight>{row[key]}</AlignRight>;
  233. };
  234. return (
  235. <PageErrorProvider>
  236. <DetailPanel detailKey={webVital ?? undefined} onClose={onClose}>
  237. {webVital && projectData && (
  238. <WebVitalDetailHeader
  239. value={
  240. webVital !== 'cls'
  241. ? getDuration(
  242. (projectData?.data[0][`p75(measurements.${webVital})`] as number) /
  243. 1000,
  244. 2,
  245. true
  246. )
  247. : (
  248. projectData?.data[0][`p75(measurements.${webVital})`] as number
  249. ).toFixed(2)
  250. }
  251. webVital={webVital}
  252. score={projectScore[`${webVital}Score`]}
  253. />
  254. )}
  255. <ChartContainer>
  256. {webVital && <WebVitalStatusLineChart webVitalSeries={webVitalData} />}
  257. </ChartContainer>
  258. <GridEditable
  259. data={tableData}
  260. isLoading={isTransactionWebVitalsQueryLoading}
  261. columnOrder={columnOrder}
  262. columnSortBy={[sort]}
  263. grid={{
  264. renderHeadCell,
  265. renderBodyCell,
  266. }}
  267. location={location}
  268. />
  269. <PageErrorAlert />
  270. </DetailPanel>
  271. </PageErrorProvider>
  272. );
  273. }
  274. const NoOverflow = styled('span')`
  275. overflow: hidden;
  276. text-overflow: ellipsis;
  277. `;
  278. const AlignRight = styled('span')<{color?: string}>`
  279. text-align: right;
  280. width: 100%;
  281. ${p => (p.color ? `color: ${p.color};` : '')}
  282. `;
  283. const AlignCenter = styled('span')`
  284. text-align: center;
  285. width: 100%;
  286. `;
  287. const ChartContainer = styled('div')`
  288. position: relative;
  289. flex: 1;
  290. `;
  291. const NoValue = styled('span')`
  292. color: ${p => p.theme.gray300};
  293. `;