Commit 39fbf027 authored by 李科's avatar 李科

feat: 预警详情推送记录

parent 79818049
......@@ -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:失败
......
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
}
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"`
}
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)
}
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)
}
}
......@@ -61,6 +61,8 @@ func Load(r *gin.Engine, middleware ...gin.HandlerFunc) {
InitPrometheusRouter(r)
// 初始化预警总览配置路由
InitAlertOverviewRouter(r)
// 初始化推送记录配置路由
InitPushRecordRouter(r)
// 初始化工单管理路由
InitWorkOrderRouter(r)
}
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
}
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