feat(规则匹配): 事件名支持通配符匹配
Motivation: 通知规则的事件名此前仅支持精确匹配,无法用一条规则覆盖多类事件(如 trade.* 下的全部交易事件),规则配置维护成本高。本次引入通配符匹配,让规则可按模式批量命中事件,同时保证精确规则优先。 Changes: * 事件名支持 * 和 ? 通配符匹配 * 匹配优先级:精确匹配 > 更具体的通配 > 泛匹配,同分时取更小 ID * 精确命中未命中时回退为列出源下启用规则并按优先级挑选 * 抽取缓存预热逻辑为独立方法 * 更新文档说明事件名通配规则
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"aiaa-notification-service/internal/model"
|
||||
)
|
||||
|
||||
// PickEventRule selects the best enabled rule for an event.
|
||||
// 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
|
||||
bestScore := -1
|
||||
for i := range rules {
|
||||
r := &rules[i]
|
||||
if r.Enabled == 0 {
|
||||
continue
|
||||
}
|
||||
if r.Event == event {
|
||||
if exact == nil || r.ID < exact.ID {
|
||||
exact = r
|
||||
}
|
||||
continue
|
||||
}
|
||||
if !strings.ContainsAny(r.Event, "*?") {
|
||||
continue
|
||||
}
|
||||
ok, err := path.Match(r.Event, event)
|
||||
if err != nil || !ok {
|
||||
continue
|
||||
}
|
||||
score := globSpecificity(r.Event)
|
||||
if score > bestScore || (score == bestScore && (best == nil || r.ID < best.ID)) {
|
||||
bestScore = score
|
||||
best = r
|
||||
}
|
||||
}
|
||||
if exact != nil {
|
||||
return exact
|
||||
}
|
||||
return best
|
||||
}
|
||||
|
||||
func globSpecificity(pattern string) int {
|
||||
n := 0
|
||||
for _, r := range pattern {
|
||||
if r != '*' && r != '?' {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"aiaa-notification-service/internal/model"
|
||||
)
|
||||
|
||||
func TestPickEventRuleExact(t *testing.T) {
|
||||
rules := []model.Rule{
|
||||
{ID: 1, Event: "trade.*", Enabled: 1},
|
||||
{ID: 2, Event: "trade.open", Enabled: 1},
|
||||
}
|
||||
got := PickEventRule("trade.open", rules)
|
||||
if got == nil || got.ID != 2 {
|
||||
t.Fatalf("want exact id=2, got %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPickEventRuleWildcard(t *testing.T) {
|
||||
rules := []model.Rule{
|
||||
{ID: 1, Event: "trade.*", Enabled: 1},
|
||||
}
|
||||
got := PickEventRule("trade.close", rules)
|
||||
if got == nil || got.Event != "trade.*" {
|
||||
t.Fatalf("want trade.*, got %#v", got)
|
||||
}
|
||||
if PickEventRule("order.open", rules) != nil {
|
||||
t.Fatal("trade.* must not match order.open")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPickEventRuleMoreSpecificWildcardWins(t *testing.T) {
|
||||
rules := []model.Rule{
|
||||
{ID: 1, Event: "*", Enabled: 1},
|
||||
{ID: 2, Event: "trade.*", Enabled: 1},
|
||||
}
|
||||
got := PickEventRule("trade.open", rules)
|
||||
if got == nil || got.ID != 2 {
|
||||
t.Fatalf("want trade.* id=2, got %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPickEventRuleSkipsDisabled(t *testing.T) {
|
||||
rules := []model.Rule{
|
||||
{ID: 1, Event: "trade.*", Enabled: 0},
|
||||
{ID: 2, Event: "*", Enabled: 1},
|
||||
}
|
||||
got := PickEventRule("trade.open", rules)
|
||||
if got == nil || got.ID != 2 {
|
||||
t.Fatalf("want * id=2, got %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPickEventRuleNoMatch(t *testing.T) {
|
||||
rules := []model.Rule{
|
||||
{ID: 1, Event: "trade.open", Enabled: 1},
|
||||
}
|
||||
if PickEventRule("trade.close", rules) != nil {
|
||||
t.Fatal("expected no match")
|
||||
}
|
||||
}
|
||||
+29
-19
@@ -33,28 +33,38 @@ func (m *Matcher) Match(ctx context.Context, sourceID int, event string) (*model
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to DB
|
||||
rule, err := m.store.GetRuleBySourceEvent(ctx, sourceID, event)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("match rule: %w", err)
|
||||
}
|
||||
|
||||
// Warm cache
|
||||
if m.cache != nil {
|
||||
tmpl, err := m.store.GetTemplate(ctx, rule.TemplateID)
|
||||
if err != nil {
|
||||
return rule, nil // rule found but template fetch failed — still return rule
|
||||
}
|
||||
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)
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user