createSampleEventButton.tsx 4.8 KB

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