[优化] 添加清除规则链debug日志接口,修复日志读取长度

This commit is contained in:
PandaX
2023-10-30 11:19:30 +08:00
parent 036e3c32b7
commit cac66b9fd0
6 changed files with 29 additions and 9 deletions

View File

@@ -34,6 +34,12 @@ func (r *RuleChainApi) GetNodeDebug(rc *restfulx.ReqCtx) {
}
}
func (r *RuleChainApi) ClearNodeDebug(rc *restfulx.ReqCtx) {
ruleId := restfulx.QueryParam(rc, "ruleId")
nodeId := restfulx.QueryParam(rc, "nodeId")
rule_engine.ClearDebugData(ruleId, nodeId)
}
// GetRuleChainList WorkInfo列表数据
func (p *RuleChainApi) GetRuleChainList(rc *restfulx.ReqCtx) {
data := entity.RuleChain{}

View File

@@ -36,9 +36,16 @@ func InitRuleChainRouter(container *restful.Container) {
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("/node/debug/clear").To(func(request *restful.Request, response *restful.Response) {
restfulx.NewReqCtx(request, response).WithNeedCasbin(false).WithLog("清除规则链节点日志").Handle(s.ClearNodeDebug)
}).
Doc("清除规则链节点日志").
Param(ws.QueryParameter("ruleId", "规则ID").Required(false).DataType("string")).
Param(ws.QueryParameter("nodeId", "节点ID").Required(false).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.GetRuleChainList)
}).

View File

@@ -16,6 +16,12 @@ func GetDebugData(ruleId, nodeId string) []message.DebugData {
return nil
}
func ClearDebugData(ruleId, nodeId string) {
if data, ok := ruleChainDebugData.Data[ruleId]; ok {
data.Clear(nodeId)
}
}
func GetDebugDataPage(page, pageSize int, ruleId, nodeId string) (int64, []message.DebugData) {
if page < 1 {
page = 1
@@ -26,7 +32,7 @@ func GetDebugDataPage(page, pageSize int, ruleId, nodeId string) (int64, []messa
total := len(data.Get(nodeId).Items)
end := offset + pageSize
if end >= total {
end = total - 1
end = total
}
return int64(total), data.Get(nodeId).Items[offset:end]
}

View File

@@ -55,7 +55,7 @@ func NewMessage(user, messageType string, msg Msg, metadata Metadata) *Message {
MsgType: messageType,
Msg: msg,
Metadata: metadata,
DeBugChan: make(chan DebugData, 100),
DeBugChan: make(chan DebugData),
EndDeBugChan: make(chan struct{}),
}
}
@@ -65,7 +65,7 @@ func (t *Message) Debug(nodeId, nodeName, debugType, error string) {
logrus.Infof("%s handle message '%s'", nodeName, t.MsgType)
}
debug := DebugData{
Ts: time.Now().Format("2006-01-02 15:04:05.000"),
Ts: time.Now().Format("2006-01-02 15:04:05"),
NodeId: nodeId,
MsgId: t.Id,
DebugType: debugType,

View File

@@ -41,8 +41,8 @@ func (n *createAlarmNode) Handle(msg *message.Message) error {
alarm.Details = string(marshal)
err = services.DeviceAlarmModelDao.Update(*alarm)
if err == nil {
if updated != nil {
n.Debug(msg, message.DEBUGOUT, "")
if updated != nil {
return updated.Handle(msg)
}
}
@@ -62,8 +62,8 @@ func (n *createAlarmNode) Handle(msg *message.Message) error {
alarm.Details = string(marshal)
err = services.DeviceAlarmModelDao.Insert(*alarm)
if err == nil {
if created != nil {
n.Debug(msg, message.DEBUGOUT, "")
if created != nil {
return created.Handle(msg)
}
}

View File

@@ -25,6 +25,7 @@ func (f scriptFilterNodeFactory) Create(id string, meta Properties) (Node, error
func (n *scriptFilterNode) Handle(msg *message.Message) error {
n.Debug(msg, message.DEBUGIN, "")
trueLabelNode := n.GetLinkedNode("True")
falseLabelNode := n.GetLinkedNode("False")
failureLabelNode := n.GetLinkedNode("Failure")
@@ -32,18 +33,18 @@ func (n *scriptFilterNode) Handle(msg *message.Message) error {
scriptEngine := NewScriptEngine(*msg, "Filter", n.Script)
isTrue, err := scriptEngine.ScriptOnFilter()
if err != nil {
if failureLabelNode != nil {
n.Debug(msg, message.DEBUGOUT, err.Error())
if failureLabelNode != nil {
return failureLabelNode.Handle(msg)
}
}
if isTrue == true && trueLabelNode != nil {
if isTrue && trueLabelNode != nil {
n.Debug(msg, message.DEBUGOUT, "")
return trueLabelNode.Handle(msg)
} else {
if falseLabelNode != nil {
n.Debug(msg, message.DEBUGOUT, "Script脚本执行失败")
if falseLabelNode != nil {
return falseLabelNode.Handle(msg)
}
}