diff --git a/src/bean/entity/alert_rules.go b/src/bean/entity/alert_rules.go index b322fe4389129cc6e43e120dc0b240a78d351825..a1f8e710f5e57437fcaa456c8fc3c7dbf5c9ad13 100644 --- a/src/bean/entity/alert_rules.go +++ b/src/bean/entity/alert_rules.go @@ -15,6 +15,7 @@ type AlertRules struct { DurationUnit string `json:"duration_unit" xorm:"'duration_unit'"` // 持续时间单位 s m h CheckPeriod int `json:"check_period" xorm:"'check_period'"` // 检查周期 单位:分钟 NotifyMethod string `json:"notify_method" xorm:"'notify_method'"` // 预警通知方式 all dingtalk sms + NotifyRecipients string `json:"notify_recipients" xorm:"notify_recipients"` // 预警推送用户 NotifyPushCount int `json:"notify_push_count" xorm:"'notify_push_count'"` // 消息推送次数 NotifyPushFrequency int `json:"notify_push_frequency" xorm:"'notify_push_frequency'"` // 消息推送频率 分钟 IsEnabled int `json:"is_enabled" xorm:"'is_enabled'"` // 是否开启 0:关闭 1:启动 @@ -39,3 +40,9 @@ type AlertCondition struct { ThresholdsMin int `json:"thresholds_min" form:"thresholds_min" binding:"required"` RiskLevel int `json:"risk_level" form:"risk_level" binding:"required"` } + +type NotifyRecipients struct { + SystemAccount string `json:"system_account" form:"system_account" binding:"required"` + Name string `json:"user_name" form:"user_name" binding:"required"` + Phone string `json:"phone" form:"phone" binding:"required"` +} diff --git a/src/bean/vo/request/alert_rules.go b/src/bean/vo/request/alert_rules.go index 0624303673bdff97ba9950a418d2999e5d8543bc..83c9029d0867204e8bfd62553a43f660c1612e19 100644 --- a/src/bean/vo/request/alert_rules.go +++ b/src/bean/vo/request/alert_rules.go @@ -3,44 +3,46 @@ package request 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"` // 预警规则名称(指标名称) - DetectionType int `json:"detection_type" form:"detection_type" binding:"oneof=1 2"` // 检测类型 1:静态阈值 2:自定义 - ClassId int `json:"class_id" form:"class_id" binding:"required_if=DetectionType 1"` // 预警对象id(级联:预警分类/预警对象) - ClassParentName string `json:"class_parent_name" form:"class_parent_name" binding:"required_if=DetectionType 2"` // 预警分类名称 - ClassName string `json:"class_name" form:"class_name" binding:"required_if=DetectionType 2"` // 预警对象名称 - MetricConfigName string `json:"metric_config_name" form:"metric_config_name" binding:"required_if=DetectionType 2"` // 预警指标名称(映射entity.MetricConfig.MetricName) - Expr string `json:"expr" form:"expr" binding:"required_if=DetectionType 2"` // 指标表达式(PromQL语句) - AlertCondition []entity.AlertCondition `json:"alert_condition" form:"alert_condition" binding:"required"` // 预警规则 字典值 - AlertRuleType string `json:"alert_rule_type" form:"alert_rule_type" binding:"required_if=DetectionType 2"` // 预警规则类型 关联字典表 - AlertRange []entity.RulesAlertRange `json:"alert_range" form:"alert_range" binding:"required,dive"` // 预警范围 字典值 - Duration int `json:"duration" form:"duration" binding:"gte=0"` // 持续时间 - DurationUnit string `json:"duration_unit" form:"duration_unit" binding:"oneof=s m h"` // 持续时间单位 s m h - CheckPeriod int `json:"check_period" form:"check_period" binding:"oneof=1 3 5 10 20 30"` // 检查周期 单位:分钟 - NotifyMethod string `json:"notify_method" form:"notify_method" binding:"oneof=all dingtalk sms"` // 预警通知方式 all dingtalk sms - NotifyPushCount int `json:"notify_push_count" form:"notify_push_count" binding:"gte=1"` // 消息推送次数 - NotifyPushFrequency int `json:"notify_push_frequency" form:"notify_push_frequency" binding:"gte=1"` // 消息推送频率 分钟 - IsEnabled int `json:"is_enabled" form:"is_enabled" binding:"omitempty,oneof=0 1 2"` // 是否开启 1:是 2:否 + MetricName string `json:"metric_name" form:"metric_name" binding:"required"` // 预警规则名称(指标名称) + DetectionType int `json:"detection_type" form:"detection_type" binding:"oneof=1 2"` // 检测类型 1:静态阈值 2:自定义 + ClassId int `json:"class_id" form:"class_id" binding:"required_if=DetectionType 1"` // 预警对象id(级联:预警分类/预警对象) + ClassParentName string `json:"class_parent_name" form:"class_parent_name" binding:"required_if=DetectionType 2"` // 预警分类名称 + ClassName string `json:"class_name" form:"class_name" binding:"required_if=DetectionType 2"` // 预警对象名称 + MetricConfigName string `json:"metric_config_name" form:"metric_config_name" binding:"required_if=DetectionType 2"` // 预警指标名称(映射entity.MetricConfig.MetricName) + Expr string `json:"expr" form:"expr" binding:"required_if=DetectionType 2"` // 指标表达式(PromQL语句) + AlertCondition []entity.AlertCondition `json:"alert_condition" form:"alert_condition" binding:"required"` // 预警规则 字典值 + AlertRuleType string `json:"alert_rule_type" form:"alert_rule_type" binding:"required_if=DetectionType 2"` // 预警规则类型 关联字典表 + AlertRange []entity.RulesAlertRange `json:"alert_range" form:"alert_range" binding:"required,dive"` // 预警范围 字典值 + Duration int `json:"duration" form:"duration" binding:"gte=0"` // 持续时间 + DurationUnit string `json:"duration_unit" form:"duration_unit" binding:"oneof=s m h"` // 持续时间单位 s m h + CheckPeriod int `json:"check_period" form:"check_period" binding:"oneof=1 3 5 10 20 30"` // 检查周期 单位:分钟 + NotifyMethod []string `json:"notify_method" form:"notify_method" binding:"max=2,dive,oneof=dingtalk sms"` // 预警通知方式 all dingtalk sms + NotifyRecipients []entity.NotifyRecipients `json:"notify_recipients" form:"notify_recipients" binding:"dive"` // 预警推送用户 + NotifyPushCount int `json:"notify_push_count" form:"notify_push_count" binding:"gte=1"` // 消息推送次数 + NotifyPushFrequency int `json:"notify_push_frequency" form:"notify_push_frequency" binding:"gte=1"` // 消息推送频率 分钟 + IsEnabled int `json:"is_enabled" form:"is_enabled" binding:"omitempty,oneof=0 1 2"` // 是否开启 1:是 2:否 } type UpdateAlertRules struct { - Id string `json:"id" binding:"required"` - MetricName string `json:"metric_name" form:"metric_name" binding:"required"` // 预警规则名称(指标名称) - DetectionType int `json:"detection_type" form:"detection_type" binding:"oneof=1 2"` // 检测类型 1:静态阈值 2:自定义 - ClassId int `json:"class_id" form:"class_id" binding:"required"` // 预警对象id(级联:预警分类/预警对象) - ClassParentName string `json:"class_parent_name" form:"class_parent_name" binding:"required_if=DetectionType 2"` // 预警分类名称 - ClassName string `json:"class_name" form:"class_name" binding:"required_if=DetectionType 2"` // 预警对象名称 - MetricConfigName string `json:"metric_config_name" form:"metric_config_name" binding:"required_if=DetectionType 2"` // 预警指标名称(detection_type=2时有值) - Expr string `json:"expr" form:"expr" binding:"required_if=DetectionType 2"` // 指标表达式(PromQL语句) - AlertCondition string `json:"alert_condition" form:"alert_condition" binding:"required"` // 预警规则 字典值 - AlertRuleType string `json:"alert_rule_type" form:"alert_rule_type" binding:"required_if=DetectionType 2"` // 预警规则类型 关联字典表 - AlertRange string `json:"alert_range" form:"alert_range"` // 预警范围 字典值 - Duration int `json:"duration" form:"duration" binding:"gte=0"` // 持续时间 - DurationUnit string `json:"duration_unit" form:"duration_unit" binding:"oneof=s m h"` // 持续时间单位 s m h - CheckPeriod int `json:"check_period" form:"check_period" binding:"gte=0"` // 检查周期 单位:分钟 - NotifyMethod string `json:"notify_method" form:"notify_method" binding:"oneof=all dingtalk sms"` // 预警通知方式 all dingtalk sms - NotifyPushCount int `json:"notify_push_count" form:"notify_push_count" binding:"gte=1"` // 消息推送次数 - NotifyPushFrequency int `json:"notify_push_frequency" form:"notify_push_frequency" binding:"gte=1"` // 消息推送频率 分钟 - IsEnabled int `json:"is_enabled" form:"is_enabled" binding:"omitempty,oneof=0 1 2"` // 是否开启 1:是 2:否 + Id string `json:"id" form:"id" binding:"required"` + MetricName string `json:"metric_name" form:"metric_name" binding:"required"` // 预警规则名称(指标名称) + DetectionType int `json:"detection_type" form:"detection_type" binding:"oneof=1 2"` // 检测类型 1:静态阈值 2:自定义 + ClassId int `json:"class_id" form:"class_id" binding:"required_if=DetectionType 1"` // 预警对象id(级联:预警分类/预警对象) + ClassParentName string `json:"class_parent_name" form:"class_parent_name" binding:"required_if=DetectionType 2"` // 预警分类名称 + ClassName string `json:"class_name" form:"class_name" binding:"required_if=DetectionType 2"` // 预警对象名称 + MetricConfigName string `json:"metric_config_name" form:"metric_config_name" binding:"required_if=DetectionType 2"` // 预警指标名称(映射entity.MetricConfig.MetricName) + Expr string `json:"expr" form:"expr" binding:"required_if=DetectionType 2"` // 指标表达式(PromQL语句) + AlertCondition []entity.AlertCondition `json:"alert_condition" form:"alert_condition" binding:"required"` // 预警规则 字典值 + AlertRuleType string `json:"alert_rule_type" form:"alert_rule_type" binding:"required_if=DetectionType 2"` // 预警规则类型 关联字典表 + AlertRange []entity.RulesAlertRange `json:"alert_range" form:"alert_range" binding:"required,dive"` // 预警范围 字典值 + Duration int `json:"duration" form:"duration" binding:"gte=0"` // 持续时间 + DurationUnit string `json:"duration_unit" form:"duration_unit" binding:"oneof=s m h"` // 持续时间单位 s m h + CheckPeriod int `json:"check_period" form:"check_period" binding:"oneof=1 3 5 10 20 30"` // 检查周期 单位:分钟 + NotifyMethod []string `json:"notify_method" form:"notify_method" binding:"max=2,dive,oneof=dingtalk sms"` // 预警通知方式 all dingtalk sms + NotifyRecipients []entity.NotifyRecipients `json:"notify_recipients" form:"notify_recipients" binding:"dive"` // 预警推送用户 + NotifyPushCount int `json:"notify_push_count" form:"notify_push_count" binding:"gte=1"` // 消息推送次数 + NotifyPushFrequency int `json:"notify_push_frequency" form:"notify_push_frequency" binding:"gte=1"` // 消息推送频率 分钟 + IsEnabled int `json:"is_enabled" form:"is_enabled" binding:"omitempty,oneof=0 1 2"` // 是否开启 1:是 2:否 } type DeleteAlertRules struct { diff --git a/src/bean/vo/response/alert_rules.go b/src/bean/vo/response/alert_rules.go index 928b253a8d159a55cd2d1f7609f7530b7eee1cb2..15dcdb2d72148852f044f75e46daa4e4559658f9 100644 --- a/src/bean/vo/response/alert_rules.go +++ b/src/bean/vo/response/alert_rules.go @@ -4,6 +4,10 @@ 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"` } type AlertRulesList struct { diff --git a/src/service/alert_class.go b/src/service/alert_class.go index d4e3e6ad5d1833205eb5369194ff26d7b475f3d6..bcde691ce7122a59de7af0336fde95fb4829a5f9 100644 --- a/src/service/alert_class.go +++ b/src/service/alert_class.go @@ -28,11 +28,14 @@ func (m *AlertClassSvc) Add(session *xorm.Session, req request.AddAlertClass) (c } _ = copier.Copy(&data, &req) - max, err := m.SortOrderMax(data.ParentId) - if err != nil { - return + var max int + if data.SortOrder >= 0 { + max, err = m.SortOrderMax(data.ParentId) + if err != nil { + return + } + data.SortOrder = max + 1 } - data.SortOrder = max + 1 _, err = session.Insert(&data) classId = data.ClassId return diff --git a/src/service/alert_rules.go b/src/service/alert_rules.go index ec3c993a108943cba8b8937b38cd7c91660cd096..c9a4b3cb5aa57b69c462f06c0921c2bd37876610 100644 --- a/src/service/alert_rules.go +++ b/src/service/alert_rules.go @@ -9,6 +9,7 @@ import ( "gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/jsontime" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp" + "gitlab.wodcloud.com/smart-operation/so-operation-api/src/util" "xorm.io/xorm" ) @@ -31,9 +32,13 @@ func (m *AlertRulesSvc) Add(req request.AddAlertRules) error { UpdatedAt: now, } _ = copier.Copy(&data, &req) + data.AlertCondition = util.ConvertToString(req.AlertCondition) + data.AlertRange = util.ConvertToString(req.AlertRange) + data.NotifyMethod = util.ConvertToString(req.NotifyMethod) + data.NotifyRecipients = util.ConvertToString(req.NotifyRecipients) switch req.DetectionType { case 1: - _, err = db.Update(&data) + _, err = db.Insert(&data) if err != nil { return err } @@ -82,6 +87,9 @@ func (m *AlertRulesSvc) Add(req request.AddAlertRules) error { _, err = session.Insert(&data) return nil, err }) + if err != nil { + return err + } } // TODO 告警规则添加到普罗米修斯表 @@ -100,6 +108,10 @@ func (m *AlertRulesSvc) Update(req request.UpdateAlertRules) error { UpdatedAt: now, } _ = copier.Copy(&data, &req) + data.AlertCondition = util.ConvertToString(req.AlertCondition) + data.AlertRange = util.ConvertToString(req.AlertRange) + data.NotifyMethod = util.ConvertToString(req.NotifyMethod) + data.NotifyRecipients = util.ConvertToString(req.NotifyRecipients) session := db.NewSession() var dbAlertRules entity.AlertRules @@ -109,7 +121,7 @@ func (m *AlertRulesSvc) Update(req request.UpdateAlertRules) error { } switch req.DetectionType { case 1: - _, err = db.Update(&data) + _, err = db.ID(data.Id).Update(&data) if err != nil { return err } @@ -152,9 +164,12 @@ func (m *AlertRulesSvc) Update(req request.UpdateAlertRules) error { } // 更新预警规则配置 - _, err = session.Update(&data) + _, err = session.ID(data.Id).Update(&data) return nil, err }) + if err != nil { + return err + } } return nil }