123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- import * as React from 'react';
- import {Fragment} from 'react';
- import styled from '@emotion/styled';
- import pick from 'lodash/pick';
- import {addErrorMessage} from 'sentry/actionCreators/indicator';
- import {Client} from 'sentry/api';
- import Feature from 'sentry/components/acl/feature';
- import SearchBar from 'sentry/components/events/searchBar';
- import FormField from 'sentry/components/forms/formField';
- import SelectControl from 'sentry/components/forms/selectControl';
- import SelectField from 'sentry/components/forms/selectField';
- import ListItem from 'sentry/components/list/listItem';
- import {Panel, PanelBody} from 'sentry/components/panels';
- import Tooltip from 'sentry/components/tooltip';
- import {IconQuestion} from 'sentry/icons';
- import {t} from 'sentry/locale';
- import space from 'sentry/styles/space';
- import {Environment, Organization, SelectValue} from 'sentry/types';
- import {MobileVital, WebVital} from 'sentry/utils/discover/fields';
- import {getDisplayName} from 'sentry/utils/environment';
- import {
- convertDatasetEventTypesToSource,
- DATA_SOURCE_LABELS,
- DATA_SOURCE_TO_SET_AND_EVENT_TYPES,
- } from 'sentry/views/alerts/utils';
- import {AlertType, getFunctionHelpText} from 'sentry/views/alerts/wizard/options';
- import {isCrashFreeAlert} from './utils/isCrashFreeAlert';
- import {
- COMPARISON_DELTA_OPTIONS,
- DEFAULT_AGGREGATE,
- DEFAULT_TRANSACTION_AGGREGATE,
- } from './constants';
- import MetricField from './metricField';
- import {AlertRuleComparisonType, Dataset, Datasource, TimeWindow} from './types';
- const TIME_WINDOW_MAP: Record<TimeWindow, string> = {
- [TimeWindow.ONE_MINUTE]: t('1 minute'),
- [TimeWindow.FIVE_MINUTES]: t('5 minutes'),
- [TimeWindow.TEN_MINUTES]: t('10 minutes'),
- [TimeWindow.FIFTEEN_MINUTES]: t('15 minutes'),
- [TimeWindow.THIRTY_MINUTES]: t('30 minutes'),
- [TimeWindow.ONE_HOUR]: t('1 hour'),
- [TimeWindow.TWO_HOURS]: t('2 hours'),
- [TimeWindow.FOUR_HOURS]: t('4 hours'),
- [TimeWindow.ONE_DAY]: t('24 hours'),
- };
- type Props = {
- alertType: AlertType;
- api: Client;
- comparisonType: AlertRuleComparisonType;
- dataset: Dataset;
- disabled: boolean;
- hasAlertWizardV3: boolean;
- onComparisonDeltaChange: (value: number) => void;
- onFilterSearch: (query: string) => void;
- onTimeWindowChange: (value: number) => void;
- organization: Organization;
- projectSlug: string;
- thresholdChart: React.ReactNode;
- timeWindow: number;
- allowChangeEventTypes?: boolean;
- comparisonDelta?: number;
- };
- type State = {
- environments: Environment[] | null;
- };
- class RuleConditionsForm extends React.PureComponent<Props, State> {
- state: State = {
- environments: null,
- };
- componentDidMount() {
- this.fetchData();
- }
- async fetchData() {
- const {api, organization, projectSlug} = this.props;
- try {
- const environments = await api.requestPromise(
- `/projects/${organization.slug}/${projectSlug}/environments/`,
- {
- query: {
- visibility: 'visible',
- },
- }
- );
- this.setState({environments});
- } catch (_err) {
- addErrorMessage(t('Unable to fetch environments'));
- }
- }
- get timeWindowOptions() {
- let options: Record<string, string> = TIME_WINDOW_MAP;
- if (isCrashFreeAlert(this.props.dataset)) {
- options = pick(TIME_WINDOW_MAP, [
- // TimeWindow.THIRTY_MINUTES, leaving this option out until we figure out the sub-hour session resolution chart limitations
- TimeWindow.ONE_HOUR,
- TimeWindow.TWO_HOURS,
- TimeWindow.FOUR_HOURS,
- TimeWindow.ONE_DAY,
- ]);
- }
- return Object.entries(options).map(([value, label]) => ({
- value: parseInt(value, 10),
- label,
- }));
- }
- get searchPlaceholder() {
- switch (this.props.dataset) {
- case Dataset.ERRORS:
- return t('Filter events by level, message, and other properties\u2026');
- case Dataset.METRICS:
- case Dataset.SESSIONS:
- return t('Filter sessions by release version\u2026');
- case Dataset.TRANSACTIONS:
- default:
- return t('Filter transactions by URL, tags, and other properties\u2026');
- }
- }
- get searchSupportedTags() {
- if (isCrashFreeAlert(this.props.dataset)) {
- return {
- release: {
- key: 'release',
- name: 'release',
- },
- };
- }
- return undefined;
- }
- renderInterval() {
- const {
- organization,
- disabled,
- alertType,
- hasAlertWizardV3,
- timeWindow,
- comparisonDelta,
- comparisonType,
- onTimeWindowChange,
- onComparisonDeltaChange,
- } = this.props;
- const formElemBaseStyle = {
- padding: `${space(0.5)}`,
- border: 'none',
- };
- const {labelText, timeWindowText} = getFunctionHelpText(alertType);
- const intervalLabelText = hasAlertWizardV3 ? t('Define your metric') : labelText;
- return (
- <Fragment>
- <StyledListItem>
- <StyledListTitle>
- <div>{intervalLabelText}</div>
- <Tooltip
- title={t(
- 'Time window over which the metric is evaluated. Alerts are evaluated every minute regardless of this value.'
- )}
- >
- <IconQuestion size="sm" color="gray200" />
- </Tooltip>
- </StyledListTitle>
- </StyledListItem>
- <FormRow>
- {timeWindowText && (
- <MetricField
- name="aggregate"
- help={null}
- organization={organization}
- disabled={disabled}
- style={{
- ...formElemBaseStyle,
- }}
- inline={false}
- flexibleControlStateSize
- columnWidth={200}
- alertType={alertType}
- required
- />
- )}
- {timeWindowText && <FormRowText>{timeWindowText}</FormRowText>}
- <SelectControl
- name="timeWindow"
- styles={{
- control: (provided: {[x: string]: string | number | boolean}) => ({
- ...provided,
- minWidth: 130,
- maxWidth: 300,
- }),
- }}
- options={this.timeWindowOptions}
- required
- isDisabled={disabled}
- value={timeWindow}
- onChange={({value}) => onTimeWindowChange(value)}
- inline={false}
- flexibleControlStateSize
- />
- {!hasAlertWizardV3 && (
- <Feature
- features={['organizations:change-alerts']}
- organization={organization}
- >
- {comparisonType === AlertRuleComparisonType.CHANGE && (
- <ComparisonContainer>
- {t(' compared to ')}
- <SelectControl
- name="comparisonDelta"
- styles={{
- container: (provided: {
- [x: string]: string | number | boolean;
- }) => ({
- ...provided,
- marginLeft: space(1),
- }),
- control: (provided: {[x: string]: string | number | boolean}) => ({
- ...provided,
- minWidth: 500,
- maxWidth: 1000,
- }),
- }}
- value={comparisonDelta}
- onChange={({value}) => onComparisonDeltaChange(value)}
- options={COMPARISON_DELTA_OPTIONS}
- required={comparisonType === AlertRuleComparisonType.CHANGE}
- />
- </ComparisonContainer>
- )}
- </Feature>
- )}
- </FormRow>
- </Fragment>
- );
- }
- render() {
- const {
- organization,
- disabled,
- onFilterSearch,
- allowChangeEventTypes,
- alertType,
- hasAlertWizardV3,
- dataset,
- } = this.props;
- const {environments} = this.state;
- const environmentOptions: SelectValue<string | null>[] =
- environments?.map((env: Environment) => ({
- value: env.name,
- label: getDisplayName(env),
- })) ?? [];
- environmentOptions.unshift({
- value: null,
- label: t('All'),
- });
- const dataSourceOptions = [
- {
- label: t('Errors'),
- options: [
- {
- value: Datasource.ERROR_DEFAULT,
- label: DATA_SOURCE_LABELS[Datasource.ERROR_DEFAULT],
- },
- {
- value: Datasource.DEFAULT,
- label: DATA_SOURCE_LABELS[Datasource.DEFAULT],
- },
- {
- value: Datasource.ERROR,
- label: DATA_SOURCE_LABELS[Datasource.ERROR],
- },
- ],
- },
- ];
- if (organization.features.includes('performance-view') && alertType === 'custom') {
- dataSourceOptions.push({
- label: t('Transactions'),
- options: [
- {
- value: Datasource.TRANSACTION,
- label: DATA_SOURCE_LABELS[Datasource.TRANSACTION],
- },
- ],
- });
- }
- const transactionTags = [
- 'transaction',
- 'transaction.duration',
- 'transaction.op',
- 'transaction.status',
- ];
- const measurementTags = Object.values({...WebVital, ...MobileVital});
- const eventOmitTags =
- dataset === 'events' ? [...measurementTags, ...transactionTags] : [];
- const formElemBaseStyle = {
- padding: `${space(0.5)}`,
- border: 'none',
- };
- return (
- <React.Fragment>
- <ChartPanel>
- <StyledPanelBody>{this.props.thresholdChart}</StyledPanelBody>
- </ChartPanel>
- {hasAlertWizardV3 && this.renderInterval()}
- <StyledListItem>{t('Filter events')}</StyledListItem>
- <FormRow>
- <SelectField
- name="environment"
- placeholder={t('All')}
- style={{
- ...formElemBaseStyle,
- minWidth: 230,
- flex: 1,
- }}
- styles={{
- singleValue: (base: any) => ({
- ...base,
- }),
- option: (base: any) => ({
- ...base,
- }),
- }}
- options={environmentOptions}
- isDisabled={disabled || this.state.environments === null}
- isClearable
- inline={false}
- flexibleControlStateSize
- inFieldLabel={t('Environment: ')}
- />
- {allowChangeEventTypes && (
- <FormField
- name="datasource"
- inline={false}
- style={{
- ...formElemBaseStyle,
- minWidth: 300,
- flex: 2,
- }}
- flexibleControlStateSize
- >
- {({onChange, onBlur, model}) => {
- const formDataset = model.getValue('dataset');
- const formEventTypes = model.getValue('eventTypes');
- const mappedValue = convertDatasetEventTypesToSource(
- formDataset,
- formEventTypes
- );
- return (
- <SelectControl
- value={mappedValue}
- inFieldLabel={t('Events: ')}
- onChange={optionObj => {
- const optionValue = optionObj.value;
- onChange(optionValue, {});
- onBlur(optionValue, {});
- // Reset the aggregate to the default (which works across
- // datatypes), otherwise we may send snuba an invalid query
- // (transaction aggregate on events datasource = bad).
- optionValue === 'transaction'
- ? model.setValue('aggregate', DEFAULT_TRANSACTION_AGGREGATE)
- : model.setValue('aggregate', DEFAULT_AGGREGATE);
- // set the value of the dataset and event type from data source
- const {dataset: datasetFromDataSource, eventTypes} =
- DATA_SOURCE_TO_SET_AND_EVENT_TYPES[optionValue] ?? {};
- model.setValue('dataset', datasetFromDataSource);
- model.setValue('eventTypes', eventTypes);
- }}
- options={dataSourceOptions}
- isDisabled={disabled}
- />
- );
- }}
- </FormField>
- )}
- <FormField
- name="query"
- inline={false}
- style={{
- ...formElemBaseStyle,
- flex: '6 0 500px',
- }}
- flexibleControlStateSize
- >
- {({onChange, onBlur, onKeyDown, initialData}) => (
- <SearchContainer>
- <StyledSearchBar
- searchSource="alert_builder"
- defaultQuery={initialData?.query ?? ''}
- omitTags={[
- 'event.type',
- 'release.version',
- 'release.stage',
- 'release.package',
- 'release.build',
- 'project',
- ...eventOmitTags,
- ]}
- includeSessionTagsValues={dataset === Dataset.SESSIONS}
- disabled={disabled}
- useFormWrapper={false}
- organization={organization}
- placeholder={this.searchPlaceholder}
- onChange={onChange}
- onKeyDown={e => {
- /**
- * Do not allow enter key to submit the alerts form since it is unlikely
- * users will be ready to create the rule as this sits above required fields.
- */
- if (e.key === 'Enter') {
- e.preventDefault();
- e.stopPropagation();
- }
- onKeyDown?.(e);
- }}
- onBlur={query => {
- onFilterSearch(query);
- onBlur(query);
- }}
- onSearch={query => {
- onFilterSearch(query);
- onChange(query, {});
- }}
- {...(this.searchSupportedTags
- ? {supportedTags: this.searchSupportedTags}
- : {})}
- hasRecentSearches={dataset !== Dataset.SESSIONS}
- />
- </SearchContainer>
- )}
- </FormField>
- </FormRow>
- {!hasAlertWizardV3 && this.renderInterval()}
- </React.Fragment>
- );
- }
- }
- const StyledListTitle = styled('div')`
- display: flex;
- span {
- margin-left: ${space(1)};
- }
- `;
- const ChartPanel = styled(Panel)`
- margin-bottom: ${space(4)};
- `;
- const StyledPanelBody = styled(PanelBody)`
- ol,
- h4 {
- margin-bottom: ${space(1)};
- }
- `;
- const SearchContainer = styled('div')`
- display: flex;
- `;
- const StyledSearchBar = styled(SearchBar)`
- flex-grow: 1;
- `;
- const StyledListItem = styled(ListItem)`
- margin-bottom: ${space(1)};
- font-size: ${p => p.theme.fontSizeExtraLarge};
- line-height: 1.3;
- `;
- const FormRow = styled('div')`
- display: flex;
- flex-direction: row;
- align-items: center;
- flex-wrap: wrap;
- margin-bottom: ${space(4)};
- `;
- const FormRowText = styled('div')`
- margin: ${space(1)};
- `;
- const ComparisonContainer = styled('div')`
- margin-left: ${space(1)};
- display: flex;
- flex-direction: row;
- align-items: center;
- `;
- export default RuleConditionsForm;
|