ruleConditionsForm.tsx 18 KB

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