createSampleEventButton.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import * as React 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 from 'sentry/components/button';
  11. import {t} from 'sentry/locale';
  12. import {Organization, Project} from 'sentry/types';
  13. import {trackAdhocEvent, trackAnalyticsEvent} from 'sentry/utils/analytics';
  14. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  15. import withApi from 'sentry/utils/withApi';
  16. import withOrganization from 'sentry/utils/withOrganization';
  17. type Props = React.ComponentProps<typeof Button> & {
  18. api: Client;
  19. organization: Organization;
  20. source: string;
  21. project?: Project;
  22. };
  23. type State = {
  24. creating: boolean;
  25. };
  26. const EVENT_POLL_RETRIES = 15;
  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 => 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 React.Component<Props, State> {
  48. state: State = {
  49. creating: false,
  50. };
  51. componentDidMount() {
  52. const {organization, project, source} = this.props;
  53. if (!project) {
  54. return;
  55. }
  56. trackAdhocEvent({
  57. eventKey: 'sample_event.button_viewed',
  58. org_id: organization.id,
  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'}`;
  69. const eventName = `Sample Event ${eventCreated ? 'Created' : 'Failed'}`;
  70. trackAnalyticsEvent({
  71. eventKey,
  72. eventName,
  73. organization_id: organization.id,
  74. project_id: project.id,
  75. platform: project.platform || '',
  76. interval: EVENT_POLL_INTERVAL,
  77. retries,
  78. duration,
  79. source,
  80. });
  81. }
  82. createSampleGroup = async () => {
  83. // TODO(dena): swap out for action creator
  84. const {api, organization, project} = this.props;
  85. let eventData;
  86. if (!project) {
  87. return;
  88. }
  89. trackAdvancedAnalyticsEvent('growth.onboarding_view_sample_event', {
  90. platform: project.platform,
  91. organization,
  92. });
  93. addLoadingMessage(t('Processing sample event...'), {
  94. duration: EVENT_POLL_RETRIES * EVENT_POLL_INTERVAL,
  95. });
  96. this.setState({creating: true});
  97. try {
  98. const url = `/projects/${organization.slug}/${project.slug}/create-sample/`;
  99. eventData = await api.requestPromise(url, {method: 'POST'});
  100. } catch (error) {
  101. Sentry.withScope(scope => {
  102. scope.setExtra('error', error);
  103. Sentry.captureException(new Error('Failed to create sample event'));
  104. });
  105. this.setState({creating: false});
  106. clearIndicators();
  107. addErrorMessage(t('Failed to create a new sample event'));
  108. return;
  109. }
  110. // Wait for the event to be fully processed and available on the group
  111. // before redirecting.
  112. const t0 = performance.now();
  113. const {eventCreated, retries} = await latestEventAvailable(api, eventData.groupID);
  114. const t1 = performance.now();
  115. clearIndicators();
  116. this.setState({creating: false});
  117. const duration = Math.ceil(t1 - t0);
  118. this.recordAnalytics({eventCreated, retries, duration});
  119. if (!eventCreated) {
  120. addErrorMessage(t('Failed to load sample event'));
  121. Sentry.withScope(scope => {
  122. scope.setTag('groupID', eventData.groupID);
  123. scope.setTag('platform', project.platform || '');
  124. scope.setTag('interval', EVENT_POLL_INTERVAL.toString());
  125. scope.setTag('retries', retries.toString());
  126. scope.setTag('duration', duration.toString());
  127. scope.setLevel(Sentry.Severity.Warning);
  128. Sentry.captureMessage('Failed to load sample event');
  129. });
  130. return;
  131. }
  132. browserHistory.push(
  133. `/organizations/${organization.slug}/issues/${eventData.groupID}/?project=${project.id}`
  134. );
  135. };
  136. render() {
  137. const {
  138. api: _api,
  139. organization: _organization,
  140. project: _project,
  141. source: _source,
  142. ...props
  143. } = this.props;
  144. const {creating} = this.state;
  145. return (
  146. <Button
  147. {...props}
  148. data-test-id="create-sample-event"
  149. disabled={props.disabled || creating}
  150. onClick={this.createSampleGroup}
  151. />
  152. );
  153. }
  154. }
  155. export default withApi(withOrganization(CreateSampleEventButton));