ruleConditionsForm.tsx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. import {Fragment, PureComponent} from 'react';
  2. import {InjectedRouter} from 'react-router';
  3. import {components} from 'react-select';
  4. import {css} from '@emotion/react';
  5. import styled from '@emotion/styled';
  6. import pick from 'lodash/pick';
  7. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  8. import {Client} from 'sentry/api';
  9. import Feature from 'sentry/components/acl/feature';
  10. import Alert from 'sentry/components/alert';
  11. import SearchBar from 'sentry/components/events/searchBar';
  12. import FormField from 'sentry/components/forms/formField';
  13. import SelectControl from 'sentry/components/forms/selectControl';
  14. import SelectField from 'sentry/components/forms/selectField';
  15. import IdBadge from 'sentry/components/idBadge';
  16. import ExternalLink from 'sentry/components/links/externalLink';
  17. import ListItem from 'sentry/components/list/listItem';
  18. import {Panel, PanelBody} from 'sentry/components/panels';
  19. import Tooltip from 'sentry/components/tooltip';
  20. import {IconQuestion} from 'sentry/icons';
  21. import {t, tct} from 'sentry/locale';
  22. import space from 'sentry/styles/space';
  23. import {Environment, Organization, Project, SelectValue} from 'sentry/types';
  24. import {MobileVital, WebVital} from 'sentry/utils/discover/fields';
  25. import {getDisplayName} from 'sentry/utils/environment';
  26. import {isActiveSuperuser} from 'sentry/utils/isActiveSuperuser';
  27. import withProjects from 'sentry/utils/withProjects';
  28. import WizardField from 'sentry/views/alerts/rules/metric/wizardField';
  29. import {
  30. convertDatasetEventTypesToSource,
  31. DATA_SOURCE_LABELS,
  32. DATA_SOURCE_TO_SET_AND_EVENT_TYPES,
  33. } from 'sentry/views/alerts/utils';
  34. import {AlertType, getFunctionHelpText} from 'sentry/views/alerts/wizard/options';
  35. import {isCrashFreeAlert} from './utils/isCrashFreeAlert';
  36. import {
  37. COMPARISON_DELTA_OPTIONS,
  38. DEFAULT_AGGREGATE,
  39. DEFAULT_TRANSACTION_AGGREGATE,
  40. } from './constants';
  41. import MetricField from './metricField';
  42. import {AlertRuleComparisonType, Dataset, Datasource, TimeWindow} from './types';
  43. const TIME_WINDOW_MAP: Record<TimeWindow, string> = {
  44. [TimeWindow.ONE_MINUTE]: t('1 minute'),
  45. [TimeWindow.FIVE_MINUTES]: t('5 minutes'),
  46. [TimeWindow.TEN_MINUTES]: t('10 minutes'),
  47. [TimeWindow.FIFTEEN_MINUTES]: t('15 minutes'),
  48. [TimeWindow.THIRTY_MINUTES]: t('30 minutes'),
  49. [TimeWindow.ONE_HOUR]: t('1 hour'),
  50. [TimeWindow.TWO_HOURS]: t('2 hours'),
  51. [TimeWindow.FOUR_HOURS]: t('4 hours'),
  52. [TimeWindow.ONE_DAY]: t('24 hours'),
  53. };
  54. type Props = {
  55. alertType: AlertType;
  56. api: Client;
  57. comparisonType: AlertRuleComparisonType;
  58. dataset: Dataset;
  59. disabled: boolean;
  60. hasAlertWizardV3: boolean;
  61. onComparisonDeltaChange: (value: number) => void;
  62. onFilterSearch: (query: string) => void;
  63. onTimeWindowChange: (value: number) => void;
  64. organization: Organization;
  65. project: Project;
  66. projects: Project[];
  67. router: InjectedRouter;
  68. showMEPAlertBanner: boolean;
  69. thresholdChart: React.ReactNode;
  70. timeWindow: number;
  71. allowChangeEventTypes?: boolean;
  72. comparisonDelta?: number;
  73. disableProjectSelector?: boolean;
  74. loadingProjects?: boolean;
  75. };
  76. type State = {
  77. environments: Environment[] | null;
  78. };
  79. class RuleConditionsForm extends PureComponent<Props, State> {
  80. state: State = {
  81. environments: null,
  82. };
  83. componentDidMount() {
  84. this.fetchData();
  85. }
  86. componentDidUpdate(prevProps: Props) {
  87. if (prevProps.project.id === this.props.project.id) {
  88. return;
  89. }
  90. this.fetchData();
  91. }
  92. formElemBaseStyle = {
  93. padding: `${space(0.5)}`,
  94. border: 'none',
  95. };
  96. async fetchData() {
  97. const {api, organization, project} = this.props;
  98. try {
  99. const environments = await api.requestPromise(
  100. `/projects/${organization.slug}/${project.slug}/environments/`,
  101. {
  102. query: {
  103. visibility: 'visible',
  104. },
  105. }
  106. );
  107. this.setState({environments});
  108. } catch (_err) {
  109. addErrorMessage(t('Unable to fetch environments'));
  110. }
  111. }
  112. get timeWindowOptions() {
  113. let options: Record<string, string> = TIME_WINDOW_MAP;
  114. if (isCrashFreeAlert(this.props.dataset)) {
  115. options = pick(TIME_WINDOW_MAP, [
  116. // TimeWindow.THIRTY_MINUTES, leaving this option out until we figure out the sub-hour session resolution chart limitations
  117. TimeWindow.ONE_HOUR,
  118. TimeWindow.TWO_HOURS,
  119. TimeWindow.FOUR_HOURS,
  120. TimeWindow.ONE_DAY,
  121. ]);
  122. }
  123. return Object.entries(options).map(([value, label]) => ({
  124. value: parseInt(value, 10),
  125. label: this.props.hasAlertWizardV3
  126. ? tct('[timeWindow] interval', {
  127. timeWindow: label.slice(-1) === 's' ? label.slice(0, -1) : label,
  128. })
  129. : label,
  130. }));
  131. }
  132. get searchPlaceholder() {
  133. switch (this.props.dataset) {
  134. case Dataset.ERRORS:
  135. return t('Filter events by level, message, and other properties\u2026');
  136. case Dataset.METRICS:
  137. case Dataset.SESSIONS:
  138. return t('Filter sessions by release version\u2026');
  139. case Dataset.TRANSACTIONS:
  140. default:
  141. return t('Filter transactions by URL, tags, and other properties\u2026');
  142. }
  143. }
  144. get searchSupportedTags() {
  145. if (isCrashFreeAlert(this.props.dataset)) {
  146. return {
  147. release: {
  148. key: 'release',
  149. name: 'release',
  150. },
  151. };
  152. }
  153. return undefined;
  154. }
  155. renderEventTypeFilter() {
  156. const {organization, disabled, alertType} = this.props;
  157. const dataSourceOptions = [
  158. {
  159. label: t('Errors'),
  160. options: [
  161. {
  162. value: Datasource.ERROR_DEFAULT,
  163. label: DATA_SOURCE_LABELS[Datasource.ERROR_DEFAULT],
  164. },
  165. {
  166. value: Datasource.DEFAULT,
  167. label: DATA_SOURCE_LABELS[Datasource.DEFAULT],
  168. },
  169. {
  170. value: Datasource.ERROR,
  171. label: DATA_SOURCE_LABELS[Datasource.ERROR],
  172. },
  173. ],
  174. },
  175. ];
  176. if (organization.features.includes('performance-view') && alertType === 'custom') {
  177. dataSourceOptions.push({
  178. label: t('Transactions'),
  179. options: [
  180. {
  181. value: Datasource.TRANSACTION,
  182. label: DATA_SOURCE_LABELS[Datasource.TRANSACTION],
  183. },
  184. ],
  185. });
  186. }
  187. return (
  188. <FormField
  189. name="datasource"
  190. inline={false}
  191. style={{
  192. ...this.formElemBaseStyle,
  193. minWidth: 300,
  194. flex: 2,
  195. }}
  196. flexibleControlStateSize
  197. >
  198. {({onChange, onBlur, model}) => {
  199. const formDataset = model.getValue('dataset');
  200. const formEventTypes = model.getValue('eventTypes');
  201. const mappedValue = convertDatasetEventTypesToSource(
  202. formDataset,
  203. formEventTypes
  204. );
  205. return (
  206. <SelectControl
  207. value={mappedValue}
  208. inFieldLabel={t('Events: ')}
  209. onChange={optionObj => {
  210. const optionValue = optionObj.value;
  211. onChange(optionValue, {});
  212. onBlur(optionValue, {});
  213. // Reset the aggregate to the default (which works across
  214. // datatypes), otherwise we may send snuba an invalid query
  215. // (transaction aggregate on events datasource = bad).
  216. optionValue === 'transaction'
  217. ? model.setValue('aggregate', DEFAULT_TRANSACTION_AGGREGATE)
  218. : model.setValue('aggregate', DEFAULT_AGGREGATE);
  219. // set the value of the dataset and event type from data source
  220. const {dataset: datasetFromDataSource, eventTypes} =
  221. DATA_SOURCE_TO_SET_AND_EVENT_TYPES[optionValue] ?? {};
  222. model.setValue('dataset', datasetFromDataSource);
  223. model.setValue('eventTypes', eventTypes);
  224. }}
  225. options={dataSourceOptions}
  226. isDisabled={disabled}
  227. />
  228. );
  229. }}
  230. </FormField>
  231. );
  232. }
  233. renderIdBadge(project: Project) {
  234. return (
  235. <IdBadge
  236. project={project}
  237. avatarProps={{consistentWidth: true}}
  238. avatarSize={18}
  239. disableLink
  240. hideName
  241. />
  242. );
  243. }
  244. renderProjectSelector() {
  245. const {
  246. project: _selectedProject,
  247. projects,
  248. disabled,
  249. organization,
  250. disableProjectSelector,
  251. } = this.props;
  252. const hasOpenMembership = organization.features.includes('open-membership');
  253. const myProjects = projects.filter(project => project.hasAccess && project.isMember);
  254. const allProjects = projects.filter(
  255. project => project.hasAccess && !project.isMember
  256. );
  257. const myProjectOptions = myProjects.map(myProject => ({
  258. value: myProject.id,
  259. label: myProject.slug,
  260. leadingItems: this.renderIdBadge(myProject),
  261. }));
  262. const openMembershipProjects = [
  263. {
  264. label: t('My Projects'),
  265. options: myProjectOptions,
  266. },
  267. {
  268. label: t('All Projects'),
  269. options: allProjects.map(allProject => ({
  270. value: allProject.id,
  271. label: allProject.slug,
  272. leadingItems: this.renderIdBadge(allProject),
  273. })),
  274. },
  275. ];
  276. const projectOptions =
  277. hasOpenMembership || isActiveSuperuser()
  278. ? openMembershipProjects
  279. : myProjectOptions;
  280. return (
  281. <FormField
  282. name="projectId"
  283. inline={false}
  284. style={{
  285. ...this.formElemBaseStyle,
  286. minWidth: 300,
  287. flex: 2,
  288. }}
  289. flexibleControlStateSize
  290. >
  291. {({onChange, onBlur, model}) => {
  292. const selectedProject =
  293. projects.find(({id}) => id === model.getValue('projectId')) ||
  294. _selectedProject;
  295. return (
  296. <SelectControl
  297. isDisabled={disabled || disableProjectSelector}
  298. value={selectedProject.id}
  299. options={projectOptions}
  300. onChange={({value}: {value: Project['id']}) => {
  301. // if the current owner/team isn't part of project selected, update to the first available team
  302. const nextSelectedProject =
  303. projects.find(({id}) => id === value) ?? selectedProject;
  304. const ownerId: String | undefined = model
  305. .getValue('owner')
  306. ?.split(':')[1];
  307. if (
  308. ownerId &&
  309. nextSelectedProject.teams.find(({id}) => id === ownerId) ===
  310. undefined &&
  311. nextSelectedProject.teams.length
  312. ) {
  313. model.setValue('owner', `team:${nextSelectedProject.teams[0].id}`);
  314. }
  315. onChange(value, {});
  316. onBlur(value, {});
  317. }}
  318. components={{
  319. SingleValue: containerProps => (
  320. <components.ValueContainer {...containerProps}>
  321. <IdBadge
  322. project={selectedProject}
  323. avatarProps={{consistentWidth: true}}
  324. avatarSize={18}
  325. disableLink
  326. />
  327. </components.ValueContainer>
  328. ),
  329. }}
  330. />
  331. );
  332. }}
  333. </FormField>
  334. );
  335. }
  336. renderInterval() {
  337. const {
  338. organization,
  339. disabled,
  340. alertType,
  341. hasAlertWizardV3,
  342. timeWindow,
  343. comparisonDelta,
  344. comparisonType,
  345. onTimeWindowChange,
  346. onComparisonDeltaChange,
  347. } = this.props;
  348. const {labelText, timeWindowText} = getFunctionHelpText(alertType);
  349. const intervalLabelText = hasAlertWizardV3 ? t('Define your metric') : labelText;
  350. return (
  351. <Fragment>
  352. <StyledListItem>
  353. <StyledListTitle>
  354. <div>{intervalLabelText}</div>
  355. {!hasAlertWizardV3 && (
  356. <Tooltip
  357. title={t(
  358. 'Time window over which the metric is evaluated. Alerts are evaluated every minute regardless of this value.'
  359. )}
  360. >
  361. <IconQuestion size="sm" color="gray200" />
  362. </Tooltip>
  363. )}
  364. </StyledListTitle>
  365. </StyledListItem>
  366. <FormRow>
  367. {hasAlertWizardV3 ? (
  368. <WizardField
  369. name="aggregate"
  370. help={null}
  371. organization={organization}
  372. disabled={disabled}
  373. style={{
  374. ...this.formElemBaseStyle,
  375. flex: 1,
  376. }}
  377. inline={false}
  378. flexibleControlStateSize
  379. columnWidth={200}
  380. alertType={alertType}
  381. required
  382. />
  383. ) : (
  384. <MetricField
  385. name="aggregate"
  386. help={null}
  387. organization={organization}
  388. disabled={disabled}
  389. style={{
  390. ...this.formElemBaseStyle,
  391. }}
  392. inline={false}
  393. flexibleControlStateSize
  394. columnWidth={200}
  395. alertType={alertType}
  396. required
  397. />
  398. )}
  399. {!hasAlertWizardV3 && timeWindowText && (
  400. <FormRowText>{timeWindowText}</FormRowText>
  401. )}
  402. <SelectControl
  403. name="timeWindow"
  404. styles={{
  405. control: (provided: {[x: string]: string | number | boolean}) => ({
  406. ...provided,
  407. minWidth: hasAlertWizardV3 ? 200 : 130,
  408. maxWidth: 300,
  409. }),
  410. container: (provided: {[x: string]: string | number | boolean}) => ({
  411. ...provided,
  412. margin: hasAlertWizardV3 ? `${space(0.5)}` : 0,
  413. }),
  414. }}
  415. options={this.timeWindowOptions}
  416. required
  417. isDisabled={disabled}
  418. value={timeWindow}
  419. onChange={({value}) => onTimeWindowChange(value)}
  420. inline={false}
  421. flexibleControlStateSize
  422. />
  423. {!hasAlertWizardV3 && (
  424. <Feature
  425. features={['organizations:change-alerts']}
  426. organization={organization}
  427. >
  428. {comparisonType === AlertRuleComparisonType.CHANGE && (
  429. <ComparisonContainer>
  430. {t(' compared to ')}
  431. <SelectControl
  432. name="comparisonDelta"
  433. styles={{
  434. container: (provided: {
  435. [x: string]: string | number | boolean;
  436. }) => ({
  437. ...provided,
  438. marginLeft: space(1),
  439. }),
  440. control: (provided: {[x: string]: string | number | boolean}) => ({
  441. ...provided,
  442. minWidth: 500,
  443. maxWidth: 1000,
  444. }),
  445. }}
  446. value={comparisonDelta}
  447. onChange={({value}) => onComparisonDeltaChange(value)}
  448. options={COMPARISON_DELTA_OPTIONS}
  449. required={comparisonType === AlertRuleComparisonType.CHANGE}
  450. />
  451. </ComparisonContainer>
  452. )}
  453. </Feature>
  454. )}
  455. </FormRow>
  456. </Fragment>
  457. );
  458. }
  459. render() {
  460. const {
  461. organization,
  462. disabled,
  463. onFilterSearch,
  464. allowChangeEventTypes,
  465. hasAlertWizardV3,
  466. dataset,
  467. showMEPAlertBanner,
  468. } = this.props;
  469. const {environments} = this.state;
  470. const environmentOptions: SelectValue<string | null>[] = [
  471. {
  472. value: null,
  473. label: t('All Environments'),
  474. },
  475. ...(environments?.map(env => ({value: env.name, label: getDisplayName(env)})) ??
  476. []),
  477. ];
  478. const transactionTags = [
  479. 'transaction',
  480. 'transaction.duration',
  481. 'transaction.op',
  482. 'transaction.status',
  483. ];
  484. const measurementTags = Object.values({...WebVital, ...MobileVital});
  485. const eventOmitTags =
  486. dataset === 'events' ? [...measurementTags, ...transactionTags] : [];
  487. return (
  488. <Fragment>
  489. <ChartPanel>
  490. <StyledPanelBody>{this.props.thresholdChart}</StyledPanelBody>
  491. </ChartPanel>
  492. {showMEPAlertBanner &&
  493. organization.features.includes('metrics-performance-alerts') && (
  494. <AlertContainer>
  495. <Alert type="info" showIcon>
  496. {tct(
  497. 'Filtering by these conditions automatically switch you to indexed events. [link:Learn more].',
  498. {
  499. link: (
  500. <ExternalLink href="https://docs.sentry.io/product/sentry-basics/search/" />
  501. ),
  502. }
  503. )}
  504. </Alert>
  505. </AlertContainer>
  506. )}
  507. {hasAlertWizardV3 && this.renderInterval()}
  508. <StyledListItem>{t('Filter events')}</StyledListItem>
  509. <FormRow
  510. noMargin
  511. columns={1 + (allowChangeEventTypes ? 1 : 0) + (hasAlertWizardV3 ? 1 : 0)}
  512. >
  513. {hasAlertWizardV3 && this.renderProjectSelector()}
  514. <SelectField
  515. name="environment"
  516. placeholder={t('All Environments')}
  517. style={{
  518. ...this.formElemBaseStyle,
  519. minWidth: 230,
  520. flex: 1,
  521. }}
  522. styles={{
  523. singleValue: (base: any) => ({
  524. ...base,
  525. }),
  526. option: (base: any) => ({
  527. ...base,
  528. }),
  529. }}
  530. options={environmentOptions}
  531. isDisabled={disabled || this.state.environments === null}
  532. isClearable
  533. inline={false}
  534. flexibleControlStateSize
  535. />
  536. {allowChangeEventTypes && this.renderEventTypeFilter()}
  537. </FormRow>
  538. <FormRow>
  539. <FormField
  540. name="query"
  541. inline={false}
  542. style={{
  543. ...this.formElemBaseStyle,
  544. flex: '6 0 500px',
  545. }}
  546. flexibleControlStateSize
  547. >
  548. {({onChange, onBlur, onKeyDown, initialData}) => (
  549. <SearchContainer>
  550. <StyledSearchBar
  551. searchSource="alert_builder"
  552. defaultQuery={initialData?.query ?? ''}
  553. omitTags={[
  554. 'event.type',
  555. 'release.version',
  556. 'release.stage',
  557. 'release.package',
  558. 'release.build',
  559. 'project',
  560. ...eventOmitTags,
  561. ]}
  562. includeSessionTagsValues={dataset === Dataset.SESSIONS}
  563. disabled={disabled}
  564. useFormWrapper={false}
  565. organization={organization}
  566. placeholder={this.searchPlaceholder}
  567. onChange={onChange}
  568. query={initialData.query}
  569. onKeyDown={e => {
  570. /**
  571. * Do not allow enter key to submit the alerts form since it is unlikely
  572. * users will be ready to create the rule as this sits above required fields.
  573. */
  574. if (e.key === 'Enter') {
  575. e.preventDefault();
  576. e.stopPropagation();
  577. }
  578. onKeyDown?.(e);
  579. }}
  580. onBlur={query => {
  581. onFilterSearch(query);
  582. onBlur(query);
  583. }}
  584. onSearch={query => {
  585. onFilterSearch(query);
  586. onChange(query, {});
  587. }}
  588. {...(this.searchSupportedTags
  589. ? {supportedTags: this.searchSupportedTags}
  590. : {})}
  591. hasRecentSearches={dataset !== Dataset.SESSIONS}
  592. />
  593. </SearchContainer>
  594. )}
  595. </FormField>
  596. </FormRow>
  597. {!hasAlertWizardV3 && this.renderInterval()}
  598. </Fragment>
  599. );
  600. }
  601. }
  602. const StyledListTitle = styled('div')`
  603. display: flex;
  604. span {
  605. margin-left: ${space(1)};
  606. }
  607. `;
  608. const ChartPanel = styled(Panel)`
  609. margin-bottom: ${space(1)};
  610. `;
  611. const AlertContainer = styled('div')`
  612. margin-bottom: ${space(2)};
  613. `;
  614. const StyledPanelBody = styled(PanelBody)`
  615. ol,
  616. h4 {
  617. margin-bottom: ${space(1)};
  618. }
  619. `;
  620. const SearchContainer = styled('div')`
  621. display: flex;
  622. `;
  623. const StyledSearchBar = styled(SearchBar)`
  624. flex-grow: 1;
  625. `;
  626. const StyledListItem = styled(ListItem)`
  627. margin-bottom: ${space(1)};
  628. font-size: ${p => p.theme.fontSizeExtraLarge};
  629. line-height: 1.3;
  630. `;
  631. const FormRow = styled('div')<{columns?: number; noMargin?: boolean}>`
  632. display: flex;
  633. flex-direction: row;
  634. align-items: center;
  635. flex-wrap: wrap;
  636. margin-bottom: ${p => (p.noMargin ? 0 : space(4))};
  637. ${p =>
  638. p.columns !== undefined &&
  639. css`
  640. display: grid;
  641. grid-template-columns: repeat(${p.columns}, auto);
  642. `}
  643. `;
  644. const FormRowText = styled('div')`
  645. margin: ${space(1)};
  646. `;
  647. const ComparisonContainer = styled('div')`
  648. margin-left: ${space(1)};
  649. display: flex;
  650. flex-direction: row;
  651. align-items: center;
  652. `;
  653. export default withProjects(RuleConditionsForm);