feat: implement V2 alert center and scoped alert governance
This commit is contained in:
@@ -5,13 +5,16 @@ import (
|
||||
"fmt"
|
||||
"github.com/go-kratos/kratos/v3/errors"
|
||||
"kra/internal/biz/system"
|
||||
"math"
|
||||
"math/big"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf16"
|
||||
)
|
||||
|
||||
var alertDecimal = regexp.MustCompile(`^[0-9]{1,12}(\.[0-9]{1,2})?$`)
|
||||
|
||||
var (
|
||||
ErrAlertRuleNotFound = errors.NotFound("SW_ALERT_RULE_NOT_FOUND", "规则不存在")
|
||||
ErrAlertRuleScope = errors.Forbidden("SW_ALERT_RULE_SCOPE", "无权限操作该规则")
|
||||
@@ -53,6 +56,9 @@ func LevelOneOwner(actor system.Actor, a *Account) LevelOneScope {
|
||||
}
|
||||
|
||||
type AlertRule struct {
|
||||
ConditionMode int
|
||||
AmountDirection int
|
||||
AmountThreshold *string
|
||||
ID uint64
|
||||
Name, Type string
|
||||
Metric int
|
||||
@@ -134,6 +140,48 @@ func validateAlertRule(r *AlertRule) error {
|
||||
return ErrAlertRuleInput
|
||||
}
|
||||
r.Type = strings.TrimSpace(r.Type)
|
||||
if r.ConditionMode == 0 {
|
||||
r.ConditionMode = 1
|
||||
}
|
||||
if r.ConditionMode != 1 && r.ConditionMode != 2 {
|
||||
return ErrAlertRuleInput
|
||||
}
|
||||
// 非当前指标的输入不参与业务配置,避免隐藏字段绕过校验写入存储。
|
||||
if r.Metric == 3 {
|
||||
r.StatPeriodDays = nil
|
||||
r.MomDirection = nil
|
||||
r.MomThreshold = nil
|
||||
} else {
|
||||
r.UnloginDays = nil
|
||||
}
|
||||
if r.ConditionMode == 2 {
|
||||
r.MomDirection = nil
|
||||
r.MomThreshold = nil
|
||||
} else {
|
||||
r.AmountThreshold = nil
|
||||
r.AmountDirection = 1
|
||||
}
|
||||
if r.ConditionMode == 2 {
|
||||
if r.Metric != 2 || r.StatPeriodDays == nil || *r.StatPeriodDays <= 0 {
|
||||
return ErrAlertRuleInput
|
||||
}
|
||||
if r.AmountDirection != 1 && r.AmountDirection != 2 {
|
||||
return ErrAlertRuleInput
|
||||
}
|
||||
if r.AmountThreshold == nil || !alertDecimal.MatchString(*r.AmountThreshold) {
|
||||
return ErrAlertRuleInput
|
||||
}
|
||||
n, ok := new(big.Rat).SetString(*r.AmountThreshold)
|
||||
if !ok || n.Sign() <= 0 || n.Cmp(big.NewRat(99999999999999, 100)) > 0 || !new(big.Rat).Mul(n, big.NewRat(100, 1)).IsInt() {
|
||||
return ErrAlertRuleInput
|
||||
}
|
||||
amount := n.FloatString(2)
|
||||
r.AmountThreshold = &amount
|
||||
if r.Type == "" || len(utf16.Encode([]rune(r.Type))) > 50 {
|
||||
return ErrAlertRuleInput
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if r.Type == "" || len(utf16.Encode([]rune(r.Type))) > 50 {
|
||||
return ErrAlertRuleInput
|
||||
}
|
||||
@@ -145,11 +193,11 @@ func validateAlertRule(r *AlertRule) error {
|
||||
if r.MomDirection == nil || (*r.MomDirection != 1 && *r.MomDirection != 2) {
|
||||
return ErrAlertRuleDirection
|
||||
}
|
||||
if r.MomThreshold == nil {
|
||||
if r.MomThreshold == nil || !alertDecimal.MatchString(*r.MomThreshold) {
|
||||
return ErrAlertRuleThreshold
|
||||
}
|
||||
n, err := strconv.ParseFloat(*r.MomThreshold, 64)
|
||||
if err != nil || math.IsNaN(n) || math.IsInf(n, 0) || n <= 0 || n > 9999 {
|
||||
n, ok := new(big.Rat).SetString(*r.MomThreshold)
|
||||
if !ok || n.Sign() <= 0 || n.Cmp(big.NewRat(9999, 1)) > 0 {
|
||||
return ErrAlertRuleThreshold
|
||||
}
|
||||
case 3:
|
||||
@@ -269,6 +317,13 @@ func optionalInt(value *int) string {
|
||||
return strconv.Itoa(*value)
|
||||
}
|
||||
func (r *AlertRule) Description() string {
|
||||
if r.Metric == 2 && r.ConditionMode == 2 && r.AmountThreshold != nil {
|
||||
direction := "满"
|
||||
if r.AmountDirection == 2 {
|
||||
direction = "未满"
|
||||
}
|
||||
return fmt.Sprintf("渠道近 %s 天充值金额%s %s 元时触发预警(汇总子渠道)", optionalInt(r.StatPeriodDays), direction, *r.AmountThreshold)
|
||||
}
|
||||
if r.Metric == 3 {
|
||||
return "渠道连续 " + optionalInt(r.UnloginDays) + " 天未登录时触发预警"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user