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
+46 -12
View File
@@ -9,14 +9,21 @@ import (
)
type triggerRepoFake struct {
input *AlertTriggerInput
metrics map[ChannelKey]AlertComparison
hits []*AlertRecord
metricCalls int
current *AlertRecord
failure error
input *AlertTriggerInput
metrics map[ChannelKey]AlertComparison
hits []*AlertRecord
metricCalls int
snapshotCalls int
current *AlertRecord
failure error
}
func (r *triggerRepoFake) FilterWritable(_ context.Context, hits []*AlertRecord, _ []int, allow func(*AlertRecord) bool) ([]*AlertRecord, error) {
if !allow(r.current) {
return nil, nil
}
return hits, nil
}
func (r *triggerRepoFake) Input(context.Context) (*AlertTriggerInput, error) { return r.input, nil }
func (r *triggerRepoFake) Comparison(context.Context, int, []ChannelKey, time.Time, int) (map[ChannelKey]AlertComparison, error) {
r.metricCalls++
@@ -52,7 +59,7 @@ func TestAlertTriggerSeparatesOwnerPlatformAndConflictingBindings(t *testing.T)
r, now := triggerFixture()
days := 3
r.input.Rules = []*AlertRule{{ID: 1, CreatorID: 10, Type: "未登录", Metric: 3, UnloginDays: &days}, {ID: 2, CreatorID: 20, Type: "越级", Metric: 3, UnloginDays: &days}}
result, err := NewAlertTriggerUsecase(r).Detect(context.Background(), now)
result, err := NewAlertTriggerUsecase(r, nil).Detect(context.Background(), now)
if err != nil || result.Written != 1 || result.Skipped != 1 || len(r.hits) != 1 {
t.Fatalf("检测结果 %+v %v", result, err)
}
@@ -62,8 +69,8 @@ func TestAlertTriggerSeparatesOwnerPlatformAndConflictingBindings(t *testing.T)
}
r.hits = nil
r.current = &AlertRecord{Status: 2}
result, err = NewAlertTriggerUsecase(r).Detect(context.Background(), now)
if err != nil || result.Written != 0 || len(r.hits) != 0 {
result, err = NewAlertTriggerUsecase(r, nil).Detect(context.Background(), now)
if err != nil || result.Written != 0 || len(r.hits) != 0 || r.snapshotCalls != 1 {
t.Fatalf("在途记录不应刷新: %+v %v", result, err)
}
}
@@ -75,7 +82,7 @@ func TestAlertTriggerMoMBoundariesAndSnapshots(t *testing.T) {
r.input.Rules = []*AlertRule{{ID: 1, CreatorID: 10, Type: "充值", Metric: 2, StatPeriodDays: &days, MomDirection: &direction, MomThreshold: &threshold},
{ID: 2, CreatorID: 10, Type: "另一个充值规则", Metric: 2, StatPeriodDays: &days, MomDirection: &direction, MomThreshold: &threshold}}
r.metrics = map[ChannelKey]AlertComparison{{"lx", 10}: {FirstUnix: now.AddDate(0, 0, -4).Unix(), Current: "110000", Previous: "100000"}}
result, err := NewAlertTriggerUsecase(r).Detect(context.Background(), now)
result, err := NewAlertTriggerUsecase(r, nil).Detect(context.Background(), now)
if err != nil || result.Written != 2 || r.metricCalls != 1 {
t.Fatalf("同窗口应共享指标: %+v calls=%d %v", result, r.metricCalls, err)
}
@@ -84,12 +91,12 @@ func TestAlertTriggerMoMBoundariesAndSnapshots(t *testing.T) {
}
r.metrics[ChannelKey{"lx", 10}] = AlertComparison{FirstUnix: now.AddDate(0, 0, -3).Unix(), Current: "110000", Previous: "100000"}
r.hits = nil
result, err = NewAlertTriggerUsecase(r).Detect(context.Background(), now)
result, err = NewAlertTriggerUsecase(r, nil).Detect(context.Background(), now)
if err != nil || result.Written != 0 {
t.Fatalf("不足双周期不应触发: %+v %v", result, err)
}
r.failure = errors.New("指标不可用")
_, err = NewAlertTriggerUsecase(r).Detect(context.Background(), now)
_, err = NewAlertTriggerUsecase(r, nil).Detect(context.Background(), now)
if err == nil || !strings.Contains(err.Error(), "2 项失败") {
t.Fatalf("读取失败不能报告成功: %v", err)
}
@@ -110,3 +117,30 @@ func TestAlertTriggerRoundedThresholdAndNoBaseline(t *testing.T) {
}
}
}
func (r *triggerRepoFake) Snapshots(_ context.Context, keys []ChannelKey, _ time.Time, _ bool) (map[ChannelKey]AlertSnapshot, error) {
r.snapshotCalls++
out := map[ChannelKey]AlertSnapshot{}
for _, key := range keys {
out[key] = AlertSnapshot{Pay: "0"}
}
return out, nil
}
func TestAlertExemptionIsLimitedToItsBusinessOwner(t *testing.T) {
r, now := triggerFixture()
days := 3
r.input.Rules = []*AlertRule{{ID: 1, CreatorID: 10, Type: "未登录", Metric: 3, UnloginDays: &days}}
key := ChannelKey{SourceDB: "lx", ID: 10}
r.input.Exemptions = map[AlertExemptionKey]bool{{ChannelKey: key, OwnerID: 11}: true}
uc := NewAlertTriggerUsecase(r, nil)
first, err := uc.Detect(context.Background(), now)
if err != nil || first.Written != 1 {
t.Fatalf("其他商务的豁免影响了当前商务: %+v %v", first, err)
}
r.input.Exemptions[AlertExemptionKey{ChannelKey: key, OwnerID: 10}] = true
second, err := uc.Detect(context.Background(), now)
if err != nil || second.Written != 0 || r.snapshotCalls != 1 {
t.Fatalf("当前商务的豁免未在统计前生效: %+v %v", second, err)
}
}