Documentation
¶
Index ¶
- Constants
- Variables
- type ExprAssignNode
- func (x *ExprAssignNode) Destroy()
- func (x *ExprAssignNode) Init(config types.Config, configuration types.Configuration) error
- func (x *ExprAssignNode) New() types.Node
- func (x *ExprAssignNode) OnMsg(ctx context.Context, msg types.RuleMsg) (next string, err error)
- func (x *ExprAssignNode) Type() types.NodeType
- type ExprAssignNodeConfiguration
- type ExprFilterNode
- func (x *ExprFilterNode) Destroy()
- func (x *ExprFilterNode) Init(ruleConfig types.Config, configuration types.Configuration) error
- func (x *ExprFilterNode) New() types.Node
- func (x *ExprFilterNode) OnMsg(ctx context.Context, msg types.RuleMsg) (string, error)
- func (x *ExprFilterNode) Type() types.NodeType
- type ExprFilterNodeConfiguration
- type ExprSwitchNode
- func (x *ExprSwitchNode) Destroy()
- func (x *ExprSwitchNode) Init(config types.Config, configuration types.Configuration) error
- func (x *ExprSwitchNode) New() types.Node
- func (x *ExprSwitchNode) OnMsg(ctx context.Context, msg types.RuleMsg) (string, error)
- func (x *ExprSwitchNode) Type() types.NodeType
- type ExprSwitchNodeConfiguration
- type JsFilterNode
- type JsFilterNodeConfiguration
- type JsSwitchNode
- type JsSwitchNodeConfiguration
Constants ¶
const ( // JsFilterFuncName JS函数名 JsFilterFuncName = "Filter" // JsFilterType JsFilter组件类型 JsFilterType = "jsFilter" // JsFilterFuncTemplate JS函数模板 JsFilterFuncTemplate = "function Filter(msg, metadata, msgType, dataType) { %s }" )
Variables ¶
var ExprAssignReturnFormatErr = errors.New("return the value is not an array")
E x p rAssignReturnFormatErr JavaScript脚本必须返回数组
var JsSwitchReturnFormatErr = errors.New("return the value is not string")
JsSwitchReturnFormatErr JavaScript脚本必须返回数组
var Registry = &types.SafeComponentSlice{}
Functions ¶
This section is empty.
Types ¶
type ExprAssignNode ¶
type ExprAssignNode struct {
// Config 节点配置
Config ExprAssignNodeConfiguration
// contains filtered or unexported fields
}
ExprAssignNode 使用JavaScript确定消息路由路径的开关节点
func (*ExprAssignNode) Init ¶
func (x *ExprAssignNode) Init(config types.Config, configuration types.Configuration) error
Init 初始化节点
func (*ExprAssignNode) Type ¶
func (x *ExprAssignNode) Type() types.NodeType
Type 返回组件类型 Type returns the component type identifier.
type ExprAssignNodeConfiguration ¶
type ExprAssignNodeConfiguration struct {
// ExprScript JavaScript脚本,用于确定消息路由路径
// 函数参数:msg, metadata, msgType, dataType
// 必须返回字符串数组,表示路由关系类型
//
// 内置变量:
// - $ctx: 上下文对象,提供缓存操作
// - global: 全局配置属性
// - vars: 规则链变量
// - UDF函数: 用户自定义函数
//
// 示例: "return ['route1', 'route2'];"
Script string `json:"script"`
}
ExprAssignNodeConfiguration ExprAssignNode配置结构
type ExprFilterNode ¶
type ExprFilterNode struct {
// Config 表达式过滤器配置
// Config holds the expression filter configuration
Config ExprFilterNodeConfiguration
// contains filtered or unexported fields
}
ExprFilterNode 使用expr-lang表达式进行布尔评估来过滤消息的过滤组件 ExprFilterNode filters messages using expr-lang expressions for boolean evaluation.
核心算法: Core Algorithm: 1. 初始化时编译表达式为优化的程序 - Compile expression to optimized program during initialization 2. 准备消息评估环境(id、ts、data、msg、metadata等)- Prepare message evaluation environment 3. 执行编译的表达式程序 - Execute compiled expression program 4. 根据布尔结果路由消息到True/False关系 - Route message to True/False relation based on boolean result
表达式语言特性 - Expression language features:
- 算术运算符:+, -, *, /, % - Arithmetic operators
- 比较运算符:==, !=, <, <=, >, >= - Comparison operators
- 逻辑运算符:&&, ||, ! - Logical operators
- 字符串操作:contains, startsWith, endsWith - String operations
- 数学函数:abs, ceil, floor, round - Mathematical functions
func (*ExprFilterNode) Destroy ¶
func (x *ExprFilterNode) Destroy()
Destroy 清理资源 Destroy cleans up resources.
func (*ExprFilterNode) Init ¶
func (x *ExprFilterNode) Init(ruleConfig types.Config, configuration types.Configuration) error
Init 初始化组件,验证并编译表达式 Init initializes the component.
func (*ExprFilterNode) New ¶
func (x *ExprFilterNode) New() types.Node
New 创建新实例 New creates a new instance.
func (*ExprFilterNode) OnMsg ¶
OnMsg 处理消息,通过评估编译的表达式来过滤消息 OnMsg processes incoming messages by evaluating the compiled expression.
func (*ExprFilterNode) Type ¶
func (x *ExprFilterNode) Type() types.NodeType
Type 返回组件类型 Type returns the component type identifier.
type ExprFilterNodeConfiguration ¶
type ExprFilterNodeConfiguration struct {
// Expr 用于过滤评估的表达式,必须返回布尔值
// Expr contains the expression to evaluate for filtering.
// The expression has access to the following variables:
// - id: Message ID (string)
// - ts: Message timestamp (int64)
// - data: Original message data (string)
// - msg: Parsed message body (object for JSON, string otherwise)
// - metadata: Message metadata (object with key-value pairs)
// - type: Message type (string)
// - dataType: Message data type (string)
//
// The expression must evaluate to a boolean value:
// - true: Message passes the filter (routed to "True" relation)
// - false: Message fails the filter (routed to "False" relation)
//
// Example expressions:
// 表达式示例:
// - "msg.temperature > 50"
// - "metadata.deviceType == 'sensor' && msg.value > 100"
// - "type == 'TELEMETRY' && data contains 'alarm'"
// - "ts > 1640995200 && msg.status == 'active'"
Script string `json:"script"`
}
ExprFilterNodeConfiguration ExprFilterNode配置结构 ExprFilterNodeConfiguration defines the configuration structure for the ExprFilterNode component.
type ExprSwitchNode ¶
type ExprSwitchNode struct {
// Config 开关节点配置
// Config holds the switch node configuration
Config ExprSwitchNodeConfiguration
// contains filtered or unexported fields
}
SwitchNode 基于表达式评估提供条件消息路由的过滤组件 SwitchNode provides conditional message routing based on expression evaluation.
核心算法: Core Algorithm: 1. 初始化时编译所有case表达式为优化程序 - Compile all case expressions to optimized programs during initialization 2. 按顺序评估每个case表达式 - Evaluate each case expression sequentially 3. 第一个评估为true的case决定路由 - First case that evaluates to true determines routing 4. 无匹配时路由到"Default"关系 - Route to "Default" relation if no matches
评估逻辑 - Evaluation logic:
- 按配置中出现的顺序评估case - Cases evaluated in configuration order
- 在第一个成功匹配时停止评估 - Evaluation stops at first successful match
- 布尔true结果触发路由到case关系 - Boolean true result triggers routing to case relation
- 无匹配导致路由到默认关系 - No matches result in routing to default relation
表达式语言特性 - Expression language features:
- 算术运算符:+, -, *, /, % - Arithmetic operators
- 比较运算符:==, !=, <, <=, >, >= - Comparison operators
- 逻辑运算符:&&, ||, ! - Logical operators
- 字符串操作:contains, startsWith, endsWith - String operations
- 数学函数:abs, ceil, floor, round - Mathematical functions
性能优化 - Performance optimization:
- 表达式在初始化期间编译一次 - Expressions compiled once during initialization
- 早期终止减少不必要的评估 - Early termination reduces unnecessary evaluations
- 按概率排序case以获得最佳性能 - Order cases by probability for optimal performance
func (*ExprSwitchNode) Destroy ¶
func (x *ExprSwitchNode) Destroy()
Destroy 清理资源 Destroy cleans up resources.
func (*ExprSwitchNode) Init ¶
func (x *ExprSwitchNode) Init(config types.Config, configuration types.Configuration) error
Init 初始化组件,编译所有case表达式 Init initializes the component.
func (*ExprSwitchNode) New ¶
func (x *ExprSwitchNode) New() types.Node
New 创建新实例 New creates a new instance.
func (*ExprSwitchNode) OnMsg ¶
OnMsg 处理消息,按顺序评估case表达式并路由到第一个匹配的case或默认关系 OnMsg processes incoming messages by evaluating case expressions sequentially.
func (*ExprSwitchNode) Type ¶
func (x *ExprSwitchNode) Type() types.NodeType
Type 返回组件类型 Type returns the component type identifier.
type ExprSwitchNodeConfiguration ¶
type ExprSwitchNodeConfiguration struct {
// JsScript JavaScript脚本,用于确定消息路由路径
// 函数参数:msg, metadata, msgType, dataType
// 必须返回字符串数组,表示路由关系类型
//
// 内置变量:
// - $ctx: 上下文对象,提供缓存操作
// - global: 全局配置属性
// - vars: 规则链变量
// - UDF函数: 用户自定义函数
//
// 示例: "return ['route1', 'route2'];"
// student=="3" ? "A" : ((score > 75 && level == "B")|| student == "C") ? "B" : (score > 60) ? "C" : "Default"
Script string `json:"script"`
Cases []types.Case `json:"cases"`
}
SwitchNodeConfiguration SwitchNode配置结构 SwitchNodeConfiguration defines the configuration structure for the SwitchNode component.
type JsFilterNode ¶
type JsFilterNode struct {
// Config 节点配置
Config JsFilterNodeConfiguration
// contains filtered or unexported fields
}
JsFilterNode 使用JavaScript评估布尔条件的过滤器节点
func (*JsFilterNode) Init ¶
func (x *JsFilterNode) Init(ruleConfig types.Config, configuration types.Configuration) error
Init 初始化节点
type JsFilterNodeConfiguration ¶
type JsFilterNodeConfiguration struct {
// JsScript JavaScript脚本,用于评估过滤条件
// 函数参数:msg, metadata, msgType, dataType
// 必须返回布尔值:true通过过滤,false不通过
//
// 内置变量:
// - $ctx: 上下文对象,提供缓存操作
// - global: 全局配置属性
// - vars: 规则链变量
// - UDF函数: 用户自定义函数
//
// 示例: "return msg.temperature > 25.0;"
Script string `json:"script"`
}
JsFilterNodeConfiguration JsFilterNode配置结构
type JsSwitchNode ¶
type JsSwitchNode struct {
// Config 节点配置
Config JsSwitchNodeConfiguration
// contains filtered or unexported fields
}
JsSwitchNode 使用JavaScript确定消息路由路径的开关节点
func (*JsSwitchNode) Init ¶
func (x *JsSwitchNode) Init(config types.Config, configuration types.Configuration) error
Init 初始化节点
type JsSwitchNodeConfiguration ¶
type JsSwitchNodeConfiguration struct {
// JsScript JavaScript脚本,用于确定消息路由路径
// 函数参数:msg, metadata, msgType, dataType
// 必须返回字符串数组,表示路由关系类型
//
// 内置变量:
// - $ctx: 上下文对象,提供缓存操作
// - global: 全局配置属性
// - vars: 规则链变量
// - UDF函数: 用户自定义函数
//
// 示例: "return ['route1', 'route2'];"
Script string `json:"script"`
}
JsSwitchNodeConfiguration JsSwitchNode配置结构