content.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import {Component} from 'react';
  2. import {browserHistory, InjectedRouter} from 'react-router';
  3. import {Location} from 'history';
  4. import isEqual from 'lodash/isEqual';
  5. import {loadOrganizationTags} from 'app/actionCreators/tags';
  6. import {Client} from 'app/api';
  7. import Alert from 'app/components/alert';
  8. import Button from 'app/components/button';
  9. import GlobalSdkUpdateAlert from 'app/components/globalSdkUpdateAlert';
  10. import LightWeightNoProjectMessage from 'app/components/lightWeightNoProjectMessage';
  11. import GlobalSelectionHeader from 'app/components/organizations/globalSelectionHeader';
  12. import PageHeading from 'app/components/pageHeading';
  13. import SentryDocumentTitle from 'app/components/sentryDocumentTitle';
  14. import {ALL_ACCESS_PROJECTS} from 'app/constants/globalSelectionHeader';
  15. import {IconFlag} from 'app/icons';
  16. import {t} from 'app/locale';
  17. import {PageContent, PageHeader} from 'app/styles/organization';
  18. import {GlobalSelection, Organization, Project} from 'app/types';
  19. import {trackAnalyticsEvent} from 'app/utils/analytics';
  20. import EventView from 'app/utils/discover/eventView';
  21. import {decodeScalar} from 'app/utils/queryString';
  22. import {QueryResults, tokenizeSearch} from 'app/utils/tokenizeSearch';
  23. import withApi from 'app/utils/withApi';
  24. import withGlobalSelection from 'app/utils/withGlobalSelection';
  25. import withOrganization from 'app/utils/withOrganization';
  26. import withProjects from 'app/utils/withProjects';
  27. import LandingContent from './landing/content';
  28. import {DEFAULT_MAX_DURATION} from './trends/utils';
  29. import {DEFAULT_STATS_PERIOD, generatePerformanceEventView} from './data';
  30. import Onboarding from './onboarding';
  31. import {addRoutePerformanceContext, getPerformanceTrendsUrl} from './utils';
  32. type Props = {
  33. api: Client;
  34. organization: Organization;
  35. selection: GlobalSelection;
  36. location: Location;
  37. router: InjectedRouter;
  38. projects: Project[];
  39. loadingProjects: boolean;
  40. demoMode?: boolean;
  41. };
  42. type State = {
  43. eventView: EventView;
  44. error: string | undefined;
  45. };
  46. class PerformanceContent extends Component<Props, State> {
  47. static getDerivedStateFromProps(nextProps: Readonly<Props>, prevState: State): State {
  48. return {
  49. ...prevState,
  50. eventView: generatePerformanceEventView(
  51. nextProps.organization,
  52. nextProps.location,
  53. nextProps.projects
  54. ),
  55. };
  56. }
  57. state: State = {
  58. eventView: generatePerformanceEventView(
  59. this.props.organization,
  60. this.props.location,
  61. this.props.projects
  62. ),
  63. error: undefined,
  64. };
  65. componentDidMount() {
  66. const {api, organization, selection} = this.props;
  67. loadOrganizationTags(api, organization.slug, selection);
  68. addRoutePerformanceContext(selection);
  69. trackAnalyticsEvent({
  70. eventKey: 'performance_views.overview.view',
  71. eventName: 'Performance Views: Transaction overview view',
  72. organization_id: parseInt(organization.id, 10),
  73. show_onboarding: this.shouldShowOnboarding(),
  74. });
  75. }
  76. componentDidUpdate(prevProps: Props) {
  77. const {api, organization, selection} = this.props;
  78. if (
  79. !isEqual(prevProps.selection.projects, selection.projects) ||
  80. !isEqual(prevProps.selection.datetime, selection.datetime)
  81. ) {
  82. loadOrganizationTags(api, organization.slug, selection);
  83. addRoutePerformanceContext(selection);
  84. }
  85. }
  86. renderError() {
  87. const {error} = this.state;
  88. if (!error) {
  89. return null;
  90. }
  91. return (
  92. <Alert type="error" icon={<IconFlag size="md" />}>
  93. {error}
  94. </Alert>
  95. );
  96. }
  97. setError = (error: string | undefined) => {
  98. this.setState({error});
  99. };
  100. handleSearch = (searchQuery: string) => {
  101. const {location, organization} = this.props;
  102. trackAnalyticsEvent({
  103. eventKey: 'performance_views.overview.search',
  104. eventName: 'Performance Views: Transaction overview search',
  105. organization_id: parseInt(organization.id, 10),
  106. });
  107. browserHistory.push({
  108. pathname: location.pathname,
  109. query: {
  110. ...location.query,
  111. cursor: undefined,
  112. query: String(searchQuery).trim() || undefined,
  113. },
  114. });
  115. };
  116. handleTrendsClick() {
  117. const {location, organization} = this.props;
  118. const newQuery = {
  119. ...location.query,
  120. };
  121. const query = decodeScalar(location.query.query, '');
  122. const conditions = tokenizeSearch(query);
  123. trackAnalyticsEvent({
  124. eventKey: 'performance_views.change_view',
  125. eventName: 'Performance Views: Change View',
  126. organization_id: parseInt(organization.id, 10),
  127. view_name: 'TRENDS',
  128. });
  129. const modifiedConditions = new QueryResults([]);
  130. if (conditions.hasTag('tpm()')) {
  131. modifiedConditions.setTagValues('tpm()', conditions.getTagValues('tpm()'));
  132. } else {
  133. modifiedConditions.setTagValues('tpm()', ['>0.01']);
  134. }
  135. if (conditions.hasTag('transaction.duration')) {
  136. modifiedConditions.setTagValues(
  137. 'transaction.duration',
  138. conditions.getTagValues('transaction.duration')
  139. );
  140. } else {
  141. modifiedConditions.setTagValues('transaction.duration', [
  142. '>0',
  143. `<${DEFAULT_MAX_DURATION}`,
  144. ]);
  145. }
  146. newQuery.query = modifiedConditions.formatString();
  147. browserHistory.push({
  148. pathname: getPerformanceTrendsUrl(organization),
  149. query: {...newQuery},
  150. });
  151. }
  152. shouldShowOnboarding() {
  153. const {projects, demoMode} = this.props;
  154. const {eventView} = this.state;
  155. // XXX used by getsentry to bypass onboarding for the upsell demo state.
  156. if (demoMode) {
  157. return false;
  158. }
  159. if (projects.length === 0) {
  160. return false;
  161. }
  162. // Current selection is 'my projects' or 'all projects'
  163. if (eventView.project.length === 0 || eventView.project === [ALL_ACCESS_PROJECTS]) {
  164. return (
  165. projects.filter(p => p.firstTransactionEvent === false).length === projects.length
  166. );
  167. }
  168. // Any other subset of projects.
  169. return (
  170. projects.filter(
  171. p =>
  172. eventView.project.includes(parseInt(p.id, 10)) &&
  173. p.firstTransactionEvent === false
  174. ).length === eventView.project.length
  175. );
  176. }
  177. renderBody() {
  178. const {organization, projects} = this.props;
  179. const eventView = this.state.eventView;
  180. const showOnboarding = this.shouldShowOnboarding();
  181. return (
  182. <PageContent>
  183. <LightWeightNoProjectMessage organization={organization}>
  184. <PageHeader>
  185. <PageHeading>{t('Performance')}</PageHeading>
  186. {!showOnboarding && (
  187. <Button
  188. priority="primary"
  189. data-test-id="landing-header-trends"
  190. onClick={() => this.handleTrendsClick()}
  191. >
  192. {t('View Trends')}
  193. </Button>
  194. )}
  195. </PageHeader>
  196. <GlobalSdkUpdateAlert />
  197. {this.renderError()}
  198. {showOnboarding ? (
  199. <Onboarding organization={organization} project={projects[0]} />
  200. ) : (
  201. <LandingContent
  202. eventView={eventView}
  203. projects={projects}
  204. organization={organization}
  205. setError={this.setError}
  206. handleSearch={this.handleSearch}
  207. />
  208. )}
  209. </LightWeightNoProjectMessage>
  210. </PageContent>
  211. );
  212. }
  213. render() {
  214. const {organization} = this.props;
  215. return (
  216. <SentryDocumentTitle title={t('Performance')} orgSlug={organization.slug}>
  217. <GlobalSelectionHeader
  218. defaultSelection={{
  219. datetime: {
  220. start: null,
  221. end: null,
  222. utc: false,
  223. period: DEFAULT_STATS_PERIOD,
  224. },
  225. }}
  226. >
  227. {this.renderBody()}
  228. </GlobalSelectionHeader>
  229. </SentryDocumentTitle>
  230. );
  231. }
  232. }
  233. export default withApi(
  234. withOrganization(withProjects(withGlobalSelection(PerformanceContent)))
  235. );