feat: implement V2 alert center and scoped alert governance

This commit is contained in:
Yvan
2026-11-05 20:08:29 +08:00
parent f8e26c5baf
commit 999c9a6315
57 changed files with 2711 additions and 216 deletions
+43 -2
View File
@@ -19,6 +19,13 @@ var (
)
type AlertRecord struct {
OwnerScope *UserScope
// 检测写入策略,仅供任务与仓储使用,不向 HTTP 返回。
GapDays int
IndependentRule bool
TotalPlayers *int64
TotalPay, WarningColor *string
Lost *bool
ID, RuleID, TargetPromoteID uint64
PlatformCode, PlatformName, RuleName, RuleType string
MonitorMetric *int
@@ -47,6 +54,8 @@ type AlertRecordScope struct {
}
type AlertRecordFilter struct {
AlertContent string
Lost *bool
PlatformCodes []string
TargetPromoteID *uint64
PlatformBusierID *uint64
@@ -69,12 +78,22 @@ type AlertRecordDetail struct {
}
type AlertRecordRepo interface {
Platforms(context.Context, AlertRecordScope) ([]string, error)
Ignore(context.Context, AlertRecordScope, []uint64, *AlertLog) error
List(context.Context, AlertRecordScope, AlertRecordFilter, ...ListOption) (*AlertRecordPage, error)
Detail(context.Context, AlertRecordScope, uint64) (*AlertRecordDetail, error)
// 在同一事务和行锁内校验当前记录、执行业务回调、写入状态与日志。
WithRecord(context.Context, AlertRecordScope, uint64, func(*AlertRecord) ([]*AlertLog, error)) error
}
func (uc *AlertRecordUsecase) Platforms(ctx context.Context) ([]string, error) {
scope, _, err := uc.scope(ctx, nil)
if err != nil {
return nil, err
}
return uc.repo.Platforms(ctx, scope)
}
type AlertAction string
const (
@@ -83,6 +102,7 @@ const (
AlertPendingAudit = 3
AlertAuditRejected = 4
AlertProcessed = 5
AlertIgnored = 6
)
const (
@@ -205,13 +225,34 @@ func (uc *AlertRecordUsecase) AddNote(ctx context.Context, id uint64, note strin
if err != nil {
return err
}
return uc.repo.WithRecord(ctx, scope, id, func(_ *AlertRecord) ([]*AlertLog, error) {
return uc.repo.WithRecord(ctx, scope, id, func(record *AlertRecord) ([]*AlertLog, error) {
if record.Status == AlertProcessed || record.Status == AlertIgnored {
return nil, ErrAlertRecordChanged
}
return []*AlertLog{{AlertID: id, OperatorID: account.ID, OperatorName: account.Username, LogType: 2, NoteContent: note, AttachmentIDs: attachments}}, nil
})
}
func AlertStatusLabel(status int) string {
return map[int]string{1: "未处理", 2: "处理中", 3: "待审核", 4: "审核驳回", 5: "已处理"}[status]
return map[int]string{1: "未处理", 2: "处理中", 3: "待审核", 4: "审核驳回", 5: "已处理", 6: "已忽略"}[status]
}
func (uc *AlertRecordUsecase) Ignore(ctx context.Context, ids []uint64) error {
actor, account, err := uc.accounts.actor(ctx)
if err != nil {
return err
}
if actor.AuthorityID != 888 && account.Level != 1 {
return ErrAlertGovernanceLevel
}
if len(ids) == 0 || len(ids) > 500 {
return ErrAlertRecordInput
}
scope, _, err := uc.scope(ctx, nil)
if err != nil {
return err
}
return uc.repo.Ignore(ctx, scope, ids, &AlertLog{OperatorID: account.ID, OperatorName: account.Username, LogType: 1, ActionDetail: "ignore"})
}
func AlertLogTypeLabel(logType int) string {