规则链

This commit is contained in:
XM-GO
2023-04-07 17:02:58 +08:00
parent adc96f4176
commit 282668c0f9
33 changed files with 403 additions and 104 deletions

View File

@@ -2,7 +2,7 @@ package api
// ==========================================================================
// 生成日期2023-03-29 20:01:11 +0800 CST
// 生成路径: apps/flow/api/flow_work_info.go
// 生成路径: apps/flow/api/rulechain.go
// 生成人panda
// ==========================================================================
import (

View File

@@ -1,6 +1,6 @@
// ==========================================================================
// 生成日期2023-03-29 20:01:11 +0800 CST
// 生成路径: apps/flow/router/flow_work_info.go
// 生成路径: apps/flow/router/rulechain.go
// 生成人panda
// ==========================================================================
package router

View File

@@ -1,6 +1,6 @@
// ==========================================================================
// 生成日期2023-03-29 20:01:11 +0800 CST
// 生成路径: apps/flow/services/flow_work_info.go
// 生成路径: apps/flow/services/rulechain.go
// 生成人panda
// ==========================================================================

View File

@@ -1,7 +1,6 @@
package api
import (
"context"
"fmt"
"github.com/XM-GO/PandaKit/biz"
"github.com/XM-GO/PandaKit/restfulx"
@@ -10,8 +9,6 @@ import (
"github.com/gorilla/websocket"
"github.com/kakuilan/kgo"
"pandax/pkg/middleware"
"pandax/pkg/rule_engine"
"pandax/pkg/rule_engine/message"
"runtime"
)
@@ -83,15 +80,3 @@ func (s *System) ConnectWs(request *restful.Request, response *restful.Response)
la := rc.LoginAccount
ws.Put(uint64(la.UserId), wsConn)
}
func (s *System) TestRuleChain(request *restful.Request, response *restful.Response) {
parameter := request.QueryParameter("code")
instance, _ := rule_engine.NewRuleChainInstance([]byte(parameter))
newMessage := message.NewMessage()
instance.StartRuleChain(context.Background(), newMessage)
response.WriteEntity(map[string]any{
"code": 200,
"logs": []map[string]interface{}{},
})
}

View File

@@ -11,6 +11,5 @@ func InitSystemRouter(container *restful.Container) {
ws.Path("/system").Produces(restful.MIME_JSON)
ws.Route(ws.GET("/").To(s.ConnectWs))
ws.Route(ws.GET("/server").To(s.ServerInfo))
ws.Route(ws.GET("/test/rulechain").To(s.ServerInfo))
container.Add(ws)
}

View File

@@ -0,0 +1,76 @@
package api
import (
"context"
"github.com/XM-GO/PandaKit/model"
"github.com/XM-GO/PandaKit/restfulx"
"pandax/apps/visual/entity"
"pandax/apps/visual/services"
"pandax/pkg/rule_engine"
"pandax/pkg/rule_engine/message"
"pandax/pkg/rule_engine/nodes"
"strings"
)
type RuleChainApi struct {
VisualRuleChainApp services.VisualRuleChainModel
}
func (r *RuleChainApi) GetNodeLabels(rc *restfulx.ReqCtx) {
rc.ResData = nodes.GetCategory()
}
func (r *RuleChainApi) RuleChainTest(rc *restfulx.ReqCtx) {
code := restfulx.QueryParam(rc, "code")
instance, _ := rule_engine.NewRuleChainInstance([]byte(code))
newMessage := message.NewMessage()
newMessage.SetMetadata(message.NewMetadata())
instance.StartRuleChain(context.Background(), newMessage)
rc.ResData = []map[string]interface{}{}
}
// GetVisualRuleChainList WorkInfo列表数据
func (p *RuleChainApi) GetVisualRuleChainList(rc *restfulx.ReqCtx) {
data := entity.VisualRuleChain{}
pageNum := restfulx.QueryInt(rc, "pageNum", 1)
pageSize := restfulx.QueryInt(rc, "pageSize", 10)
data.RuleName = restfulx.QueryParam(rc, "ruleName")
data.Status = restfulx.QueryParam(rc, "status")
list, total := p.VisualRuleChainApp.FindListPage(pageNum, pageSize, data)
rc.ResData = model.ResultPage{
Total: total,
PageNum: int64(pageNum),
PageSize: int64(pageNum),
Data: list,
}
}
// GetVisualRuleChain 获取规则链
func (p *RuleChainApi) GetVisualRuleChain(rc *restfulx.ReqCtx) {
id := restfulx.PathParam(rc, "id")
rc.ResData = p.VisualRuleChainApp.FindOne(id)
}
// InsertVisualRuleChain 添加规则链
func (p *RuleChainApi) InsertVisualRuleChain(rc *restfulx.ReqCtx) {
var data entity.VisualRuleChain
restfulx.BindQuery(rc, &data)
data.Creator = rc.LoginAccount.UserName
p.VisualRuleChainApp.Insert(data)
}
// UpdateVisualRuleChain 修改规则链
func (p *RuleChainApi) UpdateVisualRuleChain(rc *restfulx.ReqCtx) {
var data entity.VisualRuleChain
restfulx.BindQuery(rc, &data)
p.VisualRuleChainApp.Update(data)
}
// DeleteVisualRuleChain 删除规则链
func (p *RuleChainApi) DeleteVisualRuleChain(rc *restfulx.ReqCtx) {
id := restfulx.PathParam(rc, "id")
ids := strings.Split(id, ",")
p.VisualRuleChainApp.Delete(ids)
}

View File

@@ -2,14 +2,14 @@ package entity
import "github.com/XM-GO/PandaKit/model"
type DataSetGroup struct {
type VisualDataSetGroup struct {
model.BaseModelD
Name string `gorm:"name;type:varchar(64);comment:数据源类型" json:"name"`
Pid string `json:"pid"`
Level int64 `json:"level"`
}
/*type DataSetTable struct {
/*type VisualDataSetTable struct {
model.BaseModelD
TableId string `gorm:"name;type:TEXT;comment:表id" json:"tableId"`
DataSourceId string `gorm:"name;type:TEXT;comment:数据圆ID" json:"data_source_Id"`

View File

@@ -2,7 +2,7 @@ package entity
import "github.com/XM-GO/PandaKit/model"
type DataSource struct {
type VisualDataSource struct {
model.BaseModel
SourceId string `gorm:"source_id;comment:数据源Id" json:"sourceId"` // 数据源Id
SourceType string `gorm:"source_type;type:varchar(50);comment:数据源类型" json:"sourceType"` // 数据源类型
@@ -14,7 +14,7 @@ type DataSource struct {
}
type Db struct {
type VisualDb struct {
DbIp string `gorm:"db_ip" json:"dbIp"`
DbPort string `gorm:"db_port" json:"dbPort"`
DbName string `gorm:"db_name" json:"dbName"`
@@ -23,7 +23,7 @@ type Db struct {
DbJointParam string `gorm:"db_joint_param" json:"dbJointParam"` //额外的链接参数
}
type Api struct {
type VisualApi struct {
Method string `gorm:"method" json:"method"`
url string `gorm:"url" json:"url"`
Headers map[string]interface{} `gorm:"headers" json:"headers"`
@@ -31,6 +31,6 @@ type Api struct {
Auth string `gorm:"db_password" json:"dbPassword"`
}
func (DataSource) TableName() string {
return "bi_data_source"
func (VisualDataSource) TableName() string {
return "visual_data_source"
}

View File

@@ -0,0 +1,18 @@
package entity
import (
"github.com/XM-GO/PandaKit/model"
)
type VisualRuleChain struct {
UserId string `json:"userId"`
RuleId string `json:"ruleId"`
RuleName string `json:"ruleName"`
RuleDataJson string `json:"ruleDataJson"`
RuleBase64 string `json:"ruleBase64"` //缩略图 base64
RuleRemark string `json:"ruleRemark"`
Status string `json:"status"`
DeviceId string `json:"deviceId"`
Creator string `json:"creator"` //创建者
model.BaseModel
}

View File

@@ -0,0 +1,79 @@
package api
import (
"github.com/XM-GO/PandaKit/model"
"github.com/XM-GO/PandaKit/restfulx"
restfulspec "github.com/emicklei/go-restful-openapi/v2"
"github.com/emicklei/go-restful/v3"
"pandax/apps/visual/api"
"pandax/apps/visual/entity"
)
func InitRuleChainRouter(container *restful.Container) {
s := &api.RuleChainApi{}
ws := new(restful.WebService)
ws.Path("/visual/rulechain").Produces(restful.MIME_JSON)
tags := []string{"rulechain"}
ws.Route(ws.GET("/nodeLabels").To(func(request *restful.Request, response *restful.Response) {
restfulx.NewReqCtx(request, response).WithNeedCasbin(false).WithLog("获取所有节点标签").Handle(s.GetNodeLabels)
}).
Doc("获取所有节点标签").
Metadata(restfulspec.KeyOpenAPITags, tags).
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)
}).
Doc("测试规则引擎").
Param(ws.QueryParameter("code", "流程代码").DataType("string")).
Metadata(restfulspec.KeyOpenAPITags, tags))
ws.Route(ws.GET("/list").To(func(request *restful.Request, response *restful.Response) {
restfulx.NewReqCtx(request, response).WithLog("获取规则引擎分页列表").Handle(s.GetVisualRuleChainList)
}).
Doc("获取规则引擎分页列表").
Param(ws.QueryParameter("pageNum", "页数").Required(true).DataType("int")).
Param(ws.QueryParameter("pageSize", "每页条数").Required(true).DataType("int")).
Param(ws.QueryParameter("ruleName", "规则名").Required(false).DataType("string")).
Param(ws.QueryParameter("status", "状态").Required(false).DataType("string")).
Metadata(restfulspec.KeyOpenAPITags, tags).
Writes(model.ResultPage{}).
Returns(200, "OK", model.ResultPage{}))
ws.Route(ws.GET("/{id}").To(func(request *restful.Request, response *restful.Response) {
restfulx.NewReqCtx(request, response).WithLog("获取规则引擎信息").Handle(s.GetVisualRuleChain)
}).
Doc("获取规则引擎信息").
Param(ws.PathParameter("id", "Id").DataType("string")).
Metadata(restfulspec.KeyOpenAPITags, tags).
Writes(entity.VisualRuleChain{}). // on the response
Returns(200, "OK", entity.VisualRuleChain{}).
Returns(404, "Not Found", nil))
ws.Route(ws.POST("").To(func(request *restful.Request, response *restful.Response) {
restfulx.NewReqCtx(request, response).WithLog("添加规则引擎信息").Handle(s.InsertVisualRuleChain)
}).
Doc("添加规则引擎信息").
Metadata(restfulspec.KeyOpenAPITags, tags).
Reads(entity.VisualRuleChain{}))
ws.Route(ws.PUT("").To(func(request *restful.Request, response *restful.Response) {
restfulx.NewReqCtx(request, response).WithLog("修改规则引擎信息").Handle(s.UpdateVisualRuleChain)
}).
Doc("修改规则引擎信息").
Metadata(restfulspec.KeyOpenAPITags, tags).
Reads(entity.VisualRuleChain{}))
ws.Route(ws.DELETE("/{id}").To(func(request *restful.Request, response *restful.Response) {
restfulx.NewReqCtx(request, response).WithLog("删除规则引擎信息").Handle(s.DeleteVisualRuleChain)
}).
Doc("删除规则引擎信息").
Metadata(restfulspec.KeyOpenAPITags, tags).
Param(ws.PathParameter("id", "多id 1,2,3").DataType("string")))
container.Add(ws)
}

View File

@@ -0,0 +1,101 @@
// ==========================================================================
// 生成日期2023-03-29 20:01:11 +0800 CST
// 生成路径: apps/visual/services/rulechain.go
// 生成人panda
// ==========================================================================
package services
import (
"github.com/XM-GO/PandaKit/biz"
"pandax/apps/visual/entity"
"pandax/pkg/global"
)
type (
VisualRuleChainModel interface {
Insert(data entity.VisualRuleChain) *entity.VisualRuleChain
FindOne(id string) *entity.VisualRuleChain
FindListPage(page, pageSize int, data entity.VisualRuleChain) (*[]entity.VisualRuleChain, int64)
FindList(data entity.VisualRuleChain) *[]entity.VisualRuleChain
Update(data entity.VisualRuleChain) *entity.VisualRuleChain
Delete(ids []string)
}
ruleChainModelImpl struct {
table string
}
)
var VisualRuleChainModelDao VisualRuleChainModel = &ruleChainModelImpl{
table: `visual_rule_chain`,
}
func (m *ruleChainModelImpl) Insert(data entity.VisualRuleChain) *entity.VisualRuleChain {
err := global.Db.Table(m.table).Create(&data).Error
biz.ErrIsNil(err, "添加规则链失败")
return &data
}
func (m *ruleChainModelImpl) FindOne(id string) *entity.VisualRuleChain {
resData := new(entity.VisualRuleChain)
db := global.Db.Table(m.table).Where("rule_id = ?", id)
err := db.First(resData).Error
biz.ErrIsNil(err, "查询规则链失败")
return resData
}
func (m *ruleChainModelImpl) FindListPage(page, pageSize int, data entity.VisualRuleChain) (*[]entity.VisualRuleChain, int64) {
list := make([]entity.VisualRuleChain, 0)
var total int64 = 0
offset := pageSize * (page - 1)
db := global.Db.Table(m.table)
// 此处填写 where参数判断
db.Where("delete_time IS NULL")
if data.UserId != "" {
db = db.Where("user_id = ?", data.UserId)
}
if data.RuleName != "" {
db = db.Where("rule_name = ?", data.RuleName)
}
if data.RuleRemark != "" {
db = db.Where("rule_remark like ?", "%"+data.RuleRemark+"%")
}
if data.Status != "" {
db = db.Where("status = ?", data.Status)
}
err := db.Count(&total).Error
err = db.Order("create_time").Limit(pageSize).Offset(offset).Find(&list).Error
biz.ErrIsNil(err, "查询规则链分页列表失败")
return &list, total
}
func (m *ruleChainModelImpl) FindList(data entity.VisualRuleChain) *[]entity.VisualRuleChain {
list := make([]entity.VisualRuleChain, 0)
db := global.Db.Table(m.table)
// 此处填写 where参数判断
db.Where("delete_time IS NULL")
if data.UserId != "" {
db = db.Where("user_id = ?", data.UserId)
}
if data.RuleName != "" {
db = db.Where("rule_name = ?", data.RuleName)
}
if data.RuleRemark != "" {
db = db.Where("rule_remark like ?", "%"+data.RuleRemark+"%")
}
if data.Status != "" {
db = db.Where("status = ?", data.Status)
}
biz.ErrIsNil(db.Order("create_time").Find(&list).Error, "查询规则链列表失败")
return &list
}
func (m *ruleChainModelImpl) Update(data entity.VisualRuleChain) *entity.VisualRuleChain {
biz.ErrIsNil(global.Db.Table(m.table).Updates(&data).Error, "修改规则链失败")
return &data
}
func (m *ruleChainModelImpl) Delete(ids []string) {
biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.VisualRuleChain{}, "rule_id in (?)", ids).Error, "删除规则链失败")
}