pageOverviewWebVitalsDetailPanel.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. calculatePerformanceScoreFromTableDataRow,
  31. PERFORMANCE_SCORE_MEDIANS,
  32. PERFORMANCE_SCORE_P90S,
  33. } from 'sentry/views/performance/browser/webVitals/utils/queries/rawWebVitalsQueries/calculatePerformanceScore';
  34. import {useProjectRawWebVitalsQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/rawWebVitalsQueries/useProjectRawWebVitalsQuery';
  35. import {useProjectRawWebVitalsValuesTimeseriesQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/rawWebVitalsQueries/useProjectRawWebVitalsValuesTimeseriesQuery';
  36. import {useTransactionSamplesWebVitalsQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/useTransactionSamplesWebVitalsQuery';
  37. import {
  38. TransactionSampleRowWithScore,
  39. WebVitals,
  40. } from 'sentry/views/performance/browser/webVitals/utils/types';
  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} = useProjectRawWebVitalsQuery({transaction});
  77. const projectScore = calculatePerformanceScoreFromTableDataRow(projectData?.data?.[0]);
  78. // Do 3 queries filtering on LCP to get a spread of good, meh, and poor events
  79. // We can't query by performance score yet, so we're using LCP as a best estimate
  80. const {data: goodData, isLoading: isGoodTransactionWebVitalsQueryLoading} =
  81. useTransactionSamplesWebVitalsQuery({
  82. limit: 3,
  83. transaction: transaction ?? '',
  84. query: webVital
  85. ? `measurements.${webVital}:<${PERFORMANCE_SCORE_P90S[webVital]}`
  86. : undefined,
  87. enabled: Boolean(webVital),
  88. withProfiles: true,
  89. });
  90. const {data: mehData, isLoading: isMehTransactionWebVitalsQueryLoading} =
  91. useTransactionSamplesWebVitalsQuery({
  92. limit: 3,
  93. transaction: transaction ?? '',
  94. query: webVital
  95. ? `measurements.${webVital}:<${PERFORMANCE_SCORE_MEDIANS[webVital]} measurements.${webVital}:>=${PERFORMANCE_SCORE_P90S[webVital]}`
  96. : undefined,
  97. enabled: Boolean(webVital),
  98. withProfiles: true,
  99. });
  100. const {data: poorData, isLoading: isPoorTransactionWebVitalsQueryLoading} =
  101. useTransactionSamplesWebVitalsQuery({
  102. limit: 3,
  103. transaction: transaction ?? '',
  104. query: webVital
  105. ? `measurements.${webVital}:>=${PERFORMANCE_SCORE_MEDIANS[webVital]}`
  106. : undefined,
  107. enabled: Boolean(webVital),
  108. withProfiles: true,
  109. });
  110. const data = [...goodData, ...mehData, ...poorData];
  111. const isTransactionWebVitalsQueryLoading =
  112. isGoodTransactionWebVitalsQueryLoading ||
  113. isMehTransactionWebVitalsQueryLoading ||
  114. isPoorTransactionWebVitalsQueryLoading;
  115. const tableData: TransactionSampleRowWithScore[] = data.sort(
  116. (a, b) => a[`${webVital}Score`] - b[`${webVital}Score`]
  117. );
  118. const {data: timeseriesData, isLoading: isTimeseriesLoading} =
  119. useProjectRawWebVitalsValuesTimeseriesQuery({transaction});
  120. const webVitalData: LineChartSeries = {
  121. data:
  122. !isTimeseriesLoading && webVital
  123. ? timeseriesData?.[webVital].map(({name, value}) => ({
  124. name,
  125. value,
  126. }))
  127. : [],
  128. seriesName: webVital ?? '',
  129. };
  130. const getProjectSlug = (row: TransactionSampleRowWithScore): string => {
  131. return project && !Array.isArray(location.query.project)
  132. ? project.slug
  133. : row.projectSlug;
  134. };
  135. const renderHeadCell = (col: Column) => {
  136. if (col.key === 'transaction') {
  137. return <NoOverflow>{col.name}</NoOverflow>;
  138. }
  139. if (col.key === 'webVital') {
  140. return <AlignRight>{`${webVital}`}</AlignRight>;
  141. }
  142. if (col.key === 'score') {
  143. return <AlignCenter>{`${webVital} ${col.name}`}</AlignCenter>;
  144. }
  145. if (col.key === 'replayId' || col.key === 'profile.id') {
  146. return <AlignCenter>{col.name}</AlignCenter>;
  147. }
  148. return <NoOverflow>{col.name}</NoOverflow>;
  149. };
  150. const getFormattedDuration = (value: number | null) => {
  151. if (value === null) {
  152. return null;
  153. }
  154. if (value < 1000) {
  155. return getDuration(value / 1000, 0, true);
  156. }
  157. return getDuration(value / 1000, 2, true);
  158. };
  159. const renderBodyCell = (col: Column, row: TransactionSampleRowWithScore) => {
  160. const {key} = col;
  161. const projectSlug = getProjectSlug(row);
  162. if (key === 'score') {
  163. if (row[`measurements.${webVital}`] !== null) {
  164. return (
  165. <AlignCenter>
  166. <PerformanceBadge score={row[`${webVital}Score`]} />
  167. </AlignCenter>
  168. );
  169. }
  170. return null;
  171. }
  172. if (col.key === 'webVital') {
  173. if (row[key] === null) {
  174. return <NoValue>{t('(no value)')}</NoValue>;
  175. }
  176. const value = row[`measurements.${webVital}`];
  177. const formattedValue =
  178. webVital === 'cls' ? value?.toFixed(2) : getFormattedDuration(value);
  179. return <AlignRight>{formattedValue}</AlignRight>;
  180. }
  181. if (key === 'id') {
  182. const eventSlug = generateEventSlug({...row, project: projectSlug});
  183. const eventTarget = getTransactionDetailsUrl(organization.slug, eventSlug);
  184. return (
  185. <NoOverflow>
  186. <Link to={eventTarget}>{getShortEventId(row.id)}</Link>
  187. </NoOverflow>
  188. );
  189. }
  190. if (key === 'replayId') {
  191. const replayTarget =
  192. row['transaction.duration'] !== null &&
  193. replayLinkGenerator(
  194. organization,
  195. {
  196. replayId: row.replayId,
  197. id: row.id,
  198. 'transaction.duration': row['transaction.duration'],
  199. timestamp: row.timestamp,
  200. },
  201. undefined
  202. );
  203. return row.replayId && replayTarget ? (
  204. <AlignCenter>
  205. <Link to={replayTarget}>{getShortEventId(row.replayId)}</Link>
  206. </AlignCenter>
  207. ) : (
  208. <AlignCenter>
  209. <NoValue>{t('(no value)')}</NoValue>
  210. </AlignCenter>
  211. );
  212. }
  213. if (key === 'profile.id') {
  214. if (!defined(project) || !defined(row['profile.id'])) {
  215. return (
  216. <AlignCenter>
  217. <NoValue>{t('(no value)')}</NoValue>
  218. </AlignCenter>
  219. );
  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. const webVitalScore = projectScore[`${webVital}Score`];
  235. return (
  236. <PageErrorProvider>
  237. <DetailPanel detailKey={webVital ?? undefined} onClose={onClose}>
  238. {webVital && projectData && webVitalScore !== null && (
  239. <WebVitalDetailHeader
  240. value={
  241. webVital !== 'cls'
  242. ? getDuration(
  243. (projectData?.data[0][`p75(measurements.${webVital})`] as number) /
  244. 1000,
  245. 2,
  246. true
  247. )
  248. : (
  249. projectData?.data[0][`p75(measurements.${webVital})`] as number
  250. )?.toFixed(2)
  251. }
  252. webVital={webVital}
  253. score={webVitalScore}
  254. />
  255. )}
  256. <ChartContainer>
  257. {webVital && <WebVitalStatusLineChart webVitalSeries={webVitalData} />}
  258. </ChartContainer>
  259. <TableContainer>
  260. <GridEditable
  261. data={tableData}
  262. isLoading={isTransactionWebVitalsQueryLoading}
  263. columnOrder={columnOrder}
  264. columnSortBy={[sort]}
  265. grid={{
  266. renderHeadCell,
  267. renderBodyCell,
  268. }}
  269. location={location}
  270. />
  271. </TableContainer>
  272. <PageErrorAlert />
  273. </DetailPanel>
  274. </PageErrorProvider>
  275. );
  276. }
  277. const NoOverflow = styled('span')`
  278. overflow: hidden;
  279. text-overflow: ellipsis;
  280. `;
  281. const AlignRight = styled('span')<{color?: string}>`
  282. text-align: right;
  283. width: 100%;
  284. ${p => (p.color ? `color: ${p.color};` : '')}
  285. `;
  286. const AlignCenter = styled('span')`
  287. text-align: center;
  288. width: 100%;
  289. `;
  290. const ChartContainer = styled('div')`
  291. position: relative;
  292. flex: 1;
  293. `;
  294. const NoValue = styled('span')`
  295. color: ${p => p.theme.gray300};
  296. `;
  297. const TableContainer = styled('div')`
  298. margin-bottom: 80px;
  299. `;