feat(规则): 支持同一事件多规则按条件区分并全部发送

Motivation:
止盈、止损等不同策略信号会映射到同一事件(如 trade.close),但原有唯一约束要求每个事件只能有一条规则,无法按策略区分处理。放开该约束后,同一事件可配置多条规则,通过规则条件与精确/通配优先级区分,命中条件的规则全部发送;同时将去重键从消息原文改为信号维度,避免同一信号因时间戳等无关字段差异被误判为重复。

Changes:

* 移除规则 source_id+event 的唯一约束,改为普通索引
* 事件匹配改为返回命中优先级内所有启用规则,并按 ID 逐条派发
* 通知服务遍历多条规则,按条件过滤后聚合发送渠道
* 去重键由消息 body 哈希改为策略/币种/周期/方向/价格信号维度哈希
This commit is contained in:
2026-08-17 01:20:49 +08:00
parent f5f64f7653
commit d2e8476398
13 changed files with 349 additions and 115 deletions
+29 -10
View File
@@ -11,18 +11,33 @@ import (
// Exact event wins; otherwise glob patterns (* and ?) via path.Match.
// Among globs, more literal characters win; ties go to the smaller ID.
func PickEventRule(event string, rules []model.Rule) *model.Rule {
var exact *model.Rule
var best *model.Rule
picked := PickEventRules(event, rules)
if len(picked) == 0 {
return nil
}
best := &picked[0]
for i := range picked[1:] {
if picked[i+1].ID < best.ID {
best = &picked[i+1]
}
}
return best
}
// PickEventRules returns every enabled rule at the winning specificity.
// All exact event matches win as a group; otherwise all globs that share
// the highest literal-character score.
func PickEventRules(event string, rules []model.Rule) []model.Rule {
var exact []model.Rule
var globs []model.Rule
bestScore := -1
for i := range rules {
r := &rules[i]
r := rules[i]
if r.Enabled == 0 {
continue
}
if r.Event == event {
if exact == nil || r.ID < exact.ID {
exact = r
}
exact = append(exact, r)
continue
}
if !strings.ContainsAny(r.Event, "*?") {
@@ -33,15 +48,19 @@ func PickEventRule(event string, rules []model.Rule) *model.Rule {
continue
}
score := globSpecificity(r.Event)
if score > bestScore || (score == bestScore && (best == nil || r.ID < best.ID)) {
if score > bestScore {
bestScore = score
best = r
globs = []model.Rule{r}
continue
}
if score == bestScore {
globs = append(globs, r)
}
}
if exact != nil {
if len(exact) > 0 {
return exact
}
return best
return globs
}
func globSpecificity(pattern string) int {
+29
View File
@@ -60,3 +60,32 @@ func TestPickEventRuleNoMatch(t *testing.T) {
t.Fatal("expected no match")
}
}
func TestPickEventRulesAllExactMatches(t *testing.T) {
rules := []model.Rule{
{ID: 12, Event: "trade.*", Enabled: 1},
{ID: 17, Event: "trade.close", Enabled: 1},
{ID: 18, Event: "trade.close", Enabled: 1},
{ID: 19, Event: "trade.close", Enabled: 0},
}
got := PickEventRules("trade.close", rules)
if len(got) != 2 {
t.Fatalf("want 2 exact trade.close, got %#v", got)
}
ids := []int{got[0].ID, got[1].ID}
if ids[0] != 17 || ids[1] != 18 {
t.Fatalf("ids=%v", ids)
}
}
func TestPickEventRulesSameGlobBoth(t *testing.T) {
rules := []model.Rule{
{ID: 1, Event: "trade.*", Enabled: 1},
{ID: 2, Event: "trade.*", Enabled: 1},
{ID: 3, Event: "*", Enabled: 1},
}
got := PickEventRules("trade.open", rules)
if len(got) != 2 {
t.Fatalf("want both trade.*, got %#v", got)
}
}
+10 -50
View File
@@ -2,7 +2,6 @@ package engine
import (
"context"
"encoding/json"
"fmt"
"aiaa-notification-service/internal/cache"
@@ -12,59 +11,20 @@ import (
type Matcher struct {
store *store.Store
cache *cache.Cache
}
func NewMatcher(s *store.Store, c *cache.Cache) *Matcher {
return &Matcher{store: s, cache: c}
func NewMatcher(s *store.Store, _ *cache.Cache) *Matcher {
return &Matcher{store: s}
}
func (m *Matcher) Match(ctx context.Context, sourceID int, event string) (*model.Rule, error) {
// Try cache first
if m.cache != nil {
cr, err := m.cache.GetRule(ctx, sourceID, event)
if err == nil {
rule := &model.Rule{ID: cr.RuleID, TemplateID: cr.TemplateID, SourceID: sourceID, Event: event}
if cr.Conditions != "" && cr.Conditions != "null" {
raw := json.RawMessage(cr.Conditions)
rule.Conditions = &raw
}
return rule, nil
}
}
rule, err := m.store.GetRuleBySourceEvent(ctx, sourceID, event)
func (m *Matcher) Match(ctx context.Context, sourceID int, event string) ([]model.Rule, error) {
rules, err := m.store.ListEnabledRulesBySource(ctx, sourceID)
if err != nil {
rules, listErr := m.store.ListEnabledRulesBySource(ctx, sourceID)
if listErr != nil {
return nil, fmt.Errorf("match rule: %w", listErr)
}
picked := PickEventRule(event, rules)
if picked == nil {
return nil, fmt.Errorf("match rule: %w", err)
}
return picked, nil
return nil, fmt.Errorf("match rule: %w", err)
}
m.warmRuleCache(ctx, sourceID, event, rule)
return rule, nil
}
func (m *Matcher) warmRuleCache(ctx context.Context, sourceID int, event string, rule *model.Rule) {
if m.cache == nil {
return
}
tmpl, err := m.store.GetTemplate(ctx, rule.TemplateID)
if err != nil {
return
}
cr := &cache.CachedRule{
RuleID: rule.ID,
TemplateID: rule.TemplateID,
Content: tmpl.Content,
}
if rule.Conditions != nil {
cr.Conditions = string(*rule.Conditions)
}
_ = m.cache.SetRule(ctx, sourceID, event, cr)
picked := PickEventRules(event, rules)
if len(picked) == 0 {
return nil, fmt.Errorf("match rule: no rule for source %d event %s", sourceID, event)
}
return picked, nil
}