diff --git a/src/bean/vo/request/alert.go b/src/bean/vo/request/alert.go index 8555bd35cb05cc2042e62c2d121f5ee70cb95088..cc23a28dfa8cf8fb6fab55606925f3eee53f58e5 100644 --- a/src/bean/vo/request/alert.go +++ b/src/bean/vo/request/alert.go @@ -19,9 +19,13 @@ type ListAlert struct { Pagination } +type ExistAlert struct { + Id int `json:"id" form:"id"` + AlertRulesId string `json:"alert_rules_id" form:"'alert_rules_id'"` // 告警规则id +} + type UpdateAlert struct { - Id int `json:"id" form:"id" binding:"required"` - //Ids []int `json:"ids" form:"ids" binding:"required_without=Id"` // 预警ids + Id int `json:"id" form:"id" binding:"required"` CloseRemark string `json:"close_remark" form:"close_remark"` // 关闭备注 DeferPush int `json:"defer_push" form:"defer_push" binding:"omitempty,oneof=0 1"` // 延迟三天推送: 0:否 1:是 三天内将不再自动推送该告警信息给处置人员,可手动推送,但告警数据依然会出现 RiskLevel int `json:"risk_level" form:"risk_level" binding:"omitempty,oneof=1 2 3 4"` // 风险等级,1:低风险,2:一般风险,3:较大风险,4:重大风险 diff --git a/src/service/alert.go b/src/service/alert.go index ab2ec2dfd3898b80b1a1414ac1816bc9a72710b8..e725a4a7be52f17ba4d49d070bd2f8d8d3b80853 100644 --- a/src/service/alert.go +++ b/src/service/alert.go @@ -437,6 +437,62 @@ func (a *AlertSvc) IndexSearch(req request.ListAlert) (resp response.AlertList, return } +func (a *AlertSvc) IndexDocExist(req request.ExistAlert) (exist bool, err error) { + var ( + sources response.OpenSearchSource + ) + + cli, err := client.GetOpenSearch() + if err != nil { + return + } + + boolQuery := elastic.NewBoolQuery() + if req.Id != 0 { + boolQuery.Must(elastic.NewTermQuery("id", req.Id)) + } + if req.AlertRulesId != "" { + boolQuery.Must(elastic.NewTermQuery("alert_rules_id", req.AlertRulesId)) + } + + querySource, _ := boolQuery.Source() + b, _ := json.Marshal(querySource) + + content := strings.NewReader(fmt.Sprintf(`{ + "query": %s, + "from": %d, + "size": %d}`, string(b), 0, 1)) + + res := opensearchapi.SearchRequest{ + Index: []string{OpenSearchIndex}, + Body: content, + Sort: []string{"id"}, + } + do, err := res.Do(context.Background(), cli) + if err != nil { + return + } + defer do.Body.Close() + if do.StatusCode != http.StatusOK { + err = errors.New(do.String()) + return + } + body, err := io.ReadAll(do.Body) + if err != nil { + return + } + + err = json.Unmarshal(body, &sources) + if err != nil { + return + } + + if sources.Hits.Total.Value == 1 { + exist = true + } + return +} + func (a *AlertSvc) IndexUpdate(req request.UpdateAlert) (err error) { var ( sources response.OpenSearchSource diff --git a/src/service/alert_rules.go b/src/service/alert_rules.go index 2ce25d3afc74c0364feba8ad0e5511c864ebba59..2fa275b5f019bac00ea2ca385a8c7e77e4eb3866 100644 --- a/src/service/alert_rules.go +++ b/src/service/alert_rules.go @@ -1,6 +1,7 @@ package service import ( + "errors" "github.com/google/uuid" "github.com/jinzhu/copier" json "github.com/json-iterator/go" @@ -260,11 +261,22 @@ func (a *AlertRulesSvc) List(req request.ListAlertRules) (resp response.AlertRul } func (a *AlertRulesSvc) Delete(ids []string) (err error) { + var exist bool db, err := client.GetDbClient() if err != nil { return } - // TODO 批量删除表中的多出数据 - _, err = db.NewSession().In("id", ids).Delete(new(entity.AlertRules)) + alertSvc := AlertSvc{User: a.User} + for _, id := range ids { + exist, err = alertSvc.IndexDocExist(request.ExistAlert{AlertRulesId: id}) + if err != nil { + return + } + if !exist { + _, err = db.NewSession().Where("id = ?", id).Delete(new(entity.AlertRules)) + } else { + return errors.New("alert_rules_id already exists in opensearch") + } + } return }