form.tsx 8.6 KB

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