ruleConditionsForm.tsx 18 KB

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