规则引擎

This commit is contained in:
PandaGoAdmin
2023-03-21 11:42:02 +08:00
parent e267703a97
commit 186453f5a6
11 changed files with 102 additions and 42 deletions

View File

@@ -35,11 +35,10 @@ type delayNodeFactory struct{}
func (f delayNodeFactory) Name() string { return DelayNodeName }
func (f delayNodeFactory) Category() string { return NODE_CATEGORY_ACTION }
func (f delayNodeFactory) Labels() []string { return []string{"Success", "Failure"} }
func (f delayNodeFactory) Create(id string, meta Metadata) (Node, error) {
labels := []string{"Success", "Failure"}
node := &delayNode{
bareNode: newBareNode(f.Name(), id, meta, labels),
bareNode: newBareNode(f.Name(), id, meta, f.Labels()),
lock: sync.Mutex{},
}
_, err := decodePath(meta, node)

View File

@@ -20,6 +20,7 @@ const (
type Factory interface {
Name() string
Category() string
Labels() []string
Create(id string, meta Metadata) (Node, error)
}
@@ -28,7 +29,7 @@ var (
allNodeFactories map[string]Factory = make(map[string]Factory)
// allNodeCategories hold node's metadata by category
allNodeCategories map[string][]string = make(map[string][]string)
allNodeCategories map[string][]map[string]interface{} = make(map[string][]map[string]interface{})
)
// RegisterFactory add a new node factory and classify its category for
@@ -37,9 +38,9 @@ func RegisterFactory(f Factory) {
allNodeFactories[f.Name()] = f
if allNodeCategories[f.Category()] == nil {
allNodeCategories[f.Category()] = []string{}
allNodeCategories[f.Category()] = []map[string]interface{}{}
}
allNodeCategories[f.Category()] = append(allNodeCategories[f.Category()], f.Name())
allNodeCategories[f.Category()] = append(allNodeCategories[f.Category()], map[string]interface{}{"name": f.Name(), "labels": f.Labels()})
}
// NewNode is the only way to create a new node
@@ -51,4 +52,4 @@ func NewNode(nodeType string, id string, meta Metadata) (Node, error) {
}
// GetCategoryNodes return specified category's all nodes
func GetCategoryNodes() map[string][]string { return allNodeCategories }
func GetCategoryNodes() map[string][]map[string]interface{} { return allNodeCategories }

View File

@@ -14,11 +14,10 @@ type messageTypeSwitchNodeFactory struct{}
func (f messageTypeSwitchNodeFactory) Name() string { return "MessageTypeSwitchNode" }
func (f messageTypeSwitchNodeFactory) Category() string { return NODE_CATEGORY_FILTER }
func (f messageTypeSwitchNodeFactory) Labels() []string { return []string{"True", "False"} }
func (f messageTypeSwitchNodeFactory) Create(id string, meta Metadata) (Node, error) {
labels := []string{"True", "False"}
node := &messageTypeSwitchNode{
bareNode: newBareNode(f.Name(), id, meta, labels),
bareNode: newBareNode(f.Name(), id, meta, f.Labels()),
}
return decodePath(meta, node)
}

View File

@@ -25,11 +25,15 @@ type switchFilterNodeFactory struct{}
func (f switchFilterNodeFactory) Name() string { return "SwitchNode" }
func (f switchFilterNodeFactory) Category() string { return NODE_CATEGORY_FILTER }
func (f switchFilterNodeFactory) Labels() []string {
return []string{
"True", "False", message.MessageTypePostTelemetryRequest,
message.MessageTypeConnectEvent,
}
}
func (f switchFilterNodeFactory) Create(id string, meta Metadata) (Node, error) {
labels := []string{}
node := &switchFilterNode{
bareNode: newBareNode(f.Name(), id, meta, labels),
bareNode: newBareNode(f.Name(), id, meta, f.Labels()),
}
return decodePath(meta, node)
}
@@ -40,7 +44,7 @@ func (n *switchFilterNode) Handle(msg message.Message) error {
scriptEngine := NewScriptEngine()
SwitchResults, err := scriptEngine.ScriptOnSwitch(msg, n.Scripts)
if err != nil {
return nil
return err
}
nodes := n.GetLinkedNodes()
for label, node := range nodes {

View File

@@ -15,11 +15,10 @@ type inputNodeFactory struct{}
func (f inputNodeFactory) Name() string { return "InputNode" }
func (f inputNodeFactory) Category() string { return NODE_CATEGORY_OTHERS }
func (f inputNodeFactory) Labels() []string { return []string{} }
func (f inputNodeFactory) Create(id string, meta Metadata) (Node, error) {
labels := []string{}
node := &inputNode{
bareNode: newBareNode(InputNodeName, id, meta, labels),
bareNode: newBareNode(InputNodeName, id, meta, f.Labels()),
}
return node, nil
}

View File

@@ -1,6 +1,10 @@
package nodes
import "pandax/pkg/rule_engine/message"
import (
"github.com/dop251/goja"
"github.com/sirupsen/logrus"
"pandax/pkg/rule_engine/message"
)
type ScriptEngine interface {
ScriptOnMessage(msg message.Message, script string) (message.Message, error)
@@ -24,8 +28,20 @@ func (bse *baseScriptEngine) ScriptOnMessage(msg message.Message, script string)
}
func (bse *baseScriptEngine) ScriptOnSwitch(msg message.Message, script string) ([]string, error) {
return nil, nil
vm := goja.New()
_, err := vm.RunString(script)
if err != nil {
logrus.Info("JS代码有问题")
return nil, err
}
var fn func(message.Message, message.Metadata, string) []string
err = vm.ExportTo(vm.Get("Switch"), &fn)
if err != nil {
logrus.Info("Js函数映射到 Go 函数失败!")
return nil, err
}
datas := fn(msg, msg.GetMetadata(), msg.GetType())
return datas, nil
}
func (bse *baseScriptEngine) ScriptOnFilter(msg message.Message, script string) (bool, error) {