mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-24 19:38:35 +08:00
[feat]添加规则引擎debug功能
This commit is contained in:
@@ -3,17 +3,21 @@ package rule_engine
|
||||
import (
|
||||
"context"
|
||||
"github.com/sirupsen/logrus"
|
||||
"pandax/pkg/global"
|
||||
"pandax/pkg/rule_engine/manifest"
|
||||
"pandax/pkg/rule_engine/message"
|
||||
"pandax/pkg/rule_engine/nodes"
|
||||
)
|
||||
|
||||
var ruleChainDebugData = message.NewRuleChainDebugData(100)
|
||||
|
||||
type ruleChainInstance struct {
|
||||
ruleId string
|
||||
firstRuleNodeId string
|
||||
nodes map[string]nodes.Node
|
||||
}
|
||||
|
||||
func NewRuleChainInstance(data []byte) (*ruleChainInstance, []error) {
|
||||
func NewRuleChainInstance(ruleId string, data []byte) (*ruleChainInstance, []error) {
|
||||
errors := make([]error, 0)
|
||||
|
||||
manifest, err := manifest.New(data)
|
||||
@@ -22,7 +26,12 @@ func NewRuleChainInstance(data []byte) (*ruleChainInstance, []error) {
|
||||
logrus.WithError(err).Errorf("invalidi manifest file")
|
||||
return nil, errors
|
||||
}
|
||||
return newInstanceWithManifest(manifest)
|
||||
withManifest, errs := newInstanceWithManifest(manifest)
|
||||
if len(errs) > 0 {
|
||||
return nil, errs
|
||||
}
|
||||
withManifest.ruleId = ruleId
|
||||
return withManifest, nil
|
||||
}
|
||||
|
||||
// newWithManifest create rule chain by user's manifest file
|
||||
@@ -42,8 +51,24 @@ func newInstanceWithManifest(m *manifest.Manifest) (*ruleChainInstance, []error)
|
||||
|
||||
// StartRuleChain TODO 是否需要添加context
|
||||
func (c *ruleChainInstance) StartRuleChain(context context.Context, message *message.Message) error {
|
||||
// 处理debug的通道消息
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case debugMsg := <-message.DeBugChan:
|
||||
// 保存到tdengine时序数据库中
|
||||
ruleChainDebugData.Add(c.ruleId, debugMsg.NodeId, debugMsg)
|
||||
case <-message.EndDeBugChan:
|
||||
global.Log.Debug("规则链%s,执行结束", message.Id)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
}()
|
||||
if node, found := c.nodes[c.firstRuleNodeId]; found {
|
||||
return node.Handle(message)
|
||||
err := node.Handle(message)
|
||||
message.EndDeBugChan <- struct{}{}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user