changeExplorer.tsx 8.7 KB

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