pageOverviewWebVitalsDetailPanel.tsx 12 KB

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