diff --git a/src/bean/entity/system_user.go b/src/bean/entity/system_user.go index 6b70e552226bdddbe5659a2d319331088708f61a..8fdd6e44e85851f9cb083a4b8aec57f9d5a86f32 100644 --- a/src/bean/entity/system_user.go +++ b/src/bean/entity/system_user.go @@ -25,9 +25,9 @@ type SystemUser struct { } type SystemUserInfo struct { - Id int `json:"id" xorm:"pk autoincr" ` // id + Id int `json:"id" xorm:"pk autoincr"` // id Name string `json:"name" ` // 名称 - SystemAccount string `json:"system_account" ` // 账号 + SystemAccount string `json:"system_account"` // 账号 OrganizationId string `json:"organization_id" xorm:"organization_id"` // 所属组织 Password string `json:"password,omitempty"` // 密码 State int `json:"state" xorm:"state"` // 状态0禁用1启用 diff --git a/src/bean/vo/request/alert.go b/src/bean/vo/request/alert.go index c8f8cc5b317a62444dd1853f73b3381c08ba36a7..0c67cb6f6fa2330755a2c5c744bd4a11455e0207 100644 --- a/src/bean/vo/request/alert.go +++ b/src/bean/vo/request/alert.go @@ -9,14 +9,15 @@ type DetailAlert struct { } type ListAlert struct { - Id int `json:"id" form:"id"` - Ids []int `json:"ids" form:"ids"` // 预警ids - AlertRulesId string `json:"alert_rules_id" form:"alert_rules_id"` // 告警规则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"` + Id int `json:"id" form:"id"` + Ids []int `json:"ids" form:"ids"` // 预警ids + AlertRulesId string `json:"alert_rules_id" form:"alert_rules_id"` // 告警规则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"` // 预警点/分类/指标 + SystemAccount string `json:"system_account" form:"system_account"` // 预警推送用户 + 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"` Pagination } diff --git a/src/controller/alert.go b/src/controller/alert.go index 418a9c196d309c7736c22daa94504bf756f8fae3..20a4d8dd902d09f2408b870006184b49f176196d 100644 --- a/src/controller/alert.go +++ b/src/controller/alert.go @@ -31,6 +31,7 @@ func ListAlert(c *gin.Context) { return } svc := service.AlertSvc{User: header.GetUser(c)} + // TODO 我的预警工单 data, err := svc.List(req) if err != nil { SendJsonResponse(c, resp.FAIL.WithError(err), nil) diff --git a/src/controller/work_order_manage.go b/src/controller/work_order_manage.go index 0fbb35a566d603273d9b307838b95f7e66c46563..df57112c1098b1695e59ddf8101379e101de7e8c 100644 --- a/src/controller/work_order_manage.go +++ b/src/controller/work_order_manage.go @@ -243,3 +243,25 @@ func ListWorkOrderMe(c *gin.Context) { } SendJsonPageResponse(c, resp.OK, list, total) } + +// WorkOrderListAlert 我的预警工单 +func WorkOrderListAlert(c *gin.Context) { + var req request.ListAlert + if err := c.ShouldBind(&req); err != nil { + SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) + return + } + user := header.GetUser(c) + req.SystemAccount = user.SystemAccount + if user.SystemAccount == "" { + SendJsonResponse(c, resp.FAIL.WithError(errors.New("system_account cannot be empty")), nil) + return + } + svc := service.AlertSvc{User: user} + 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/alertrouter.go.go b/src/router/alertrouter.go.go index 72195be3f884b99af372bf50cdbb5f59cbf1ff1b..a3900422f659e094a7c9e99a558b9e9797aaddcd 100644 --- a/src/router/alertrouter.go.go +++ b/src/router/alertrouter.go.go @@ -5,11 +5,12 @@ import ( "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" + "gitlab.wodcloud.com/smart-operation/so-operation-api/src/router/middleware/header" ) // InitAlertListRouter 初始化预警列表配置路由 func InitAlertListRouter(e *gin.Engine) { - group := e.Group(fmt.Sprintf("%s/alert", conf.Options.Prefix)) + group := e.Group(fmt.Sprintf("%s/alert", conf.Options.Prefix), header.SetContext) { group.GET("", controller.DetailAlert) group.GET("list", controller.ListAlert) diff --git a/src/router/alertrulesrouter.go.go b/src/router/alertrulesrouter.go.go index 2a262a8b541e3a5dd2f982c10c8072b5e3999156..89b82b10cdb2c90ae16427fb31c70ed7b528b2e3 100644 --- a/src/router/alertrulesrouter.go.go +++ b/src/router/alertrulesrouter.go.go @@ -5,11 +5,12 @@ import ( "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" + "gitlab.wodcloud.com/smart-operation/so-operation-api/src/router/middleware/header" ) // InitAlertRulesRouter 初始化预警规则配置路由 func InitAlertRulesRouter(e *gin.Engine) { - group := e.Group(fmt.Sprintf("%s/alert_rules", conf.Options.Prefix)) + group := e.Group(fmt.Sprintf("%s/alert_rules", conf.Options.Prefix), header.SetContext) { group.POST("", controller.AddAlertRules) group.PUT("", controller.UpdateAlertRules) diff --git a/src/router/alertwebhookrouter.go.go b/src/router/alertwebhookrouter.go.go index bb884f76ec80ba12d25ff72a6cc145da9196b641..09e380a0754d9f7cc5e8690c62e5b1d7ddfbddd5 100644 --- a/src/router/alertwebhookrouter.go.go +++ b/src/router/alertwebhookrouter.go.go @@ -5,11 +5,12 @@ import ( "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" + "gitlab.wodcloud.com/smart-operation/so-operation-api/src/router/middleware/header" ) // InitAlertWebhookRouter 初始化预警回调路由 func InitAlertWebhookRouter(e *gin.Engine) { - group := e.Group(fmt.Sprintf("%s/alert_webhook", conf.Options.Prefix)) + group := e.Group(fmt.Sprintf("%s/alert_webhook", conf.Options.Prefix), header.SetContext) { group.POST("", controller.AlertWebhook) } diff --git a/src/router/metricconfigrouter.go b/src/router/metricconfigrouter.go index 133ca691a24d29d4cb3075175bbe4f1c7e70a178..4367b10f65ccc0928fde1cbe6994ed7a4a52a36c 100644 --- a/src/router/metricconfigrouter.go +++ b/src/router/metricconfigrouter.go @@ -5,11 +5,12 @@ import ( "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" + "gitlab.wodcloud.com/smart-operation/so-operation-api/src/router/middleware/header" ) // InitMetricConfigRouter 初始化指标配置路由 func InitMetricConfigRouter(e *gin.Engine) { - mcGroup := e.Group(fmt.Sprintf("%s/metric_config", conf.Options.Prefix)) + mcGroup := e.Group(fmt.Sprintf("%s/metric_config", conf.Options.Prefix), header.SetContext) { mcGroup.POST("", controller.AddMetricConfig) mcGroup.PUT("", controller.UpdateMetricConfig) @@ -18,7 +19,7 @@ func InitMetricConfigRouter(e *gin.Engine) { mcGroup.GET("list", controller.ListMetricConfig) } - acGroup := e.Group(fmt.Sprintf("%s/alert_class", conf.Options.Prefix)) + acGroup := e.Group(fmt.Sprintf("%s/alert_class", conf.Options.Prefix), header.SetContext) { acGroup.POST("", controller.AddAlertClass) acGroup.PUT("move/:direction", controller.MoveAlertClass) diff --git a/src/router/workorderrouter.go b/src/router/workorderrouter.go index ee47a2f3c38a712a25403eaff83146db71b935b9..64392eac8d5704b6c037d879bea6757c9d12bb0e 100644 --- a/src/router/workorderrouter.go +++ b/src/router/workorderrouter.go @@ -10,13 +10,13 @@ import ( // InitWorkOrderRouter 初始化工单路由 func InitWorkOrderRouter(e *gin.Engine) { - so := e.Group(fmt.Sprintf("%s/work_order", conf.Options.Prefix)) + so := e.Group(fmt.Sprintf("%s/work_order", conf.Options.Prefix), header.SetContext) //预警工单管理 alert := so.Group("/alert") { - alert.GET("", controller.DetailAlert) // 详情 - alert.GET("/list", controller.ListAlert) // 列表 - alert.PUT("/dispose", controller.DisposeAlert) // 处置反馈 + alert.GET("", controller.DetailAlert) // 详情 + alert.GET("/list", controller.WorkOrderListAlert) // 列表 + alert.PUT("/dispose", controller.DisposeAlert) // 处置反馈 } //业务工单管理 diff --git a/src/service/alert.go b/src/service/alert.go index 243997dda5cea9772c5e77d7a318d4a5feaef607..9518bac999a6da79990b80137fcb051397f078e4 100644 --- a/src/service/alert.go +++ b/src/service/alert.go @@ -399,6 +399,11 @@ func (a *AlertSvc) DocSearch(req request.ListAlert) (resp response.AlertList, er boolQuery.Filter(elastic.NewRangeQuery("created_at").Gte(req.StartTime).Lte(req.EndTime)) } + if req.SystemAccount != "" { // 匹配我的预警工单 + boolQuery.Must(elastic.NewNestedQuery("push_records", elastic.NewTermQuery("push_records.system_account", req.SystemAccount))) + + } + querySource, _ := boolQuery.Source() b, _ := json.Marshal(querySource) log.Printf("es statements: %s\n", string(b)) @@ -715,17 +720,9 @@ func (a *AlertSvc) BatchPushAlert(req request.BatchPushAlert) (err error) { } for _, alert := range alertList.List { - pushRecords := alert.PushRecords - // TODO 批量推送短信 - //alertRulesSvc := AlertRulesSvc{User: a.User} - /*var alertRulesItem response.AlertRulesItem - alertRulesItem, err = alertRulesSvc.GetDataById(request.DetailAlertRules{Id: alert.AlertRulesId}) - if err != nil { - return - }*/ - + pushRecords := alert.PushRecords // 原始推送记录 var phones []string - for _, v := range req.NotifyRecipients { + for _, v := range req.NotifyRecipients { // +临时推送记录 phones = append(phones, v.Phone) pushRecords = append(pushRecords, entity.PushRecord{ AlertId: alert.Id, @@ -745,7 +742,7 @@ func (a *AlertSvc) BatchPushAlert(req request.BatchPushAlert) (err error) { }) } - for _, method := range req.NotifyMethod { + for _, method := range req.NotifyMethod { // 按照通知方式循环推送 switch method { // dingtalk sms case "sms": // 发送短信 @@ -781,7 +778,7 @@ func (a *AlertSvc) BatchPushAlert(req request.BatchPushAlert) (err error) { } } - for i := 0; i < len(pushRecords); i++ { + for i := 0; i < len(pushRecords); i++ { // id重新序列化 pushRecords[i].Id = i + 1 } err = a.DocUpdate(request.UpdateAlert{ @@ -793,9 +790,6 @@ func (a *AlertSvc) BatchPushAlert(req request.BatchPushAlert) (err error) { return } } - - // TODO 批量推送用户告警 - conf.Logger.Info("batch push", zap.Any("payload", req)) time.Sleep(time.Second) return nil }