Commit 4cbae66d authored by 李科's avatar 李科

feat: 预警分类

parent a66408fb
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"
}
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
}
......@@ -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
}
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"`
}
......@@ -8,6 +8,6 @@ type MetricConfigItem struct {
}
type UnitsList struct {
TotalCount int `json:"total_count"`
TotalCount int64 `json:"total_count"`
List []MetricConfigItem `json:"list"`
}
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)
}
......@@ -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)
}
......@@ -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)
}
}
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
}
......@@ -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 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment