From 39fbf027fffdc662dbc3b42143c72d05a8976f95 Mon Sep 17 00:00:00 2001 From: like Date: Thu, 6 Jul 2023 20:13:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=A2=84=E8=AD=A6=E8=AF=A6=E6=83=85?= =?UTF-8?q?=E6=8E=A8=E9=80=81=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bean/entity/push_record.go | 2 +- src/bean/vo/request/push_record.go | 43 +++++++++++ src/bean/vo/response/push_record.go | 13 ++++ src/controller/push_record.go | 109 ++++++++++++++++++++++++++ src/router/pushrecordrouter.go | 18 +++++ src/router/router.go | 2 + src/service/push_record.go | 115 ++++++++++++++++++++++++++++ 7 files changed, 301 insertions(+), 1 deletion(-) create mode 100644 src/bean/vo/request/push_record.go create mode 100644 src/bean/vo/response/push_record.go create mode 100644 src/controller/push_record.go create mode 100644 src/router/pushrecordrouter.go create mode 100644 src/service/push_record.go diff --git a/src/bean/entity/push_record.go b/src/bean/entity/push_record.go index 9a01572..e9ebce3 100644 --- a/src/bean/entity/push_record.go +++ b/src/bean/entity/push_record.go @@ -7,7 +7,7 @@ type PushRecord struct { 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'"` // 预警通知方式 dingtalk sms - SystemAccount string `json:"system_account" xorm:"system_account"` // 预警推送用户(NotifyRecipient) // 账号 + SystemAccount string `json:"system_account" xorm:"system_account"` // 预警推送用户 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:失败 diff --git a/src/bean/vo/request/push_record.go b/src/bean/vo/request/push_record.go new file mode 100644 index 0000000..1248e98 --- /dev/null +++ b/src/bean/vo/request/push_record.go @@ -0,0 +1,43 @@ +package request + +import "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" + +type AddPushRecord struct { + ClassId int `json:"class_id" form:"class_id" binding:"required"` // 预警对象分类id + MetricName string `json:"metric_name" form:"metric_name" binding:"required"` // 指标名称 + Expr string `json:"expr" form:"expr" binding:"required"` // 指标表达式(PromQL语句) + AlertRange []entity.AlertRange `json:"alert_range" form:"alert_range" binding:"required,dive"` // 预警范围 字典值 + Duration int `json:"duration" form:"duration"` // 持续时间 + DurationUnit string `json:"duration_unit" form:"duration_unit" binding:"required,oneof=s m h"` // 持续时间单位 s m h + 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:自定义 +} + +type UpdatePushRecord struct { + Id string `json:"id" form:"id" binding:"required"` // 主键id + ClassId int `json:"class_id" form:"class_id"` // 预警对象分类id + MetricName string `json:"metric_name" form:"metric_name"` // 指标名称 + Expr string `json:"expr" form:"expr"` // 指标表达式(PromQL语句) + AlertRange []entity.AlertRange `json:"alert_range" form:"alert_range"` // 预警范围 字典值 + Duration int `json:"duration" form:"duration"` // 持续时间 + DurationUnit string `json:"duration_unit" form:"duration_unit" binding:"omitempty,oneof=s m h"` // 持续时间单位 s m h + 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:自定义 +} + +type DeletePushRecord struct { + Id string `json:"id" form:"id"` + Ids []string `json:"ids" form:"ids" binding:"required_without=Id"` +} + +type DetailPushRecord struct { + Id string `json:"id" form:"id" binding:"required"` +} + +type ListPushRecord struct { + AlertListId int `json:"alert_list_id" form:"alert_list_id" binding:"required"` // 预警列表id +} diff --git a/src/bean/vo/response/push_record.go b/src/bean/vo/response/push_record.go new file mode 100644 index 0000000..c3a85e2 --- /dev/null +++ b/src/bean/vo/response/push_record.go @@ -0,0 +1,13 @@ +package response + +import "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" + +type PushRecordItem struct { + entity.PushRecord `xorm:"extends"` + NotifyMethod []string `json:"notify_method" xorm:"notify_method"` // 预警通知方式 dingtalk sms +} + +type PushRecordList struct { + TotalCount int64 `json:"total_count"` + List []PushRecordItem `json:"list"` +} diff --git a/src/controller/push_record.go b/src/controller/push_record.go new file mode 100644 index 0000000..0bfa1e1 --- /dev/null +++ b/src/controller/push_record.go @@ -0,0 +1,109 @@ +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" +) + +// AddPushRecord 新增任务 +func AddPushRecord(c *gin.Context) { + var req request.AddPushRecord + if err := c.ShouldBind(&req); err != nil { + SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) + return + } + + svc := service.PushRecordSvc{User: header.GetUser(c)} + db, err := client.GetDbClient() + if err != nil { + SendJsonResponse(c, resp.DbConnectError.WithError(err), nil) + return + } + _, err = svc.Add(db.NewSession(), req) + if err != nil { + SendJsonResponse(c, resp.FAIL.WithError(err), nil) + return + } + SendJsonResponse(c, resp.OK, nil) +} + +func UpdatePushRecord(c *gin.Context) { + var req request.UpdatePushRecord + if err := c.ShouldBind(&req); err != nil { + SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) + return + } + + svc := service.PushRecordSvc{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 DeletePushRecord(c *gin.Context) { + var req request.DeletePushRecord + if err := c.ShouldBind(&req); err != nil { + SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) + return + } + + var ids []string + switch len(req.Ids) { + case 0: + ids = append(ids, req.Id) + default: + ids = req.Ids + } + + svc := service.PushRecordSvc{User: header.GetUser(c)} + err := svc.Delete(ids) + if err != nil { + SendJsonResponse(c, resp.FAIL.WithError(err), nil) + return + } + SendJsonResponse(c, resp.OK, nil) +} + +func DetailPushRecord(c *gin.Context) { + var req request.DetailPushRecord + if err := c.ShouldBind(&req); err != nil { + SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) + return + } + + svc := service.PushRecordSvc{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 ListPushRecord(c *gin.Context) { + var req request.ListPushRecord + if err := c.ShouldBind(&req); err != nil { + SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) + return + } + + svc := service.PushRecordSvc{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/pushrecordrouter.go b/src/router/pushrecordrouter.go new file mode 100644 index 0000000..a141f37 --- /dev/null +++ b/src/router/pushrecordrouter.go @@ -0,0 +1,18 @@ +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" +) + +// InitPushRecordRouter 初始化推送记录配置路由 +func InitPushRecordRouter(e *gin.Engine) { + group := e.Group(fmt.Sprintf("%s/push_record", conf.Options.Prefix)) + { + group.GET("", controller.DetailPushRecord) + group.GET("list", controller.ListPushRecord) + group.PUT("", controller.UpdatePushRecord) + } +} diff --git a/src/router/router.go b/src/router/router.go index 0856e53..d73f6ae 100644 --- a/src/router/router.go +++ b/src/router/router.go @@ -61,6 +61,8 @@ func Load(r *gin.Engine, middleware ...gin.HandlerFunc) { InitPrometheusRouter(r) // 初始化预警总览配置路由 InitAlertOverviewRouter(r) + // 初始化推送记录配置路由 + InitPushRecordRouter(r) // 初始化工单管理路由 InitWorkOrderRouter(r) } diff --git a/src/service/push_record.go b/src/service/push_record.go new file mode 100644 index 0000000..c832f73 --- /dev/null +++ b/src/service/push_record.go @@ -0,0 +1,115 @@ +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/common/client" + "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/jsontime" + "xorm.io/xorm" +) + +type PushRecordSvc struct { + User entity.SystemUserInfo +} + +func (m *PushRecordSvc) Add(session *xorm.Session, req request.AddPushRecord) (id string, err error) { + now := jsontime.Now() + data := entity.PushRecord{ + CreatedBy: m.User.SystemAccount, + CreatedAt: now, + UpdatedBy: m.User.SystemAccount, + UpdatedAt: now, + } + _ = copier.Copy(&data, &req) + _, err = session.Insert(&data) + if err != nil { + return + } + return +} + +func (m *PushRecordSvc) Update(session *xorm.Session, req request.UpdatePushRecord) error { + now := jsontime.Now() + data := entity.PushRecord{ + UpdatedBy: m.User.SystemAccount, + UpdatedAt: now, + } + _ = copier.Copy(&data, &req) + _, err := session.ID(req.Id).Update(&data) + if err != nil { + return err + } + return nil +} + +func (m *PushRecordSvc) GetDataById(req request.DetailPushRecord) (resp response.PushRecordItem, err error) { + now := jsontime.Time{} + data := response.PushRecordItem{ + PushRecord: entity.PushRecord{ + Id: 1, + AlertRulesId: "83343ef6-4a99-47bd-abb4-bcff52feb2ec", + RiskLevel: 1, + SystemAccount: "xiaowang", + PushTime: now, + PushType: 1, + Status: 1, + CreatedBy: "admin", + CreatedAt: now, + UpdatedBy: "admin", + UpdatedAt: now, + }, + NotifyMethod: []string{"dingtalk", "sms"}, + } + resp = data + return +} + +func (m *PushRecordSvc) List(req request.ListPushRecord) (resp response.PushRecordList, err error) { + now := jsontime.Time{} + data1 := response.PushRecordItem{ + PushRecord: entity.PushRecord{ + Id: 1, + AlertRulesId: "83343ef6-4a99-47bd-abb4-bcff52feb2ec", + RiskLevel: 1, + SystemAccount: "xiaowang", + PushTime: now, + PushType: 1, + Status: 1, + CreatedBy: "admin", + CreatedAt: now, + UpdatedBy: "admin", + UpdatedAt: now, + }, + NotifyMethod: []string{"dingtalk", "sms"}, + } + data2 := response.PushRecordItem{ + PushRecord: entity.PushRecord{ + Id: 1, + AlertRulesId: "83343ef6-4a99-47bd-abb4-bcff52feb2ec", + RiskLevel: 2, + SystemAccount: "xiaozhang", + PushTime: now, + PushType: 2, + Status: 2, + CreatedBy: "admin", + CreatedAt: now, + UpdatedBy: "admin", + UpdatedAt: now, + }, + NotifyMethod: []string{"dingtalk", "sms"}, + } + resp.List = append(resp.List, data1, data2) + resp.TotalCount = int64(len(resp.List)) + return +} + +func (m *PushRecordSvc) Delete(ids []string) (err error) { + db, err := client.GetDbClient() + if err != nil { + return + } + _, err = db.NewSession().In("id", ids).Delete(new(entity.PushRecord)) + return +} -- 2.26.0