createSampleEventButton.tsx 4.7 KB

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