ruleConditionsForm.tsx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. import {Fragment, PureComponent} from 'react';
  2. import {components} from 'react-select';
  3. import {css} from '@emotion/react';
  4. import styled from '@emotion/styled';
  5. import omit from 'lodash/omit';
  6. import pick from 'lodash/pick';
  7. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  8. import {fetchTagValues} from 'sentry/actionCreators/tags';
  9. import type {Client} from 'sentry/api';
  10. import Alert from 'sentry/components/alert';
  11. import {
  12. OnDemandMetricAlert,
  13. OnDemandWarningIcon,
  14. } from 'sentry/components/alerts/onDemandMetricAlert';
  15. import {getHasTag} from 'sentry/components/events/searchBar';
  16. import {
  17. STATIC_FIELD_TAGS,
  18. STATIC_FIELD_TAGS_WITHOUT_ERROR_FIELDS,
  19. STATIC_FIELD_TAGS_WITHOUT_TRACING,
  20. STATIC_FIELD_TAGS_WITHOUT_TRANSACTION_FIELDS,
  21. STATIC_SEMVER_TAGS,
  22. STATIC_SPAN_TAGS,
  23. } from 'sentry/components/events/searchBarFieldConstants';
  24. import SelectControl from 'sentry/components/forms/controls/selectControl';
  25. import SelectField from 'sentry/components/forms/fields/selectField';
  26. import FormField from 'sentry/components/forms/formField';
  27. import IdBadge from 'sentry/components/idBadge';
  28. import ListItem from 'sentry/components/list/listItem';
  29. import {MetricSearchBar} from 'sentry/components/metrics/metricSearchBar';
  30. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  31. import Panel from 'sentry/components/panels/panel';
  32. import PanelBody from 'sentry/components/panels/panelBody';
  33. import {EAPSpanSearchQueryBuilder} from 'sentry/components/performance/spanSearchQueryBuilder';
  34. import {SearchQueryBuilder} from 'sentry/components/searchQueryBuilder';
  35. import {InvalidReason} from 'sentry/components/searchSyntax/parser';
  36. import {t, tct} from 'sentry/locale';
  37. import {space} from 'sentry/styles/space';
  38. import type {SelectValue} from 'sentry/types/core';
  39. import type {Tag, TagCollection} from 'sentry/types/group';
  40. import type {InjectedRouter} from 'sentry/types/legacyReactRouter';
  41. import type {Organization} from 'sentry/types/organization';
  42. import type {Environment, Project} from 'sentry/types/project';
  43. import {defined} from 'sentry/utils';
  44. import {isAggregateField, isMeasurement} from 'sentry/utils/discover/fields';
  45. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  46. import {getDisplayName} from 'sentry/utils/environment';
  47. import {
  48. ALLOWED_EXPLORE_VISUALIZE_AGGREGATES,
  49. DEVICE_CLASS_TAG_VALUES,
  50. FieldKind,
  51. isDeviceClass,
  52. } from 'sentry/utils/fields';
  53. import {
  54. getMeasurements,
  55. type MeasurementCollection,
  56. } from 'sentry/utils/measurements/measurements';
  57. import {hasCustomMetrics} from 'sentry/utils/metrics/features';
  58. import {getMRI} from 'sentry/utils/metrics/mri';
  59. import {getOnDemandKeys, isOnDemandQueryString} from 'sentry/utils/onDemandMetrics';
  60. import {hasOnDemandMetricAlertFeature} from 'sentry/utils/onDemandMetrics/features';
  61. import withApi from 'sentry/utils/withApi';
  62. import withProjects from 'sentry/utils/withProjects';
  63. import withTags from 'sentry/utils/withTags';
  64. import WizardField from 'sentry/views/alerts/rules/metric/wizardField';
  65. import {
  66. convertDatasetEventTypesToSource,
  67. DATA_SOURCE_LABELS,
  68. DATA_SOURCE_TO_SET_AND_EVENT_TYPES,
  69. } from 'sentry/views/alerts/utils';
  70. import type {AlertType} from 'sentry/views/alerts/wizard/options';
  71. import {getSupportedAndOmittedTags} from 'sentry/views/alerts/wizard/options';
  72. import {
  73. SpanTagsContext,
  74. SpanTagsProvider,
  75. } from 'sentry/views/explore/contexts/spanTagsContext';
  76. import {hasEAPAlerts} from 'sentry/views/insights/common/utils/hasEAPAlerts';
  77. import {getProjectOptions} from '../utils';
  78. import {isCrashFreeAlert} from './utils/isCrashFreeAlert';
  79. import {DEFAULT_AGGREGATE, DEFAULT_TRANSACTION_AGGREGATE} from './constants';
  80. import {AlertRuleComparisonType, Dataset, Datasource, TimeWindow} from './types';
  81. const TIME_WINDOW_MAP: Record<TimeWindow, string> = {
  82. [TimeWindow.ONE_MINUTE]: t('1 minute'),
  83. [TimeWindow.FIVE_MINUTES]: t('5 minutes'),
  84. [TimeWindow.TEN_MINUTES]: t('10 minutes'),
  85. [TimeWindow.FIFTEEN_MINUTES]: t('15 minutes'),
  86. [TimeWindow.THIRTY_MINUTES]: t('30 minutes'),
  87. [TimeWindow.ONE_HOUR]: t('1 hour'),
  88. [TimeWindow.TWO_HOURS]: t('2 hours'),
  89. [TimeWindow.FOUR_HOURS]: t('4 hours'),
  90. [TimeWindow.ONE_DAY]: t('24 hours'),
  91. };
  92. type Props = {
  93. aggregate: string;
  94. alertType: AlertType;
  95. api: Client;
  96. comparisonType: AlertRuleComparisonType;
  97. dataset: Dataset;
  98. disabled: boolean;
  99. isEditing: boolean;
  100. onComparisonDeltaChange: (value: number) => void;
  101. onFilterSearch: (query: string, isQueryValid: any) => void;
  102. onTimeWindowChange: (value: number) => void;
  103. organization: Organization;
  104. project: Project;
  105. projects: Project[];
  106. router: InjectedRouter;
  107. tags: TagCollection;
  108. thresholdChart: React.ReactNode;
  109. timeWindow: number;
  110. // optional props
  111. allowChangeEventTypes?: boolean;
  112. comparisonDelta?: number;
  113. disableProjectSelector?: boolean;
  114. isErrorMigration?: boolean;
  115. isExtrapolatedChartData?: boolean;
  116. isForLlmMetric?: boolean;
  117. isLowConfidenceChartData?: boolean;
  118. isTransactionMigration?: boolean;
  119. loadingProjects?: boolean;
  120. };
  121. type State = {
  122. environments: Environment[] | null;
  123. filterKeys: TagCollection;
  124. measurements: MeasurementCollection;
  125. };
  126. class RuleConditionsForm extends PureComponent<Props, State> {
  127. state: State = {
  128. environments: null,
  129. measurements: {},
  130. filterKeys: {},
  131. };
  132. componentDidMount() {
  133. this.fetchData();
  134. const measurements = getMeasurements();
  135. const filterKeys = this.getFilterKeys();
  136. this.setState({measurements, filterKeys});
  137. }
  138. componentDidUpdate(prevProps: Props) {
  139. if (prevProps.project.id === this.props.project.id) {
  140. return;
  141. }
  142. this.fetchData();
  143. }
  144. getFilterKeys = () => {
  145. const {organization, dataset, tags} = this.props;
  146. const {measurements} = this.state;
  147. const measurementsWithKind = Object.keys(measurements).reduce(
  148. (measurement_tags, key) => {
  149. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  150. measurement_tags[key] = {
  151. ...measurements[key],
  152. kind: FieldKind.MEASUREMENT,
  153. };
  154. return measurement_tags;
  155. },
  156. {}
  157. );
  158. const orgHasPerformanceView = organization.features.includes('performance-view');
  159. const combinedTags: TagCollection =
  160. dataset === Dataset.ERRORS
  161. ? Object.assign({}, STATIC_FIELD_TAGS_WITHOUT_TRANSACTION_FIELDS)
  162. : dataset === Dataset.TRANSACTIONS
  163. ? Object.assign(
  164. {},
  165. measurementsWithKind,
  166. STATIC_SPAN_TAGS,
  167. STATIC_FIELD_TAGS_WITHOUT_ERROR_FIELDS
  168. )
  169. : orgHasPerformanceView
  170. ? Object.assign({}, measurementsWithKind, STATIC_SPAN_TAGS, STATIC_FIELD_TAGS)
  171. : Object.assign({}, STATIC_FIELD_TAGS_WITHOUT_TRACING);
  172. const tagsWithKind = Object.keys(tags).reduce<Record<string, Tag>>((acc, key) => {
  173. acc[key] = {
  174. ...tags[key]!,
  175. kind: FieldKind.TAG,
  176. };
  177. return acc;
  178. }, {});
  179. const {omitTags} = getSupportedAndOmittedTags(dataset, organization);
  180. Object.assign(combinedTags, tagsWithKind, STATIC_SEMVER_TAGS);
  181. combinedTags.has = getHasTag(combinedTags);
  182. const list =
  183. omitTags && omitTags.length > 0 ? omit(combinedTags, omitTags) : combinedTags;
  184. return list;
  185. };
  186. formElemBaseStyle = {
  187. padding: `${space(0.5)}`,
  188. border: 'none',
  189. };
  190. async fetchData() {
  191. const {api, organization, project} = this.props;
  192. try {
  193. const environments = await api.requestPromise(
  194. `/projects/${organization.slug}/${project.slug}/environments/`,
  195. {
  196. query: {
  197. visibility: 'visible',
  198. },
  199. }
  200. );
  201. this.setState({environments});
  202. } catch (_err) {
  203. addErrorMessage(t('Unable to fetch environments'));
  204. }
  205. }
  206. getEventFieldValues = async (tag: any, query: any): Promise<string[]> => {
  207. const {api, organization, project, dataset, router} = this.props;
  208. if (isAggregateField(tag.key) || isMeasurement(tag.key)) {
  209. // We can't really auto suggest values for aggregate fields
  210. // or measurements, so we simply don't
  211. // NOTE: these in particular are for discover queries. We may not need/support these
  212. return Promise.resolve([]);
  213. }
  214. // device.class is stored as "numbers" in snuba, but we want to suggest high, medium,
  215. // and low search filter values because discover maps device.class to these values.
  216. if (isDeviceClass(tag.key)) {
  217. return Promise.resolve(DEVICE_CLASS_TAG_VALUES);
  218. }
  219. const values = await fetchTagValues({
  220. api,
  221. orgSlug: organization.slug,
  222. tagKey: tag.key,
  223. search: query,
  224. projectIds: [project.id],
  225. endpointParams: normalizeDateTimeParams(router.location.query), // allows searching for tags on transactions as well
  226. includeTransactions: true, // allows searching for tags on sessions as well
  227. includeSessions: dataset === Dataset.SESSIONS,
  228. });
  229. return values.filter(({name}) => defined(name)).map(({name}) => name);
  230. };
  231. get timeWindowOptions() {
  232. let options: Record<string, string> = TIME_WINDOW_MAP;
  233. const {alertType} = this.props;
  234. if (alertType === 'custom_metrics') {
  235. // Do not show ONE MINUTE interval as an option for custom_metrics alert
  236. options = omit(options, TimeWindow.ONE_MINUTE.toString());
  237. }
  238. if (isCrashFreeAlert(this.props.dataset)) {
  239. options = pick(TIME_WINDOW_MAP, [
  240. // TimeWindow.THIRTY_MINUTES, leaving this option out until we figure out the sub-hour session resolution chart limitations
  241. TimeWindow.ONE_HOUR,
  242. TimeWindow.TWO_HOURS,
  243. TimeWindow.FOUR_HOURS,
  244. TimeWindow.ONE_DAY,
  245. ]);
  246. }
  247. if (this.props.comparisonType === AlertRuleComparisonType.DYNAMIC) {
  248. options = pick(TIME_WINDOW_MAP, [
  249. TimeWindow.FIFTEEN_MINUTES,
  250. TimeWindow.THIRTY_MINUTES,
  251. TimeWindow.ONE_HOUR,
  252. ]);
  253. }
  254. if (this.props.dataset === Dataset.EVENTS_ANALYTICS_PLATFORM) {
  255. options = pick(TIME_WINDOW_MAP, [
  256. TimeWindow.FIVE_MINUTES,
  257. TimeWindow.TEN_MINUTES,
  258. TimeWindow.FIFTEEN_MINUTES,
  259. TimeWindow.THIRTY_MINUTES,
  260. TimeWindow.ONE_HOUR,
  261. TimeWindow.TWO_HOURS,
  262. TimeWindow.FOUR_HOURS,
  263. TimeWindow.ONE_DAY,
  264. ]);
  265. }
  266. return Object.entries(options).map(([value, label]) => ({
  267. value: parseInt(value, 10),
  268. label: tct('[timeWindow] interval', {
  269. timeWindow: label.slice(-1) === 's' ? label.slice(0, -1) : label,
  270. }),
  271. }));
  272. }
  273. get searchPlaceholder() {
  274. switch (this.props.dataset) {
  275. case Dataset.ERRORS:
  276. return t('Filter events by level, message, and other properties\u2026');
  277. case Dataset.METRICS:
  278. case Dataset.SESSIONS:
  279. return t('Filter sessions by release version\u2026');
  280. default:
  281. return t('Filter transactions by URL, tags, and other properties\u2026');
  282. }
  283. }
  284. get selectControlStyles() {
  285. return {
  286. control: (provided: {[x: string]: string | number | boolean}) => ({
  287. ...provided,
  288. minWidth: 200,
  289. maxWidth: 300,
  290. }),
  291. container: (provided: {[x: string]: string | number | boolean}) => ({
  292. ...provided,
  293. margin: `${space(0.5)}`,
  294. }),
  295. };
  296. }
  297. renderEventTypeFilter() {
  298. const {organization, disabled, alertType, isErrorMigration} = this.props;
  299. const dataSourceOptions = [
  300. {
  301. label: t('Errors'),
  302. options: [
  303. {
  304. value: Datasource.ERROR_DEFAULT,
  305. label: DATA_SOURCE_LABELS[Datasource.ERROR_DEFAULT],
  306. },
  307. {
  308. value: Datasource.DEFAULT,
  309. label: DATA_SOURCE_LABELS[Datasource.DEFAULT],
  310. },
  311. {
  312. value: Datasource.ERROR,
  313. label: DATA_SOURCE_LABELS[Datasource.ERROR],
  314. },
  315. ],
  316. },
  317. ];
  318. if (
  319. organization.features.includes('performance-view') &&
  320. (alertType === 'custom_transactions' || alertType === 'custom_metrics')
  321. ) {
  322. dataSourceOptions.push({
  323. label: t('Transactions'),
  324. options: [
  325. {
  326. value: Datasource.TRANSACTION,
  327. label: DATA_SOURCE_LABELS[Datasource.TRANSACTION],
  328. },
  329. ],
  330. });
  331. }
  332. return (
  333. <FormField
  334. name="datasource"
  335. inline={false}
  336. style={{
  337. ...this.formElemBaseStyle,
  338. minWidth: 300,
  339. flex: 2,
  340. }}
  341. flexibleControlStateSize
  342. >
  343. {({onChange, onBlur, model}: any) => {
  344. const formDataset = model.getValue('dataset');
  345. const formEventTypes = model.getValue('eventTypes');
  346. const aggregate = model.getValue('aggregate');
  347. const mappedValue = convertDatasetEventTypesToSource(
  348. formDataset,
  349. formEventTypes
  350. );
  351. return (
  352. <SelectControl
  353. value={mappedValue}
  354. inFieldLabel={t('Events: ')}
  355. onChange={({value}: any) => {
  356. onChange(value, {});
  357. onBlur(value, {});
  358. // Reset the aggregate to the default (which works across
  359. // datatypes), otherwise we may send snuba an invalid query
  360. // (transaction aggregate on events datasource = bad).
  361. const newAggregate =
  362. value === Datasource.TRANSACTION
  363. ? DEFAULT_TRANSACTION_AGGREGATE
  364. : DEFAULT_AGGREGATE;
  365. if (alertType === 'custom_transactions' && aggregate !== newAggregate) {
  366. model.setValue('aggregate', newAggregate);
  367. }
  368. // set the value of the dataset and event type from data source
  369. const {dataset: datasetFromDataSource, eventTypes} =
  370. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  371. DATA_SOURCE_TO_SET_AND_EVENT_TYPES[value] ?? {};
  372. model.setValue('dataset', datasetFromDataSource);
  373. model.setValue('eventTypes', eventTypes);
  374. }}
  375. options={dataSourceOptions}
  376. isDisabled={disabled || isErrorMigration}
  377. />
  378. );
  379. }}
  380. </FormField>
  381. );
  382. }
  383. renderProjectSelector() {
  384. const {
  385. project: _selectedProject,
  386. projects, // note: org projects
  387. disabled,
  388. organization,
  389. disableProjectSelector,
  390. } = this.props;
  391. const projectOptions = getProjectOptions({
  392. organization,
  393. projects,
  394. isFormDisabled: disabled,
  395. });
  396. return (
  397. <FormField
  398. name="projectId"
  399. inline={false}
  400. style={{
  401. ...this.formElemBaseStyle,
  402. minWidth: 300,
  403. flex: 2,
  404. }}
  405. flexibleControlStateSize
  406. >
  407. {({onChange, onBlur, model}: any) => {
  408. const selectedProject =
  409. projects.find(({id}) => id === model.getValue('projectId')) ||
  410. _selectedProject;
  411. return (
  412. <SelectControl
  413. isDisabled={disabled || disableProjectSelector}
  414. value={selectedProject.id}
  415. options={projectOptions}
  416. onChange={({value}: {value: Project['id']}) => {
  417. // if the current owner/team isn't part of project selected, update to the first available team
  418. const nextSelectedProject =
  419. projects.find(({id}) => id === value) ?? selectedProject;
  420. const ownerId: string | undefined = model
  421. .getValue('owner')
  422. ?.split(':')[1];
  423. if (
  424. ownerId &&
  425. nextSelectedProject.teams.find(({id}) => id === ownerId) ===
  426. undefined &&
  427. nextSelectedProject.teams.length
  428. ) {
  429. model.setValue('owner', `team:${nextSelectedProject.teams[0]!.id}`);
  430. }
  431. onChange(value, {});
  432. onBlur(value, {});
  433. }}
  434. components={{
  435. SingleValue: (containerProps: any) => (
  436. <components.ValueContainer {...containerProps}>
  437. <IdBadge
  438. project={selectedProject}
  439. avatarProps={{consistentWidth: true}}
  440. avatarSize={18}
  441. disableLink
  442. />
  443. </components.ValueContainer>
  444. ),
  445. }}
  446. />
  447. );
  448. }}
  449. </FormField>
  450. );
  451. }
  452. renderInterval() {
  453. const {
  454. organization,
  455. timeWindow,
  456. disabled,
  457. alertType,
  458. project,
  459. isForLlmMetric,
  460. onTimeWindowChange,
  461. } = this.props;
  462. return (
  463. <Fragment>
  464. <StyledListItem>
  465. <StyledListTitle>
  466. <div>{t('Define your metric')}</div>
  467. </StyledListTitle>
  468. </StyledListItem>
  469. <FormRow>
  470. {isForLlmMetric ? null : (
  471. <WizardField
  472. name="aggregate"
  473. help={null}
  474. organization={organization}
  475. disabled={disabled}
  476. project={project}
  477. style={{
  478. ...this.formElemBaseStyle,
  479. flex: 1,
  480. }}
  481. inline={false}
  482. flexibleControlStateSize
  483. columnWidth={200}
  484. alertType={alertType}
  485. required
  486. />
  487. )}
  488. <SelectControl
  489. name="timeWindow"
  490. styles={this.selectControlStyles}
  491. options={this.timeWindowOptions}
  492. isDisabled={disabled}
  493. value={timeWindow}
  494. onChange={({value}: any) => onTimeWindowChange(value)}
  495. inline={false}
  496. flexibleControlStateSize
  497. />
  498. </FormRow>
  499. </Fragment>
  500. );
  501. }
  502. render() {
  503. const {
  504. alertType,
  505. organization,
  506. disabled,
  507. onFilterSearch,
  508. allowChangeEventTypes,
  509. dataset,
  510. isExtrapolatedChartData,
  511. isTransactionMigration,
  512. isErrorMigration,
  513. aggregate,
  514. project,
  515. comparisonType,
  516. isLowConfidenceChartData,
  517. } = this.props;
  518. const {environments, filterKeys} = this.state;
  519. const environmentOptions: Array<SelectValue<string | null>> = [
  520. {
  521. value: null,
  522. label: t('All Environments'),
  523. },
  524. ...(environments?.map(env => ({value: env.name, label: getDisplayName(env)})) ??
  525. []),
  526. ];
  527. const confidenceEnabled = hasEAPAlerts(organization);
  528. return (
  529. <Fragment>
  530. <ChartPanel>
  531. <StyledPanelBody>{this.props.thresholdChart}</StyledPanelBody>
  532. </ChartPanel>
  533. {isTransactionMigration ? (
  534. <Fragment>
  535. <Spacer />
  536. <HiddenListItem />
  537. <HiddenListItem />
  538. </Fragment>
  539. ) : (
  540. <Fragment>
  541. <SpanTagsProvider
  542. dataset={DiscoverDatasets.SPANS_EAP}
  543. enabled={
  544. organization.features.includes('alerts-eap') &&
  545. alertType === 'eap_metrics'
  546. }
  547. >
  548. {isExtrapolatedChartData && (
  549. <OnDemandMetricAlert
  550. message={t(
  551. 'The chart data above is an estimate based on the stored transactions that match the filters specified.'
  552. )}
  553. />
  554. )}
  555. {confidenceEnabled && isLowConfidenceChartData && (
  556. <Alert showIcon type="warning">
  557. {t(
  558. 'Your low sample count may impact the accuracy of this alert. Edit your query or increase your sampling rate.'
  559. )}
  560. </Alert>
  561. )}
  562. {!isErrorMigration && this.renderInterval()}
  563. <StyledListItem>{t('Filter events')}</StyledListItem>
  564. <FormRow noMargin columns={1 + (allowChangeEventTypes ? 1 : 0) + 1}>
  565. {this.renderProjectSelector()}
  566. <SelectField
  567. name="environment"
  568. placeholder={t('All Environments')}
  569. style={{
  570. ...this.formElemBaseStyle,
  571. minWidth: 230,
  572. flex: 1,
  573. }}
  574. styles={{
  575. singleValue: (base: any) => ({
  576. ...base,
  577. }),
  578. option: (base: any) => ({
  579. ...base,
  580. }),
  581. }}
  582. options={environmentOptions}
  583. isDisabled={
  584. disabled || this.state.environments === null || isErrorMigration
  585. }
  586. isClearable
  587. inline={false}
  588. flexibleControlStateSize
  589. />
  590. {allowChangeEventTypes && this.renderEventTypeFilter()}
  591. </FormRow>
  592. <FormRow noMargin>
  593. <FormField
  594. name="query"
  595. inline={false}
  596. style={{
  597. ...this.formElemBaseStyle,
  598. flex: '6 0 500px',
  599. }}
  600. flexibleControlStateSize
  601. >
  602. {({onChange, onBlur, initialData, value}: any) => {
  603. return hasCustomMetrics(organization) &&
  604. alertType === 'custom_metrics' ? (
  605. <MetricSearchBar
  606. mri={getMRI(aggregate)}
  607. projectIds={[project.id]}
  608. placeholder={this.searchPlaceholder}
  609. query={initialData.query}
  610. defaultQuery={initialData?.query ?? ''}
  611. useFormWrapper={false}
  612. searchSource="alert_builder"
  613. onChange={query => {
  614. onFilterSearch(query, true);
  615. onChange(query, {});
  616. }}
  617. />
  618. ) : alertType === 'eap_metrics' ? (
  619. <SpanTagsContext.Consumer>
  620. {tags => (
  621. <EAPSpanSearchQueryBuilder
  622. numberTags={tags?.number ?? {}}
  623. stringTags={tags?.string ?? {}}
  624. initialQuery={value ?? ''}
  625. searchSource="alerts"
  626. onSearch={(query, {parsedQuery}) => {
  627. onFilterSearch(query, parsedQuery);
  628. onChange(query, {});
  629. }}
  630. supportedAggregates={ALLOWED_EXPLORE_VISUALIZE_AGGREGATES}
  631. projects={[parseInt(project.id, 10)]}
  632. />
  633. )}
  634. </SpanTagsContext.Consumer>
  635. ) : (
  636. <SearchContainer>
  637. <SearchQueryBuilder
  638. initialQuery={initialData?.query ?? ''}
  639. getTagValues={this.getEventFieldValues}
  640. placeholder={this.searchPlaceholder}
  641. searchSource="alert_builder"
  642. filterKeys={filterKeys}
  643. disabled={disabled || isErrorMigration}
  644. onChange={onChange}
  645. invalidMessages={{
  646. [InvalidReason.WILDCARD_NOT_ALLOWED]: t(
  647. 'The wildcard operator is not supported here.'
  648. ),
  649. [InvalidReason.FREE_TEXT_NOT_ALLOWED]: t(
  650. 'Free text search is not allowed. If you want to partially match transaction names, use glob patterns like "transaction:*transaction-name*"'
  651. ),
  652. }}
  653. onSearch={query => {
  654. onFilterSearch(query, true);
  655. onChange(query, {});
  656. }}
  657. onBlur={(query, {parsedQuery}) => {
  658. onFilterSearch(query, parsedQuery);
  659. onBlur(query);
  660. }}
  661. // We only need strict validation for Transaction queries, everything else is fine
  662. disallowUnsupportedFilters={
  663. organization.features.includes('alert-allow-indexed') ||
  664. (hasOnDemandMetricAlertFeature(organization) &&
  665. isOnDemandQueryString(value))
  666. ? false
  667. : dataset === Dataset.GENERIC_METRICS
  668. }
  669. />
  670. {isExtrapolatedChartData && isOnDemandQueryString(value) && (
  671. <OnDemandWarningIcon
  672. color="gray500"
  673. msg={tct(
  674. `We don’t routinely collect metrics from [fields]. However, we’ll do so [strong:once this alert has been saved.]`,
  675. {
  676. fields: (
  677. <strong>
  678. {getOnDemandKeys(value)
  679. .map(key => `"${key}"`)
  680. .join(', ')}
  681. </strong>
  682. ),
  683. strong: <strong />,
  684. }
  685. )}
  686. />
  687. )}
  688. </SearchContainer>
  689. );
  690. }}
  691. </FormField>
  692. </FormRow>
  693. <FormRow noMargin>
  694. <FormField
  695. name="query"
  696. inline={false}
  697. style={{
  698. ...this.formElemBaseStyle,
  699. flex: '6 0 500px',
  700. }}
  701. flexibleControlStateSize
  702. >
  703. {(args: any) => {
  704. if (
  705. args.value?.includes('is:unresolved') &&
  706. comparisonType === AlertRuleComparisonType.DYNAMIC
  707. ) {
  708. return (
  709. <OnDemandMetricAlert
  710. message={t(
  711. "'is:unresolved' queries are not supported by Anomaly Detection alerts."
  712. )}
  713. />
  714. );
  715. }
  716. return null;
  717. }}
  718. </FormField>
  719. </FormRow>
  720. </SpanTagsProvider>
  721. </Fragment>
  722. )}
  723. </Fragment>
  724. );
  725. }
  726. }
  727. const StyledListTitle = styled('div')`
  728. display: flex;
  729. span {
  730. margin-left: ${space(1)};
  731. }
  732. `;
  733. // This is a temporary hacky solution to hide list items without changing the numbering of the rest of the list
  734. // TODO(issues): Remove this once the migration is complete
  735. const HiddenListItem = styled(ListItem)`
  736. position: absolute;
  737. width: 0px;
  738. height: 0px;
  739. opacity: 0;
  740. pointer-events: none;
  741. `;
  742. const Spacer = styled('div')`
  743. margin-bottom: ${space(2)};
  744. `;
  745. const ChartPanel = styled(Panel)`
  746. margin-bottom: ${space(1)};
  747. `;
  748. const StyledPanelBody = styled(PanelBody)`
  749. ol,
  750. h4 {
  751. margin-bottom: ${space(1)};
  752. }
  753. `;
  754. const SearchContainer = styled('div')`
  755. display: flex;
  756. align-items: center;
  757. gap: ${space(1)};
  758. `;
  759. const StyledListItem = styled(ListItem)`
  760. margin-bottom: ${space(0.5)};
  761. font-size: ${p => p.theme.fontSizeExtraLarge};
  762. line-height: 1.3;
  763. `;
  764. const FormRow = styled('div')<{columns?: number; noMargin?: boolean}>`
  765. display: flex;
  766. flex-direction: row;
  767. align-items: center;
  768. flex-wrap: wrap;
  769. margin-bottom: ${p => (p.noMargin ? 0 : space(4))};
  770. ${p =>
  771. p.columns !== undefined &&
  772. css`
  773. display: grid;
  774. grid-template-columns: repeat(${p.columns}, auto);
  775. `}
  776. `;
  777. export default withApi(withProjects(withTags(RuleConditionsForm)));