From 5ce39beebd4dacdea658ca4a71e002ee911f6bde Mon Sep 17 00:00:00 2001 From: like Date: Wed, 5 Jul 2023 09:42:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AE=8C=E5=96=84=E9=A2=84=E8=AD=A6?= =?UTF-8?q?=E8=A7=84=E5=88=99=E8=AE=BE=E7=BD=AE=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bean/entity/alert_rules.go | 2 +- src/bean/vo/request/alert_rules.go | 29 +++++++++--------------- src/bean/vo/response/alert_rules.go | 12 ++++++---- src/service/alert_rules.go | 34 ++++++++++++++++++++--------- 4 files changed, 43 insertions(+), 34 deletions(-) diff --git a/src/bean/entity/alert_rules.go b/src/bean/entity/alert_rules.go index a1f8e71..8087ad7 100644 --- a/src/bean/entity/alert_rules.go +++ b/src/bean/entity/alert_rules.go @@ -32,7 +32,7 @@ func (m *AlertRules) TableName() string { type RulesAlertRange struct { Name string `json:"name" form:"name" binding:"required"` Value string `json:"value" form:"value" binding:"required"` - Compare string `json:"compare" form:"compare" binding:"required"` + Compare string `json:"compare" form:"compare" binding:"required,oneof== != =~ !~"` } type AlertCondition struct { diff --git a/src/bean/vo/request/alert_rules.go b/src/bean/vo/request/alert_rules.go index 83c9029..e0956a5 100644 --- a/src/bean/vo/request/alert_rules.go +++ b/src/bean/vo/request/alert_rules.go @@ -1,6 +1,8 @@ package request -import "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" +import ( + "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" +) type AddAlertRules struct { MetricName string `json:"metric_name" form:"metric_name" binding:"required"` // 预警规则名称(指标名称) @@ -55,23 +57,12 @@ type DetailAlertRules struct { } type ListAlertRules struct { - Id string `json:"id" form:"id"` - MetricName string `json:"metric_name" form:"metric_name"` // 预警规则名称(指标名称) - DetectionType int `json:"detection_type" form:"detection_type"` // 检测类型 1:静态阈值 2:自定义 - ClassId int `json:"class_id" form:"class_id"` // 预警对象id(级联:预警分类/预警对象) - ClassParentName string `json:"class_parent_name" form:"class_parent_name"` // 预警分类名称 - ClassName string `json:"class_name" form:"class_name"` // 预警对象名称 - MetricConfigName string `json:"metric_config_name" form:"metric_config_name"` // 预警指标名称(detection_type=2时有值) - Expr string `json:"expr" form:"expr"` // 指标表达式(PromQL语句) - AlertCondition string `json:"alert_condition" form:"alert_condition"` // 预警规则 字典值 - AlertRuleType string `json:"alert_rule_type" form:"alert_rule_type"` // 预警规则类型 关联字典表 - AlertRange string `json:"alert_range" form:"alert_range"` // 预警范围 字典值 - Duration int `json:"duration" form:"duration"` // 持续时间 - DurationUnit string `json:"duration_unit" form:"duration_unit"` // 持续时间单位 s m h - CheckPeriod int `json:"check_period" form:"check_period"` // 检查周期 单位:分钟 - NotifyMethod string `json:"notify_method" form:"notify_method"` // 预警通知方式 all dingtalk sms - NotifyPushCount int `json:"notify_push_count" form:"notify_push_count"` // 消息推送次数 - NotifyPushFrequency int `json:"notify_push_frequency" form:"notify_push_frequency"` // 消息推送频率 分钟 - IsEnabled int `json:"is_enabled" form:"is_enabled"` // 是否开启 1:是 2:否 + // 请输入预警规则名称/预警对象/预警分类/预警指标 + Id string `json:"id" form:"id"` + NotifyMethod string `json:"notify_method" form:"notify_method" binding:"omitempty,oneof=all dingtalk sms"` // 预警通知方式 all dingtalk sms + IsEnabled int `json:"is_enabled" form:"is_enabled" binding:"omitempty,oneof=0 1 2"` // 是否开启 1:是 2:否 + Keyword string `json:"keyword" form:"keyword"` // 预警规则名称(指标名称) + StartTime string `json:"start_time" form:"start_time"` + EndTime string `json:"end_time" form:"end_time"` Pagination } diff --git a/src/bean/vo/response/alert_rules.go b/src/bean/vo/response/alert_rules.go index 15dcdb2..ff1bc07 100644 --- a/src/bean/vo/response/alert_rules.go +++ b/src/bean/vo/response/alert_rules.go @@ -4,10 +4,14 @@ import "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" type AlertRulesItem struct { entity.AlertRules `xorm:"extends"` - AlertCondition []entity.AlertCondition `json:"alert_condition" form:"alert_condition" binding:"required"` - AlertRange []entity.RulesAlertRange `json:"alert_range" form:"alert_range" binding:"required,dive"` - NotifyMethod []string `json:"notify_method" form:"notify_method" binding:"max=2,dive,oneof=dingtalk sms"` - NotifyRecipients []entity.NotifyRecipients `json:"notify_recipients" form:"notify_recipients" binding:"dive"` + AlertCondition []entity.AlertCondition `json:"alert_condition" xorm:"alert_condition"` + AlertRange []entity.RulesAlertRange `json:"alert_range" xorm:"alert_range"` + NotifyMethod []string `json:"notify_method" xorm:"notify_method"` + NotifyRecipients []entity.NotifyRecipients `json:"notify_recipients" xorm:"notify_recipients"` +} + +func (a *AlertRulesItem) TableName() string { + return "alert_rules" } type AlertRulesList struct { diff --git a/src/service/alert_rules.go b/src/service/alert_rules.go index c9a4b3c..e706986 100644 --- a/src/service/alert_rules.go +++ b/src/service/alert_rules.go @@ -3,6 +3,7 @@ package service import ( "github.com/google/uuid" "github.com/jinzhu/copier" + json "github.com/json-iterator/go" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response" @@ -183,27 +184,40 @@ func (m *AlertRulesSvc) GetDataById(req request.DetailAlertRules) (resp response return } -func (m *AlertRulesSvc) List(req request.ListAlertRules) (resp response.MetricConfigList, err error) { +func (m *AlertRulesSvc) List(req request.ListAlertRules) (resp response.AlertRulesList, err error) { db, err := client.GetDbClient() if err != nil { return } session := db.NewSession() defer session.Close() + session.Table(new(entity.AlertRules)).Alias("r").Select("r.*") + session.Join("LEFT", "metric_config mc", "mc.id = r.metric_config_id") + session.Join("LEFT", "alert_class ac", "ac.class_id = r.class_id") + session.Join("LEFT", "alert_class acp", "acp.class_id = ac.parent_id") if req.Id != "" { - session.Where("id = ?", req.Id) + session.Where("r.id = ?", req.Id) } - if req.ClassId != 0 { - session.Where("class_id = ?", req.ClassId) + if req.NotifyMethod != "" && req.NotifyMethod != "all" { + session.Where("r.notify_method LIKE ?", "%\""+req.NotifyMethod+"\"%") // ["dingtalk","sms"] } - if req.MetricName != "" { - session.Where("metric_name LIKE ?", "%"+req.MetricName+"%") + if req.IsEnabled > 0 { + session.Where("r.is_enabled = ?", req.IsEnabled) } - if req.IsEnabled != 0 { - session.Where("is_enabled = ?", req.IsEnabled) + if req.Keyword != "" { + // 预警对象/预警分类/预警指标 + session.Where("r.metric_name LIKE ?", "%"+req.Keyword+"%"). + Or("mc.metric_name LIKE ?", "%"+req.Keyword+"%"). + Or("ac.class_name LIKE ?", "%"+req.Keyword+"%"). + Or("acp.class_name LIKE ?", "%"+req.Keyword+"%") + } + resp.TotalCount, err = session.Limit(req.GetPageSize(), (req.GetPage()-1)*req.GetPageSize()).FindAndCount(&resp.List) + for i := 0; i < len(resp.List); i++ { + _ = json.Unmarshal([]byte(resp.List[i].AlertRules.AlertCondition), &resp.List[i].AlertCondition) + _ = json.Unmarshal([]byte(resp.List[i].AlertRules.AlertRange), &resp.List[i].AlertRange) + _ = json.Unmarshal([]byte(resp.List[i].AlertRules.NotifyMethod), &resp.List[i].NotifyMethod) + _ = json.Unmarshal([]byte(resp.List[i].AlertRules.NotifyRecipients), &resp.List[i].NotifyRecipients) } - resp.TotalCount, err = session.Table(new(entity.AlertRules)).Limit(req.GetPageSize(), (req.GetPage()-1)*req.GetPageSize()). - OrderBy("id").FindAndCount(&resp.List) return } -- 2.26.0