From 3ee9feb2691a5bb38eaaabfec78b358d1d5c265f Mon Sep 17 00:00:00 2001 From: like Date: Thu, 27 Jul 2023 14:21:43 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=95=B0=E6=8D=AE=E6=9D=A5=E6=BA=90?= =?UTF-8?q?=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bean/entity/alert_class.go | 18 +++++++++--------- src/bean/entity/alert_webhook.go | 7 ------- src/bean/entity/metric_config.go | 2 +- src/bean/vo/request/alert_class.go | 8 ++++---- src/bean/vo/request/metric_config.go | 4 ++-- src/main.go | 6 +++--- src/pkg/beagle/constant/constant.go | 6 ++++++ src/service/alert_class.go | 12 ++++++++---- src/service/alert_overview.go | 2 +- src/service/alert_rules.go | 19 ++++++++++--------- src/service/metric_config.go | 4 ++++ 11 files changed, 48 insertions(+), 40 deletions(-) delete mode 100644 src/bean/entity/alert_webhook.go diff --git a/src/bean/entity/alert_class.go b/src/bean/entity/alert_class.go index 0e11ad6..32ba580 100644 --- a/src/bean/entity/alert_class.go +++ b/src/bean/entity/alert_class.go @@ -3,15 +3,15 @@ package entity import "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/jsontime" type AlertClass struct { - ClassId int `json:"class_id" xorm:"'class_id' pk autoincr"` // 主键id - ClassName string `json:"class_name" xorm:"'class_name'"` // 分类名称 - ParentId int `json:"parent_id" xorm:"'parent_id'"` // 父级id - SortOrder int `json:"sort_order" xorm:"'sort_order'"` // 排序 - SourceFrom int `json:"source_from" xorm:"source_from"` // 数据来源 1:默认 2:自定义 - CreatedBy string `json:"created_by" xorm:"'created_by'"` // 创建人 - CreatedAt jsontime.Time `json:"created_at" xorm:"'created_at'"` // 创建时间 - UpdatedBy string `json:"updated_by" xorm:"'updated_by'"` // 更新人 - UpdatedAt jsontime.Time `json:"updated_at" xorm:"'updated_at'"` // 更新时间 + ClassId int `json:"class_id" xorm:"'class_id' pk autoincr"` // 主键id + ClassName string `json:"class_name" xorm:"'class_name'"` // 分类名称 + ParentId int `json:"parent_id" xorm:"'parent_id'"` // 父级id + SortOrder int `json:"sort_order" xorm:"'sort_order'"` // 排序 + Source int `json:"source" xorm:"source"` // 数据来源 1:默认 2:自定义 + CreatedBy string `json:"created_by" xorm:"'created_by'"` // 创建人 + CreatedAt jsontime.Time `json:"created_at" xorm:"'created_at'"` // 创建时间 + UpdatedBy string `json:"updated_by" xorm:"'updated_by'"` // 更新人 + UpdatedAt jsontime.Time `json:"updated_at" xorm:"'updated_at'"` // 更新时间 } func (m *AlertClass) TableName() string { diff --git a/src/bean/entity/alert_webhook.go b/src/bean/entity/alert_webhook.go deleted file mode 100644 index bcc3251..0000000 --- a/src/bean/entity/alert_webhook.go +++ /dev/null @@ -1,7 +0,0 @@ -package entity - -import ( - "github.com/prometheus/alertmanager/notify/webhook" -) - -type Message webhook.Message diff --git a/src/bean/entity/metric_config.go b/src/bean/entity/metric_config.go index 08cee08..c64858f 100644 --- a/src/bean/entity/metric_config.go +++ b/src/bean/entity/metric_config.go @@ -13,7 +13,7 @@ type MetricConfig struct { CheckPeriod int `json:"check_period" xorm:"'check_period'"` // 检查周期 单位:分钟 IsEnabled int `json:"is_enabled" xorm:"'is_enabled'"` // 是否开启 1:是 2:否 AlertRuleType string `json:"alert_rule_type" xorm:"'alert_rule_type'"` // 预警规则类型 关联字典表 - SourceFrom int `json:"source_from" xorm:"source_from"` // 数据来源 1:默认 2:自定义 + Source int `json:"source" xorm:"source"` // 数据来源 1:默认 2:自定义 CreatedBy string `json:"created_by" xorm:"'created_by'"` // 创建人 CreatedAt jsontime.Time `json:"created_at" xorm:"'created_at'"` // 创建时间 UpdatedBy string `json:"updated_by" xorm:"'updated_by'"` // 更新人 diff --git a/src/bean/vo/request/alert_class.go b/src/bean/vo/request/alert_class.go index 49046ea..19da5ef 100644 --- a/src/bean/vo/request/alert_class.go +++ b/src/bean/vo/request/alert_class.go @@ -1,10 +1,10 @@ package request type AddAlertClass struct { - ClassName string `json:"class_name" form:"class_name" binding:"required"` - ParentId int `json:"parent_id" form:"parent_id" binding:"omitempty,oneof=0 1"` - SortOrder int `json:"sort_order" form:"sort_order"` - SourceFrom int `json:"source_from" form:"source_from" binding:"omitempty,oneof=1 2"` // 数据来源 1:默认 2:自定义 + ClassName string `json:"class_name" form:"class_name" binding:"required"` + ParentId int `json:"parent_id" form:"parent_id" binding:"omitempty,oneof=0 1"` + SortOrder int `json:"sort_order" form:"sort_order"` + Source int `json:"source" form:"source" binding:"omitempty,oneof=1 2"` // 数据来源 1:默认 2:自定义 } type UpdateAlertClass struct { diff --git a/src/bean/vo/request/metric_config.go b/src/bean/vo/request/metric_config.go index fc06ac1..a197822 100644 --- a/src/bean/vo/request/metric_config.go +++ b/src/bean/vo/request/metric_config.go @@ -12,7 +12,7 @@ type AddMetricConfig struct { CheckPeriod int `json:"check_period" form:"check_period" binding:"oneof=1 3 5 10 20 30"` // 检查周期 单位:分钟 IsEnabled int `json:"is_enabled" form:"is_enabled" binding:"omitempty,oneof=1 2"` // 是否开启 1:是 2:否 AlertRuleType string `json:"alert_rule_type" form:"alert_rule_type" binding:"required"` // 预警规则类型 关联字典表 - SourceFrom int `json:"source_from" form:"source_from" binding:"omitempty,oneof=1 2"` // 数据来源 1:默认 2:自定义 + Source int `json:"source" form:"source" binding:"omitempty,oneof=1 2"` // 数据来源(自定义为非正常数据) 1:默认 2:自定义 } type UpdateMetricConfig struct { @@ -26,7 +26,7 @@ type UpdateMetricConfig struct { CheckPeriod int `json:"check_period" form:"check_period" binding:"omitempty,oneof=1 3 5 10 20 30"` // 检查周期 单位:分钟 IsEnabled int `json:"is_enabled" form:"is_enabled" binding:"omitempty,oneof=1 2"` // 是否开启 1:是 2:否 AlertRuleType string `json:"alert_rule_type" form:"alert_rule_type"` // 预警规则类型 关联字典表 - SourceFrom int `json:"source_from" form:"source_from" binding:"omitempty,oneof=1 2"` // 数据来源 1:默认 2:自定义 + Source int `json:"source" form:"source" binding:"omitempty,oneof=1 2"` // 数据来源 1:默认 2:自定义 } type DeleteMetricConfig struct { diff --git a/src/main.go b/src/main.go index 5b1a95b..eab109c 100644 --- a/src/main.go +++ b/src/main.go @@ -90,9 +90,9 @@ func initConfig() { SmsSignName: util.SetEnvStr("SMS_SIGN_NAME", "比格数据"), // 签名 AweRestURL: util.SetEnvStr("AWE_REST_URL", "http://awecloud-rest.beagle-system/awecloud/rest"), // awecloud-rest KubernetesToken: util.SetEnvStr("AWE_REST_K8S_TOKEN", "eyJhbGciOiJSUzI1NiIsImtpZCI6InJ1alJzNEVGamN5UC0wRU1rS1BKQ0JZVUtNNWpzR0t2bmlrSlJhY2Q3R00ifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJiZWFnbGUtc3lzdGVtIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZWNyZXQubmFtZSI6InJvb3QiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoicm9vdCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6IjRlMDM0OTI3LTc0ZTMtNDQ5Yy1hN2RlLWExMGE3MjU1NGYyMCIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDpiZWFnbGUtc3lzdGVtOnJvb3QifQ.YPLE_E2kIeo-YFQtKScBt5p4KhnniJF9n3iWN2i9UMYS06lIsq2-2wBrgON-YsJihWJupYyDQRiZ9h8bUWTrQzhnpsnuJ_aUclKyAw3QOT9rjvZhJp7qP--27dmdspSHncKtvIiprWE7UTUKzvF33WsMB0fSYFqYXOggNFMoT-fXmWwUXjgar3op0iOl3c3deJ_GeBzFyLSHEuGM7OVdjU8032aUmTen0Kji_P1yB4-O3Iqd0OdVs33BQy_tycjbxhQ8TDEpqrqhLnXjAwJCprLDEpFMx7ODZbjB9Wmuns8yJhaRDxTO47rTME7ZIAxjZ-zLR_QybtW97rlwnUTaNw"), - OpenSearchAddresses: util.SetEnvStr("Open_Search_Addresses", "https://so-opensearch.wodcloud.com"), // 短信验证码模板 - OpenSearchUserName: util.SetEnvStr("Open_Search_User_Name", ""), // 短信验证码模板 - OpenSearchPassword: util.SetEnvStr("Open_Search_Password", ""), // 短信验证码模板 + OpenSearchAddresses: util.SetEnvStr("Open_Search_Addresses", "https://so-opensearch.wodcloud.com"), // OpenSearch连接地址 + OpenSearchUserName: util.SetEnvStr("Open_Search_User_Name", ""), // OpenSearch用户名 + OpenSearchPassword: util.SetEnvStr("Open_Search_Password", ""), // OpenSearch密码 } } diff --git a/src/pkg/beagle/constant/constant.go b/src/pkg/beagle/constant/constant.go index d042500..7a5eba7 100644 --- a/src/pkg/beagle/constant/constant.go +++ b/src/pkg/beagle/constant/constant.go @@ -123,6 +123,12 @@ var OpTypeIntMap = map[OpType]int{ Export: 14, } +// 数据来源(自定义为非正常数据) 1:默认 2:自定义 +const ( + SourceDefault = 1 + SourceCustom = 2 +) + // RiskLevel 风险等级 const ( RiskLevelLow = iota + 1 // 1:低风险 diff --git a/src/service/alert_class.go b/src/service/alert_class.go index a76f727..60f320d 100644 --- a/src/service/alert_class.go +++ b/src/service/alert_class.go @@ -8,6 +8,7 @@ import ( "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client" + "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/constant" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/jsontime" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp" "sort" @@ -26,6 +27,9 @@ func (a *AlertClassSvc) Add(session *xorm.Session, req request.AddAlertClass) (c UpdatedBy: a.User.SystemAccount, UpdatedAt: now, } + if req.Source != constant.SourceCustom { + req.Source = constant.SourceDefault + } _ = copier.Copy(&data, &req) var max int @@ -66,7 +70,7 @@ func (a *AlertClassSvc) Move(req request.MoveAlertClass) (err error) { _ = copier.Copy(&data, &req) var list []entity.AlertClass _, err = db.NewSession().Where("class_id = ?", req.ClassId).Get(&data) - err = db.NewSession().Where("parent_id = ?", data.ParentId).Where("source_from = 1").OrderBy("sort_order asc").Find(&list) + err = db.NewSession().Where("parent_id = ?", data.ParentId).Where("source = 1").OrderBy("sort_order asc").Find(&list) var previousIndex int var nextIndex int for i := 0; i < len(list); i++ { @@ -139,7 +143,7 @@ func (a *AlertClassSvc) List(req request.ListAlertClass) (resp response.AlertCla } session := db.NewSession() defer session.Close() - session.Where("source_from = 1") + session.Where("source = 1") if req.ClassId != 0 { session.Where("class_id = ?", req.ClassId) } @@ -159,7 +163,7 @@ func (a *AlertClassSvc) Tree(req request.ListAlertClass) (resp []*response.Alert session := db.NewSession() defer session.Close() var list []entity.AlertClass - session.Where("source_from = 1") + session.Where("source = 1") _, err = session.OrderBy("sort_order").FindAndCount(&list) // TODO 对req进行过滤 resp, err = AlertClassTree(list) @@ -211,7 +215,7 @@ func (a *AlertClassSvc) SortOrderMax(parentId int) (max int, err error) { } _, err = db.NewSession().Table(new(entity.AlertClass)). Select("max(sort_order)"). - Where("parent_id = ?", parentId).Where("source_from = 1").Get(&max) + Where("parent_id = ?", parentId).Where("source = 1").Get(&max) return } diff --git a/src/service/alert_overview.go b/src/service/alert_overview.go index 4a70ec1..581e2df 100644 --- a/src/service/alert_overview.go +++ b/src/service/alert_overview.go @@ -247,7 +247,7 @@ func (a *AlertOverviewSvc) List(req request.ListAlertOverview) (resp response.Al } session := db.NewSession() defer session.Close() - session.Where("source_from = 1") + session.Where("source = 1") if req.ClassId != 0 { session.Where("class_id = ?", req.ClassId) } diff --git a/src/service/alert_rules.go b/src/service/alert_rules.go index 32b89d3..d57812c 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/bean/vo/request" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client" + "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/constant" "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" @@ -56,18 +57,18 @@ func (a *AlertRulesSvc) Add(req request.AddAlertRules) (err error) { alertClassSvc := AlertClassSvc{User: a.User} classParentId, err = alertClassSvc.Add(session, request.AddAlertClass{ - ClassName: req.ClassParentName, - SortOrder: -1, - SourceFrom: 2, + ClassName: req.ClassParentName, + SortOrder: -1, + Source: constant.SourceCustom, }) if err != nil { return nil, err } classId, err = alertClassSvc.Add(session, request.AddAlertClass{ - ClassName: req.ClassName, - ParentId: classParentId, - SortOrder: -1, - SourceFrom: 2, + ClassName: req.ClassName, + ParentId: classParentId, + SortOrder: -1, + Source: constant.SourceCustom, }) if err != nil { return nil, err @@ -77,7 +78,7 @@ func (a *AlertRulesSvc) Add(req request.AddAlertRules) (err error) { // 添加指标配置 metricConfigSvc := MetricConfigSvc{User: a.User} _ = copier.Copy(&addMetricConfig, &req) - addMetricConfig.SourceFrom = 2 + addMetricConfig.Source = constant.SourceCustom addMetricConfig.MetricName = req.MetricConfigName metricConfigId, err = metricConfigSvc.Add(session, addMetricConfig) if err != nil { @@ -159,7 +160,7 @@ func (a *AlertRulesSvc) Update(req request.UpdateAlertRules) (err error) { // 更新指标配置 metricConfigSvc := MetricConfigSvc{User: a.User} _ = copier.Copy(&updateMetricConfig, &req) - updateMetricConfig.SourceFrom = 2 + updateMetricConfig.Source = constant.SourceCustom updateMetricConfig.Id = dbAlertRules.MetricConfigId err = metricConfigSvc.Update(session, updateMetricConfig) if err != nil { diff --git a/src/service/metric_config.go b/src/service/metric_config.go index 6792a5b..0039073 100644 --- a/src/service/metric_config.go +++ b/src/service/metric_config.go @@ -7,6 +7,7 @@ import ( "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client" + "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/constant" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/jsontime" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/util" "xorm.io/xorm" @@ -25,6 +26,9 @@ func (m *MetricConfigSvc) Add(session *xorm.Session, req request.AddMetricConfig UpdatedBy: m.User.SystemAccount, UpdatedAt: now, } + if req.Source != constant.SourceCustom { + req.Source = constant.SourceDefault + } _ = copier.Copy(&data, &req) data.AlertRange = util.ConvertToString(req.AlertRange) _, err = session.Insert(&data) -- 2.26.0