ruleConditionsForm.tsx 18 KB

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