收口审计治理与套餐任务进展
Constraint: 在线热修前必须保存当前迭代分支全部有效代码进展 Confidence: medium Scope-risk: broad Directive: 后续修改需保持审计事件与业务事务边界一致 Tested: git diff --cached --check Not-tested: 未运行全量测试,提交用于切换分支前保存既有工作
This commit is contained in:
@@ -12,21 +12,32 @@ import (
|
||||
"github.com/bytedance/sonic"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
|
||||
auditinfra "github.com/break/junhong_cmp_fiber/internal/infrastructure/audit"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/middleware"
|
||||
)
|
||||
|
||||
// AlertService 告警服务
|
||||
type AlertService struct {
|
||||
ruleStore *postgres.PollingAlertRuleStore
|
||||
historyStore *postgres.PollingAlertHistoryStore
|
||||
db *gorm.DB
|
||||
auditWriter *auditinfra.Writer
|
||||
redis *redis.Client
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// SetAudit 注入轮询告警规则事务与统一审计 Writer。
|
||||
func (s *AlertService) SetAudit(db *gorm.DB, writer *auditinfra.Writer) {
|
||||
s.db = db
|
||||
s.auditWriter = writer
|
||||
}
|
||||
|
||||
// NewAlertService 创建告警服务实例
|
||||
func NewAlertService(
|
||||
ruleStore *postgres.PollingAlertRuleStore,
|
||||
@@ -44,6 +55,10 @@ func NewAlertService(
|
||||
|
||||
// CreateRule 创建告警规则
|
||||
func (s *AlertService) CreateRule(ctx context.Context, rule *model.PollingAlertRule) error {
|
||||
operatorID := middleware.GetUserIDFromContext(ctx)
|
||||
if operatorID == 0 {
|
||||
return errors.New(errors.CodeUnauthorized, "未授权访问")
|
||||
}
|
||||
// 验证参数
|
||||
if rule.RuleName == "" {
|
||||
return errors.New(errors.CodeInvalidParam, "规则名称不能为空")
|
||||
@@ -62,7 +77,28 @@ func (s *AlertService) CreateRule(ctx context.Context, rule *model.PollingAlertR
|
||||
if rule.Operator == "" {
|
||||
rule.Operator = ">" // 默认大于
|
||||
}
|
||||
return s.ruleStore.Create(ctx, rule)
|
||||
rule.CreatedBy = &operatorID
|
||||
rule.UpdatedBy = &operatorID
|
||||
err := runPollingTransaction(ctx, s.db, s.auditWriter, func(tx *gorm.DB) error {
|
||||
if err := s.ruleStore.WithTx(tx).Create(ctx, rule); err != nil {
|
||||
return err
|
||||
}
|
||||
return writePollingAudit(ctx, tx, s.auditWriter, auditinfra.PollingInput{
|
||||
ActionCode: constants.AuditActionPollingAlertRuleCreated, Summary: "创建轮询告警规则",
|
||||
ResourceType: constants.AuditResourcePollingAlertRule, ResourceID: rule.ID,
|
||||
ResourceKey: pollingManualTriggerKey(rule.ID), DisplayName: rule.RuleName, OperatorID: operatorID,
|
||||
IdentitySnapshot: pollingAlertRuleIdentity(rule), AfterData: pollingAlertRuleState(rule),
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
recordPollingFailure(ctx, s.db, s.auditWriter, auditinfra.PollingInput{
|
||||
ActionCode: constants.AuditActionPollingAlertRuleCreated, Summary: "创建轮询告警规则失败",
|
||||
ResourceType: constants.AuditResourcePollingAlertRule, ResourceKey: rule.RuleName,
|
||||
DisplayName: rule.RuleName, OperatorID: operatorID,
|
||||
IdentitySnapshot: pollingAlertRuleIdentity(rule), AfterData: pollingAlertRuleState(rule),
|
||||
}, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// GetRule 获取告警规则
|
||||
@@ -81,10 +117,15 @@ func (s *AlertService) ListRules(ctx context.Context) ([]*model.PollingAlertRule
|
||||
|
||||
// UpdateRule 更新告警规则
|
||||
func (s *AlertService) UpdateRule(ctx context.Context, id uint, updates map[string]interface{}) error {
|
||||
operatorID := middleware.GetUserIDFromContext(ctx)
|
||||
if operatorID == 0 {
|
||||
return errors.New(errors.CodeUnauthorized, "未授权访问")
|
||||
}
|
||||
rule, err := s.ruleStore.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.CodeNotFound, err, "告警规则不存在")
|
||||
}
|
||||
before := *rule
|
||||
|
||||
if name, ok := updates["rule_name"].(string); ok && name != "" {
|
||||
rule.RuleName = name
|
||||
@@ -104,17 +145,60 @@ func (s *AlertService) UpdateRule(ctx context.Context, id uint, updates map[stri
|
||||
if channels, ok := updates["notification_channels"].(string); ok {
|
||||
rule.NotificationChannels = channels
|
||||
}
|
||||
|
||||
return s.ruleStore.Update(ctx, rule)
|
||||
rule.UpdatedBy = &operatorID
|
||||
err = runPollingTransaction(ctx, s.db, s.auditWriter, func(tx *gorm.DB) error {
|
||||
if err := s.ruleStore.WithTx(tx).Update(ctx, rule); err != nil {
|
||||
return err
|
||||
}
|
||||
return writePollingAudit(ctx, tx, s.auditWriter, auditinfra.PollingInput{
|
||||
ActionCode: constants.AuditActionPollingAlertRuleUpdated, Summary: "更新轮询告警规则",
|
||||
ResourceType: constants.AuditResourcePollingAlertRule, ResourceID: rule.ID,
|
||||
ResourceKey: pollingManualTriggerKey(rule.ID), DisplayName: rule.RuleName, OperatorID: operatorID,
|
||||
IdentitySnapshot: pollingAlertRuleIdentity(rule),
|
||||
BeforeData: pollingAlertRuleState(&before), AfterData: pollingAlertRuleState(rule),
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
recordPollingFailure(ctx, s.db, s.auditWriter, auditinfra.PollingInput{
|
||||
ActionCode: constants.AuditActionPollingAlertRuleUpdated, Summary: "更新轮询告警规则失败",
|
||||
ResourceType: constants.AuditResourcePollingAlertRule, ResourceID: rule.ID,
|
||||
ResourceKey: pollingManualTriggerKey(rule.ID), DisplayName: rule.RuleName, OperatorID: operatorID,
|
||||
IdentitySnapshot: pollingAlertRuleIdentity(rule), BeforeData: pollingAlertRuleState(&before),
|
||||
}, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// DeleteRule 删除告警规则
|
||||
func (s *AlertService) DeleteRule(ctx context.Context, id uint) error {
|
||||
_, err := s.ruleStore.GetByID(ctx, id)
|
||||
operatorID := middleware.GetUserIDFromContext(ctx)
|
||||
if operatorID == 0 {
|
||||
return errors.New(errors.CodeUnauthorized, "未授权访问")
|
||||
}
|
||||
rule, err := s.ruleStore.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.CodeNotFound, err, "告警规则不存在")
|
||||
}
|
||||
return s.ruleStore.Delete(ctx, id)
|
||||
err = runPollingTransaction(ctx, s.db, s.auditWriter, func(tx *gorm.DB) error {
|
||||
if err := s.ruleStore.WithTx(tx).Delete(ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
return writePollingAudit(ctx, tx, s.auditWriter, auditinfra.PollingInput{
|
||||
ActionCode: constants.AuditActionPollingAlertRuleDeleted, Summary: "删除轮询告警规则",
|
||||
ResourceType: constants.AuditResourcePollingAlertRule, ResourceID: rule.ID,
|
||||
ResourceKey: pollingManualTriggerKey(rule.ID), DisplayName: rule.RuleName, OperatorID: operatorID,
|
||||
IdentitySnapshot: pollingAlertRuleIdentity(rule), BeforeData: pollingAlertRuleState(rule),
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
recordPollingFailure(ctx, s.db, s.auditWriter, auditinfra.PollingInput{
|
||||
ActionCode: constants.AuditActionPollingAlertRuleDeleted, Summary: "删除轮询告警规则失败",
|
||||
ResourceType: constants.AuditResourcePollingAlertRule, ResourceID: rule.ID,
|
||||
ResourceKey: pollingManualTriggerKey(rule.ID), DisplayName: rule.RuleName, OperatorID: operatorID,
|
||||
IdentitySnapshot: pollingAlertRuleIdentity(rule), BeforeData: pollingAlertRuleState(rule),
|
||||
}, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// ListHistory 获取告警历史
|
||||
|
||||
Reference in New Issue
Block a user