hpds_jkw_web/internal/service/model.go

371 lines
10 KiB
Go
Raw Normal View History

2023-01-06 14:38:22 +08:00
package service
import (
"context"
2023-03-23 18:03:09 +08:00
"encoding/json"
2023-01-06 14:38:22 +08:00
"fmt"
2023-01-06 19:15:52 +08:00
"git.hpds.cc/Component/logging"
2023-03-23 18:03:09 +08:00
"git.hpds.cc/pavement/hpds_node"
"hpds-iot-web/config"
2023-01-06 14:38:22 +08:00
"hpds-iot-web/internal/proto"
"hpds-iot-web/model"
2023-03-23 18:03:09 +08:00
"hpds-iot-web/mq"
2023-01-06 14:38:22 +08:00
"net/http"
2023-03-23 18:03:09 +08:00
"strings"
2023-01-06 14:38:22 +08:00
"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)
2023-03-23 18:03:09 +08:00
ModelIssue(ctx context.Context, req proto.ModelIssueRequest) (rsp *proto.BaseResponse, err error)
GetModelWorkflow(ctx context.Context, req proto.ModelItemRequest) (rsp *proto.BaseResponse, err error)
2023-01-06 14:38:22 +08:00
}
2023-03-23 18:03:09 +08:00
func NewModelService(cfg *config.WebConfig, engine *xorm.Engine, logger *logging.Logger) ModelService {
2023-01-06 14:38:22 +08:00
return &repo{
2023-03-23 18:03:09 +08:00
AppConfig: cfg,
engine: engine,
logger: logger,
2023-01-06 14:38:22 +08:00
}
}
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("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,
2023-03-23 18:03:09 +08:00
BizType: req.BizType,
2023-01-06 14:38:22 +08:00
ModelVersion: req.ModelVersion,
ModelDesc: req.ModelDesc,
ModelFiles: req.ModelFiles,
ModelParamsFiles: req.ModelParamsFiles,
ModelExecScript: req.ModelExecScript,
2023-03-23 18:03:09 +08:00
DockerFile: strings.Join(req.DockerFile, "|"),
MappedPort: req.MappedPort,
ModelCommand: req.ModelCommand,
InPath: req.InPath,
OutPath: req.OutPath,
HttpUrl: req.HttpUrl,
IsLightWeight: req.IsLightWeight,
2023-01-06 14:38:22 +08:00
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
}
2023-03-23 18:03:09 +08:00
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
2023-01-06 14:38:22 +08:00
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
}
2023-03-23 18:03:09 +08:00
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.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) 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
}