form.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. import {Component, Fragment, PureComponent} from 'react';
  2. import styled from '@emotion/styled';
  3. import {fetchOrgMembers} from 'sentry/actionCreators/members';
  4. import type {Client} from 'sentry/api';
  5. import FieldGroup from 'sentry/components/forms/fieldGroup';
  6. import {IconDiamond} from 'sentry/icons';
  7. import {t} from 'sentry/locale';
  8. import {space} from 'sentry/styles/space';
  9. import type {Organization} from 'sentry/types/organization';
  10. import type {Project} from 'sentry/types/project';
  11. import type {Config} from 'sentry/types/system';
  12. import withApi from 'sentry/utils/withApi';
  13. import withConfig from 'sentry/utils/withConfig';
  14. import {getThresholdUnits} from 'sentry/views/alerts/rules/metric/constants';
  15. import ThresholdControl from 'sentry/views/alerts/rules/metric/triggers/thresholdControl';
  16. import {isSessionAggregate} from '../../../utils';
  17. import type {
  18. AlertRuleThresholdType,
  19. ThresholdControlValue,
  20. Trigger,
  21. UnsavedMetricRule,
  22. UnsavedTrigger,
  23. } from '../types';
  24. import {AlertRuleComparisonType, AlertRuleTriggerType} from '../types';
  25. type Props = {
  26. aggregate: UnsavedMetricRule['aggregate'];
  27. api: Client;
  28. comparisonType: AlertRuleComparisonType;
  29. config: Config;
  30. disabled: boolean;
  31. fieldHelp: React.ReactNode;
  32. isCritical: boolean;
  33. onChange: (trigger: Trigger, changeObj: Partial<Trigger>) => void;
  34. onThresholdPeriodChange: (value: number) => void;
  35. onThresholdTypeChange: (thresholdType: AlertRuleThresholdType) => void;
  36. organization: Organization;
  37. placeholder: string;
  38. projects: Project[];
  39. resolveThreshold: UnsavedMetricRule['resolveThreshold'];
  40. thresholdPeriod: UnsavedMetricRule['thresholdPeriod'];
  41. thresholdType: UnsavedMetricRule['thresholdType'];
  42. trigger: Trigger;
  43. triggerIndex: number;
  44. triggerLabel: React.ReactNode;
  45. /**
  46. * Map of fieldName -> errorMessage
  47. */
  48. error?: {[fieldName: string]: string};
  49. hideControl?: boolean;
  50. };
  51. class TriggerFormItem extends PureComponent<Props> {
  52. /**
  53. * Handler for threshold changes coming from slider or chart.
  54. * Needs to sync state with the form.
  55. */
  56. handleChangeThreshold = (value: ThresholdControlValue) => {
  57. const {onChange, trigger} = this.props;
  58. onChange(
  59. {
  60. ...trigger,
  61. alertThreshold: value.threshold,
  62. },
  63. {alertThreshold: value.threshold}
  64. );
  65. };
  66. render() {
  67. const {
  68. disabled,
  69. error,
  70. trigger,
  71. isCritical,
  72. thresholdType,
  73. thresholdPeriod,
  74. hideControl,
  75. comparisonType,
  76. fieldHelp,
  77. triggerLabel,
  78. placeholder,
  79. onThresholdTypeChange,
  80. onThresholdPeriodChange,
  81. } = this.props;
  82. return (
  83. <StyledField
  84. label={triggerLabel}
  85. help={fieldHelp}
  86. required={isCritical}
  87. error={error?.alertThreshold}
  88. >
  89. <ThresholdControl
  90. disabled={disabled}
  91. disableThresholdType={!isCritical}
  92. type={trigger.label}
  93. thresholdType={thresholdType}
  94. thresholdPeriod={thresholdPeriod}
  95. hideControl={hideControl}
  96. threshold={trigger.alertThreshold}
  97. comparisonType={comparisonType}
  98. placeholder={placeholder}
  99. onChange={this.handleChangeThreshold}
  100. onThresholdTypeChange={onThresholdTypeChange}
  101. onThresholdPeriodChange={onThresholdPeriodChange}
  102. />
  103. </StyledField>
  104. );
  105. }
  106. }
  107. type TriggerFormContainerProps = Omit<
  108. React.ComponentProps<typeof TriggerFormItem>,
  109. | 'onChange'
  110. | 'isCritical'
  111. | 'error'
  112. | 'triggerIndex'
  113. | 'trigger'
  114. | 'fieldHelp'
  115. | 'triggerHelp'
  116. | 'triggerLabel'
  117. | 'placeholder'
  118. > & {
  119. onChange: (triggerIndex: number, trigger: Trigger, changeObj: Partial<Trigger>) => void;
  120. onResolveThresholdChange: (
  121. resolveThreshold: UnsavedMetricRule['resolveThreshold']
  122. ) => void;
  123. triggers: Trigger[];
  124. errors?: Map<number, {[fieldName: string]: string}>;
  125. };
  126. class TriggerFormContainer extends Component<TriggerFormContainerProps> {
  127. componentDidMount() {
  128. const {api, organization} = this.props;
  129. fetchOrgMembers(api, organization.slug);
  130. }
  131. handleChangeTrigger =
  132. (triggerIndex: number) => (trigger: Trigger, changeObj: Partial<Trigger>) => {
  133. const {onChange} = this.props;
  134. onChange(triggerIndex, trigger, changeObj);
  135. };
  136. handleChangeResolveTrigger = (trigger: Trigger, _: Partial<Trigger>) => {
  137. const {onResolveThresholdChange} = this.props;
  138. onResolveThresholdChange(trigger.alertThreshold);
  139. };
  140. getCriticalThresholdPlaceholder(
  141. aggregate: string,
  142. comparisonType: AlertRuleComparisonType
  143. ) {
  144. if (aggregate.includes('failure_rate')) {
  145. return '0.05';
  146. }
  147. if (isSessionAggregate(aggregate)) {
  148. return '97';
  149. }
  150. if (comparisonType === AlertRuleComparisonType.CHANGE) {
  151. return '100';
  152. }
  153. return '300';
  154. }
  155. getIndicator(type: AlertRuleTriggerType) {
  156. if (type === AlertRuleTriggerType.CRITICAL) {
  157. return <StyledIconDiamond color="errorText" size="sm" />;
  158. }
  159. if (type === AlertRuleTriggerType.WARNING) {
  160. return <StyledIconDiamond color="warningText" size="sm" />;
  161. }
  162. return <StyledIconDiamond color="successText" size="sm" />;
  163. }
  164. render() {
  165. const {
  166. api,
  167. config,
  168. disabled,
  169. errors,
  170. organization,
  171. triggers,
  172. thresholdType,
  173. thresholdPeriod,
  174. comparisonType,
  175. aggregate,
  176. resolveThreshold,
  177. projects,
  178. onThresholdTypeChange,
  179. onThresholdPeriodChange,
  180. } = this.props;
  181. const resolveTrigger: UnsavedTrigger = {
  182. label: AlertRuleTriggerType.RESOLVE,
  183. alertThreshold: resolveThreshold,
  184. actions: [],
  185. };
  186. const thresholdUnits = getThresholdUnits(aggregate, comparisonType);
  187. return (
  188. <Fragment>
  189. {triggers.map((trigger, index) => {
  190. const isCritical = index === 0;
  191. // eslint-disable-next-line no-use-before-define
  192. return (
  193. <TriggerFormItem
  194. key={index}
  195. api={api}
  196. config={config}
  197. disabled={disabled}
  198. error={errors?.get(index)}
  199. trigger={trigger}
  200. thresholdPeriod={thresholdPeriod}
  201. thresholdType={thresholdType}
  202. comparisonType={comparisonType}
  203. aggregate={aggregate}
  204. resolveThreshold={resolveThreshold}
  205. organization={organization}
  206. projects={projects}
  207. triggerIndex={index}
  208. isCritical={isCritical}
  209. fieldHelp={null}
  210. triggerLabel={
  211. <TriggerLabel>
  212. {this.getIndicator(
  213. isCritical
  214. ? AlertRuleTriggerType.CRITICAL
  215. : AlertRuleTriggerType.WARNING
  216. )}
  217. {isCritical ? t('Critical') : t('Warning')}
  218. </TriggerLabel>
  219. }
  220. placeholder={
  221. isCritical
  222. ? `${this.getCriticalThresholdPlaceholder(aggregate, comparisonType)}${
  223. comparisonType === AlertRuleComparisonType.COUNT
  224. ? thresholdUnits
  225. : ''
  226. }`
  227. : t('None')
  228. }
  229. onChange={this.handleChangeTrigger(index)}
  230. onThresholdTypeChange={onThresholdTypeChange}
  231. onThresholdPeriodChange={onThresholdPeriodChange}
  232. />
  233. );
  234. })}
  235. <TriggerFormItem
  236. api={api}
  237. config={config}
  238. disabled={disabled}
  239. error={errors?.get(2)}
  240. trigger={resolveTrigger}
  241. // Flip rule thresholdType to opposite
  242. thresholdPeriod={thresholdPeriod}
  243. thresholdType={+!thresholdType}
  244. comparisonType={comparisonType}
  245. aggregate={aggregate}
  246. resolveThreshold={resolveThreshold}
  247. organization={organization}
  248. projects={projects}
  249. triggerIndex={2}
  250. isCritical={false}
  251. fieldHelp={null}
  252. triggerLabel={
  253. <TriggerLabel>
  254. {this.getIndicator(AlertRuleTriggerType.RESOLVE)}
  255. {t('Resolved')}
  256. </TriggerLabel>
  257. }
  258. placeholder={t('Automatic')}
  259. onChange={this.handleChangeResolveTrigger}
  260. onThresholdTypeChange={onThresholdTypeChange}
  261. onThresholdPeriodChange={onThresholdPeriodChange}
  262. />
  263. </Fragment>
  264. );
  265. }
  266. }
  267. const TriggerLabel = styled('div')`
  268. display: flex;
  269. flex-direction: row;
  270. align-items: center;
  271. `;
  272. const StyledIconDiamond = styled(IconDiamond)`
  273. margin-right: ${space(0.75)};
  274. `;
  275. const StyledField = styled(FieldGroup)`
  276. & > label > div:first-child > span {
  277. display: flex;
  278. flex-direction: row;
  279. }
  280. `;
  281. export default withConfig(withApi(TriggerFormContainer));