| 
									
										
										
										
											2024-03-27 01:36:30 +08:00
										 |  |  | import { Menu } from '@grafana/ui'; | 
					
						
							| 
									
										
										
										
											2024-05-14 16:01:08 +08:00
										 |  |  | import { useAppNotification } from 'app/core/copy/appNotification'; | 
					
						
							| 
									
										
										
										
											2024-06-24 21:04:43 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   isGrafanaRulerRule, | 
					
						
							|  |  |  |   isGrafanaRulerRulePaused, | 
					
						
							|  |  |  |   getRuleGroupLocationFromCombinedRule, | 
					
						
							|  |  |  | } from 'app/features/alerting/unified/utils/rules'; | 
					
						
							| 
									
										
										
										
											2024-03-27 01:36:30 +08:00
										 |  |  | import { CombinedRule } from 'app/types/unified-alerting'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-24 21:04:43 +08:00
										 |  |  | import { usePauseRuleInGroup } from '../hooks/useProduceNewRuleGroup'; | 
					
						
							|  |  |  | import { stringifyErrorLike } from '../utils/misc'; | 
					
						
							| 
									
										
										
										
											2024-05-14 16:01:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-27 01:36:30 +08:00
										 |  |  | interface Props { | 
					
						
							|  |  |  |   rule: CombinedRule; | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Method invoked after the request to change the paused state has completed | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2024-03-28 23:20:19 +08:00
										 |  |  |   onPauseChange?: () => void; | 
					
						
							| 
									
										
										
										
											2024-03-27 01:36:30 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Menu item to display correct text for pausing/resuming an alert, | 
					
						
							|  |  |  |  * and triggering API call to do so | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const MenuItemPauseRule = ({ rule, onPauseChange }: Props) => { | 
					
						
							| 
									
										
										
										
											2024-05-14 16:01:08 +08:00
										 |  |  |   const notifyApp = useAppNotification(); | 
					
						
							| 
									
										
										
										
											2024-06-24 21:04:43 +08:00
										 |  |  |   const [pauseRule, updateState] = usePauseRuleInGroup(); | 
					
						
							| 
									
										
										
										
											2024-05-14 16:01:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-27 01:36:30 +08:00
										 |  |  |   const isPaused = isGrafanaRulerRule(rule.rulerRule) && isGrafanaRulerRulePaused(rule.rulerRule); | 
					
						
							|  |  |  |   const icon = isPaused ? 'play' : 'pause'; | 
					
						
							| 
									
										
										
										
											2024-03-28 23:20:40 +08:00
										 |  |  |   const title = isPaused ? 'Resume evaluation' : 'Pause evaluation'; | 
					
						
							| 
									
										
										
										
											2024-03-27 01:36:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Triggers API call to update the current rule to the new `is_paused` state | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   const setRulePause = async (newIsPaused: boolean) => { | 
					
						
							|  |  |  |     if (!isGrafanaRulerRule(rule.rulerRule)) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-05-14 16:01:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-24 21:04:43 +08:00
										 |  |  |     try { | 
					
						
							|  |  |  |       const ruleGroupId = getRuleGroupLocationFromCombinedRule(rule); | 
					
						
							|  |  |  |       const ruleUID = rule.rulerRule.grafana_alert.uid; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       await pauseRule(ruleGroupId, ruleUID, newIsPaused); | 
					
						
							|  |  |  |     } catch (error) { | 
					
						
							|  |  |  |       notifyApp.error(`Failed to ${newIsPaused ? 'pause' : 'resume'} the rule: ${stringifyErrorLike(error)}`); | 
					
						
							| 
									
										
										
										
											2024-05-14 16:01:08 +08:00
										 |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-03-27 01:36:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     onPauseChange?.(); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							|  |  |  |     <Menu.Item | 
					
						
							|  |  |  |       label={title} | 
					
						
							|  |  |  |       icon={icon} | 
					
						
							| 
									
										
										
										
											2024-06-24 21:04:43 +08:00
										 |  |  |       disabled={updateState.isLoading} | 
					
						
							| 
									
										
										
										
											2024-03-27 01:36:30 +08:00
										 |  |  |       onClick={() => { | 
					
						
							|  |  |  |         setRulePause(!isPaused); | 
					
						
							|  |  |  |       }} | 
					
						
							|  |  |  |     /> | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default MenuItemPauseRule; |