Commit 5ce39bee authored by 李科's avatar 李科

fix: 完善预警规则设置列表

parent dfddde9d
......@@ -32,7 +32,7 @@ func (m *AlertRules) TableName() string {
type RulesAlertRange struct {
Name string `json:"name" form:"name" binding:"required"`
Value string `json:"value" form:"value" binding:"required"`
Compare string `json:"compare" form:"compare" binding:"required"`
Compare string `json:"compare" form:"compare" binding:"required,oneof== != =~ !~"`
}
type AlertCondition struct {
......
package request
import "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity"
import (
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity"
)
type AddAlertRules struct {
MetricName string `json:"metric_name" form:"metric_name" binding:"required"` // 预警规则名称(指标名称)
......@@ -55,23 +57,12 @@ type DetailAlertRules struct {
}
type ListAlertRules struct {
// 请输入预警规则名称/预警对象/预警分类/预警指标
Id string `json:"id" form:"id"`
MetricName string `json:"metric_name" form:"metric_name"` // 预警规则名称(指标名称)
DetectionType int `json:"detection_type" form:"detection_type"` // 检测类型 1:静态阈值 2:自定义
ClassId int `json:"class_id" form:"class_id"` // 预警对象id(级联:预警分类/预警对象)
ClassParentName string `json:"class_parent_name" form:"class_parent_name"` // 预警分类名称
ClassName string `json:"class_name" form:"class_name"` // 预警对象名称
MetricConfigName string `json:"metric_config_name" form:"metric_config_name"` // 预警指标名称(detection_type=2时有值)
Expr string `json:"expr" form:"expr"` // 指标表达式(PromQL语句)
AlertCondition string `json:"alert_condition" form:"alert_condition"` // 预警规则 字典值
AlertRuleType string `json:"alert_rule_type" form:"alert_rule_type"` // 预警规则类型 关联字典表
AlertRange string `json:"alert_range" form:"alert_range"` // 预警范围 字典值
Duration int `json:"duration" form:"duration"` // 持续时间
DurationUnit string `json:"duration_unit" form:"duration_unit"` // 持续时间单位 s m h
CheckPeriod int `json:"check_period" form:"check_period"` // 检查周期 单位:分钟
NotifyMethod string `json:"notify_method" form:"notify_method"` // 预警通知方式 all dingtalk sms
NotifyPushCount int `json:"notify_push_count" form:"notify_push_count"` // 消息推送次数
NotifyPushFrequency int `json:"notify_push_frequency" form:"notify_push_frequency"` // 消息推送频率 分钟
IsEnabled int `json:"is_enabled" form:"is_enabled"` // 是否开启 1:是 2:否
NotifyMethod string `json:"notify_method" form:"notify_method" binding:"omitempty,oneof=all dingtalk sms"` // 预警通知方式 all dingtalk sms
IsEnabled int `json:"is_enabled" form:"is_enabled" binding:"omitempty,oneof=0 1 2"` // 是否开启 1:是 2:否
Keyword string `json:"keyword" form:"keyword"` // 预警规则名称(指标名称)
StartTime string `json:"start_time" form:"start_time"`
EndTime string `json:"end_time" form:"end_time"`
Pagination
}
......@@ -4,10 +4,14 @@ import "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity"
type AlertRulesItem struct {
entity.AlertRules `xorm:"extends"`
AlertCondition []entity.AlertCondition `json:"alert_condition" form:"alert_condition" binding:"required"`
AlertRange []entity.RulesAlertRange `json:"alert_range" form:"alert_range" binding:"required,dive"`
NotifyMethod []string `json:"notify_method" form:"notify_method" binding:"max=2,dive,oneof=dingtalk sms"`
NotifyRecipients []entity.NotifyRecipients `json:"notify_recipients" form:"notify_recipients" binding:"dive"`
AlertCondition []entity.AlertCondition `json:"alert_condition" xorm:"alert_condition"`
AlertRange []entity.RulesAlertRange `json:"alert_range" xorm:"alert_range"`
NotifyMethod []string `json:"notify_method" xorm:"notify_method"`
NotifyRecipients []entity.NotifyRecipients `json:"notify_recipients" xorm:"notify_recipients"`
}
func (a *AlertRulesItem) TableName() string {
return "alert_rules"
}
type AlertRulesList struct {
......
......@@ -3,6 +3,7 @@ package service
import (
"github.com/google/uuid"
"github.com/jinzhu/copier"
json "github.com/json-iterator/go"
"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"
......@@ -183,27 +184,40 @@ func (m *AlertRulesSvc) GetDataById(req request.DetailAlertRules) (resp response
return
}
func (m *AlertRulesSvc) List(req request.ListAlertRules) (resp response.MetricConfigList, err error) {
func (m *AlertRulesSvc) List(req request.ListAlertRules) (resp response.AlertRulesList, err error) {
db, err := client.GetDbClient()
if err != nil {
return
}
session := db.NewSession()
defer session.Close()
session.Table(new(entity.AlertRules)).Alias("r").Select("r.*")
session.Join("LEFT", "metric_config mc", "mc.id = r.metric_config_id")
session.Join("LEFT", "alert_class ac", "ac.class_id = r.class_id")
session.Join("LEFT", "alert_class acp", "acp.class_id = ac.parent_id")
if req.Id != "" {
session.Where("id = ?", req.Id)
session.Where("r.id = ?", req.Id)
}
if req.ClassId != 0 {
session.Where("class_id = ?", req.ClassId)
if req.NotifyMethod != "" && req.NotifyMethod != "all" {
session.Where("r.notify_method LIKE ?", "%\""+req.NotifyMethod+"\"%") // ["dingtalk","sms"]
}
if req.MetricName != "" {
session.Where("metric_name LIKE ?", "%"+req.MetricName+"%")
if req.IsEnabled > 0 {
session.Where("r.is_enabled = ?", req.IsEnabled)
}
if req.IsEnabled != 0 {
session.Where("is_enabled = ?", req.IsEnabled)
if req.Keyword != "" {
// 预警对象/预警分类/预警指标
session.Where("r.metric_name LIKE ?", "%"+req.Keyword+"%").
Or("mc.metric_name LIKE ?", "%"+req.Keyword+"%").
Or("ac.class_name LIKE ?", "%"+req.Keyword+"%").
Or("acp.class_name LIKE ?", "%"+req.Keyword+"%")
}
resp.TotalCount, err = session.Limit(req.GetPageSize(), (req.GetPage()-1)*req.GetPageSize()).FindAndCount(&resp.List)
for i := 0; i < len(resp.List); i++ {
_ = json.Unmarshal([]byte(resp.List[i].AlertRules.AlertCondition), &resp.List[i].AlertCondition)
_ = json.Unmarshal([]byte(resp.List[i].AlertRules.AlertRange), &resp.List[i].AlertRange)
_ = json.Unmarshal([]byte(resp.List[i].AlertRules.NotifyMethod), &resp.List[i].NotifyMethod)
_ = json.Unmarshal([]byte(resp.List[i].AlertRules.NotifyRecipients), &resp.List[i].NotifyRecipients)
}
resp.TotalCount, err = session.Table(new(entity.AlertRules)).Limit(req.GetPageSize(), (req.GetPage()-1)*req.GetPageSize()).
OrderBy("id").FindAndCount(&resp.List)
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