createSampleEventButton.tsx 5.2 KB

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