ruleConditionsForm.tsx 21 KB

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