hpds_jkw_web/internal/service/model.go

434 lines
12 KiB
Go

package service
import (
"context"
"encoding/json"
"fmt"
"git.hpds.cc/Component/logging"
"git.hpds.cc/pavement/hpds_node"
"hpds-iot-web/config"
"hpds-iot-web/internal/proto"
"hpds-iot-web/model"
"hpds-iot-web/mq"
"net/http"
"strings"
"time"
"xorm.io/xorm"
)
type ModelService interface {
ModelList(ctx context.Context, req proto.ModelRequest) (rsp *proto.BaseResponse, err error)
AddModel(ctx context.Context, req proto.ModelItemRequest) (rsp *proto.BaseResponse, err error)
EditModel(ctx context.Context, req proto.ModelItemRequest) (rsp *proto.BaseResponse, err error)
DelModel(ctx context.Context, req proto.ModelItemRequest) (rsp *proto.BaseResponse, err error)
ModelIssue(ctx context.Context, req proto.ModelIssueRequest) (rsp *proto.BaseResponse, err error)
ModelIssueLog(ctx context.Context, req proto.ModelIssueLogRequest) (rsp *proto.BaseResponse, err error)
GetModelWorkflow(ctx context.Context, req proto.ModelItemRequest) (rsp *proto.BaseResponse, err error)
}
func NewModelService(cfg *config.WebConfig, engine *xorm.Engine, logger *logging.Logger) ModelService {
return &repo{
AppConfig: cfg,
engine: engine,
logger: logger,
}
}
func (rp *repo) ModelList(ctx context.Context, req proto.ModelRequest) (rsp *proto.BaseResponse, err error) {
rsp = new(proto.BaseResponse)
select {
case <-ctx.Done():
err = fmt.Errorf("超时/取消")
rsp.Code = http.StatusInternalServerError
rsp.Status = http.StatusText(http.StatusInternalServerError)
rsp.Message = "超时/取消"
rsp.Err = ctx.Err()
return rsp, ctx.Err()
default:
data := make([]model.Model, 0)
count, err := rp.engine.Where("(? = '' or model_name like ?)", req.ModelName, "%"+req.ModelName+"%").
And("(?=0 or biz_type=?)", req.BizType, req.BizType).
And("status = 1").Limit(int(req.Size), int(((req.Page)-1)*req.Size)).
FindAndCount(&data)
if err != nil {
goto ReturnPoint
}
rsp.Code = http.StatusOK
rsp.Status = http.StatusText(http.StatusOK)
rsp.Message = "成功"
rsp = FillPaging(count, req.Page, req.Size, data, rsp)
rsp.Err = err
return rsp, err
}
ReturnPoint:
if err != nil {
rsp.Code = http.StatusInternalServerError
rsp.Status = http.StatusText(http.StatusInternalServerError)
rsp.Err = err
rsp.Message = "失败"
}
return rsp, err
}
func (rp *repo) AddModel(ctx context.Context, req proto.ModelItemRequest) (rsp *proto.BaseResponse, err error) {
rsp = new(proto.BaseResponse)
select {
case <-ctx.Done():
err = fmt.Errorf("超时/取消")
rsp.Code = http.StatusInternalServerError
rsp.Status = http.StatusText(http.StatusInternalServerError)
rsp.Message = "超时/取消"
rsp.Err = ctx.Err()
return rsp, ctx.Err()
default:
item := &model.Model{
ModelName: req.ModelName,
BizType: req.BizType,
ModelVersion: req.ModelVersion,
ModelDesc: req.ModelDesc,
ModelFiles: req.ModelFiles,
ModelParamsFiles: req.ModelParamsFiles,
ModelExecScript: req.ModelExecScript,
DockerFile: strings.Join(req.DockerFile, "|"),
MappedPort: req.MappedPort,
ModelCommand: req.ModelCommand,
InPath: req.InPath,
OutPath: req.OutPath,
HttpUrl: req.HttpUrl,
IsLightWeight: req.IsLightWeight,
Status: 1,
CreateAt: time.Now().Unix(),
UpdateAt: time.Now().Unix(),
}
_, err = rp.engine.Insert(item)
if err != nil {
goto ReturnPoint
}
rsp.Code = http.StatusOK
rsp.Status = http.StatusText(http.StatusOK)
rsp.Message = "新增模型成功"
rsp.Err = ctx.Err()
rsp.Data = item
return rsp, err
}
ReturnPoint:
if err != nil {
rsp.Code = http.StatusInternalServerError
rsp.Status = http.StatusText(http.StatusInternalServerError)
rsp.Err = err
rsp.Message = "失败"
}
return rsp, err
}
func (rp *repo) EditModel(ctx context.Context, req proto.ModelItemRequest) (rsp *proto.BaseResponse, err error) {
rsp = new(proto.BaseResponse)
select {
case <-ctx.Done():
err = fmt.Errorf("超时/取消")
rsp.Code = http.StatusInternalServerError
rsp.Status = http.StatusText(http.StatusInternalServerError)
rsp.Message = "超时/取消"
rsp.Err = ctx.Err()
return rsp, ctx.Err()
default:
var h bool
item := new(model.Model)
h, err = rp.engine.ID(req.ModelId).Get(item)
if err != nil {
goto ReturnPoint
}
if !h {
err = fmt.Errorf("未能找到对应的模型")
goto ReturnPoint
}
if len(req.ModelName) > 0 {
item.ModelName = req.ModelName
}
if len(req.ModelVersion) > 0 {
item.ModelVersion = req.ModelVersion
}
if len(req.ModelDesc) > 0 {
item.ModelDesc = req.ModelDesc
}
if len(req.ModelFiles) > 0 {
item.ModelFiles = req.ModelFiles
}
if len(req.ModelParamsFiles) > 0 {
item.ModelParamsFiles = req.ModelParamsFiles
}
if len(req.ModelExecScript) > 0 {
item.ModelExecScript = req.ModelExecScript
}
if req.BizType > 0 {
item.BizType = req.BizType
}
if len(req.DockerFile) > 0 {
item.DockerFile = strings.Join(req.DockerFile, "|")
}
if req.MappedPort > 0 {
item.MappedPort = req.MappedPort
}
if len(req.ModelCommand) > 0 {
item.ModelCommand = req.ModelCommand
}
if len(req.InPath) > 0 {
item.InPath = req.InPath
}
if len(req.OutPath) > 0 {
item.OutPath = req.OutPath
}
if len(req.HttpUrl) > 0 {
item.HttpUrl = req.HttpUrl
}
if len(req.Workflow) > 0 {
item.Workflow = req.Workflow
}
item.IsLightWeight = req.IsLightWeight
item.UpdateAt = time.Now().Unix()
_, err = rp.engine.ID(req.ModelId).AllCols().Update(item)
if err != nil {
goto ReturnPoint
}
rsp.Code = http.StatusOK
rsp.Status = http.StatusText(http.StatusOK)
rsp.Message = "修改模型成功"
rsp.Err = ctx.Err()
rsp.Data = item
return rsp, err
}
ReturnPoint:
if err != nil {
rsp.Code = http.StatusInternalServerError
rsp.Status = http.StatusText(http.StatusInternalServerError)
rsp.Err = err
rsp.Message = "失败"
}
return rsp, err
}
func (rp *repo) DelModel(ctx context.Context, req proto.ModelItemRequest) (rsp *proto.BaseResponse, err error) {
rsp = new(proto.BaseResponse)
select {
case <-ctx.Done():
err = fmt.Errorf("超时/取消")
rsp.Code = http.StatusInternalServerError
rsp.Status = http.StatusText(http.StatusInternalServerError)
rsp.Message = "超时/取消"
rsp.Err = ctx.Err()
return rsp, ctx.Err()
default:
var h bool
item := new(model.Model)
h, err = rp.engine.ID(req.ModelId).Get(item)
if err != nil {
goto ReturnPoint
}
if !h {
err = fmt.Errorf("未能找到对应的模型")
goto ReturnPoint
}
item.Status = 0
item.UpdateAt = time.Now().Unix()
_, err = rp.engine.ID(req.ModelId).AllCols().Update(item)
if err != nil {
goto ReturnPoint
}
rsp.Code = http.StatusOK
rsp.Status = http.StatusText(http.StatusOK)
rsp.Message = "删除模型成功"
rsp.Err = ctx.Err()
rsp.Data = item
return rsp, err
}
ReturnPoint:
if err != nil {
rsp.Code = http.StatusInternalServerError
rsp.Status = http.StatusText(http.StatusInternalServerError)
rsp.Err = err
rsp.Message = "失败"
}
return rsp, err
}
func (rp *repo) ModelIssue(ctx context.Context, req proto.ModelIssueRequest) (rsp *proto.BaseResponse, err error) {
rsp = new(proto.BaseResponse)
select {
case <-ctx.Done():
err = fmt.Errorf("超时/取消")
rsp.Code = http.StatusInternalServerError
rsp.Status = http.StatusText(http.StatusInternalServerError)
rsp.Message = "超时/取消"
rsp.Err = ctx.Err()
return rsp, ctx.Err()
default:
var h bool
m := new(model.Model)
h, err = rp.engine.ID(req.ModelId).Get(m)
if err != nil {
goto ReturnPoint
}
if !h {
err = fmt.Errorf("未能找到对应的模型")
goto ReturnPoint
}
node := new(model.Node)
h, err = rp.engine.ID(req.NodeId).Get(node)
if err != nil {
goto ReturnPoint
}
if !h {
err = fmt.Errorf("未能找到对应的节点")
goto ReturnPoint
}
item := new(model.IssueModel)
h, err = rp.engine.Where("model_id = ? and node_id= ?", req.ModelId, req.NodeId).Get(item)
if err != nil {
goto ReturnPoint
}
if h {
err = fmt.Errorf("已经有该模型")
goto ReturnPoint
}
item.ModelId = req.ModelId
item.NodeId = req.NodeId
item.Status = 1
item.CreateAt = time.Now().Unix()
item.UpdateAt = time.Now().Unix()
_, err = rp.engine.Insert(item)
if err != nil {
goto ReturnPoint
}
mqClient := mq.GetMqClient("task-request", 1)
payload := make(map[string]interface{})
payload["modelId"] = item.ModelId
payload["modelVersion"] = m.ModelVersion
payload["modelCommand"] = m.ModelCommand
payload["nodeId"] = item.NodeId
payload["dockerFile"] = m.DockerFile
payload["mappedPort"] = m.MappedPort
payload["inPath"] = m.InPath
payload["outPath"] = m.OutPath
payload["httpUrl"] = m.HttpUrl
payload["nodeGuid"] = node.NodeGuid
mqPayload := &mq.InstructionReq{
Command: mq.ModelIssue,
Payload: payload,
}
b, _ := json.Marshal(mqPayload)
err = mq.GenerateAndSendData(mqClient.EndPoint.(hpds_node.AccessPoint), b, rp.logger)
rsp.Code = http.StatusOK
rsp.Status = http.StatusText(http.StatusOK)
rsp.Message = "模型下发成功"
rsp.Err = ctx.Err()
rsp.Data = item
return rsp, err
}
ReturnPoint:
if err != nil {
rsp.Code = http.StatusInternalServerError
rsp.Status = http.StatusText(http.StatusInternalServerError)
rsp.Err = err
rsp.Message = "失败"
}
return rsp, err
}
func (rp *repo) ModelIssueLog(ctx context.Context, req proto.ModelIssueLogRequest) (rsp *proto.BaseResponse, err error) {
rsp = new(proto.BaseResponse)
select {
case <-ctx.Done():
err = fmt.Errorf("超时/取消")
rsp.Code = http.StatusInternalServerError
rsp.Status = http.StatusText(http.StatusInternalServerError)
rsp.Message = "超时/取消"
rsp.Err = ctx.Err()
return rsp, ctx.Err()
default:
var (
h bool
count int64
)
m := new(model.Model)
h, err = rp.engine.ID(req.ModelId).Get(m)
if err != nil {
goto ReturnPoint
}
if !h {
err = fmt.Errorf("未能找到对应的模型")
goto ReturnPoint
}
logList := make([]model.IssueModel, 0)
count, err = rp.engine.Where("model_id=?", req.ModelId).FindAndCount(&logList)
if err != nil {
goto ReturnPoint
}
list := make([]proto.ModelIssueLogItem, len(logList))
for k, v := range logList {
list[k] = proto.ModelIssueLogItem{
Id: v.Id,
ModelId: v.ModelId,
NodeId: v.NodeId,
NodeName: model.GetNodeName(v.NodeId),
Status: v.Status,
IssueResult: v.IssueResult,
CreateAt: v.CreateAt,
UpdateAt: v.UpdateAt,
}
}
rsp.Code = http.StatusOK
rsp.Status = http.StatusText(http.StatusOK)
rsp.Message = "获取模型下发日志成功"
rsp.Err = ctx.Err()
rsp = FillPaging(count, req.Page, req.Size, list, rsp)
return rsp, err
}
ReturnPoint:
if err != nil {
rsp.Code = http.StatusInternalServerError
rsp.Status = http.StatusText(http.StatusInternalServerError)
rsp.Err = err
rsp.Message = "失败"
}
return rsp, err
}
func (rp *repo) GetModelWorkflow(ctx context.Context, req proto.ModelItemRequest) (rsp *proto.BaseResponse, err error) {
rsp = new(proto.BaseResponse)
select {
case <-ctx.Done():
err = fmt.Errorf("超时/取消")
rsp.Code = http.StatusInternalServerError
rsp.Status = http.StatusText(http.StatusInternalServerError)
rsp.Message = "超时/取消"
rsp.Err = ctx.Err()
return rsp, ctx.Err()
default:
var h bool
item := new(model.Model)
h, err = rp.engine.ID(req.ModelId).Get(item)
if err != nil {
goto ReturnPoint
}
if !h {
err = fmt.Errorf("未能找到对应的模型")
goto ReturnPoint
}
rsp.Code = http.StatusOK
rsp.Status = http.StatusText(http.StatusOK)
rsp.Message = "删除模型成功"
rsp.Err = ctx.Err()
rsp.Data = item.Workflow
return rsp, err
}
ReturnPoint:
if err != nil {
rsp.Code = http.StatusInternalServerError
rsp.Status = http.StatusText(http.StatusInternalServerError)
rsp.Err = err
rsp.Message = "失败"
}
return rsp, err
}