ruleConditionsForm.tsx 21 KB

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