index.tsx 5.4 KB

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