create.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import {Component, Fragment} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import * as Layout from 'sentry/components/layouts/thirds';
  4. import LoadingIndicator from 'sentry/components/loadingIndicator';
  5. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  6. import {t} from 'sentry/locale';
  7. import type {Member, Organization} from 'sentry/types/organization';
  8. import type {Project} from 'sentry/types/project';
  9. import EventView from 'sentry/utils/discover/eventView';
  10. import {uniqueId} from 'sentry/utils/guid';
  11. import type {WithRouteAnalyticsProps} from 'sentry/utils/routeAnalytics/withRouteAnalytics';
  12. import withRouteAnalytics from 'sentry/utils/routeAnalytics/withRouteAnalytics';
  13. import Teams from 'sentry/utils/teams';
  14. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  15. import BuilderBreadCrumbs from 'sentry/views/alerts/builder/builderBreadCrumbs';
  16. import IssueRuleEditor from 'sentry/views/alerts/rules/issue';
  17. import MetricRulesCreate from 'sentry/views/alerts/rules/metric/create';
  18. import MetricRuleDuplicate from 'sentry/views/alerts/rules/metric/duplicate';
  19. import {AlertRuleType} from 'sentry/views/alerts/types';
  20. import type {
  21. AlertType as WizardAlertType,
  22. WizardRuleTemplate,
  23. } from 'sentry/views/alerts/wizard/options';
  24. import {
  25. AlertWizardAlertNames,
  26. DEFAULT_WIZARD_TEMPLATE,
  27. } from 'sentry/views/alerts/wizard/options';
  28. import {getAlertTypeFromAggregateDataset} from 'sentry/views/alerts/wizard/utils';
  29. type RouteParams = {
  30. alertType?: AlertRuleType;
  31. projectId?: string;
  32. };
  33. type Props = RouteComponentProps<RouteParams, {}> &
  34. WithRouteAnalyticsProps & {
  35. hasMetricAlerts: boolean;
  36. members: Member[] | undefined;
  37. organization: Organization;
  38. project: Project;
  39. };
  40. type State = {
  41. alertType: AlertRuleType;
  42. };
  43. class Create extends Component<Props, State> {
  44. state = this.getInitialState();
  45. getInitialState(): State {
  46. const {organization, location, project, params, router} = this.props;
  47. const {aggregate, dataset, eventTypes, createFromDuplicate} = location?.query ?? {};
  48. const alertType = params.alertType || AlertRuleType.METRIC;
  49. // TODO(taylangocmen): Remove redirect with aggregate && dataset && eventTypes, init from template
  50. if (
  51. alertType === AlertRuleType.METRIC &&
  52. !(aggregate && dataset && eventTypes) &&
  53. !createFromDuplicate
  54. ) {
  55. router.replace(
  56. normalizeUrl({
  57. ...location,
  58. pathname: `/organizations/${organization.slug}/alerts/new/${alertType}`,
  59. query: {
  60. ...location.query,
  61. ...DEFAULT_WIZARD_TEMPLATE,
  62. project: project.slug,
  63. },
  64. })
  65. );
  66. }
  67. return {alertType};
  68. }
  69. componentDidMount() {
  70. const {project} = this.props;
  71. this.props.setRouteAnalyticsParams({
  72. project_id: project.id,
  73. session_id: this.sessionId,
  74. alert_type: this.state.alertType,
  75. duplicate_rule: this.isDuplicateRule ? 'true' : 'false',
  76. wizard_v3: 'true',
  77. });
  78. this.props.setEventNames('new_alert_rule.viewed', 'New Alert Rule: Viewed');
  79. }
  80. /** Used to track analytics within one visit to the creation page */
  81. sessionId = uniqueId();
  82. get isDuplicateRule(): boolean {
  83. const {location} = this.props;
  84. const createFromDuplicate = location?.query.createFromDuplicate === 'true';
  85. return createFromDuplicate && location?.query.duplicateRuleId;
  86. }
  87. render() {
  88. const {hasMetricAlerts, organization, project, location, members} = this.props;
  89. const {alertType} = this.state;
  90. const {aggregate, dataset, eventTypes, createFromWizard, createFromDiscover, query} =
  91. location?.query ?? {};
  92. const wizardTemplate: WizardRuleTemplate = {
  93. aggregate: aggregate ?? DEFAULT_WIZARD_TEMPLATE.aggregate,
  94. dataset: dataset ?? DEFAULT_WIZARD_TEMPLATE.dataset,
  95. eventTypes: eventTypes ?? DEFAULT_WIZARD_TEMPLATE.eventTypes,
  96. query: query ?? DEFAULT_WIZARD_TEMPLATE.query,
  97. };
  98. const eventView = createFromDiscover ? EventView.fromLocation(location) : undefined;
  99. let wizardAlertType: undefined | WizardAlertType;
  100. if (createFromWizard && alertType === AlertRuleType.METRIC) {
  101. wizardAlertType = wizardTemplate
  102. ? getAlertTypeFromAggregateDataset(wizardTemplate)
  103. : 'issues';
  104. }
  105. const title = t('New Alert Rule');
  106. return (
  107. <Fragment>
  108. <SentryDocumentTitle title={title} projectSlug={project.slug} />
  109. <Layout.Header>
  110. <Layout.HeaderContent>
  111. <BuilderBreadCrumbs
  112. organization={organization}
  113. alertName={t('Set Conditions')}
  114. title={wizardAlertType ? t('Select Alert') : title}
  115. projectSlug={project.slug}
  116. />
  117. <Layout.Title>
  118. {wizardAlertType
  119. ? `${t('Set Conditions for')} ${AlertWizardAlertNames[wizardAlertType]}`
  120. : title}
  121. </Layout.Title>
  122. </Layout.HeaderContent>
  123. </Layout.Header>
  124. <Layout.Body>
  125. <Teams provideUserTeams>
  126. {({teams, initiallyLoaded}) =>
  127. initiallyLoaded ? (
  128. <Fragment>
  129. {(!hasMetricAlerts || alertType === AlertRuleType.ISSUE) && (
  130. <IssueRuleEditor
  131. {...this.props}
  132. project={project}
  133. userTeamIds={teams.map(({id}) => id)}
  134. members={members}
  135. />
  136. )}
  137. {hasMetricAlerts &&
  138. alertType === AlertRuleType.METRIC &&
  139. (this.isDuplicateRule ? (
  140. <MetricRuleDuplicate
  141. {...this.props}
  142. eventView={eventView}
  143. wizardTemplate={wizardTemplate}
  144. sessionId={this.sessionId}
  145. project={project}
  146. userTeamIds={teams.map(({id}) => id)}
  147. />
  148. ) : (
  149. <MetricRulesCreate
  150. {...this.props}
  151. eventView={eventView}
  152. wizardTemplate={wizardTemplate}
  153. sessionId={this.sessionId}
  154. project={project}
  155. userTeamIds={teams.map(({id}) => id)}
  156. />
  157. ))}
  158. </Fragment>
  159. ) : (
  160. <LoadingIndicator />
  161. )
  162. }
  163. </Teams>
  164. </Layout.Body>
  165. </Fragment>
  166. );
  167. }
  168. }
  169. export default withRouteAnalytics(Create);