diff --git a/src/bean/entity/alert_class.go b/src/bean/entity/alert_class.go new file mode 100644 index 0000000000000000000000000000000000000000..d7726fe7d858c6963d37017b0a5b7136dd09f237 --- /dev/null +++ b/src/bean/entity/alert_class.go @@ -0,0 +1,18 @@ +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'"` // 排序 + 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 { + return "alert_class" +} diff --git a/src/bean/vo/request/alert_class.go b/src/bean/vo/request/alert_class.go new file mode 100644 index 0000000000000000000000000000000000000000..16fd9fec2c9a5b5281d5db9a1410d59771930a65 --- /dev/null +++ b/src/bean/vo/request/alert_class.go @@ -0,0 +1,31 @@ +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"` + SortOrder int `json:"sort_order" form:"sort_order"` +} + +type UpdateAlertClass struct { + ClassId int `json:"class_id" form:"class_id" binding:"required"` + ClassName string `json:"class_name" form:"class_name" binding:"required"` +} + +type DeleteAlertClass struct { + ClassId int `json:"class_id" form:"class_id" binding:"required"` +} + +type MoveAlertClass struct { + ClassId int `json:"class_id" form:"class_id" binding:"required"` + Direction string `json:"direction" form:"direction" binding:"oneof=up down"` +} + +type DetailAlertClass struct { + ClassId int `json:"class_id" form:"class_id" binding:"required"` +} + +type ListAlertClass struct { + ClassId int `json:"class_id" form:"class_id"` + ClassName string `json:"class_name" form:"class_name"` + Pagination +} diff --git a/src/bean/vo/request/metric_config.go b/src/bean/vo/request/metric_config.go index 40f85f194f8234d75d9c13cde24e66c605cd767a..33b3e36e254a18b809866860471d06e3f1a3abab 100644 --- a/src/bean/vo/request/metric_config.go +++ b/src/bean/vo/request/metric_config.go @@ -10,7 +10,7 @@ type AddMetricConfig struct { Duration int `json:"duration" form:"duration"` // 持续时间 DurationUnit string `json:"duration_unit" form:"duration_unit" binding:"required"` // 持续时间单位 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" ` // 是否开启 0:关闭 1:启动 + IsEnabled int `json:"is_enabled" form:"is_enabled" binding:"oneof=0 1 2"` // 是否开启 1:启动 2:停用 AlertRuleType string `json:"alert_rule_type" form:"alert_rule_type" binding:"required"` // 预警规则类型 关联字典表 } @@ -23,14 +23,22 @@ type UpdateMetricConfig struct { Duration int `json:"duration" form:"duration"` // 持续时间 DurationUnit string `json:"duration_unit" form:"duration_unit" binding:"required"` // 持续时间单位 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" ` // 是否开启 0:关闭 1:启动 + IsEnabled int `json:"is_enabled" form:"is_enabled" binding:"oneof=0 1 2"` // 是否开启 1:启动 2:停用 AlertRuleType string `json:"alert_rule_type" form:"alert_rule_type" binding:"required"` // 预警规则类型 关联字典表 } -type GetMetricConfig struct { - Id string `json:"id" form:"id" binding:"required"` // 主键id +type DeleteMetricConfig struct { + Id string `json:"id" form:"id" binding:"required"` } -type DeleteMetricConfig struct { - Id string `json:"id" form:"id" binding:"required"` // 主键id +type DetailMetricConfig struct { + Id string `json:"id" form:"id" binding:"required"` +} + +type ListMetricConfig struct { + Id string `json:"id" form:"id"` + ClassId int `json:"class_id" form:"class_id"` + MetricName string `json:"metric_name" form:"metric_name"` + IsEnabled int `json:"is_enabled" form:"is_enabled" binding:"omitempty,oneof=0 1 2"` + Pagination } diff --git a/src/bean/vo/response/alert_class.go b/src/bean/vo/response/alert_class.go new file mode 100644 index 0000000000000000000000000000000000000000..f9179eaeac8baf87cb9d4267d62bbd95fe1d6017 --- /dev/null +++ b/src/bean/vo/response/alert_class.go @@ -0,0 +1,12 @@ +package response + +import "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" + +type AlertClassItem struct { + entity.AlertClass `xorm:"extends"` +} + +type AlertClassList struct { + TotalCount int64 `json:"total_count"` + List []AlertClassItem `json:"list"` +} diff --git a/src/bean/vo/response/metric_config.go b/src/bean/vo/response/metric_config.go index 88973644fe201618b3f723902d1fb23a82030b45..470084e1f4ad13d2dc065291c90b5db367af2214 100644 --- a/src/bean/vo/response/metric_config.go +++ b/src/bean/vo/response/metric_config.go @@ -8,6 +8,6 @@ type MetricConfigItem struct { } type UnitsList struct { - TotalCount int `json:"total_count"` + TotalCount int64 `json:"total_count"` List []MetricConfigItem `json:"list"` } diff --git a/src/controller/alert_class.go b/src/controller/alert_class.go new file mode 100644 index 0000000000000000000000000000000000000000..f9015d5a32acb6a9c496410900a57173ba67e4c1 --- /dev/null +++ b/src/controller/alert_class.go @@ -0,0 +1,107 @@ +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/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" +) + +// AddAlertClass 新增任务 +func AddAlertClass(c *gin.Context) { + var req request.AddAlertClass + if err := c.ShouldBind(&req); err != nil { + SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) + return + } + + svc := service.AlertClassSvc{User: header.GetUser(c)} + err := svc.Add(req) + if err != nil { + SendJsonResponse(c, resp.FAIL.WithError(err), nil) + return + } + SendJsonResponse(c, resp.OK, nil) +} + +func UpdateAlertClass(c *gin.Context) { + var req request.UpdateAlertClass + if err := c.ShouldBind(&req); err != nil { + SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) + return + } + + svc := service.AlertClassSvc{User: header.GetUser(c)} + err := svc.Update(req) + if err != nil { + SendJsonResponse(c, resp.FAIL.WithError(err), nil) + return + } + SendJsonResponse(c, resp.OK, nil) +} + +func MoveAlertClass(c *gin.Context) { + direction := c.Param("direction") + req := request.MoveAlertClass{ + Direction: direction, + } + if err := c.ShouldBind(&req); err != nil { + SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) + return + } + svc := service.AlertClassSvc{User: header.GetUser(c)} + err := svc.Move(req) + if err != nil { + SendJsonResponse(c, resp.FAIL.WithError(err), nil) + return + } + SendJsonResponse(c, resp.OK, nil) +} + +func DetailAlertClass(c *gin.Context) { + var req request.DetailAlertClass + if err := c.ShouldBind(&req); err != nil { + SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) + return + } + + svc := service.AlertClassSvc{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 ListAlertClass(c *gin.Context) { + var req request.ListAlertClass + if err := c.ShouldBind(&req); err != nil { + SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) + return + } + svc := service.AlertClassSvc{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) +} + +func DeleteAlertClass(c *gin.Context) { + var req request.DeleteAlertClass + if err := c.ShouldBind(&req); err != nil { + SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) + return + } + + svc := service.AlertClassSvc{User: header.GetUser(c)} + err := svc.DeleteDataById(req) + if err != nil { + SendJsonResponse(c, resp.FAIL.WithError(err), nil) + return + } + SendJsonResponse(c, resp.OK, nil) +} diff --git a/src/controller/metric_config.go b/src/controller/metric_config.go index 3f3eb81d7ddcfdbf639ac9c161f1ccd7a126a617..a0a3a2a448be501081bb7d831f2985f283b8779e 100644 --- a/src/controller/metric_config.go +++ b/src/controller/metric_config.go @@ -4,6 +4,7 @@ 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/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" ) @@ -15,10 +16,10 @@ func AddMetricConfig(c *gin.Context) { return } - svc := service.MetricConfigSvc{} + svc := service.MetricConfigSvc{User: header.GetUser(c)} err := svc.Add(req) if err != nil { - SendJsonResponse(c, err, nil) + SendJsonResponse(c, resp.FAIL.WithError(err), nil) return } SendJsonResponse(c, resp.OK, nil) @@ -31,43 +32,59 @@ func UpdateMetricConfig(c *gin.Context) { return } - svc := service.MetricConfigSvc{} + svc := service.MetricConfigSvc{User: header.GetUser(c)} err := svc.Update(req) if err != nil { - SendJsonResponse(c, err, nil) + SendJsonResponse(c, resp.FAIL.WithError(err), nil) + return + } + SendJsonResponse(c, resp.OK, nil) +} + +func DeleteMetricConfig(c *gin.Context) { + var req request.DeleteMetricConfig + if err := c.ShouldBind(&req); err != nil { + SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) + return + } + + svc := service.MetricConfigSvc{User: header.GetUser(c)} + err := svc.DeleteDataById(req) + if err != nil { + SendJsonResponse(c, resp.FAIL.WithError(err), nil) return } SendJsonResponse(c, resp.OK, nil) } func DetailMetricConfig(c *gin.Context) { - var req request.GetMetricConfig + var req request.DetailMetricConfig if err := c.ShouldBind(&req); err != nil { SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) return } - svc := service.MetricConfigSvc{} + svc := service.MetricConfigSvc{User: header.GetUser(c)} data, err := svc.GetDataById(req) if err != nil { - SendJsonResponse(c, err, nil) + SendJsonResponse(c, resp.FAIL.WithError(err), nil) return } SendJsonResponse(c, resp.OK, data) } -func DeleteMetricConfig(c *gin.Context) { - var req request.DeleteMetricConfig +func ListMetricConfig(c *gin.Context) { + var req request.ListMetricConfig if err := c.ShouldBind(&req); err != nil { SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil) return } - svc := service.MetricConfigSvc{} - err := svc.DeleteDataById(req) + svc := service.MetricConfigSvc{User: header.GetUser(c)} + data, err := svc.List(req) if err != nil { - SendJsonResponse(c, err, nil) + SendJsonResponse(c, resp.FAIL.WithError(err), nil) return } - SendJsonResponse(c, resp.OK, nil) + SendJsonResponse(c, resp.OK, data) } diff --git a/src/router/metricconfigrouter.go b/src/router/metricconfigrouter.go index 3d9f0039deaf711c3933f0c254eedec076087f43..ffeeebe748945ccc83719e6983262c18c1ad8b29 100644 --- a/src/router/metricconfigrouter.go +++ b/src/router/metricconfigrouter.go @@ -9,11 +9,22 @@ import ( // InitMetricConfigRouter 初始化指标配置路由 func InitMetricConfigRouter(e *gin.Engine) { - base := e.Group(fmt.Sprintf("%s/metric_config", conf.Options.Prefix)) + mcGroup := e.Group(fmt.Sprintf("%s/metric_config", conf.Options.Prefix)) { - base.POST("", controller.AddMetricConfig) - base.PUT("", controller.UpdateMetricConfig) - base.GET("", controller.DetailMetricConfig) - base.DELETE("", controller.DeleteMetricConfig) + mcGroup.POST("", controller.AddMetricConfig) + mcGroup.PUT("", controller.UpdateMetricConfig) + mcGroup.DELETE("", controller.DeleteMetricConfig) + mcGroup.GET("", controller.DetailMetricConfig) + mcGroup.GET("list", controller.ListMetricConfig) + } + + acGroup := e.Group(fmt.Sprintf("%s/alert_class", conf.Options.Prefix)) + { + acGroup.POST("", controller.AddAlertClass) + acGroup.PUT("move/:direction", controller.MoveAlertClass) + acGroup.PUT("", controller.UpdateAlertClass) + acGroup.DELETE("", controller.DeleteAlertClass) + acGroup.GET("", controller.DetailAlertClass) + acGroup.GET("list", controller.ListAlertClass) } } diff --git a/src/service/alert_class.go b/src/service/alert_class.go new file mode 100644 index 0000000000000000000000000000000000000000..9f360553f4c9f496c822839290fb520af90fd001 --- /dev/null +++ b/src/service/alert_class.go @@ -0,0 +1,177 @@ +package service + +import ( + "errors" + "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" + "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp" +) + +type AlertClassSvc struct { + User entity.SystemUserInfo +} + +func (m *AlertClassSvc) Add(req request.AddAlertClass) (err error) { + db, err := client.GetDbClient() + if err != nil { + err = resp.DbConnectError.WithError(err) + return err + } + now := jsontime.Now() + data := entity.AlertClass{ + CreatedBy: "", + CreatedAt: now, + UpdatedBy: "", + UpdatedAt: now, + } + _ = copier.Copy(&data, &req) + + max, err := m.SortOrderMax(data.ParentId) + if err != nil { + return + } + data.SortOrder = max + 1 + _, err = db.NewSession().Insert(&data) + return nil +} + +func (m *AlertClassSvc) Update(req request.UpdateAlertClass) error { + db, err := client.GetDbClient() + if err != nil { + err = resp.DbConnectError.WithError(err) + return err + } + now := jsontime.Now() + data := entity.AlertClass{ + UpdatedBy: "", + UpdatedAt: now, + } + _ = copier.Copy(&data, &req) + _, err = db.NewSession().Cols("class_name", "updated_by", "updated_at").ID(data.ClassId).Update(&data) + if err != nil { + return err + } + return nil +} + +func (m *AlertClassSvc) Move(req request.MoveAlertClass) (err error) { + db, err := client.GetDbClient() + if err != nil { + err = resp.DbConnectError.WithError(err) + return err + } + now := jsontime.Now() + data := entity.AlertClass{ + UpdatedBy: "", + UpdatedAt: now, + } + _ = copier.Copy(&data, &req) + var list []entity.AlertClass + _, err = db.NewSession().Where("id = ?", req.ClassId).Get(&data) + err = db.NewSession().Where("parent_id = ?", data.ParentId).OrderBy("sort_order asc").Find(&list) + var previousIndex int + var nextIndex int + for i := 0; i < len(list); i++ { + if list[i].ClassId == req.ClassId { + previousIndex = i - 1 + nextIndex = i + 1 + break + } + } + + var ( + mover entity.AlertClass + moved entity.AlertClass + ) + + switch req.Direction { + case "up": + if previousIndex < 0 { + err = errors.New("top data cannot be moved") + return + } + list[previousIndex].SortOrder, list[previousIndex+1].SortOrder = list[previousIndex+1].SortOrder, list[previousIndex].SortOrder + mover, moved = list[previousIndex+1], list[previousIndex] + + case "down": + if nextIndex > len(list)-1 { + err = errors.New("bottom data cannot be moved") + return + } + + list[nextIndex-1].SortOrder, list[nextIndex].SortOrder = list[nextIndex].SortOrder, list[nextIndex-1].SortOrder + mover, moved = list[nextIndex-1], list[nextIndex] + } + mover.UpdatedBy, moved.UpdatedBy = "", "" + mover.UpdatedAt, moved.UpdatedAt = now, now + + session := db.NewSession() + defer session.Close() + err = session.Begin() + _, err = session.Cols("sort_order", "updated_by", "updated_at").ID(mover.ClassId).Update(&mover) + if err != nil { + err = session.Rollback() + return + } + _, err = session.Cols("sort_order", "updated_by", "updated_at").ID(moved.ClassId).Update(&moved) + if err != nil { + err = session.Rollback() + return + } + err = session.Commit() + if err != nil { + return + } + return nil +} + +func (m *AlertClassSvc) GetDataById(req request.DetailAlertClass) (resp response.AlertClassItem, err error) { + db, err := client.GetDbClient() + if err != nil { + return + } + _, err = db.NewSession().Table(resp.TableName()).ID(req.ClassId).Get(&resp) + return +} + +func (m *AlertClassSvc) List(req request.ListAlertClass) (resp response.AlertClassList, err error) { + db, err := client.GetDbClient() + if err != nil { + return + } + session := db.NewSession() + defer session.Close() + if req.ClassId != 0 { + session.Where("class_id = ?", req.ClassId) + } + if req.ClassName != "" { + session.Where("class_name LIKE ?", "%"+req.ClassName+"%") + } + resp.TotalCount, err = session.Limit(req.GetPageSize(), (req.GetPage()-1)*req.GetPageSize()). + OrderBy("sort_order").FindAndCount(&resp.List) + return +} + +func (m *AlertClassSvc) SortOrderMax(parentId int) (max int, err error) { + db, err := client.GetDbClient() + if err != nil { + return + } + _, err = db.NewSession().Table(new(entity.AlertClass)). + Select("max(sort_order)"). + Where("parent_id = ?", parentId).Get(&max) + return +} + +func (m *AlertClassSvc) DeleteDataById(req request.DeleteAlertClass) (err error) { + db, err := client.GetDbClient() + if err != nil { + return + } + _, err = db.NewSession().ID(req.ClassId).Delete(&entity.AlertClass{}) + return +} diff --git a/src/service/metric_config.go b/src/service/metric_config.go index 780addf2ee3899fda20f50778d60a4016f3bd386..26c575e104108b75334d8a7883752c7e301c6185 100644 --- a/src/service/metric_config.go +++ b/src/service/metric_config.go @@ -13,7 +13,7 @@ import ( ) type MetricConfigSvc struct { - User *entity.SystemUser + User entity.SystemUserInfo } func (m *MetricConfigSvc) Add(req request.AddMetricConfig) error { @@ -61,7 +61,7 @@ func (m *MetricConfigSvc) Update(req request.UpdateMetricConfig) error { return nil } -func (m *MetricConfigSvc) GetDataById(req request.GetMetricConfig) (resp response.MetricConfigItem, err error) { +func (m *MetricConfigSvc) GetDataById(req request.DetailMetricConfig) (resp response.MetricConfigItem, err error) { db, err := client.GetDbClient() if err != nil { return @@ -70,6 +70,30 @@ func (m *MetricConfigSvc) GetDataById(req request.GetMetricConfig) (resp respons return } +func (m *MetricConfigSvc) List(req request.ListMetricConfig) (resp response.UnitsList, err error) { + db, err := client.GetDbClient() + if err != nil { + return + } + session := db.NewSession() + defer session.Close() + if req.Id != "" { + session.Where("id = ?", req.Id) + } + if req.ClassId != 0 { + session.Where("class_id = ?", req.ClassId) + } + if req.MetricName != "" { + session.Where("class_name LIKE ?", "%"+req.MetricName+"%") + } + if req.IsEnabled != 0 { + session.Where("is_enabled = ?", req.IsEnabled) + } + resp.TotalCount, err = session.Limit(req.GetPageSize(), (req.GetPage()-1)*req.GetPageSize()). + OrderBy("id").FindAndCount(&resp.List) + return +} + func (m *MetricConfigSvc) DeleteDataById(req request.DeleteMetricConfig) (err error) { db, err := client.GetDbClient() if err != nil {