ruleConditionsForm.tsx 19 KB

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