createSampleEventButton.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import {Component} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import * as Sentry from '@sentry/react';
  4. import {
  5. addErrorMessage,
  6. addLoadingMessage,
  7. clearIndicators,
  8. } from 'sentry/actionCreators/indicator';
  9. import {Client} from 'sentry/api';
  10. import {Button, ButtonProps} from 'sentry/components/button';
  11. import {t} from 'sentry/locale';
  12. import {Organization, Project} from 'sentry/types';
  13. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  14. import withApi from 'sentry/utils/withApi';
  15. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  16. import withOrganization from 'sentry/utils/withOrganization';
  17. type CreateSampleEventButtonProps = {
  18. api: Client;
  19. organization: Organization;
  20. source: string;
  21. onCreateSampleGroup?: () => void;
  22. project?: Project;
  23. } & ButtonProps;
  24. type State = {
  25. creating: boolean;
  26. };
  27. const EVENT_POLL_RETRIES = 30;
  28. const EVENT_POLL_INTERVAL = 1000;
  29. async function latestEventAvailable(
  30. api: Client,
  31. groupID: string
  32. ): Promise<{eventCreated: boolean; retries: number}> {
  33. let retries = 0;
  34. // eslint-disable-next-line no-constant-condition
  35. while (true) {
  36. if (retries > EVENT_POLL_RETRIES) {
  37. return {eventCreated: false, retries: retries - 1};
  38. }
  39. await new Promise(resolve => window.setTimeout(resolve, EVENT_POLL_INTERVAL));
  40. try {
  41. await api.requestPromise(`/issues/${groupID}/events/latest/`);
  42. return {eventCreated: true, retries};
  43. } catch {
  44. ++retries;
  45. }
  46. }
  47. }
  48. class CreateSampleEventButton extends Component<CreateSampleEventButtonProps, State> {
  49. state: State = {
  50. creating: false,
  51. };
  52. componentDidMount() {
  53. const {organization, project, source} = this.props;
  54. if (!project) {
  55. return;
  56. }
  57. trackAdvancedAnalyticsEvent('sample_event.button_viewed', {
  58. organization,
  59. project_id: project.id,
  60. source,
  61. });
  62. }
  63. componentWillUnmount() {
  64. this._isMounted = false;
  65. }
  66. private _isMounted = true;
  67. recordAnalytics({eventCreated, retries, duration}) {
  68. const {organization, project, source} = this.props;
  69. if (!project) {
  70. return;
  71. }
  72. const eventKey = `sample_event.${eventCreated ? 'created' : 'failed'}` as const;
  73. trackAdvancedAnalyticsEvent(eventKey, {
  74. organization,
  75. project_id: project.id,
  76. platform: project.platform || '',
  77. interval: EVENT_POLL_INTERVAL,
  78. retries,
  79. duration,
  80. source,
  81. });
  82. }
  83. createSampleGroup = async () => {
  84. // TODO(dena): swap out for action creator
  85. const {api, organization, project, onCreateSampleGroup} = this.props;
  86. let eventData;
  87. if (!project) {
  88. return;
  89. }
  90. if (onCreateSampleGroup) {
  91. onCreateSampleGroup();
  92. } else {
  93. trackAdvancedAnalyticsEvent('growth.onboarding_view_sample_event', {
  94. platform: project.platform,
  95. organization,
  96. });
  97. }
  98. addLoadingMessage(t('Processing sample event...'), {
  99. duration: EVENT_POLL_RETRIES * EVENT_POLL_INTERVAL,
  100. });
  101. this.setState({creating: true});
  102. try {
  103. const url = `/projects/${organization.slug}/${project.slug}/create-sample/`;
  104. eventData = await api.requestPromise(url, {method: 'POST'});
  105. } catch (error) {
  106. Sentry.withScope(scope => {
  107. scope.setExtra('error', error);
  108. Sentry.captureException(new Error('Failed to create sample event'));
  109. });
  110. this.setState({creating: false});
  111. clearIndicators();
  112. addErrorMessage(t('Failed to create a new sample event'));
  113. return;
  114. }
  115. // Wait for the event to be fully processed and available on the group
  116. // before redirecting.
  117. const t0 = performance.now();
  118. const {eventCreated, retries} = await latestEventAvailable(api, eventData.groupID);
  119. // Navigated away before event was created - skip analytics and error messages
  120. // latestEventAvailable will succeed even if the request was cancelled
  121. if (!this._isMounted) {
  122. return;
  123. }
  124. const t1 = performance.now();
  125. clearIndicators();
  126. this.setState({creating: false});
  127. const duration = Math.ceil(t1 - t0);
  128. this.recordAnalytics({eventCreated, retries, duration});
  129. if (!eventCreated) {
  130. addErrorMessage(t('Failed to load sample event'));
  131. Sentry.withScope(scope => {
  132. scope.setTag('groupID', eventData.groupID);
  133. scope.setTag('platform', project.platform || '');
  134. scope.setTag('interval', EVENT_POLL_INTERVAL.toString());
  135. scope.setTag('retries', retries.toString());
  136. scope.setTag('duration', duration.toString());
  137. scope.setLevel('warning');
  138. Sentry.captureMessage('Failed to load sample event');
  139. });
  140. return;
  141. }
  142. browserHistory.push(
  143. normalizeUrl(
  144. `/organizations/${organization.slug}/issues/${eventData.groupID}/?project=${project.id}&referrer=sample-error`
  145. )
  146. );
  147. };
  148. render() {
  149. const {
  150. api: _api,
  151. organization: _organization,
  152. project: _project,
  153. source: _source,
  154. ...props
  155. } = this.props;
  156. const {creating} = this.state;
  157. return (
  158. <Button
  159. {...props}
  160. disabled={props.disabled || creating}
  161. onClick={this.createSampleGroup}
  162. />
  163. );
  164. }
  165. }
  166. export default withApi(withOrganization(CreateSampleEventButton));