From ef1ffd7b8f8afb1ed7390cb04f3b17d20ba2c6d9 Mon Sep 17 00:00:00 2001 From: like Date: Thu, 6 Jul 2023 10:21:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=A2=84=E8=AD=A6=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_class.go | 18 ++--- src/bean/entity/alert_list.go | 29 ++++++++ src/bean/entity/alert_rules.go | 2 +- src/bean/entity/push_record.go | 18 +++++ src/bean/vo/request/alert_list.go | 20 +++++ src/bean/vo/response/alert_list.go | 15 ++++ src/controller/alert_list.go | 62 +++++++++++++++ src/router/alertlistrouter.go.go | 17 +++++ src/router/router.go | 2 + src/service/alert_list.go | 116 +++++++++++++++++++++++++++++ 10 files changed, 289 insertions(+), 10 deletions(-) create mode 100644 src/bean/entity/alert_list.go create mode 100644 src/bean/entity/push_record.go create mode 100644 src/bean/vo/request/alert_list.go create mode 100644 src/bean/vo/response/alert_list.go create mode 100644 src/controller/alert_list.go create mode 100644 src/router/alertlistrouter.go.go create mode 100644 src/service/alert_list.go diff --git a/src/bean/entity/alert_class.go b/src/bean/entity/alert_class.go index 801f530..0e11ad6 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'"` // 排序 + 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'"` // 更新时间 } func (m *AlertClass) TableName() string { diff --git a/src/bean/entity/alert_list.go b/src/bean/entity/alert_list.go new file mode 100644 index 0000000..6f49d71 --- /dev/null +++ b/src/bean/entity/alert_list.go @@ -0,0 +1,29 @@ +package entity + +import ( + "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/jsontime" +) + +type AlertList struct { + Id int `json:"id"` // 预警列表ID,主键,自增长 + AlertPoint string `json:"alert_point"` // 预警点 + AlertRulesId string `json:"alert_rules_id"` // 告警规则id + RiskLevel int `json:"risk_level"` // 风险等级,1:低风险,2:一般风险,3:较大风险,4:重大风险 + AlertTime jsontime.Time `json:"alert_time"` // 预警时间 + ClassId int `json:"class_id" xorm:"'class_id'"` // 预警对象id(级联:预警分类/预警对象) + MetricConfigId string `json:"metric_config_id"` // 预警指标id // 预警指标 + CurrentValue float64 `json:"current_value"` // 当前报警值 + AlertCondition string `json:"alert_condition" xorm:"'alert_condition'"` // 预警规则(预警阈值) 字典值 + NotificationCount int `json:"notification_count"` // 通知人数 + PushCount int `json:"push_count"` // 推送次数 + LastPushTime jsontime.Time `json:"last_push_time"` // 最近推送时间 + Status int `json:"status"` // 状态,1:已恢复 2:未恢复 3:已关闭 + 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 (a *AlertList) TableName() string { + return "alert_list" +} diff --git a/src/bean/entity/alert_rules.go b/src/bean/entity/alert_rules.go index 72e096f..6596081 100644 --- a/src/bean/entity/alert_rules.go +++ b/src/bean/entity/alert_rules.go @@ -38,7 +38,7 @@ type RulesAlertRange struct { type AlertCondition struct { ThresholdsMax int `json:"thresholds_max" form:"thresholds_max" binding:"required"` ThresholdsMin int `json:"thresholds_min" form:"thresholds_min" binding:"required"` - RiskLevel int `json:"risk_level" form:"risk_level" binding:"required"` + RiskLevel int `json:"risk_level" form:"risk_level" binding:"required,oneof=1 2 3 4"` } type NotifyRecipients struct { diff --git a/src/bean/entity/push_record.go b/src/bean/entity/push_record.go new file mode 100644 index 0000000..fafad4f --- /dev/null +++ b/src/bean/entity/push_record.go @@ -0,0 +1,18 @@ +package entity + +import "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/jsontime" + +type PushRecord struct { + Id int `json:"id" xorm:"'id' pk autoincr"` // 主键id + AlertRulesId string `json:"alert_rules_id" xorm:"'alert_rules_id'"` // 告警规则id + RiskLevel int `json:"risk_level" xorm:"'risk_level'"` // 风险等级,1:低风险,2:一般风险,3:较大风险,4:重大风险 + NotifyMethod string `json:"notify_method" xorm:"'notify_method'"` // 预警通知方式 all dingtalk sms + SystemAccount string `json:"system_account" xorm:"system_account"` // 预警推送用户(NotifyRecipient) // 账号 + PushTime jsontime.Time `json:"push_time" xorm:"'push_time'"` // 推送时间 + PushType int `json:"push_type" xorm:"'push_type'"` // 推送类型,1:自动推送,2:手动推送 + Status int `json:"status" xorm:"'status'"` // 推送状态,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'"` // 更新时间 +} diff --git a/src/bean/vo/request/alert_list.go b/src/bean/vo/request/alert_list.go new file mode 100644 index 0000000..cd84c79 --- /dev/null +++ b/src/bean/vo/request/alert_list.go @@ -0,0 +1,20 @@ +package request + +type DetailAlertList struct { + Id int `json:"id" form:"id" binding:"required"` +} + +type UpdateAlertList struct { + RiskLevel int `json:"risk_level" form:"risk_level" binding:"omitempty,oneof=1 2 3 4"` // 风险等级,1:低风险,2:一般风险,3:较大风险,4:重大风险 + Status int `json:"status" form:"status" binding:"omitempty,oneof=1 2 3"` // 状态,1:已恢复 2:未恢复 3:已关闭 + Keyword string `json:"keyword" form:"keyword"` // 预警点/分类/指标 +} + +type ListAlertList struct { + Id int `json:"id" form:"id"` + RiskLevel int `json:"risk_level" form:"risk_level" binding:"omitempty,oneof=1 2 3 4"` // 风险等级,1:低风险,2:一般风险,3:较大风险,4:重大风险 + Status int `json:"status" form:"status" binding:"omitempty,oneof=1 2 3"` // 状态,1:已恢复 2:未恢复 3:已关闭 + Keyword string `json:"keyword" form:"keyword"` // 预警点/分类/指标 + StartTime string `json:"start_time" form:"start_time" binding:"omitempty,datetime=2006-01-02 15:04:05"` + EndTime string `json:"end_time" form:"end_time" binding:"omitempty,datetime=2006-01-02 15:04:05"` +} diff --git a/src/bean/vo/response/alert_list.go b/src/bean/vo/response/alert_list.go new file mode 100644 index 0000000..1105cb7 --- /dev/null +++ b/src/bean/vo/response/alert_list.go @@ -0,0 +1,15 @@ +package response + +import ( + "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" +) + +type AlertListItem struct { + entity.AlertList `xorm:"extends"` + AlertCondition entity.AlertCondition `json:"alert_condition" xorm:"alert_condition"` +} + +type AlertListList struct { + TotalCount int64 `json:"total_count"` + List []AlertListItem `json:"list"` +} diff --git a/src/controller/alert_list.go b/src/controller/alert_list.go new file mode 100644 index 0000000..aa21524 --- /dev/null +++ b/src/controller/alert_list.go @@ -0,0 +1,62 @@ +package controller + +import ( + "github.com/gin-gonic/gin" + "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request" + "gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client" + "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp" + "gitlab.wodcloud.com/smart-operation/so-operation-api/src/router/middleware/header" + "gitlab.wodcloud.com/smart-operation/so-operation-api/src/service" +) + +func UpdateAlertList(c *gin.Context) { + var req request.UpdateAlertList + if err := c.ShouldBind(&req); err != nil { + SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) + return + } + + svc := service.AlertListSvc{User: header.GetUser(c)} + db, err := client.GetDbClient() + if err != nil { + SendJsonResponse(c, resp.DbConnectError.WithError(err), nil) + return + } + err = svc.Update(db.NewSession(), req) + if err != nil { + SendJsonResponse(c, resp.FAIL.WithError(err), nil) + return + } + SendJsonResponse(c, resp.OK, nil) +} + +func DetailAlertList(c *gin.Context) { + var req request.DetailAlertList + if err := c.ShouldBind(&req); err != nil { + SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) + return + } + + svc := service.AlertListSvc{User: header.GetUser(c)} + data, err := svc.GetDataById(req) + if err != nil { + SendJsonResponse(c, resp.FAIL.WithError(err), nil) + return + } + SendJsonResponse(c, resp.OK, data) +} + +func ListAlertList(c *gin.Context) { + var req request.ListAlertList + if err := c.ShouldBind(&req); err != nil { + SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) + return + } + svc := service.AlertListSvc{User: header.GetUser(c)} + data, err := svc.List(req) + if err != nil { + SendJsonResponse(c, resp.FAIL.WithError(err), nil) + return + } + SendJsonResponse(c, resp.OK, data) +} diff --git a/src/router/alertlistrouter.go.go b/src/router/alertlistrouter.go.go new file mode 100644 index 0000000..4dccf96 --- /dev/null +++ b/src/router/alertlistrouter.go.go @@ -0,0 +1,17 @@ +package router + +import ( + "fmt" + "github.com/gin-gonic/gin" + "gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/conf" + "gitlab.wodcloud.com/smart-operation/so-operation-api/src/controller" +) + +// InitAlertListRouter 初始化预警列表配置路由 +func InitAlertListRouter(e *gin.Engine) { + group := e.Group(fmt.Sprintf("%s/alert_list", conf.Options.Prefix)) + { + group.GET("", controller.DetailAlertList) + group.GET("list", controller.ListAlertList) + } +} diff --git a/src/router/router.go b/src/router/router.go index 9506742..076c1e2 100644 --- a/src/router/router.go +++ b/src/router/router.go @@ -53,6 +53,8 @@ func Load(r *gin.Engine, middleware ...gin.HandlerFunc) { InitMetricConfigRouter(r) // 初始化预警规则配置路由 InitAlertRulesRouter(r) + // 初始化预警列表配置路由 + InitAlertListRouter(r) // 初始化prometheus路由 InitPrometheusRouter(r) // 初始化工单管理路由 diff --git a/src/service/alert_list.go b/src/service/alert_list.go new file mode 100644 index 0000000..7f3b2d7 --- /dev/null +++ b/src/service/alert_list.go @@ -0,0 +1,116 @@ +package service + +import ( + "github.com/jinzhu/copier" + "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" + "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/jsontime" + "xorm.io/xorm" +) + +type AlertListSvc struct { + User entity.SystemUserInfo +} + +func (a *AlertListSvc) Update(session *xorm.Session, req request.UpdateAlertList) error { + now := jsontime.Now() + data := entity.AlertList{ + UpdatedBy: a.User.SystemAccount, + UpdatedAt: now, + } + _ = copier.Copy(&data, &req) + _, err := session.Cols("class_name", "updated_by", "updated_at").ID(data.ClassId).Update(&data) + if err != nil { + return err + } + return nil +} + +func (a *AlertListSvc) GetDataById(req request.DetailAlertList) (resp response.AlertListItem, err error) { + now := jsontime.Now() + data := response.AlertListItem{ + AlertList: entity.AlertList{ + Id: 123, + AlertPoint: "容器云/kube-apiserver", + AlertRulesId: "762ed641-6c0e-4c39-bf7c-7463abb0f8a2", + RiskLevel: 4, + AlertTime: now, + ClassId: 14, + MetricConfigId: "d773b37b-dbb4-4a7b-be11-ab40f8acc00e", + CurrentValue: 85, + NotificationCount: 3, + PushCount: 3, + LastPushTime: now, + Status: 0, + CreatedBy: "admin", + CreatedAt: now, + UpdatedBy: "admin", + UpdatedAt: now, + }, + AlertCondition: entity.AlertCondition{ + ThresholdsMax: 100, + ThresholdsMin: 80, + RiskLevel: 4, + }, + } + resp = data + return +} + +func (a *AlertListSvc) List(req request.ListAlertList) (resp response.AlertListList, err error) { + now := jsontime.Now() + data1 := response.AlertListItem{ + AlertList: entity.AlertList{ + Id: 123, + AlertPoint: "容器云/kube-apiserver", + AlertRulesId: "762ed641-6c0e-4c39-bf7c-7463abb0f8a2", + RiskLevel: 4, + AlertTime: now, + ClassId: 14, + MetricConfigId: "d773b37b-dbb4-4a7b-be11-ab40f8acc00e", + CurrentValue: 85, + NotificationCount: 3, + PushCount: 3, + LastPushTime: now, + Status: 2, + CreatedBy: "admin", + CreatedAt: now, + UpdatedBy: "admin", + UpdatedAt: now, + }, + AlertCondition: entity.AlertCondition{ + ThresholdsMax: 100, + ThresholdsMin: 80, + RiskLevel: 4, + }, + } + data2 := response.AlertListItem{ + AlertList: entity.AlertList{ + Id: 125, + AlertPoint: "容器云/apaas", + AlertRulesId: "762ed641-6c0e-4c39-bf7c-7463abb0f8a2", + RiskLevel: 3, + AlertTime: now, + ClassId: 14, + MetricConfigId: "d773b37b-dbb4-4a7b-be11-ab40f8acc00e", + CurrentValue: 85, + NotificationCount: 1, + PushCount: 1, + LastPushTime: now, + Status: 1, + CreatedBy: "admin", + CreatedAt: now, + UpdatedBy: "admin", + UpdatedAt: now, + }, + AlertCondition: entity.AlertCondition{ + ThresholdsMax: 80, + ThresholdsMin: 50, + RiskLevel: 3, + }, + } + resp.List = append(resp.List, data1, data2) + resp.TotalCount = int64(len(resp.List)) + return +} -- 2.26.0