Merge pull request 'create algorithm' (#520) from zhouqj_dev into master

This commit is contained in:
zhouqunjie 2025-07-08 16:13:07 +08:00
commit c955378918
4 changed files with 125 additions and 23 deletions

View File

@ -1,18 +1,26 @@
package ai
import (
"github.com/zeromicro/go-zero/rest/httpx"
"encoding/json"
"gitlink.org.cn/JointCloud/pcm-coordinator/internal/logic/ai"
"gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
"gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
algorithm "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types/ai"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result"
"io"
"net/http"
)
func CreateAlgorithmHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.CreateAlgorithmReq
if err := httpx.Parse(r, &req); err != nil {
var req algorithm.CreateAlgorithmReq
body, err := io.ReadAll(r.Body)
if err != nil {
result.ParamErrorResult(r, w, err)
return
}
if err = json.Unmarshal(body, &req); err != nil {
result.ParamErrorResult(r, w, err)
return
}

View File

@ -98,7 +98,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
{
// 创建算法
Method: http.MethodPost,
Path: "/ai/CreateAlgorithm/:projectId",
Path: "/ai/CreateAlgorithm",
Handler: ai.CreateAlgorithmHandler(serverCtx),
},
{

View File

@ -16,14 +16,12 @@ package ai
import (
"context"
"github.com/jinzhu/copier"
"errors"
"github.com/go-resty/resty/v2"
"github.com/zeromicro/go-zero/core/logx"
"gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
"gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
"gitlink.org.cn/JointCloud/pcm-modelarts/modelarts"
"k8s.io/apimachinery/pkg/util/json"
algorithm "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types/ai"
)
type CreateAlgorithmLogic struct {
@ -40,18 +38,21 @@ func NewCreateAlgorithmLogic(ctx context.Context, svcCtx *svc.ServiceContext) *C
}
}
func (l *CreateAlgorithmLogic) CreateAlgorithm(req *types.CreateAlgorithmReq) (resp *types.CreateAlgorithmResp, err error) {
modelartsReq := &modelarts.CreateAlgorithmReq{}
err = copier.CopyWithOption(modelartsReq, req, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: utils.Converters})
ListAlgorithmsResp, err := l.svcCtx.ModelArtsRpc.CreateAlgorithm(l.ctx, modelartsReq)
if err != nil {
return nil, result.NewDefaultError(err.Error())
func (l *CreateAlgorithmLogic) CreateAlgorithm(req *algorithm.CreateAlgorithmReq) (resp *algorithm.CreateAlgorithmResp, err error) {
cluster := &types.GetClusterByIdResp{}
tx := l.svcCtx.DbEngin.Raw("select * from t_cluster where id = ?", req.ClusterId).Scan(&cluster.ClusterInfo)
if tx.Error != nil {
logx.Errorf(tx.Error.Error())
return nil, errors.New("cluster create failed")
}
marshal, err := json.Marshal(&ListAlgorithmsResp)
if err != nil {
return nil, result.NewDefaultError(err.Error())
}
json.Unmarshal(marshal, &resp)
err = copier.CopyWithOption(&resp, &ListAlgorithmsResp, copier.Option{IgnoreEmpty: true, DeepCopy: true, Converters: utils.Converters})
return resp, nil
httpClient := resty.New().R()
createAlgorithmResp := &algorithm.CreateAlgorithmResp{}
_, err = httpClient.SetHeader("Content-Type", "application/json").
SetQueryParams(map[string]string{"pfId": cluster.ClusterInfo.Id}).
SetBody(req).
SetResult(&createAlgorithmResp).
Post(cluster.ClusterInfo.Server + "/ai/algorithm/create")
return createAlgorithmResp, err
}

View File

@ -0,0 +1,93 @@
package algorithm
import (
"encoding/json"
"fmt"
)
type CreateParameter interface {
AlgorithmCreateParam()
}
type Source struct {
Jcs JcsBase `json:"jcs,omitempty"`
}
type JcsBase struct {
UserID int `json:"userID" binding:"required"`
PackageId int `json:"packageId" binding:"required"`
BucketID int `json:"bucketID" binding:"required"`
}
type CreateAlgorithmReq struct {
Name string `json:"name" binding:"required"`
ClusterId string `json:"clusterId"`
Desc string `json:"desc"`
Src Source `json:"src,omitempty"`
Param CreateParameter `json:"param,omitempty"`
}
type CreateAlgorithmResp struct {
Code int32 `json:"code,omitempty" copier:"Code"`
Msg string `json:"msg,omitempty" copier:"Msg"`
ErrorMsg string `json:"errorMsg,omitempty" copier:"ErrorMsg"`
}
type OpenI struct {
BootFile string `json:"bootFile,omitempty"`
DefaultBranch string `json:"defaultBranch,omitempty"`
}
func (o *OpenI) AlgorithmCreateParam() {
}
type Octopus struct {
}
func (o *Octopus) AlgorithmCreateParam() {
}
func (cp *CreateAlgorithmReq) UnmarshalJSON(data []byte) error {
// 临时结构体:用于捕获原始 JSON 中的 param 字段数据
type TempCreateParam struct {
Name string `json:"name"`
ClusterId string `json:"clusterId"`
Desc string `json:"desc"`
Src Source `json:"src,omitempty"`
Param json.RawMessage `json:"param,omitempty"` // 捕获原始 JSON 数据
}
var temp TempCreateParam
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
// 将临时结构体的字段赋值给原结构体(除 Param 外)
cp.Name = temp.Name
cp.ClusterId = temp.ClusterId
cp.Desc = temp.Desc
cp.Src = temp.Src
// 解析 param 字段的原始数据为具体类型
if temp.Param != nil {
// 尝试解析为 OpenI 类型
var openi OpenI
if err := json.Unmarshal(temp.Param, &openi); err != nil {
// 打印详细错误(如字段不匹配、类型错误等)
fmt.Printf("解析 OpenI 失败: %v\n", err) // 关键调试日志
} else {
cp.Param = &openi
return nil
}
// 新增:尝试解析为 Octopus 类型
var octopus Octopus
if err := json.Unmarshal(temp.Param, &octopus); err == nil {
cp.Param = &octopus
return nil
}
return fmt.Errorf("unsupported param type in CreateParam")
}
return nil
}