create.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import {Component, Fragment} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import * as Layout from 'sentry/components/layouts/thirds';
  5. import LoadingIndicator from 'sentry/components/loadingIndicator';
  6. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  7. import {t} from 'sentry/locale';
  8. import {Organization, Project} from 'sentry/types';
  9. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  10. import EventView from 'sentry/utils/discover/eventView';
  11. import {uniqueId} from 'sentry/utils/guid';
  12. import Teams from 'sentry/utils/teams';
  13. import BuilderBreadCrumbs from 'sentry/views/alerts/builder/builderBreadCrumbs';
  14. import IssueRuleEditor from 'sentry/views/alerts/rules/issue';
  15. import MetricRulesCreate from 'sentry/views/alerts/rules/metric/create';
  16. import MetricRulesDuplicate from 'sentry/views/alerts/rules/metric/duplicate';
  17. import {AlertRuleType} from 'sentry/views/alerts/types';
  18. import {
  19. AlertType as WizardAlertType,
  20. AlertWizardAlertNames,
  21. DEFAULT_WIZARD_TEMPLATE,
  22. WizardRuleTemplate,
  23. } from 'sentry/views/alerts/wizard/options';
  24. import {getAlertTypeFromAggregateDataset} from 'sentry/views/alerts/wizard/utils';
  25. type RouteParams = {
  26. orgId: string;
  27. alertType?: AlertRuleType;
  28. projectId?: string;
  29. };
  30. type Props = RouteComponentProps<RouteParams, {}> & {
  31. hasMetricAlerts: boolean;
  32. organization: Organization;
  33. project: Project;
  34. };
  35. type State = {
  36. alertType: AlertRuleType;
  37. };
  38. class Create extends Component<Props, State> {
  39. state = this.getInitialState();
  40. getInitialState(): State {
  41. const {organization, location, project, params, router} = this.props;
  42. const {
  43. createFromDiscover,
  44. createFromWizard,
  45. aggregate,
  46. dataset,
  47. eventTypes,
  48. createFromDuplicate,
  49. } = location?.query ?? {};
  50. let alertType = AlertRuleType.ISSUE;
  51. const hasAlertWizardV3 = organization.features.includes('alert-wizard-v3');
  52. // Alerts can only be created via create from discover or alert wizard, until alert-wizard-v3 is fully implemented
  53. if (hasAlertWizardV3) {
  54. alertType = params.alertType || AlertRuleType.METRIC;
  55. // TODO(taylangocmen): Remove redirect with aggregate && dataset && eventTypes, init from template
  56. if (
  57. alertType === AlertRuleType.METRIC &&
  58. !(aggregate && dataset && eventTypes) &&
  59. !createFromDuplicate
  60. ) {
  61. router.replace({
  62. ...location,
  63. pathname: `/organizations/${organization.slug}/alerts/new/${alertType}`,
  64. query: {
  65. ...location.query,
  66. ...DEFAULT_WIZARD_TEMPLATE,
  67. project: project.slug,
  68. },
  69. });
  70. }
  71. } else if (createFromDiscover) {
  72. alertType = AlertRuleType.METRIC;
  73. } else if (createFromWizard) {
  74. if (aggregate && dataset && eventTypes) {
  75. alertType = AlertRuleType.METRIC;
  76. } else {
  77. // Just to be explicit
  78. alertType = AlertRuleType.ISSUE;
  79. }
  80. } else {
  81. router.replace(`/organizations/${organization.slug}/alerts/${project.slug}/wizard`);
  82. }
  83. return {alertType};
  84. }
  85. componentDidMount() {
  86. const {organization, project} = this.props;
  87. const hasAlertWizardV3 = organization.features.includes('alert-wizard-v3');
  88. trackAdvancedAnalyticsEvent('new_alert_rule.viewed', {
  89. organization,
  90. project_id: project.id,
  91. session_id: this.sessionId,
  92. alert_type: this.state.alertType,
  93. duplicate_rule: this.isDuplicateRule ? 'true' : 'false',
  94. wizard_v3: hasAlertWizardV3 ? 'true' : 'false',
  95. });
  96. }
  97. /** Used to track analytics within one visit to the creation page */
  98. sessionId = uniqueId();
  99. get isDuplicateRule(): boolean {
  100. const {location, organization} = this.props;
  101. const createFromDuplicate = location?.query.createFromDuplicate === 'true';
  102. const hasDuplicateAlertRules = organization.features.includes('duplicate-alert-rule');
  103. return (
  104. hasDuplicateAlertRules && createFromDuplicate && location?.query.duplicateRuleId
  105. );
  106. }
  107. render() {
  108. const {hasMetricAlerts, organization, project, location, routes} = this.props;
  109. const {alertType} = this.state;
  110. const {aggregate, dataset, eventTypes, createFromWizard, createFromDiscover} =
  111. location?.query ?? {};
  112. const wizardTemplate: WizardRuleTemplate = {
  113. aggregate: aggregate ?? DEFAULT_WIZARD_TEMPLATE.aggregate,
  114. dataset: dataset ?? DEFAULT_WIZARD_TEMPLATE.dataset,
  115. eventTypes: eventTypes ?? DEFAULT_WIZARD_TEMPLATE.eventTypes,
  116. };
  117. const eventView = createFromDiscover ? EventView.fromLocation(location) : undefined;
  118. let wizardAlertType: undefined | WizardAlertType;
  119. if (createFromWizard && alertType === AlertRuleType.METRIC) {
  120. wizardAlertType = wizardTemplate
  121. ? getAlertTypeFromAggregateDataset(wizardTemplate)
  122. : 'issues';
  123. }
  124. const title = t('New Alert Rule');
  125. return (
  126. <Fragment>
  127. <SentryDocumentTitle title={title} projectSlug={project.slug} />
  128. <Layout.Header>
  129. <StyledHeaderContent>
  130. <BuilderBreadCrumbs
  131. organization={organization}
  132. alertName={t('Set Conditions')}
  133. title={wizardAlertType ? t('Select Alert') : title}
  134. projectSlug={project.slug}
  135. alertType={alertType}
  136. routes={routes}
  137. location={location}
  138. canChangeProject
  139. />
  140. <Layout.Title>
  141. {wizardAlertType
  142. ? `${t('Set Conditions for')} ${AlertWizardAlertNames[wizardAlertType]}`
  143. : title}
  144. </Layout.Title>
  145. </StyledHeaderContent>
  146. </Layout.Header>
  147. <Body>
  148. <Teams provideUserTeams>
  149. {({teams, initiallyLoaded}) =>
  150. initiallyLoaded ? (
  151. <Fragment>
  152. {(!hasMetricAlerts || alertType === AlertRuleType.ISSUE) && (
  153. <IssueRuleEditor
  154. {...this.props}
  155. project={project}
  156. userTeamIds={teams.map(({id}) => id)}
  157. />
  158. )}
  159. {hasMetricAlerts &&
  160. alertType === AlertRuleType.METRIC &&
  161. (this.isDuplicateRule ? (
  162. <MetricRulesDuplicate
  163. {...this.props}
  164. eventView={eventView}
  165. wizardTemplate={wizardTemplate}
  166. sessionId={this.sessionId}
  167. project={project}
  168. userTeamIds={teams.map(({id}) => id)}
  169. />
  170. ) : (
  171. <MetricRulesCreate
  172. {...this.props}
  173. eventView={eventView}
  174. wizardTemplate={wizardTemplate}
  175. sessionId={this.sessionId}
  176. project={project}
  177. userTeamIds={teams.map(({id}) => id)}
  178. />
  179. ))}
  180. </Fragment>
  181. ) : (
  182. <LoadingIndicator />
  183. )
  184. }
  185. </Teams>
  186. </Body>
  187. </Fragment>
  188. );
  189. }
  190. }
  191. const StyledHeaderContent = styled(Layout.HeaderContent)`
  192. overflow: visible;
  193. `;
  194. const Body = styled(Layout.Body)`
  195. && {
  196. padding: 0;
  197. gap: 0;
  198. }
  199. grid-template-rows: 1fr;
  200. @media (min-width: ${p => p.theme.breakpoints.large}) {
  201. grid-template-columns: minmax(100px, auto) 400px;
  202. }
  203. `;
  204. export default Create;