ruleConditionsForm.tsx 20 KB

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