Commit fecb1d1c authored by 李科's avatar 李科

perf: 完善预警规则设置删除逻辑

parent e6169241
......@@ -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:重大风险
......
......@@ -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
......
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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment