ruleConditionsForm.tsx 21 KB

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