[feat]添加规则引擎debug功能

This commit is contained in:
PandaX
2023-10-27 15:28:29 +08:00
parent c2b16d5474
commit 0fcb262519
13 changed files with 2145 additions and 59 deletions

View File

@@ -11,7 +11,7 @@ type (
DeviceAlarmModel interface {
Insert(data entity.DeviceAlarm) error
FindOne(id string) *entity.DeviceAlarm
FindOneByType(deviceId, ty, state string) *entity.DeviceAlarm
FindOneByType(deviceId, ty, state string) (*entity.DeviceAlarm, error)
FindListPage(page, pageSize int, data entity.DeviceAlarmForm) (*[]entity.DeviceAlarm, int64)
Update(data entity.DeviceAlarm) error
Delete(ids []string)
@@ -41,12 +41,14 @@ func (m *alarmModelImpl) FindOne(id string) *entity.DeviceAlarm {
return resData
}
func (m *alarmModelImpl) FindOneByType(deviceId, ty, state string) *entity.DeviceAlarm {
func (m *alarmModelImpl) FindOneByType(deviceId, ty, state string) (*entity.DeviceAlarm, error) {
resData := new(entity.DeviceAlarm)
db := global.Db.Table(m.table).Where("device_id = ?", deviceId).Where("type = ? ", ty).Where("state = ? ", state)
err := db.First(resData).Error
biz.ErrIsNil(err, "查询设备告警失败")
return resData
if err != nil {
return nil, err
}
return resData, nil
}
func (m *alarmModelImpl) FindListPage(page, pageSize int, data entity.DeviceAlarmForm) (*[]entity.DeviceAlarm, int64) {

View File

@@ -28,7 +28,7 @@ func BuildRunDeviceRpc(deviceId, mode string, metadata map[string]interface{}) e
dataCode := ruleData.LfData.DataCode
code, _ := json.Marshal(dataCode)
//新建规则链实体
instance, errs := rule_engine.NewRuleChainInstance(code)
instance, errs := rule_engine.NewRuleChainInstance(findOne.Id, code)
if len(errs) > 0 {
return errs[0]
}

View File

@@ -1,7 +1,6 @@
package api
import (
"context"
"github.com/PandaXGO/PandaKit/biz"
"github.com/PandaXGO/PandaKit/model"
"github.com/PandaXGO/PandaKit/restfulx"
@@ -9,8 +8,6 @@ import (
"pandax/apps/rule/services"
"pandax/pkg/global_model"
"pandax/pkg/rule_engine"
"pandax/pkg/rule_engine/message"
"pandax/pkg/rule_engine/nodes"
"strings"
)
@@ -19,14 +16,22 @@ type RuleChainApi struct {
}
func (r *RuleChainApi) GetNodeLabels(rc *restfulx.ReqCtx) {
rc.ResData = nodes.GetCategory()
rc.ResData = rule_engine.GetCategory()
}
func (r *RuleChainApi) RuleChainTest(rc *restfulx.ReqCtx) {
code := restfulx.QueryParam(rc, "code")
instance, _ := rule_engine.NewRuleChainInstance([]byte(code))
msg := message.NewMessage("1", message.TelemetryMes, message.Msg{"temperature": 60.4, "humidity": 32.5}, message.Metadata{})
instance.StartRuleChain(context.Background(), msg)
rc.ResData = []map[string]interface{}{}
func (r *RuleChainApi) GetNodeDebug(rc *restfulx.ReqCtx) {
pageNum := restfulx.QueryInt(rc, "pageNum", 1)
pageSize := restfulx.QueryInt(rc, "pageSize", 10)
ruleId := restfulx.QueryParam(rc, "ruleId")
nodeId := restfulx.QueryParam(rc, "nodeId")
total, list := rule_engine.GetDebugDataPage(pageNum, pageSize, ruleId, nodeId)
rc.ResData = model.ResultPage{
Total: total,
PageNum: int64(pageNum),
PageSize: int64(pageSize),
Data: list,
}
}
// GetRuleChainList WorkInfo列表数据

View File

@@ -5,12 +5,15 @@ import (
)
type RuleDataJson struct {
LfData struct {
GlobalColor string `json:"globalColor"`
DataCode map[string]interface{} `json:"dataCode"`
OpenRule bool `json:"openRule"`
Setting map[string]interface{} `json:"setting"`
} `json:"lfData"`
Id string
LfData LfData `json:"lfData"`
}
type LfData struct {
GlobalColor string `json:"globalColor"`
DataCode map[string]interface{} `json:"dataCode"`
OpenRule bool `json:"openRule"`
Setting map[string]interface{} `json:"setting"`
}
// 序列化

View File

@@ -27,12 +27,17 @@ func InitRuleChainRouter(container *restful.Container) {
Metadata(restfulspec.KeyOpenAPITags, tags).
Returns(200, "OK", model.ResultPage{}))
ws.Route(ws.GET("/test").To(func(request *restful.Request, response *restful.Response) {
restfulx.NewReqCtx(request, response).WithNeedCasbin(false).WithLog("测试规则引擎").Handle(s.RuleChainTest)
ws.Route(ws.GET("/node/debug").To(func(request *restful.Request, response *restful.Response) {
restfulx.NewReqCtx(request, response).WithNeedCasbin(false).WithLog("获取规则链节点日志").Handle(s.GetNodeDebug)
}).
Doc("测试规则引擎").
Param(ws.QueryParameter("code", "流程代码").DataType("string")).
Metadata(restfulspec.KeyOpenAPITags, tags))
Doc("获取规则链节点日志").
Param(ws.QueryParameter("pageNum", "页数").Required(true).DataType("int")).
Param(ws.QueryParameter("pageSize", "每页条数").Required(true).DataType("int")).
Param(ws.QueryParameter("ruleId", "规则ID").Required(false).DataType("string")).
Param(ws.QueryParameter("nodeId", "节点ID").Required(false).DataType("string")).
Metadata(restfulspec.KeyOpenAPITags, tags).
Metadata(restfulspec.KeyOpenAPITags, tags).
Returns(200, "OK", model.ResultPage{}))
ws.Route(ws.GET("/list").To(func(request *restful.Request, response *restful.Response) {
restfulx.NewReqCtx(request, response).WithLog("获取规则引擎分页列表").Handle(s.GetRuleChainList)