form.tsx 8.6 KB

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