|
|
|
package nkatago
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.noahlan.cn/noahlan/ntool-biz/nkatago/req"
|
|
|
|
"git.noahlan.cn/noahlan/ntool-biz/nkatago/resp"
|
|
|
|
"git.noahlan.cn/noahlan/ntool/ndef"
|
|
|
|
"git.noahlan.cn/noahlan/ntool/njson"
|
|
|
|
"git.noahlan.cn/noahlan/ntool/nrandom"
|
|
|
|
"git.noahlan.cn/noahlan/ntool/nstr"
|
|
|
|
"git.noahlan.cn/noahlan/ntool/nsys/cmdn"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
analStartupDecidedFunc cmdn.LineFunc = func(sb *strings.Builder, line string) bool {
|
|
|
|
return strings.Contains(line, "Started, ready to begin handling requests")
|
|
|
|
}
|
|
|
|
analEndLineDecidedFunc cmdn.LineFunc = func(sb *strings.Builder, line string) bool {
|
|
|
|
return nstr.IsJSON(sb.String())
|
|
|
|
}
|
|
|
|
analReadIDFunc cmdn.ReadIDFunc = func(serializer ndef.Serializer, data string) (string, error) {
|
|
|
|
var s struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
}
|
|
|
|
err := serializer.Unmarshal([]byte(data), &s)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return s.ID, nil
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
type AnalysisEngine struct {
|
|
|
|
*KatagoEngine
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAnalysisEngine 创建Analysis引擎
|
|
|
|
func NewAnalysisEngine(name string, args ...string) (*AnalysisEngine, error) {
|
|
|
|
engine := &AnalysisEngine{KatagoEngine: &KatagoEngine{
|
|
|
|
Processor: cmdn.NewProcessor(
|
|
|
|
false,
|
|
|
|
cmdn.WithSerializer(njson.NewJsonSerializer()),
|
|
|
|
cmdn.WithStartupDecidedFunc(analStartupDecidedFunc),
|
|
|
|
cmdn.WithEndLineDecidedFunc(analEndLineDecidedFunc),
|
|
|
|
cmdn.WithReadIDFunc(analReadIDFunc),
|
|
|
|
),
|
|
|
|
Name: name,
|
|
|
|
Mode: EngineAnalysis,
|
|
|
|
}}
|
|
|
|
err := engine.Processor.Run(name, args...)
|
|
|
|
return engine, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query 分析查询
|
|
|
|
func (eng *AnalysisEngine) Query(req *req.AnalQueryReq, callback func(queryResp *resp.AnalQueryResp)) error {
|
|
|
|
if len(req.ID) == 0 {
|
|
|
|
req.ID = nrandom.SnowflakeIdStr()
|
|
|
|
}
|
|
|
|
return eng.Processor.Exec(req, func(serializer ndef.Serializer, data string) {
|
|
|
|
var tmp resp.AnalQueryResp
|
|
|
|
tmp.Err = serializer.Unmarshal([]byte(data), &tmp)
|
|
|
|
|
|
|
|
callback(&tmp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryVersion 查询版本号
|
|
|
|
func (eng *AnalysisEngine) QueryVersion(callback func(versionResp *resp.AnalQueryVersionResp)) error {
|
|
|
|
return eng.Processor.Exec(&req.AnalQueryActionReq{
|
|
|
|
ID: nrandom.SnowflakeIdStr(),
|
|
|
|
Action: req.AnalActionQueryVersion,
|
|
|
|
}, func(serializer ndef.Serializer, data string) {
|
|
|
|
var tmp resp.AnalQueryVersionResp
|
|
|
|
tmp.Err = serializer.Unmarshal([]byte(data), &tmp)
|
|
|
|
callback(&tmp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClearCache 清除缓存
|
|
|
|
func (eng *AnalysisEngine) ClearCache(callback func(cacheResp *resp.AnalClearCacheResp)) error {
|
|
|
|
return eng.Processor.Exec(&req.AnalQueryActionReq{
|
|
|
|
ID: nrandom.SnowflakeIdStr(),
|
|
|
|
Action: req.AnalActionClearCache,
|
|
|
|
}, func(serializer ndef.Serializer, data string) {
|
|
|
|
var tmp resp.AnalClearCacheResp
|
|
|
|
tmp.Err = serializer.Unmarshal([]byte(data), &tmp)
|
|
|
|
callback(&tmp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Terminate 终止某次分析
|
|
|
|
// terminateId: 分析轮次ID
|
|
|
|
// turnNumbers: 需要终止分析的回合数(手数),可空:所有回合
|
|
|
|
func (eng *AnalysisEngine) Terminate(terminateId string, turnNumbers []int32, callback func(terminateResp *resp.AnalTerminateResp)) error {
|
|
|
|
return eng.Processor.Exec(&req.AnalQueryActionReq{
|
|
|
|
ID: nrandom.SnowflakeIdStr(),
|
|
|
|
Action: req.AnalActionTerminate,
|
|
|
|
TerminateId: terminateId,
|
|
|
|
TurnNumbers: turnNumbers,
|
|
|
|
}, func(serializer ndef.Serializer, data string) {
|
|
|
|
var tmp resp.AnalTerminateResp
|
|
|
|
tmp.Err = serializer.Unmarshal([]byte(data), &tmp)
|
|
|
|
callback(&tmp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// TerminateAll 终止所有分析
|
|
|
|
// turnNumbers: 需要终止所有分析轮次的回合数,可空:所有回合
|
|
|
|
func (eng *AnalysisEngine) TerminateAll(turnNumbers []int32, callback func(allResp *resp.AnalTerminateAllResp)) error {
|
|
|
|
return eng.Processor.Exec(&req.AnalQueryActionReq{
|
|
|
|
ID: nrandom.SnowflakeIdStr(),
|
|
|
|
Action: req.AnalActionTerminateAll,
|
|
|
|
TurnNumbers: turnNumbers,
|
|
|
|
}, func(serializer ndef.Serializer, data string) {
|
|
|
|
var tmp resp.AnalTerminateAllResp
|
|
|
|
tmp.Err = serializer.Unmarshal([]byte(data), &tmp)
|
|
|
|
callback(&tmp)
|
|
|
|
})
|
|
|
|
}
|