index.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import {Component, Fragment} from 'react';
  2. import Panel from 'sentry/components/panels/panel';
  3. import PanelBody from 'sentry/components/panels/panelBody';
  4. import type {Organization} from 'sentry/types/organization';
  5. import type {Project} from 'sentry/types/project';
  6. import removeAtArrayIndex from 'sentry/utils/array/removeAtArrayIndex';
  7. import replaceAtArrayIndex from 'sentry/utils/array/replaceAtArrayIndex';
  8. import ActionsPanel from 'sentry/views/alerts/rules/metric/triggers/actionsPanel';
  9. import AnomalyDetectionFormField from 'sentry/views/alerts/rules/metric/triggers/anomalyAlertsForm';
  10. import DynamicAlertsFeedbackButton from 'sentry/views/alerts/rules/metric/triggers/dynamicAlertsFeedbackButton';
  11. import TriggerForm from 'sentry/views/alerts/rules/metric/triggers/form';
  12. import {
  13. type Action,
  14. AlertRuleComparisonType,
  15. type AlertRuleSensitivity,
  16. type AlertRuleThresholdType,
  17. type MetricActionTemplate,
  18. type Trigger,
  19. type UnsavedMetricRule,
  20. } from '../types';
  21. type Props = {
  22. aggregate: UnsavedMetricRule['aggregate'];
  23. availableActions: MetricActionTemplate[] | null;
  24. comparisonType: AlertRuleComparisonType;
  25. currentProject: string;
  26. disabled: boolean;
  27. errors: Map<number, {[fieldName: string]: string}>;
  28. onChange: (
  29. triggers: Trigger[],
  30. triggerIndex?: number,
  31. changeObj?: Partial<Trigger>
  32. ) => void;
  33. onResolveThresholdChange: (
  34. resolveThreshold: UnsavedMetricRule['resolveThreshold']
  35. ) => void;
  36. onSensitivityChange: (sensitivity: AlertRuleSensitivity) => void;
  37. onThresholdPeriodChange: (value: number) => void;
  38. onThresholdTypeChange: (thresholdType: AlertRuleThresholdType) => void;
  39. organization: Organization;
  40. projects: Project[];
  41. resolveThreshold: UnsavedMetricRule['resolveThreshold'];
  42. sensitivity: UnsavedMetricRule['sensitivity'];
  43. thresholdPeriod: UnsavedMetricRule['thresholdPeriod'];
  44. thresholdType: UnsavedMetricRule['thresholdType'];
  45. triggers: Trigger[];
  46. isMigration?: boolean;
  47. };
  48. /**
  49. * A list of forms to add, edit, and delete triggers.
  50. */
  51. class Triggers extends Component<Props> {
  52. handleDeleteTrigger = (index: number) => {
  53. const {triggers, onChange} = this.props;
  54. const updatedTriggers = removeAtArrayIndex(triggers, index);
  55. onChange(updatedTriggers);
  56. };
  57. handleChangeTrigger = (
  58. triggerIndex: number,
  59. trigger: Trigger,
  60. changeObj: Partial<Trigger>
  61. ) => {
  62. const {triggers, onChange} = this.props;
  63. const updatedTriggers = replaceAtArrayIndex(triggers, triggerIndex, trigger);
  64. onChange(updatedTriggers, triggerIndex, changeObj);
  65. };
  66. handleAddAction = (triggerIndex: number, action: Action) => {
  67. const {onChange, triggers} = this.props;
  68. const trigger = triggers[triggerIndex];
  69. const actions = [...trigger.actions, action];
  70. const updatedTriggers = replaceAtArrayIndex(triggers, triggerIndex, {
  71. ...trigger,
  72. actions,
  73. });
  74. onChange(updatedTriggers, triggerIndex, {actions});
  75. };
  76. handleChangeActions = (
  77. triggerIndex: number,
  78. triggers: Trigger[],
  79. actions: Action[]
  80. ): void => {
  81. const {onChange} = this.props;
  82. const trigger = triggers[triggerIndex];
  83. const updatedTriggers = replaceAtArrayIndex(triggers, triggerIndex, {
  84. ...trigger,
  85. actions,
  86. });
  87. onChange(updatedTriggers, triggerIndex, {actions});
  88. };
  89. render() {
  90. const {
  91. availableActions,
  92. currentProject,
  93. errors,
  94. organization,
  95. projects,
  96. triggers,
  97. disabled,
  98. aggregate,
  99. thresholdType,
  100. thresholdPeriod,
  101. comparisonType,
  102. resolveThreshold,
  103. isMigration,
  104. onSensitivityChange,
  105. onThresholdTypeChange,
  106. onResolveThresholdChange,
  107. onThresholdPeriodChange,
  108. sensitivity,
  109. } = this.props;
  110. // Note we only support 2 triggers max
  111. return (
  112. <Fragment>
  113. <Panel>
  114. <PanelBody>
  115. {comparisonType === AlertRuleComparisonType.DYNAMIC ? (
  116. <AnomalyDetectionFormField
  117. disabled={disabled}
  118. sensitivity={sensitivity}
  119. onSensitivityChange={onSensitivityChange}
  120. thresholdType={thresholdType}
  121. onThresholdTypeChange={onThresholdTypeChange}
  122. />
  123. ) : (
  124. <TriggerForm
  125. disabled={disabled}
  126. errors={errors}
  127. organization={organization}
  128. projects={projects}
  129. triggers={triggers}
  130. aggregate={aggregate}
  131. resolveThreshold={resolveThreshold}
  132. thresholdType={thresholdType}
  133. thresholdPeriod={thresholdPeriod}
  134. comparisonType={comparisonType}
  135. onChange={this.handleChangeTrigger}
  136. onThresholdTypeChange={onThresholdTypeChange}
  137. onResolveThresholdChange={onResolveThresholdChange}
  138. onThresholdPeriodChange={onThresholdPeriodChange}
  139. />
  140. )}
  141. </PanelBody>
  142. </Panel>
  143. {comparisonType === AlertRuleComparisonType.DYNAMIC && (
  144. <DynamicAlertsFeedbackButton />
  145. )}
  146. {isMigration ? null : (
  147. <ActionsPanel
  148. disabled={disabled}
  149. loading={availableActions === null}
  150. error={false}
  151. availableActions={availableActions}
  152. currentProject={currentProject}
  153. organization={organization}
  154. projects={projects}
  155. triggers={triggers}
  156. onChange={this.handleChangeActions}
  157. onAdd={this.handleAddAction}
  158. comparisonType={comparisonType}
  159. />
  160. )}
  161. </Fragment>
  162. );
  163. }
  164. }
  165. export default Triggers;