changeExplorer.tsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import moment from 'moment';
  5. import {Button} from 'sentry/components/button';
  6. import {getArbitraryRelativePeriod} from 'sentry/components/timeRangeSelector/utils';
  7. import {DEFAULT_RELATIVE_PERIODS} from 'sentry/constants';
  8. import {IconFire, IconOpen} from 'sentry/icons';
  9. import {t} from 'sentry/locale';
  10. import {space} from 'sentry/styles/space';
  11. import type {Organization} from 'sentry/types/organization';
  12. import type {Project} from 'sentry/types/project';
  13. import {trackAnalytics} from 'sentry/utils/analytics';
  14. import theme from 'sentry/utils/theme';
  15. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  16. import DetailPanel from 'sentry/views/insights/common/components/detailPanel';
  17. import {
  18. DisplayModes,
  19. transactionSummaryRouteWithQuery,
  20. } from 'sentry/views/performance/transactionSummary/utils';
  21. import {FunctionsList} from 'sentry/views/performance/trends/changeExplorerUtils/functionsList';
  22. import {MetricsTable} from 'sentry/views/performance/trends/changeExplorerUtils/metricsTable';
  23. import {SpansList} from 'sentry/views/performance/trends/changeExplorerUtils/spansList';
  24. import {Chart} from 'sentry/views/performance/trends/chart';
  25. import type {
  26. NormalizedTrendsTransaction,
  27. TrendParameter,
  28. TrendsStats,
  29. TrendView,
  30. } from 'sentry/views/performance/trends/types';
  31. import {TrendChangeType} from 'sentry/views/performance/trends/types';
  32. import {getTrendProjectId} from 'sentry/views/performance/trends/utils';
  33. type PerformanceChangeExplorerProps = {
  34. collapsed: boolean;
  35. isLoading: boolean;
  36. location: Location;
  37. onClose: () => void;
  38. organization: Organization;
  39. projects: Project[];
  40. statsData: TrendsStats;
  41. transaction: NormalizedTrendsTransaction;
  42. trendChangeType: TrendChangeType;
  43. trendFunction: string;
  44. trendParameter: TrendParameter;
  45. trendView: TrendView;
  46. };
  47. type ExplorerBodyProps = {
  48. isLoading: boolean;
  49. location: Location;
  50. organization: Organization;
  51. projects: Project[];
  52. statsData: TrendsStats;
  53. transaction: NormalizedTrendsTransaction;
  54. trendChangeType: TrendChangeType;
  55. trendFunction: string;
  56. trendParameter: TrendParameter;
  57. trendView: TrendView;
  58. };
  59. type HeaderProps = {
  60. organization: Organization;
  61. projects: Project[];
  62. transaction: NormalizedTrendsTransaction;
  63. trendChangeType: TrendChangeType;
  64. trendFunction: string;
  65. trendParameter: TrendParameter;
  66. trendView: TrendView;
  67. };
  68. export function PerformanceChangeExplorer({
  69. collapsed,
  70. transaction,
  71. onClose,
  72. trendChangeType,
  73. trendFunction,
  74. trendView,
  75. statsData,
  76. isLoading,
  77. organization,
  78. projects,
  79. trendParameter,
  80. location,
  81. }: PerformanceChangeExplorerProps) {
  82. return (
  83. <DetailPanel detailKey={!collapsed ? transaction.transaction : ''} onClose={onClose}>
  84. {!collapsed && (
  85. <PanelBodyWrapper>
  86. <ExplorerBody
  87. transaction={transaction}
  88. trendChangeType={trendChangeType}
  89. trendFunction={trendFunction}
  90. trendView={trendView}
  91. statsData={statsData}
  92. isLoading={isLoading}
  93. organization={organization}
  94. projects={projects}
  95. trendParameter={trendParameter}
  96. location={location}
  97. />
  98. </PanelBodyWrapper>
  99. )}
  100. </DetailPanel>
  101. );
  102. }
  103. function ExplorerBody(props: ExplorerBodyProps) {
  104. const {
  105. transaction,
  106. trendChangeType,
  107. trendFunction,
  108. trendView,
  109. trendParameter,
  110. isLoading,
  111. location,
  112. organization,
  113. projects,
  114. } = props;
  115. const breakpointDate = transaction.breakpoint
  116. ? moment(transaction.breakpoint * 1000).format('ddd, DD MMM YYYY HH:mm:ss z')
  117. : '';
  118. const start = moment(trendView.start).format('DD MMM YYYY HH:mm:ss z');
  119. const end = moment(trendView.end).format('DD MMM YYYY HH:mm:ss z');
  120. return (
  121. <Fragment>
  122. <Header
  123. transaction={transaction}
  124. trendChangeType={trendChangeType}
  125. trendView={trendView}
  126. projects={projects}
  127. organization={organization}
  128. trendFunction={trendFunction}
  129. trendParameter={trendParameter}
  130. />
  131. <div style={{display: 'flex', gap: space(4)}}>
  132. <InfoItem
  133. label={
  134. trendChangeType === TrendChangeType.REGRESSION
  135. ? t('Regression Metric')
  136. : t('Improvement Metric')
  137. }
  138. value={trendFunction}
  139. />
  140. <InfoItem label={t('Start Time')} value={breakpointDate} />
  141. </div>
  142. <GraphPanel data-test-id="pce-graph">
  143. <strong>{`${trendParameter.label} (${trendFunction})`}</strong>
  144. <ExplorerText color={theme.gray300} margin={`-${space(3)}`}>
  145. {trendView.statsPeriod
  146. ? DEFAULT_RELATIVE_PERIODS[trendView.statsPeriod] ||
  147. getArbitraryRelativePeriod(trendView.statsPeriod)[trendView.statsPeriod]
  148. : `${start} - ${end}`}
  149. </ExplorerText>
  150. <Chart
  151. query={trendView.query}
  152. project={trendView.project}
  153. environment={trendView.environment}
  154. start={trendView.start}
  155. end={trendView.end}
  156. statsPeriod={trendView.statsPeriod}
  157. disableXAxis
  158. disableLegend
  159. neutralColor
  160. {...props}
  161. />
  162. </GraphPanel>
  163. <MetricsTable
  164. isLoading={isLoading}
  165. location={location}
  166. transaction={transaction}
  167. trendFunction={trendFunction}
  168. trendView={trendView}
  169. organization={organization}
  170. />
  171. <SpansList
  172. location={location}
  173. organization={organization}
  174. trendView={trendView}
  175. transaction={transaction}
  176. breakpoint={transaction.breakpoint!}
  177. trendChangeType={trendChangeType}
  178. />
  179. <FunctionsList
  180. location={location}
  181. organization={organization}
  182. trendView={trendView}
  183. transaction={transaction}
  184. breakpoint={transaction.breakpoint!}
  185. trendChangeType={trendChangeType}
  186. />
  187. </Fragment>
  188. );
  189. }
  190. function InfoItem({label, value}: {label: string; value: string}) {
  191. return (
  192. <div>
  193. <InfoLabel>{label}</InfoLabel>
  194. <InfoText>{value}</InfoText>
  195. </div>
  196. );
  197. }
  198. function Header(props: HeaderProps) {
  199. const {
  200. transaction,
  201. trendChangeType,
  202. trendView,
  203. projects,
  204. organization,
  205. trendFunction,
  206. trendParameter,
  207. } = props;
  208. const regression = trendChangeType === TrendChangeType.REGRESSION;
  209. const transactionSummaryLink = getTransactionSummaryLink(
  210. trendView,
  211. transaction,
  212. projects,
  213. organization,
  214. trendFunction,
  215. trendParameter
  216. );
  217. const handleClickAnalytics = () => {
  218. trackAnalytics('performance_views.performance_change_explorer.summary_link_clicked', {
  219. organization,
  220. transaction: transaction.transaction,
  221. });
  222. };
  223. return (
  224. <HeaderWrapper data-test-id="pce-header">
  225. <FireIcon regression={regression}>
  226. <IconFire color="white" />
  227. </FireIcon>
  228. <HeaderTextWrapper>
  229. <ChangeType regression={regression}>
  230. {regression ? t('Ongoing Regression') : t('Ongoing Improvement')}
  231. </ChangeType>
  232. <TransactionNameWrapper>
  233. <TransactionName>{transaction.transaction}</TransactionName>
  234. <ViewTransactionButton
  235. borderless
  236. to={normalizeUrl(transactionSummaryLink)}
  237. icon={<IconOpen />}
  238. aria-label={t('View transaction summary')}
  239. onClick={handleClickAnalytics}
  240. />
  241. </TransactionNameWrapper>
  242. </HeaderTextWrapper>
  243. </HeaderWrapper>
  244. );
  245. }
  246. function getTransactionSummaryLink(
  247. eventView: TrendView,
  248. transaction: NormalizedTrendsTransaction,
  249. projects: Project[],
  250. organization: Organization,
  251. currentTrendFunction: string,
  252. trendParameter: TrendParameter
  253. ) {
  254. const summaryView = eventView.clone();
  255. const projectID = getTrendProjectId(transaction, projects);
  256. const target = transactionSummaryRouteWithQuery({
  257. orgSlug: organization.slug,
  258. transaction: String(transaction.transaction),
  259. query: summaryView.generateQueryStringObject(),
  260. projectID,
  261. display: DisplayModes.TREND,
  262. trendFunction: currentTrendFunction,
  263. additionalQuery: {
  264. trendParameter: trendParameter.column,
  265. },
  266. });
  267. return target;
  268. }
  269. const PanelBodyWrapper = styled('div')`
  270. padding: 0 ${space(2)};
  271. margin-top: ${space(1)};
  272. `;
  273. const HeaderWrapper = styled('div')`
  274. display: flex;
  275. flex-wrap: nowrap;
  276. margin-bottom: ${space(3)};
  277. `;
  278. const HeaderTextWrapper = styled('div')`
  279. ${p => p.theme.overflowEllipsis};
  280. `;
  281. type ChangeTypeProps = {regression: boolean};
  282. const ChangeType = styled('p')<ChangeTypeProps>`
  283. color: ${p => (p.regression ? p.theme.danger : p.theme.success)};
  284. margin-bottom: ${space(0)};
  285. `;
  286. const FireIcon = styled('div')<ChangeTypeProps>`
  287. padding: ${space(1.5)};
  288. background-color: ${p => (p.regression ? p.theme.danger : p.theme.success)};
  289. border-radius: ${space(0.5)};
  290. margin-right: ${space(2)};
  291. float: left;
  292. height: 40px;
  293. `;
  294. const TransactionName = styled('h4')`
  295. margin-right: ${space(1)};
  296. margin-bottom: ${space(0)};
  297. ${p => p.theme.overflowEllipsis};
  298. `;
  299. const TransactionNameWrapper = styled('div')`
  300. display: flex;
  301. align-items: center;
  302. margin-bottom: ${space(3)};
  303. max-width: fit-content;
  304. `;
  305. const ViewTransactionButton = styled(Button)`
  306. padding: ${space(0)};
  307. height: min-content;
  308. min-height: 0px;
  309. `;
  310. const InfoLabel = styled('strong')`
  311. color: ${p => p.theme.gray300};
  312. `;
  313. const InfoText = styled('h3')`
  314. font-weight: ${p => p.theme.fontWeightNormal};
  315. `;
  316. const GraphPanel = styled('div')`
  317. border: 1px solid ${p => p.theme.border};
  318. border-radius: ${p => p.theme.panelBorderRadius};
  319. margin-bottom: ${space(2)};
  320. padding: ${space(3)};
  321. display: block;
  322. `;
  323. export const ExplorerText = styled('p')<{
  324. align?: string;
  325. color?: string;
  326. margin?: string;
  327. }>`
  328. margin-bottom: ${p => (p.margin ? p.margin : space(0))};
  329. color: ${p => p.color};
  330. text-align: ${p => p.align};
  331. `;