createSampleEventButton.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. recordAnalytics({eventCreated, retries, duration}) {
  64. const {organization, project, source} = this.props;
  65. if (!project) {
  66. return;
  67. }
  68. const eventKey = `sample_event.${eventCreated ? 'created' : 'failed'}` as const;
  69. trackAdvancedAnalyticsEvent(eventKey, {
  70. organization,
  71. project_id: project.id,
  72. platform: project.platform || '',
  73. interval: EVENT_POLL_INTERVAL,
  74. retries,
  75. duration,
  76. source,
  77. });
  78. }
  79. createSampleGroup = async () => {
  80. // TODO(dena): swap out for action creator
  81. const {api, organization, project, onCreateSampleGroup} = this.props;
  82. let eventData;
  83. if (!project) {
  84. return;
  85. }
  86. if (onCreateSampleGroup) {
  87. onCreateSampleGroup();
  88. } else {
  89. trackAdvancedAnalyticsEvent('growth.onboarding_view_sample_event', {
  90. platform: project.platform,
  91. organization,
  92. });
  93. }
  94. addLoadingMessage(t('Processing sample event...'), {
  95. duration: EVENT_POLL_RETRIES * EVENT_POLL_INTERVAL,
  96. });
  97. this.setState({creating: true});
  98. try {
  99. const url = `/projects/${organization.slug}/${project.slug}/create-sample/`;
  100. eventData = await api.requestPromise(url, {method: 'POST'});
  101. } catch (error) {
  102. Sentry.withScope(scope => {
  103. scope.setExtra('error', error);
  104. Sentry.captureException(new Error('Failed to create sample event'));
  105. });
  106. this.setState({creating: false});
  107. clearIndicators();
  108. addErrorMessage(t('Failed to create a new sample event'));
  109. return;
  110. }
  111. // Wait for the event to be fully processed and available on the group
  112. // before redirecting.
  113. const t0 = performance.now();
  114. const {eventCreated, retries} = await latestEventAvailable(api, eventData.groupID);
  115. const t1 = performance.now();
  116. clearIndicators();
  117. this.setState({creating: false});
  118. const duration = Math.ceil(t1 - t0);
  119. this.recordAnalytics({eventCreated, retries, duration});
  120. if (!eventCreated) {
  121. addErrorMessage(t('Failed to load sample event'));
  122. Sentry.withScope(scope => {
  123. scope.setTag('groupID', eventData.groupID);
  124. scope.setTag('platform', project.platform || '');
  125. scope.setTag('interval', EVENT_POLL_INTERVAL.toString());
  126. scope.setTag('retries', retries.toString());
  127. scope.setTag('duration', duration.toString());
  128. scope.setLevel('warning');
  129. Sentry.captureMessage('Failed to load sample event');
  130. });
  131. return;
  132. }
  133. browserHistory.push(
  134. normalizeUrl(
  135. `/organizations/${organization.slug}/issues/${eventData.groupID}/?project=${project.id}&referrer=sample-error`
  136. )
  137. );
  138. };
  139. render() {
  140. const {
  141. api: _api,
  142. organization: _organization,
  143. project: _project,
  144. source: _source,
  145. ...props
  146. } = this.props;
  147. const {creating} = this.state;
  148. return (
  149. <Button
  150. {...props}
  151. disabled={props.disabled || creating}
  152. onClick={this.createSampleGroup}
  153. />
  154. );
  155. }
  156. }
  157. export default withApi(withOrganization(CreateSampleEventButton));