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

Changes:

* 移除规则 source_id+event 的唯一约束,改为普通索引
* 事件匹配改为返回命中优先级内所有启用规则,并按 ID 逐条派发
* 通知服务遍历多条规则,按条件过滤后聚合发送渠道
* 去重键由消息 body 哈希改为策略/币种/周期/方向/价格信号维度哈希
2026-08-17 01:20:49 +08:00

75 lines
1.5 KiB
Go

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 {
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]
if r.Enabled == 0 {
continue
}
if r.Event == event {
exact = append(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 {
bestScore = score
globs = []model.Rule{r}
continue
}
if score == bestScore {
globs = append(globs, r)
}
}
if len(exact) > 0 {
return exact
}
return globs
}
func globSpecificity(pattern string) int {
n := 0
for _, r := range pattern {
if r != '*' && r != '?' {
n++
}
}
return n
}