ruleConditionsForm.tsx 18 KB

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