ruleConditionsForm.tsx 19 KB

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