From 6d17138f56e50348835b1fdb8c0c5dba27a87eb0 Mon Sep 17 00:00:00 2001 From: like Date: Tue, 18 Jul 2023 16:05:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E9=A2=84=E8=AD=A6?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E5=AD=97=E6=AE=B5=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bean/entity/{alert_list.go => alert.go} | 7 +- .../vo/request/{alert_list.go => alert.go} | 12 +- .../vo/response/{alert_list.go => alert.go} | 26 ++-- src/controller/{alert_list.go => alert.go} | 24 ++-- src/router/alertlistrouter.go.go | 20 --- src/router/alertoverviewrouter.go.go | 9 +- src/router/alertrouter.go.go | 20 +++ src/router/workorderrouter.go | 6 +- src/service/alert_list.go | 30 ++--- src/service/alert_webhook.go | 117 ++++++++++++++++++ 10 files changed, 197 insertions(+), 74 deletions(-) rename src/bean/entity/{alert_list.go => alert.go} (97%) rename src/bean/vo/request/{alert_list.go => alert.go} (93%) rename src/bean/vo/response/{alert_list.go => alert.go} (65%) rename src/controller/{alert_list.go => alert.go} (87%) delete mode 100644 src/router/alertlistrouter.go.go create mode 100644 src/router/alertrouter.go.go create mode 100644 src/service/alert_webhook.go diff --git a/src/bean/entity/alert_list.go b/src/bean/entity/alert.go similarity index 97% rename from src/bean/entity/alert_list.go rename to src/bean/entity/alert.go index 94f0ae2..5f6617d 100644 --- a/src/bean/entity/alert_list.go +++ b/src/bean/entity/alert.go @@ -4,7 +4,8 @@ import ( "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/jsontime" ) -type AlertList struct { +// Alert 预警表 +type Alert struct { Id int `json:"id"` // 预警列表ID,主键,自增长 AlertPoint string `json:"alert_point"` // 预警点 AlertRulesId string `json:"alert_rules_id"` // 告警规则id @@ -42,6 +43,6 @@ type DisposedList struct { DisposalTime jsontime.Time `json:"disposal_time"` // 处置时间(工单管理,结果反馈) } -func (a *AlertList) TableName() string { - return "alert_list" +func (a *Alert) TableName() string { + return "alert" } diff --git a/src/bean/vo/request/alert_list.go b/src/bean/vo/request/alert.go similarity index 93% rename from src/bean/vo/request/alert_list.go rename to src/bean/vo/request/alert.go index 0db4327..8bf5146 100644 --- a/src/bean/vo/request/alert_list.go +++ b/src/bean/vo/request/alert.go @@ -4,31 +4,31 @@ import ( "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" ) -type DetailAlertList struct { +type DetailAlert struct { Id int `json:"id" form:"id" binding:"required"` } -type UpdateAlertList struct { +type UpdateAlert 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 BatchPushAlertList struct { +type BatchPushAlert struct { Id string `json:"id" form:"id"` Ids []string `json:"ids" form:"ids" binding:"required_without=Id"` // 预警ids NotifyMethod []string `json:"notify_method" form:"notify_method" binding:"max=2,dive,oneof=dingtalk sms"` // 预警通知方式 dingtalk sms NotifyRecipients []entity.NotifyRecipients `json:"notify_recipients" form:"notify_recipients" binding:"dive"` // 预警推送用户 } -type BatchCloseAlertList struct { +type BatchCloseAlert struct { Id string `json:"id" form:"id"` Ids []string `json:"ids" form:"ids" binding:"required_without=Id"` // 预警ids CloseRemark string `json:"close_remark" form:"close_remark" binding:"required"` // 关闭备注 DeferPush int `json:"defer_push" form:"defer_push" binding:"omitempty,oneof=0 1"` // 延迟三天推送: 0:否 1:是 三天内将不再自动推送该告警信息给处置人员,可手动推送,但告警数据依然会出现 } -type ListAlertList struct { +type ListAlert 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:已关闭 @@ -38,7 +38,7 @@ type ListAlertList struct { Pagination } -type DisposeAlertList struct { +type DisposeAlert struct { Id int `json:"id" form:"id" binding:"required"` Status int `json:"status" form:"status" binding:"oneof=1 2"` // 状态,1:已恢复 2:未恢复 3:已关闭 DisposalContent string `json:"disposal_content" binding:"required"` // 处置内容(工单管理,结果反馈) diff --git a/src/bean/vo/response/alert_list.go b/src/bean/vo/response/alert.go similarity index 65% rename from src/bean/vo/response/alert_list.go rename to src/bean/vo/response/alert.go index 0d335ec..994ca2a 100644 --- a/src/bean/vo/response/alert_list.go +++ b/src/bean/vo/response/alert.go @@ -4,15 +4,15 @@ 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"` - PushRecords []PushRecordItem `json:"push_records"` +type AlertItem struct { + entity.Alert `xorm:"extends"` + AlertCondition entity.AlertCondition `json:"alert_condition" xorm:"alert_condition"` + PushRecords []PushRecordItem `json:"push_records"` } -type AlertListList struct { - TotalCount int64 `json:"total_count"` - List []AlertListItem `json:"list"` +type AlertList struct { + TotalCount int64 `json:"total_count"` + List []AlertItem `json:"list"` } type CatIndices struct { @@ -46,12 +46,12 @@ type Total struct { } type SubHits struct { - Index string `json:"_index"` - Type string `json:"_type"` - ID string `json:"_id"` - Score float64 `json:"_score"` - Ignored []string `json:"_ignored"` - Source AlertListItem `json:"_source"` + Index string `json:"_index"` + Type string `json:"_type"` + ID string `json:"_id"` + Score float64 `json:"_score"` + Ignored []string `json:"_ignored"` + Source AlertItem `json:"_source"` } type Hits struct { Total Total `json:"total"` diff --git a/src/controller/alert_list.go b/src/controller/alert.go similarity index 87% rename from src/controller/alert_list.go rename to src/controller/alert.go index 8fd8a4b..e2eb268 100644 --- a/src/controller/alert_list.go +++ b/src/controller/alert.go @@ -9,8 +9,8 @@ import ( "gitlab.wodcloud.com/smart-operation/so-operation-api/src/service" ) -func UpdateAlertList(c *gin.Context) { - var req request.UpdateAlertList +func UpdateAlert(c *gin.Context) { + var req request.UpdateAlert if err := c.ShouldBind(&req); err != nil { SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) return @@ -30,8 +30,8 @@ func UpdateAlertList(c *gin.Context) { SendJsonResponse(c, resp.OK, nil) } -func BatchPushAlertList(c *gin.Context) { - var req request.BatchPushAlertList +func BatchPushAlert(c *gin.Context) { + var req request.BatchPushAlert if err := c.ShouldBind(&req); err != nil { SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) return @@ -51,8 +51,8 @@ func BatchPushAlertList(c *gin.Context) { SendJsonResponse(c, resp.OK, nil) } -func BatchCloseAlertList(c *gin.Context) { - var req request.BatchCloseAlertList +func BatchCloseAlert(c *gin.Context) { + var req request.BatchCloseAlert if err := c.ShouldBind(&req); err != nil { SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) return @@ -72,8 +72,8 @@ func BatchCloseAlertList(c *gin.Context) { SendJsonResponse(c, resp.OK, nil) } -func DetailAlertList(c *gin.Context) { - var req request.DetailAlertList +func DetailAlert(c *gin.Context) { + var req request.DetailAlert if err := c.ShouldBind(&req); err != nil { SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) return @@ -88,8 +88,8 @@ func DetailAlertList(c *gin.Context) { SendJsonResponse(c, resp.OK, data) } -func ListAlertList(c *gin.Context) { - var req request.ListAlertList +func ListAlert(c *gin.Context) { + var req request.ListAlert if err := c.ShouldBind(&req); err != nil { SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) return @@ -103,8 +103,8 @@ func ListAlertList(c *gin.Context) { SendJsonResponse(c, resp.OK, data) } -func DisposeAlertList(c *gin.Context) { - var req request.DisposeAlertList +func DisposeAlert(c *gin.Context) { + var req request.DisposeAlert if err := c.ShouldBind(&req); err != nil { SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) return diff --git a/src/router/alertlistrouter.go.go b/src/router/alertlistrouter.go.go deleted file mode 100644 index da144bf..0000000 --- a/src/router/alertlistrouter.go.go +++ /dev/null @@ -1,20 +0,0 @@ -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) - group.PUT("", controller.UpdateAlertList) - group.PUT("batch/push", controller.BatchPushAlertList) - group.PUT("batch/close", controller.BatchCloseAlertList) - } -} diff --git a/src/router/alertoverviewrouter.go.go b/src/router/alertoverviewrouter.go.go index c492732..e81d5fa 100644 --- a/src/router/alertoverviewrouter.go.go +++ b/src/router/alertoverviewrouter.go.go @@ -9,8 +9,13 @@ import ( // InitAlertOverviewRouter 初始化预警总览配置路由 func InitAlertOverviewRouter(e *gin.Engine) { - group := e.Group(fmt.Sprintf("%s/alert_overview", conf.Options.Prefix)) + overviewGroup := e.Group(fmt.Sprintf("%s/alert_overview", conf.Options.Prefix)) { - group.GET("", controller.DetailAlertOverview) + overviewGroup.GET("", controller.DetailAlertOverview) + } + screensGroup := e.Group(fmt.Sprintf("%s/screens", conf.Options.Prefix)) + { + screensGroup.GET("", controller.DetailAlert) + screensGroup.GET("list", controller.ListAlert) } } diff --git a/src/router/alertrouter.go.go b/src/router/alertrouter.go.go new file mode 100644 index 0000000..72195be --- /dev/null +++ b/src/router/alertrouter.go.go @@ -0,0 +1,20 @@ +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", conf.Options.Prefix)) + { + group.GET("", controller.DetailAlert) + group.GET("list", controller.ListAlert) + group.PUT("", controller.UpdateAlert) + group.PUT("batch/push", controller.BatchPushAlert) + group.PUT("batch/close", controller.BatchCloseAlert) + } +} diff --git a/src/router/workorderrouter.go b/src/router/workorderrouter.go index 60e6dba..0c27b12 100644 --- a/src/router/workorderrouter.go +++ b/src/router/workorderrouter.go @@ -14,9 +14,9 @@ func InitWorkOrderRouter(e *gin.Engine) { //预警工单管理 alert := so.Group("/alert", header.SetContext) { - alert.GET("", controller.DetailAlertList) // 详情 - alert.GET("/list", controller.ListAlertList) // 列表 - alert.PUT("/dispose", controller.DisposeAlertList) // 处置反馈 + alert.GET("", controller.DetailAlert) // 详情 + alert.GET("/list", controller.ListAlert) // 列表 + alert.PUT("/dispose", controller.DisposeAlert) // 处置反馈 } //业务工单管理 diff --git a/src/service/alert_list.go b/src/service/alert_list.go index 6059e87..7e8c386 100644 --- a/src/service/alert_list.go +++ b/src/service/alert_list.go @@ -238,7 +238,7 @@ func (a *AlertListSvc) Indices() (indices []string, err error) { return } -func (a *AlertListSvc) IndexSearch(req request.ListAlertList) (resp response.AlertListList, err error) { +func (a *AlertListSvc) IndexSearch(req request.ListAlert) (resp response.AlertList, err error) { var ( sources response.OpenSearchSource ) @@ -312,9 +312,9 @@ func (a *AlertListSvc) IndexSearch(req request.ListAlertList) (resp response.Ale return } -func (a *AlertListSvc) Update(session *xorm.Session, req request.UpdateAlertList) error { +func (a *AlertListSvc) Update(session *xorm.Session, req request.UpdateAlert) error { now := jsontime.Now() - data := entity.AlertList{ + data := entity.Alert{ UpdatedBy: a.User.SystemAccount, UpdatedAt: now, } @@ -326,7 +326,7 @@ func (a *AlertListSvc) Update(session *xorm.Session, req request.UpdateAlertList return nil } -func (a *AlertListSvc) BatchPushAlertList(session *xorm.Session, req request.BatchPushAlertList) error { +func (a *AlertListSvc) BatchPushAlertList(session *xorm.Session, req request.BatchPushAlert) error { now := jsontime.Now() _ = now // TODO 批量推送用户告警 @@ -334,7 +334,7 @@ func (a *AlertListSvc) BatchPushAlertList(session *xorm.Session, req request.Bat return nil } -func (a *AlertListSvc) BatchCloseAlertList(session *xorm.Session, req request.BatchCloseAlertList) error { +func (a *AlertListSvc) BatchCloseAlertList(session *xorm.Session, req request.BatchCloseAlert) error { now := jsontime.Now() _ = now // TODO 批量推送用户告警 @@ -342,10 +342,10 @@ func (a *AlertListSvc) BatchCloseAlertList(session *xorm.Session, req request.Ba return nil } -func (a *AlertListSvc) GetDataById(req request.DetailAlertList) (resp response.AlertListItem, err error) { +func (a *AlertListSvc) GetDataById(req request.DetailAlert) (resp response.AlertItem, err error) { /*now := jsontime.Now() - data := response.AlertListItem{ - AlertList: entity.AlertList{ + data := response.AlertItem{ + Alert: entity.Alert{ Id: 123, AlertPoint: "容器云/kube-apiserver", AlertRulesId: "762ed641-6c0e-4c39-bf7c-7463abb0f8a2", @@ -423,17 +423,17 @@ func (a *AlertListSvc) GetDataById(req request.DetailAlertList) (resp response.A }, }, }*/ - list, err := a.IndexSearch(request.ListAlertList{Id: req.Id}) + list, err := a.IndexSearch(request.ListAlert{Id: req.Id}) if len(list.List) > 0 { resp = list.List[0] } return } -func (a *AlertListSvc) List(req request.ListAlertList) (resp response.AlertListList, err error) { +func (a *AlertListSvc) List(req request.ListAlert) (resp response.AlertList, err error) { /*now := jsontime.Now() - data1 := response.AlertListItem{ - AlertList: entity.AlertList{ + data1 := response.AlertItem{ + Alert: entity.Alert{ Id: 123, AlertPoint: "容器云/kube-apiserver", AlertRulesId: "762ed641-6c0e-4c39-bf7c-7463abb0f8a2", @@ -517,8 +517,8 @@ func (a *AlertListSvc) List(req request.ListAlertList) (resp response.AlertListL }, }, } - data2 := response.AlertListItem{ - AlertList: entity.AlertList{ + data2 := response.AlertItem{ + Alert: entity.Alert{ Id: 125, AlertPoint: "容器云/apaas", AlertRulesId: "762ed641-6c0e-4c39-bf7c-7463abb0f8a2", @@ -605,7 +605,7 @@ func (a *AlertListSvc) List(req request.ListAlertList) (resp response.AlertListL return } -func (a *AlertListSvc) DisposeAlertList(session *xorm.Session, req request.DisposeAlertList) error { +func (a *AlertListSvc) DisposeAlertList(session *xorm.Session, req request.DisposeAlert) error { now := jsontime.Now() _ = now // TODO 我的预警工单处置 diff --git a/src/service/alert_webhook.go b/src/service/alert_webhook.go new file mode 100644 index 0000000..7ec890b --- /dev/null +++ b/src/service/alert_webhook.go @@ -0,0 +1,117 @@ +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 AlertWebhookSvc struct { + User entity.SystemUserInfo +} + +// TODO AlertManager 回调接口 +func (a *AlertWebhookSvc) WebHook(session *xorm.Session, req request.AddPushRecord) (id string, err error) { + // 报警消息 + now := jsontime.Now() + data := entity.PushRecord{ + CreatedBy: a.User.SystemAccount, + CreatedAt: now, + UpdatedBy: a.User.SystemAccount, + UpdatedAt: now, + } + _ = copier.Copy(&data, &req) + _, err = session.Insert(&data) + if err != nil { + return + } + return +} + +func (a *AlertWebhookSvc) Update(session *xorm.Session, req request.UpdatePushRecord) error { + now := jsontime.Now() + data := entity.PushRecord{ + UpdatedBy: a.User.SystemAccount, + UpdatedAt: now, + } + _ = copier.Copy(&data, &req) + _, err := session.ID(req.Id).Update(&data) + if err != nil { + return err + } + return nil +} + +func (a *AlertWebhookSvc) 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 (a *AlertWebhookSvc) 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 (a *AlertWebhookSvc) 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