anomalyAlertsForm.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import {Component, Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import SelectControl from 'sentry/components/forms/controls/selectControl';
  4. import FieldGroup from 'sentry/components/forms/fieldGroup';
  5. import {
  6. AlertRuleSensitivity,
  7. AlertRuleThresholdType,
  8. type UnsavedMetricRule,
  9. } from 'sentry/views/alerts/rules/metric/types';
  10. type Props = {
  11. disabled: boolean;
  12. onSensitivityChange: (sensitivity: AlertRuleSensitivity) => void;
  13. onThresholdTypeChange: (thresholdType: AlertRuleThresholdType) => void;
  14. sensitivity: UnsavedMetricRule['sensitivity'];
  15. thresholdType: UnsavedMetricRule['thresholdType'];
  16. /**
  17. * Map of fieldName -> errorMessage
  18. */
  19. error?: {[fieldName: string]: string};
  20. hideControl?: boolean;
  21. };
  22. type SensitivityFormItemProps = {
  23. onSensitivityChange: (sensitivity: AlertRuleSensitivity) => void;
  24. sensitivity: UnsavedMetricRule['sensitivity'];
  25. };
  26. type DirectionFormItemProps = {
  27. onThresholdTypeChange: (thresholdType: AlertRuleThresholdType) => void;
  28. thresholdType: UnsavedMetricRule['thresholdType'];
  29. };
  30. function SensitivityFormItem({
  31. sensitivity,
  32. onSensitivityChange,
  33. }: SensitivityFormItemProps) {
  34. return (
  35. <StyledField
  36. label={'Level of responsiveness'}
  37. id={'sensitivity'}
  38. help={
  39. 'Choose your level of anomaly responsiveness. Higher thresholds means alerts for most anomalies. Lower thresholds means alerts only for larger ones.'
  40. }
  41. required
  42. >
  43. <SelectContainer>
  44. <SelectControl
  45. name="sensitivity"
  46. inputId={'sensitivity'}
  47. value={sensitivity}
  48. options={[
  49. {
  50. value: AlertRuleSensitivity.LOW,
  51. label: 'Low (alert less often)',
  52. },
  53. {
  54. value: AlertRuleSensitivity.MEDIUM,
  55. label: 'Medium',
  56. },
  57. {
  58. value: AlertRuleSensitivity.HIGH,
  59. label: 'High (alert more often)',
  60. },
  61. ]}
  62. onChange={({value}) => {
  63. onSensitivityChange(value);
  64. }}
  65. />
  66. </SelectContainer>
  67. </StyledField>
  68. );
  69. }
  70. function DirectionFormItem({
  71. thresholdType,
  72. onThresholdTypeChange,
  73. }: DirectionFormItemProps) {
  74. return (
  75. <StyledField
  76. label={'Direction of anomaly movement'}
  77. help={
  78. 'Decide if you want to be alerted to anomalies that are moving above, below, or in both directions in relation to your threshold.'
  79. }
  80. required
  81. >
  82. <SelectContainer>
  83. <SelectControl
  84. name="sensitivity"
  85. value={thresholdType}
  86. options={[
  87. {
  88. value: AlertRuleThresholdType.ABOVE_AND_BELOW,
  89. label: 'Above and below bounds',
  90. },
  91. {
  92. value: AlertRuleThresholdType.ABOVE,
  93. label: 'Above bounds only',
  94. },
  95. {
  96. value: AlertRuleThresholdType.BELOW,
  97. label: 'Below bounds only',
  98. },
  99. ]}
  100. onChange={({value}) => {
  101. onThresholdTypeChange(value);
  102. }}
  103. />
  104. </SelectContainer>
  105. </StyledField>
  106. );
  107. }
  108. class AnomalyDetectionFormField extends Component<Props> {
  109. render() {
  110. const {sensitivity, onSensitivityChange, thresholdType, onThresholdTypeChange} =
  111. this.props;
  112. return (
  113. <Fragment>
  114. <SensitivityFormItem
  115. sensitivity={sensitivity}
  116. onSensitivityChange={onSensitivityChange}
  117. />
  118. <DirectionFormItem
  119. thresholdType={thresholdType}
  120. onThresholdTypeChange={onThresholdTypeChange}
  121. />
  122. </Fragment>
  123. );
  124. }
  125. }
  126. const StyledField = styled(FieldGroup)`
  127. & > label > div:first-child > span {
  128. display: flex;
  129. flex-direction: row;
  130. }
  131. `;
  132. const SelectContainer = styled('div')`
  133. flex: 1;
  134. `;
  135. export default AnomalyDetectionFormField;