ruleConditionsForm.tsx 19 KB

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