diff --git a/agent/app/service/alert.go b/agent/app/service/alert.go index 301070b50..e9fff561a 100644 --- a/agent/app/service/alert.go +++ b/agent/app/service/alert.go @@ -483,23 +483,51 @@ func (a AlertService) TestAlertConfig(req dto.AlertConfigTest) (bool, error) { func (a AlertService) ExternalUpdateAlert(updateAlert dto.AlertCreate) error { upMap := make(map[string]interface{}) + var newStatus string if updateAlert.SendCount == 0 { - upMap["status"] = constant.AlertDisable + newStatus = constant.AlertDisable } else { - upMap["status"] = constant.AlertEnable + newStatus = constant.AlertEnable upMap["send_count"] = updateAlert.SendCount + if updateAlert.Method != "" { + upMap["method"] = updateAlert.Method + } } - upMap["method"] = updateAlert.Method - alertInfo, _ := alertRepo.Get(alertRepo.WithByType(updateAlert.Type), alertRepo.WithByProject(updateAlert.Project)) + upMap["status"] = newStatus + + alertInfo, _ := alertRepo.Get( + alertRepo.WithByType(updateAlert.Type), + alertRepo.WithByProject(updateAlert.Project), + ) + if alertInfo.ID > 0 { - if err := alertRepo.Update(upMap, alertRepo.WithByProject(updateAlert.Project), alertRepo.WithByType(updateAlert.Type)); err != nil { - return err + shouldUpdate := false + + if alertInfo.Status != newStatus { + shouldUpdate = true + } + if val, ok := upMap["send_count"]; ok && val != alertInfo.SendCount { + shouldUpdate = true + } + if val, ok := upMap["method"]; ok && val != "" && val != alertInfo.Method { + shouldUpdate = true + } + + if shouldUpdate { + if err := alertRepo.Update( + upMap, + alertRepo.WithByProject(updateAlert.Project), + alertRepo.WithByType(updateAlert.Type), + ); err != nil { + return err + } } } else { - updateAlert.Status = constant.AlertEnable - err := a.CreateAlert(updateAlert) - if err != nil { - return err + if updateAlert.Method != "" && updateAlert.Title != "" { + updateAlert.Status = newStatus + if err := a.CreateAlert(updateAlert); err != nil { + return err + } } } diff --git a/agent/app/service/clam.go b/agent/app/service/clam.go index 6c7422699..62c9b1edd 100644 --- a/agent/app/service/clam.go +++ b/agent/app/service/clam.go @@ -192,7 +192,7 @@ func (c *ClamService) Create(req dto.ClamCreate) error { if err := clamRepo.Create(&clam); err != nil { return err } - if req.AlertCount != 0 { + if req.AlertCount != 0 && req.AlertTitle != "" && req.AlertMethod != "" { createAlert := dto.AlertCreate{ Title: req.AlertTitle, SendCount: req.AlertCount, diff --git a/agent/app/service/cronjob.go b/agent/app/service/cronjob.go index e65cde52b..d4f287799 100644 --- a/agent/app/service/cronjob.go +++ b/agent/app/service/cronjob.go @@ -381,7 +381,7 @@ func (u *CronjobService) Import(req []dto.CronjobTrans) error { cronjob.Status = constant.StatusDisable } _ = cronjobRepo.Create(&cronjob) - if item.AlertCount != 0 { + if item.AlertCount != 0 && item.AlertTitle != "" && item.AlertMethod != "" { createAlert := dto.AlertCreate{ Title: item.AlertTitle, SendCount: item.AlertCount, @@ -579,7 +579,7 @@ func (u *CronjobService) Create(req dto.CronjobOperate) error { if err := cronjobRepo.Create(&cronjob); err != nil { return err } - if req.AlertCount != 0 { + if req.AlertCount != 0 && req.AlertTitle != "" && req.AlertMethod != "" { createAlert := dto.AlertCreate{ Title: req.AlertTitle, SendCount: req.AlertCount,