pageOverviewWebVitalsDetailPanel.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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: '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 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. sortName: 'webVitalSort',
  90. webVital: webVital ?? undefined,
  91. });
  92. const {data: mehData, isLoading: isMehTransactionWebVitalsQueryLoading} =
  93. useTransactionSamplesWebVitalsQuery({
  94. limit: 3,
  95. transaction: transaction ?? '',
  96. query: webVital
  97. ? `measurements.${webVital}:<${PERFORMANCE_SCORE_MEDIANS[webVital]} measurements.${webVital}:>=${PERFORMANCE_SCORE_P90S[webVital]}`
  98. : undefined,
  99. enabled: Boolean(webVital),
  100. withProfiles: true,
  101. sortName: 'webVitalSort',
  102. webVital: webVital ?? undefined,
  103. });
  104. const {data: poorData, isLoading: isPoorTransactionWebVitalsQueryLoading} =
  105. useTransactionSamplesWebVitalsQuery({
  106. limit: 3,
  107. transaction: transaction ?? '',
  108. query: webVital
  109. ? `measurements.${webVital}:>=${PERFORMANCE_SCORE_MEDIANS[webVital]}`
  110. : undefined,
  111. enabled: Boolean(webVital),
  112. withProfiles: true,
  113. sortName: 'webVitalSort',
  114. webVital: webVital ?? undefined,
  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. useProjectRawWebVitalsValuesTimeseriesQuery({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) => {
  157. if (value === undefined) {
  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}`] !== undefined) {
  170. return (
  171. <AlignCenter>
  172. <PerformanceBadge score={row[`${webVital}Score`]} />
  173. </AlignCenter>
  174. );
  175. }
  176. return null;
  177. }
  178. if (col.key === 'webVital') {
  179. const value = row[`measurements.${webVital}`];
  180. if (value === undefined) {
  181. return (
  182. <AlignRight>
  183. <NoValue>{t('(no value)')}</NoValue>
  184. </AlignRight>
  185. );
  186. }
  187. const formattedValue =
  188. webVital === 'cls' ? value?.toFixed(2) : getFormattedDuration(value);
  189. return <AlignRight>{formattedValue}</AlignRight>;
  190. }
  191. if (key === 'id') {
  192. const eventSlug = generateEventSlug({...row, project: projectSlug});
  193. const eventTarget = getTransactionDetailsUrl(organization.slug, eventSlug);
  194. return (
  195. <NoOverflow>
  196. <Link to={eventTarget}>{getShortEventId(row.id)}</Link>
  197. </NoOverflow>
  198. );
  199. }
  200. if (key === 'replayId') {
  201. const replayTarget =
  202. row['transaction.duration'] !== undefined &&
  203. replayLinkGenerator(
  204. organization,
  205. {
  206. replayId: row.replayId,
  207. id: row.id,
  208. 'transaction.duration': row['transaction.duration'],
  209. timestamp: row.timestamp,
  210. },
  211. undefined
  212. );
  213. return row.replayId && replayTarget ? (
  214. <AlignCenter>
  215. <Link to={replayTarget}>{getShortEventId(row.replayId)}</Link>
  216. </AlignCenter>
  217. ) : (
  218. <AlignCenter>
  219. <NoValue>{t('(no value)')}</NoValue>
  220. </AlignCenter>
  221. );
  222. }
  223. if (key === 'profile.id') {
  224. if (!defined(project) || !defined(row['profile.id'])) {
  225. return (
  226. <AlignCenter>
  227. <NoValue>{t('(no value)')}</NoValue>
  228. </AlignCenter>
  229. );
  230. }
  231. const target = generateProfileFlamechartRoute({
  232. orgSlug: organization.slug,
  233. projectSlug,
  234. profileId: String(row['profile.id']),
  235. });
  236. return (
  237. <AlignCenter>
  238. <Link to={target}>{getShortEventId(row['profile.id'])}</Link>
  239. </AlignCenter>
  240. );
  241. }
  242. return <AlignRight>{row[key]}</AlignRight>;
  243. };
  244. const webVitalScore = projectScore[`${webVital}Score`];
  245. return (
  246. <PageErrorProvider>
  247. <DetailPanel detailKey={webVital ?? undefined} onClose={onClose}>
  248. {webVital && projectData && webVitalScore !== undefined && (
  249. <WebVitalDetailHeader
  250. value={
  251. webVital !== 'cls'
  252. ? getDuration(
  253. (projectData?.data[0][`p75(measurements.${webVital})`] as number) /
  254. 1000,
  255. 2,
  256. true
  257. )
  258. : (
  259. projectData?.data[0][`p75(measurements.${webVital})`] as number
  260. )?.toFixed(2)
  261. }
  262. webVital={webVital}
  263. score={webVitalScore}
  264. />
  265. )}
  266. <ChartContainer>
  267. {webVital && <WebVitalStatusLineChart webVitalSeries={webVitalData} />}
  268. </ChartContainer>
  269. <TableContainer>
  270. <GridEditable
  271. data={tableData}
  272. isLoading={isTransactionWebVitalsQueryLoading}
  273. columnOrder={columnOrder}
  274. columnSortBy={[sort]}
  275. grid={{
  276. renderHeadCell,
  277. renderBodyCell,
  278. }}
  279. location={location}
  280. />
  281. </TableContainer>
  282. <PageErrorAlert />
  283. </DetailPanel>
  284. </PageErrorProvider>
  285. );
  286. }
  287. const NoOverflow = styled('span')`
  288. overflow: hidden;
  289. text-overflow: ellipsis;
  290. `;
  291. const AlignRight = styled('span')<{color?: string}>`
  292. text-align: right;
  293. width: 100%;
  294. ${p => (p.color ? `color: ${p.color};` : '')}
  295. `;
  296. const AlignCenter = styled('span')`
  297. text-align: center;
  298. width: 100%;
  299. `;
  300. const ChartContainer = styled('div')`
  301. position: relative;
  302. flex: 1;
  303. `;
  304. const NoValue = styled('span')`
  305. color: ${p => p.theme.gray300};
  306. `;
  307. const TableContainer = styled('div')`
  308. margin-bottom: 80px;
  309. `;