createSampleEventButton.tsx 5.2 KB

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